FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Grid.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use \Rsi\Fred\Controller\Widget;
6 
7 class Grid extends \Rsi\Fred\Controller\View\Html\Widget{
8 
9  protected function renderHeader(){
10  $captions = $edit_alls = '';
11  $has_edit_all = false;
12  foreach($this->_widget->widgets as $id => $widget){
13  $hidden = $widget->display <= Widget::DISPLAY_HIDDEN;
14  $captions .= $this->html->th(
15  $hidden ? null : $widget->caption($id),
16  $attribs = ['class' => 'id-' . $id . ($widget->required && !$hidden ? ' fred-required' : '')]
17  );
18  $edit_all = '';
19  if(in_array($id,$this->_widget->editAll) && ($widget->display >= Widget::DISPLAY_WRITEABLE)){
20  $edit_all = $this->_view->createWidget([$this->_id,'editAll',$id],$widget)->render();
21  $has_edit_all = true;
22  }
23  $edit_alls .= $this->html->th($edit_all,$attribs);
24  }
25  return $this->html->thead(
26  $this->html->tr($captions . $this->html->th('',['class' => 'fred-actions']),['class' => 'fred-captions']) .
27  ($has_edit_all ? $this->html->tr($edit_alls . $this->html->th(''),['class' => 'fred-edit-all']) : '')
28  );
29  }
30 
31  protected function renderFooter(){
32  if(!$this->_widget->total) return null;
33  $totals = '';
34  foreach($this->_widget->widgets as $id => $widget){
35  $content = $types = null;
36  if(array_key_exists($id,$this->_widget->total)){
37  if(!is_array($types = $this->_widget->total[$id])) $types = [$types];
38  foreach($types as $type) $content .= $this->html->div('',null,'fred-total-' . $type);
39  }
40  $totals .= $this->html->th($content,['class' => 'id-' . $id . ($types ? ' total-' . implode('-',$types) : '')]);
41  }
42  return $this->html->tfoot($this->html->tr($totals . $this->html->th(''),['class' => 'fred-total']));
43  }
44 
45  protected function renderRow($index,$record = null,$raw = false){
46  $row = '';
47  foreach($this->_widget->widgets as $id => $widget){
48  $widget = $this->_view->createWidget([$this->_id,$index,$id],$widget);
49  $row .= $this->html->td(
50  $widget->render($record ? ($record[$id] ?? null) : $widget->defaultValue,$raw),
51  ['class' => 'id-' . $id]
52  );
53  }
54  return $this->html->tr(
55  $row . $this->html->td('',['class' => 'fred-actions']),
56  ['id' => $this->_id . '__' . $index,'class' => 'fred-' . ($record === null ? $index : 'grid')]
57  );
58  }
59 
60  public function render($value = null,$raw = false){
61  $rows = $this->renderRow('template');
62  $index = 0;
63  if($value) foreach($value as $record) $rows .= $this->renderRow($index++,$record,$raw);
64  return $this->container(
65  $this->html->table(
66  $this->renderHeader() .
67  $this->renderFooter() .
68  $this->html->tbody($rows),
69  array_merge($this->attribs(),['class' => 'fred-grid'])
70  ),
71  $this->_widget->display >= Widget::DISPLAY_WRITEABLE ? 'writeable' : 'readable'
72  );
73  }
74 
75 }
const DISPLAY_WRITEABLE
Show editable input element(s).
Definition: Widget.php:39
render($value=null, $raw=false)
Definition: Grid.php:60
renderRow($index, $record=null, $raw=false)
Definition: Grid.php:45
container($content, $display)
Definition: Widget.php:42
const DISPLAY_HIDDEN
Do not show (only hidden)
Definition: Widget.php:36