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  if(!is_array($value)) return [];
23  foreach($value as $index => $record) foreach($this->widgets as $id => $widget)
24  $value[$index][$id] = $widget->convert($record[$id] ?? null);
25  return $value;
26  }
27 
28  protected function purgeBase($value){
29  $result = [];
30  $index = 0;
31  if(is_array($value)) foreach($value as $key => $record) if(is_numeric($key) && ($this->_executing || $key >= 0)){
32  $array = [];
33  $empty = true;
34  foreach($this->widgets as $id => $widget)
35  if(($array[$id] = $widget->purge(\Rsi\Record::get($record,$id))) != $widget->defaultValue) $empty = false;
36  if(!$empty) $result[$this->_executing ? $key : $index++] = $array;
37  }
38  return $result;
39  }
40 
41  protected function executeChild($id){
42  if(($id = array_slice($id,1)) && array_key_exists($widget_id = array_shift($id),$this->widgets)) $this->widgets[$widget_id]->execute($id);
43  else parent::executeChild($id);
44  }
45 
46  protected function checkMin($value){
47  return parent::checkMin(count($value));
48  }
49 
50  protected function checkMax($value){
51  return parent::checkMax(count($value));
52  }
53 
54  protected function checkRecords($value){
55  foreach($value as $index => $record) foreach($this->widgets as $id => $widget){
56  $widget->indexes = array_merge($this->indexes,[$index]);
57  if($error = $widget->check($record[$id] ?? null)){
58  $this->log->debug("Validation error '$error' for record $index, column '$id'",__FILE__,__LINE__,['records' => $value,'indexes' => $widget->indexes]);
59  return false;
60  }
61  }
62  return true;
63  }
64 
65  protected function checkUnique($value){
66  foreach($this->unique as $id){
67  $values = array_filter(array_column($value,$id));
68  if(count($values) != count(array_unique($values))) return false;
69  }
70  return true;
71  }
72 
73 }
Rsi\Fred\Controller\Widget\Grid
Definition: Grid.php:5
Rsi
Rsi\Fred\Controller\Widget\Grid\checkRecords
checkRecords($value)
Definition: Grid.php:54
Rsi\Fred\Controller\Widget\Grid\clientConfig
clientConfig()
Public configuration.
Definition: Grid.php:15
Rsi\Fred\Controller\Widget\Grid\checkMin
checkMin($value)
Definition: Grid.php:46
Rsi\Fred\Controller\Widget\Grid\executeChild
executeChild($id)
Execute an action on a child widget.
Definition: Grid.php:41
Rsi\Fred\Controller\Widget\Grid\checkMax
checkMax($value)
Definition: Grid.php:50
Rsi\Fred\Controller\Widget\Grid\purgeBase
purgeBase($value)
Purge function that will be called first.
Definition: Grid.php:28
Rsi\Fred\Controller\Widget\Grid\TOTAL
const TOTAL
Show total for column (key = widget, value = total type).
Definition: Grid.php:9
Rsi\Fred\Controller\Widget
Definition: Char.php:3
Rsi\Fred\Controller\Widget\Grid\$editAll
$editAll
Definition: Grid.php:11
Rsi\Fred\Controller\Widget\Grid\$total
$total
Definition: Grid.php:13
Rsi\Fred\Controller\Widget\Grid\EDIT_ALL
const EDIT_ALL
Which widgets can be edited all at once.
Definition: Grid.php:7
Rsi\Fred\Controller\Widget\Grid\$unique
$unique
Definition: Grid.php:12
Rsi\Fred\Controller\Widget\Grid\UNIQUE
const UNIQUE
Widgets which value must be unique in the column.
Definition: Grid.php:8
Rsi\Fred\Controller\Widget\Grid\checkUnique
checkUnique($value)
Definition: Grid.php:65
Rsi\Fred\Controller\Widget\Grid\convert
convert($value)
Convert a value from user format to standard, internal format.
Definition: Grid.php:21