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