5 use \Rsi\Fred\Controller;
6 use \Rsi\Fred\Controller\Provider;
66 $this->
publish([
'key',
'descr',
'parentKey',
'search']);
70 return array_key_exists(self::CAPTION,$config = $this->columns[$column] ?: [])
71 ? $this->trans->str($config[self::CAPTION])
72 : $this->trans->id($column);
76 return array_merge([
'widget',
'table',$this->
id ?: $this->provider->id],is_array($key) ? $key : [$key]);
80 $session = $this->_controller->session;
82 foreach($this->columns as $column => $config){
83 if($caption = $this->
columnCaption($column)) $config[self::CAPTION] = $caption;
84 $columns[$column] = $config;
86 $config = array_merge(parent::clientConfig(),$this->
get([self::CODE,self::DESCR,self::PARENT_KEY,self::LINK,self::SEARCH]),[self::COLUMNS => $columns]);
87 if($search = $session->get($this->sessionKey(
'search'))) $config[self::SEARCH] = $search;
88 if($order = $session->get($this->sessionKey(
'order')) ?: $this->provider->order) $config[
'defaultOrder'] = $order;
89 if($this->downloadFormats) $config[self::DOWNLOAD_FORMATS] = array_keys($this->downloadFormats);
91 if($this->downloadBusyMessage) $config[
'downloadBusyMessage'] = $this->trans->str($this->downloadBusyMessage);
97 return $this->provider->trans($this->key,$this->code,$value,$this->params);
105 return $this->provider->trans($this->code,$this->key,$value,$this->params);
109 return $this->
format($value);
113 return $this->provider->exists($this->key,$value,$this->params);
116 protected function download($records,$format,$ext = null,$content_type = null){
117 $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
118 $sheet = $spreadsheet->setActiveSheetIndex(0)->setTitle($this->downloadName);
119 if(!($columns = $this->request->record(
'columns')))
120 $this->fred->externalError(
'Invalid columns',[
'columns' => $columns]);
122 foreach(array_values($columns) as $index => $column){
123 if(!array_key_exists($column,$this->columns)) $this->fred->externalError(
'Unknown column',[
'column' => $column]);
124 $char = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($index);
125 $sheet->setCellValue($cell = $char . 1,$this->
columnCaption($column));
126 $sheet->getStyle($cell)->getFont()->setBold(
true);
127 foreach($records as $index => $record) $sheet->setCellValue($char . ($index + 2),$record[$column] ?? null);
128 $sheet->getColumnDimension($char)->setAutoSize(
true);
130 \Rsi\Http::downloadHeaders($this->downloadName .
'.' . ($ext ?: strtolower($format)),$content_type);
131 \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet,$format)->save(
'php://output');
147 $this->
download($records,$this->pdfWriter,
'pdf');
151 $root = new \SimpleXmlElement(
'<records />');
152 foreach($records as $record){
153 $node = $root->addChild(
'record');
154 foreach($record as $key => $value) $node->addChild($key,htmlspecialchars($value));
156 \Rsi\Http::downloadHeaders($this->downloadName .
'.xml');
157 print($root->asXML());
161 \Rsi\Http::downloadHeaders($this->downloadName .
'.json');
162 print(json_encode($records));
166 $search = $order = [];
167 if(is_array($param = $this->request->complex(
'search')))
foreach($param as $column => $values)
169 array_key_exists($column,$this->columns) && is_array($values) &&
172 foreach($values as $type => $value)
if(!in_array($type,$types))
173 $this->fred->externalError(
'Invalid search type',[
'class' => get_called_class()] + compact(
'column',
'type'));
174 $search[$column] = $values;
176 if(is_array($param = $this->request->complex(
'order')))
foreach($param as $column => $type)
177 if(array_key_exists($column,$this->columns) && \Rsi\Record::get($this->columns[$column],self::ORDER,
true))
178 $order[$column] = (
int)$type;
183 $session = $this->_controller->session;
184 $session->set($this->
sessionKey(
'search'),$search);
185 $session->set($this->
sessionKey(
'order'),$order);
187 $offset = max(0,(
int)$this->request->offset);
188 $limit = min(max(0,(
int)$this->request->limit) ?: $this->batchSize,$this->batchSize);
189 $records = $this->provider->search($search,$order,$params = $this->params,$offset,$limit);
190 if($this->parentKey)
foreach($records as $index => $record) $records[$index][$this->parentKey] =
191 ($value = $record[$this->key] ?? null) && $this->provider->exists($this->parentKey,$value,$params);
192 $this->request->result = [
193 'records' => $records,
194 'eof' => count($records) < $limit
200 $this->request->result = $totals = [];
201 foreach($this->columns as $column => $config)
202 if($total = \
Rsi\Record::get($config,self::TOTAL)) $totals[$column] = $total;
206 $this->request->result[
'totals'] = $totals = $this->provider->totals($search,$totals,$this->params);
212 if($config = \
Rsi\Record::get($this->columns,[$column = $this->request->column,self::GROUP])){
214 $this->request->result = $this->provider->group(
216 $config[
'group'] ?? \
Rsi\Record::key($this->columns),
217 $config[
'column'] ?? $column,
219 $config[
'limit'] ?? null
225 if($method = \
Rsi\Record::get($this->downloadFormats,$this->request->format)){
226 $this->request->cookie($this->downloadCookieKey,null,-1,[
'httponly' =>
false]);
228 call_user_func($method,$this->provider->search($search,$order,$this->params));
236 if(!array_key_exists($this->sequence,$search)) $search[
$this->sequence] = [];
238 $count = \Rsi\Record::get(
242 if($latest) $count--;
243 if($this->request->result[
'count'] = $count) $this->request->result[
'message'] = $this->trans->str(
244 $this->sequenceMessage,
245 [
'count' => $count,
'id' => $this->
id,
'caption' => $this->
caption()]
255 if(!$this->_columns){
256 $this->_columns = $this->
config(self::COLUMNS);
257 if(!$this->_columns){
258 $this->_columns = [$this->code => null];
259 if($this->descr) $this->_columns[$this->descr] = null;
266 if($this->_downloadFormats === null){
267 $this->_downloadFormats = $this->
config(self::DOWNLOAD_FORMATS);
268 if($this->_downloadFormats === null){
269 $this->_downloadFormats = [];
270 foreach(get_class_methods($this) as $method)
if((strlen($method) > 8) && (substr($method,0,8) ==
'download'))
271 $this->_downloadFormats[substr($method,8)] = [$this,$method];
273 elseif($this->_downloadFormats)
foreach($this->_downloadFormats as &$function)
274 if(is_array($function) && ($function[0] ==
'*')) $function[0] = $this;
const SEARCH_EXACT
Search for values identical to the reference value (this may also be an array with.
const SEARCH_LIKE
Search for values that match reference value with wildcard (? = 1 char, * = multiple chars)...
const SEARCH_FROM
Search for values larger than the reference value.
publish($property, $visibility=self::READABLE)
Publish a property (or hide it again).