FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Grid.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Grid extends \Rsi\Fred\Controller\Widget{
6 
7  const EDIT_ALL = 'editAll'; //!< Which widgets can be edited all at once.
8  const UNIQUE = 'unique'; //!< Widgets which value must be unique in the column.
9  const TOTAL = 'total'; //!< Show total for column (key = widget, value = total type).
10 
11  public $editAll = [];
12  public $unique = [];
13  public $total = [];
14 
15  public function clientConfig(){
16  $captions = [];
17  foreach($this->widgets as $id => $widget) $captions[$id] = strip_tags($widget->caption($id));
18  return array_merge(parent::clientConfig(),$this->get([self::EDIT_ALL,self::UNIQUE,self::TOTAL]),['captions' => $captions]);
19  }
20 
21  public function convert($value){
22  foreach($value as $index => $record) foreach($this->widgets as $id => $widget)
23  $value[$index][$id] = $widget->convert($record[$id]);
24  return $value;
25  }
26 
27  protected function purgeBase($value){
28  $result = [];
29  if(is_array($value)) foreach($value as $index => $record) if(is_numeric($index)){
30  $array = [];
31  $empty = true;
32  foreach($this->widgets as $id => $widget)
33  if(($array[$id] = $widget->purge(\Rsi\Record::get($record,$id))) != $widget->defaultValue) $empty = false;
34  if(!$empty) $result[] = $array;
35  }
36  return $result;
37  }
38 
39  protected function executeChild($id){
40  if(array_key_exists($id = array_pop($id),$this->widgets)) $this->widgets[$this->request->widgetId = $id]->execute();
41  else parent::executeChild($id);
42  }
43 
44  protected function checkMin($value,$index = null){
45  return parent::checkMin(count($value),$index);
46  }
47 
48  protected function checkMax($value,$index = null){
49  return parent::checkMax(count($value),$index);
50  }
51 
52  protected function checkRecords($value,$index = null){
53  foreach($value as $index => $record) foreach($this->widgets as $id => $widget) if($error = $widget->check($record[$id] ?? null,$index)){
54  $this->log->debug("Validation error '$error' for record $index, column '$id'",__FILE__,__LINE__,['records' => $value]);
55  return false;
56  }
57  return true;
58  }
59 
60  protected function checkUnique($value,$index = null){
61  foreach($this->unique as $id){
62  $values = array_filter(array_column($value,$id));
63  if(count($values) != count(array_unique($values))) return false;
64  }
65  return true;
66  }
67 
68 }
const TOTAL
Show total for column (key = widget, value = total type).
Definition: Grid.php:9
checkMin($value, $index=null)
Definition: Grid.php:44
const UNIQUE
Widgets which value must be unique in the column.
Definition: Grid.php:8
checkUnique($value, $index=null)
Definition: Grid.php:60
const EDIT_ALL
Which widgets can be edited all at once.
Definition: Grid.php:7
checkMax($value, $index=null)
Definition: Grid.php:48
checkRecords($value, $index=null)
Definition: Grid.php:52