]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/JuiControls/TJuiControlOptions.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / JuiControls / TJuiControlOptions.php
1 <?php
2 /**
3  * TJuiControlOptions class file.
4  *
5  * @author Fabio Bas <ctrlaltca@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2013-2015 PradoSoft
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.JuiControls
10  */
11
12 /**
13  * IJuiOptions interface
14  *
15  * IJuiOptions is the interface that must be implemented by controls using
16  * {@link TJuiControlOptions}.
17  *
18  * @author Fabio Bas <ctrlaltca@gmail.com>
19  * @package System.Web.UI.JuiControls
20  * @since 3.3
21  */
22 interface IJuiOptions
23 {
24         public function getWidget();
25         public function getWidgetID();
26         public function getOptions();
27         public function getValidOptions();
28         public function getValidEvents();
29 }
30
31 /**
32  * TJuiControlOptions interface
33  *
34  * TJuiControlOptions is an helper class that can collect a list of options
35  * for a control. The control must implement {@link IJuiOptions}.
36  * The options are validated againg an array of valid options provided by the control itself.
37  * Since component properties are case insensitive, the array of valid options is used
38  * to ensure the option name has the correct case.
39  * The options array can then get retrieved using {@link toArray} and applied to the jQuery-ui widget.
40  * In addition to the options, this class will render the needed javascript to raise a callback
41  * for any event for which an handler is defined in the control.
42  *
43  * @author Fabio Bas <ctrlaltca@gmail.com>
44  * @package System.Web.UI.JuiControls
45  * @since 3.3
46  */
47 class TJuiControlOptions
48 {
49         /**
50          * @var TMap map of javascript options.
51          */
52         private $_options;
53         /**
54          * @var TControl parent control.
55          */
56         private $_control;
57
58         /**
59          * Constructor. Set the parent control owning these options.
60          * @param TControl parent control
61          */
62         public function __construct($control)
63         {
64     $this->setControl($control);
65         }
66
67         /**
68          * Sets the parent control.
69          * @param TControl $control
70          * @throws THttpException
71          */
72         public function setControl($control)
73         {
74           if(!$control instanceof IJuiOptions)
75             throw new THttpException(500,'juioptions_control_invalid',$control->ID);
76           $this->_control=$control;
77         }
78
79         /**
80          * Sets a named options with a value. Options are used to store and retrive
81          * named values for the javascript control.
82          * @param string option name.
83          * @param mixed option value.
84          * @throws THttpException
85          */
86         public function __set($name,$value)
87         {
88                 if($this->_options===null)
89                         $this->_options=array();
90
91                 foreach($this->_control->getValidOptions() as $option)
92                 {
93                         if(0 == strcasecmp($name, $option))
94                         {
95                                 $low = strtolower($value);
96                                 if($low === 'null')
97                                 {
98                                         $this->_options[$option] = null;
99                                 } elseif($low === 'true') {
100                                         $this->_options[$option] = true;
101                                 } elseif($low === 'false') {
102                                         $this->_options[$option] = false;
103                                 } elseif(is_numeric($value)) {
104                                         // trick to get float or integer automatically when needed
105                                         $this->_options[$option] = $value + 0;
106                                 } elseif(substr($low,0,8)=='function') {
107                                         $this->_options[$option] = new TJavaScriptLiteral($value);
108                                 } else {
109                                         $this->_options[$option] = $value;
110                                 }
111                                 return;
112                         }
113                 }
114
115                 throw new TConfigurationException('juioptions_option_invalid',$this->_control->ID, $name);
116         }
117
118         /**
119          * Gets an option named value. Options are used to store and retrive
120          * named values for the base active controls.
121          * @param string option name.
122          * @return mixed options value or null if not set.
123          */
124         public function __get($name)
125         {
126                 if($this->_options===null)
127                         $this->_options=array();
128
129                 foreach($this->_control->getValidOptions() as $option)
130                 {
131                         if(0 == strcasecmp($name, $option) && isset($this->_options[$option]))
132                         {
133                                 return $this->_options[$option];
134                         }
135                 }
136
137                 return null;
138         }
139
140         /**
141          * Only serialize the options itself, not the corresponding parent control.
142          * @return mixed array with the names of all variables of that object that should be serialized.
143          */
144         public function __sleep() {
145           return array('_options');
146         }
147
148         /**
149          * @return Array of active control options
150          */
151         public function toArray()
152         {
153                 $ret= ($this->_options===null) ? array() : $this->_options;
154
155                 foreach($this->_control->getValidEvents() as $event)
156                         if($this->_control->hasEventHandler('on'.$event))
157                                 $ret[$event]=new TJavaScriptLiteral("function( event, ui ) { Prado.JuiCallback(".TJavascript::encode($this->_control->getUniqueID()).", ".TJavascript::encode($event).", event, ui, this); }");
158
159                 return $ret;
160         }
161
162         /**
163          * Raise the specific callback event handler of the target control.
164          * @param mixed callback parameters
165          */
166         public function raiseCallbackEvent($param)
167         {
168                 $callbackParam=$param->CallbackParameter;
169                 if(isset($callbackParam->event))
170                 {
171                         $eventName = 'On'.ucfirst($callbackParam->event);
172                         if($this->_control->hasEventHandler($eventName))
173                         {
174                                 $this->_control->$eventName( new TJuiEventParameter(
175                                         $this->_control->getResponse(),
176                                         isset($callbackParam->ui) ? $callbackParam->ui : null)
177                                 );
178                         }
179                 }
180         }
181 }