5 use \Rsi\Fred\Controller;
6 use \Rsi\Fred\Controller\Provider;
73 $this->
publish([
'key',
'descr',
'parentKey',
'search']);
77 return array_key_exists(self::CAPTION,$config = $this->columns[$column] ?: [])
78 ? $this->trans->str($config[self::CAPTION])
79 : $this->trans->id($column);
83 return array_merge([
'widget',
'table',$this->
id ?: $this->provider->id],is_array($key) ? $key : [$key]);
87 $session = $this->_controller->session;
89 foreach($this->columns as $column => $config){
91 $columns[$column] = $config;
93 $config = array_merge(
94 parent::clientConfig(),
95 $this->
get([self::CODE,self::DESCR,self::PARENT_KEY,self::DETAILS,self::LINK,self::SEARCH]),
96 [self::COLUMNS => $columns]
98 if($search = $session->get($this->sessionKey(
'search'))) $config[
self::SEARCH] = $search;
99 if($order = $session->get($this->sessionKey(
'order')) ?: $this->provider->order) $config[
'defaultOrder'] = $order;
103 if($this->downloadBusyMessage) $config[
'downloadBusyMessage'] = $this->trans->str($this->downloadBusyMessage);
109 return $this->provider->trans($this->key,$this->code,$value,$this->params);
117 return $this->provider->trans($this->code,$this->key,$value,$this->params);
121 return $this->
format($value);
125 return $this->provider->exists($this->key,$value,$this->params);
128 protected function download($records,$format,$ext =
null,$content_type =
null){
129 $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
130 $sheet = $spreadsheet->setActiveSheetIndex(0)->setTitle($this->downloadName);
131 if(!($columns = $this->request->record(
'columns')))
132 $this->fred->externalError(
'Invalid columns',[
'columns' => $columns]);
134 foreach(array_values($columns) as $index => $column){
135 if(!array_key_exists($column,$this->columns)) $this->fred->externalError(
'Unknown column',[
'column' => $column]);
136 $char = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($index + 1);
137 $sheet->getStyle($cell = $char . 1)->getFont()->setBold(
true);
138 if($caption = $this->
columnCaption($column)) $sheet->setCellValue($cell,$caption);
139 foreach($records as $index => $record) $sheet->setCellValue($char . ($index + 2),$record[$column] ??
null);
140 $sheet->getColumnDimension($char)->setAutoSize(
true);
142 \Rsi\Http::downloadHeaders($this->downloadName .
'.' . ($ext ?: strtolower($format)),$content_type);
143 \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet,$format)->save(
'php://output');
159 $this->
download($records,$this->pdfWriter,
'pdf');
163 $root = new \SimpleXmlElement(
'<records />');
164 foreach($records as $record){
165 $node = $root->addChild(
'record');
166 foreach($record as $key => $value) $node->addChild($key,htmlspecialchars($value));
168 \Rsi\Http::downloadHeaders($this->downloadName .
'.xml');
169 print($root->asXML());
173 \Rsi\Http::downloadHeaders($this->downloadName .
'.json');
174 print(json_encode($records));
178 $search = $order = [];
179 if(is_array($param = $this->request->complex(
'search')))
foreach($param as $column => $values)
181 array_key_exists($column,$this->columns) && is_array($values) &&
184 foreach($values as $type => $value)
if(!in_array($type,$types))
185 $this->fred->externalError(
'Invalid search type',[
'class' => get_called_class()] + compact(
'column',
'type'));
186 $search[$column] = $values;
188 if(is_array($param = $this->request->complex(
'order')))
foreach($param as $column => $type)
189 if(array_key_exists($column,$this->columns) && \
Rsi\Record::get($this->columns[$column],self::ORDER,
true))
190 $order[$column] = (int)$type;
195 $session = $this->_controller->session;
196 $session->set($this->
sessionKey(
'search'),$search);
197 $session->set($this->
sessionKey(
'order'),$order);
199 $offset = max(0,(
int)$this->request->offset);
200 $limit = min(max(0,(
int)$this->request->limit) ?: $this->batchSize,$this->batchSize);
201 $records = $this->provider->search($search,$order,$params = $this->params,$offset,$limit);
202 if($this->parentKey)
foreach($records as $index => $record) $records[$index][$this->parentKey] =
203 ($value = $record[$this->key] ??
null) && $this->provider->exists($this->parentKey,$value,$params);
204 $this->request->result = [
205 'records' => $records,
206 'eof' => count($records) < $limit
212 $this->request->result = $totals = [];
213 foreach($this->columns as $column => $config)
214 if($total = \
Rsi\Record::get($config,self::TOTAL)) $totals[$column] = $total;
218 $this->request->result[
'totals'] = $totals = $this->provider->totals($search,$totals,$this->params);
224 if($config = \
Rsi\Record::get($this->columns,[$column = $this->request->column,self::GROUP])){
226 $this->request->result = $this->provider->group(
228 $config[
'group'] ?? \
Rsi\Record::key($this->columns),
229 $config[
'column'] ?? $column,
231 $config[
'limit'] ??
null
237 if($method = \
Rsi\Record::get($this->downloadFormats,$this->request->format)){
238 $this->request->cookie($this->downloadCookieKey,
null,-1,[
'httponly' =>
false]);
240 call_user_func($method,$this->provider->search($search,$order,$this->params));
248 if(!array_key_exists($this->sequence,$search)) $search[
$this->sequence] = [];
250 $count = \Rsi\Record::get(
254 if($latest) $count--;
255 if($this->request->result[
'count'] = $count) $this->request->result[
'message'] = $this->trans->str(
256 $this->sequenceMessage,
257 [
'count' => $count,
'id' => $this->
id,
'caption' => $this->
caption()]
267 if(!$this->_columns){
268 $this->_columns = $this->
config(self::COLUMNS);
269 if(!$this->_columns){
270 $this->_columns = [$this->code =>
null];
271 if($this->descr) $this->_columns[$this->descr] =
null;
278 if($this->_downloadFormats ===
null){
279 $this->_downloadFormats = $this->
config(self::DOWNLOAD_FORMATS);
280 if($this->_downloadFormats ===
null){
281 $this->_downloadFormats = [];
282 foreach(get_class_methods($this) as $method)
if((strlen($method) > 8) && (substr($method,0,8) ==
'download'))
283 $this->_downloadFormats[substr($method,8)] = [$this,$method];
285 elseif($this->_downloadFormats)
foreach($this->_downloadFormats as &$function)
286 if(is_array($function) && ($function[0] ==
'*')) $function[0] = $this;