FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Widget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Widget extends \Rsi\Thing{
6 
7  protected $_id = null;
8  protected $_name = null;
9  protected $_view = null;
10  protected $_widget = null;
11 
12  protected $_inputType = 'text';
13 
14  protected $_displayNames = [];
15 
16  public function __construct($id,$view,$widget){
17  $this->_id = is_array($id) ? implode('__',$id) : $id;
18  $this->_name = 'widget[' . str_replace('__','][',$this->_id) . ']';
19  $this->_view = $view;
20  $this->_widget = $widget;
21  }
22 
23  public function renderCaption(){
24  if($this->_widget->display <= \Rsi\Fred\Controller\Widget::DISPLAY_HIDDEN) return false;
25  return ($caption = $this->_widget->caption($this->_id)) ? $this->html->label($caption,['for' => $this->_id]) : null;
26  }
27 
28  protected function attribs(){
29  return [];
30  }
31 
32  protected function containerClass($display){
33  $class = ['widget',strtolower(\Rsi\File::basename(get_called_class())),$display];
34  if($this->_widget->required){
35  $class[] = 'required';
36  if(is_array($this->_widget->required)) $class[] = 'conditonal';
37  }
38  if(array_key_exists($this->_id,$this->_view->component('request')->errors)) $class[] = 'error';
39  return 'fred-' . implode(' fred-',$class);
40  }
41 
42  protected function container($content,$display){
43  return $this->html->div(
44  $this->html->fieldset($content),
45  ['title' => $this->_widget->hint($this->_id)],
46  $this->containerClass($display),
48  );
49  }
50 
51  protected function renderHidden($value,$raw){
52  return $this->html->input($value,null,'hidden',$this->_name);
53  }
54 
55  protected function renderMinimal($value,$raw){
56  return $this->container(
57  $this->renderHidden($value,$raw) .
58  $this->html->_div($raw ? $value : $this->_widget->format($value),null,'fred-value'),
59  'minimal'
60  );
61  }
62 
63  protected function renderInput($value,$raw,$attribs = null){
64  return $this->html->input(
65  $raw ? $value : $this->_widget->format($value),
66  array_merge($this->attribs(),$attribs ?: []),
69  );
70  }
71 
72  protected function renderReadable($value,$raw){
73  return $this->container(
74  $this->renderInput($value,$raw,[$this->_inputType == 'text' ? 'readonly' : 'disabled' => true]),
75  'readable'
76  );
77  }
78 
79  protected function renderWriteable($value,$raw){
80  return $this->container(
81  $this->renderInput($value,$raw),
82  'writeable'
83  );
84  }
85 
86  public function render($value = null,$raw = false){
87  $func_name = null;
88  foreach(array_reverse($this->displayNames) as $display => $name)
89  if(($display <= $this->_widget->display) && method_exists($this,$method = 'render' . $name))
90  return call_user_func([$this,$method],$value,$raw);
91  return false;
92  }
93 
94  protected function getDefaultValue(){
95  return $this->_widget->defaultValue;
96  }
97 
98  protected function getDisplayNames(){
99  if(!$this->_displayNames){
100  foreach($this->_widget->constants('DISPLAY_') as $name => $value)
101  $this->_displayNames[$value] = ucfirst(strtolower($name));
102  ksort($this->_displayNames);
103  }
104  return $this->_displayNames;
105  }
106 
107  protected function getHtml(){
108  return $this->_view->component('html');
109  }
110 
111 }
__construct($id, $view, $widget)
Definition: Widget.php:16
Basic object.
Definition: Thing.php:13
$display
Default display mode for widgets.
Definition: Controller.php:24
Framework for Rapid and Easy Development.
Definition: Fred.php:18
renderInput($value, $raw, $attribs=null)
Definition: Widget.php:63
container($content, $display)
Definition: Widget.php:42
const DISPLAY_HIDDEN
Do not show (only hidden)
Definition: Widget.php:36
render($value=null, $raw=false)
Definition: Widget.php:86