]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TRatingList.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TRatingList.php
1 <?php
2 /**
3  * TRatingList class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.WebControls
10  */
11
12 /**
13  * Includes TRadioButtonList class
14  */
15 Prado::using('System.Web.UI.WebControls.TRadioButtonList');
16
17 /**
18  * TRatingList class.
19  *
20  * This class is EXPERIMENTAL.
21  *
22  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
23  * @author Bradley Booms <bradley[dot]booms[at]gmail[dot]com>
24  * @package System.Web.UI.WebControls
25  * @since 3.0
26  */
27 class TRatingList extends TRadioButtonList
28 {
29         /**
30          * Script path relative to the TClientScriptManager::SCRIPT_PATH
31          */
32         const SCRIPT_PATH='prado/ratings';
33
34         /**
35          * @var array list of published rating images.
36          */
37         private $_ratingImages = array();
38
39         /**
40          * Sets the default repeat direction to horizontal.
41          */
42         public function __construct()
43         {
44                 parent::__construct();
45                 $this->setRepeatDirection(TRepeatDirection::Horizontal);
46         }
47
48         /**
49          * @return boolean whether the items in the column can be edited. Defaults to false.
50          */
51         public function getReadOnly()
52         {
53                 return $this->getViewState('ReadOnly',false);
54         }
55
56         /**
57          * @param boolean whether the items in the column can be edited
58          */
59         public function setReadOnly($value)
60         {
61                 $this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
62         }
63
64         /**
65          * Wrapper for {@link setReadOnly ReadOnly} property.
66          * @return boolean whether the rating list can be edited. Defaults to true.
67          */
68         public function getAllowInput()
69         {
70                 return !$this->getReadOnly();
71         }
72
73         /**
74          * Wrapper for {@link setReadOnly ReadOnly} property.
75          * @param boolean whether the rating list can be edited
76          */
77         public function setAllowInput($value)
78         {
79                 $this->setReadOnly(!TPropertyValue::ensureBoolean($value));
80         }
81
82         /**
83          * Wrapper for {@link setReadOnly ReadOnly} property.
84          * @param boolean whether the rating list can be edited
85          */
86         public function setEnabled($value)
87         {
88                 $this->setReadOnly(!TPropertyValue::ensureBoolean($value));
89         }
90
91         /**
92          * The repeat layout must be Table.
93          * @param string repeat layout type
94          * @throws TInvaliddataValueException when repeat layout is not Table.
95          */
96         public function setRepeatLayout($value)
97         {
98                 if($value!==TRepeatLayout::Table)
99                         throw new TInvalidDataValueException('ratinglist_table_layout_only');
100                 else
101                         parent::setRepeatLayout($value);
102         }
103
104         /**
105          * @return float rating value.
106          */
107         public function getRating()
108         {
109                 $rating = $this->getViewState('Rating', null);
110                 if ($rating === null)
111                         return $this->getSelectedIndex()+1;
112                 else
113                         return $rating;
114         }
115
116         /**
117          * @param float rating value, also sets the selected Index
118          */
119         public function setRating($value)
120         {
121                 $value = TPropertyValue::ensureFloat($value);
122                 $this->setViewState('Rating', $value, null);
123                 $index = $this->getRatingIndex($value);
124                 parent::setSelectedIndex($index);
125         }
126
127         public function setSelectedIndex($value)
128         {
129                 $this->setRating($value+1);
130                 parent::setSelectedIndex($value);
131         }
132
133         /**
134          * @param float rating value
135          * @return int rating as integer
136          */
137         protected function getRatingIndex($rating)
138         {
139                 $interval = $this->getHalfRatingInterval();
140                 $base = intval($rating)-1;
141                 $remainder = $rating-$base-1;
142                 return $remainder > $interval[1] ? $base+1 : $base;
143         }
144
145         /**
146          * @param int change the rating selection index
147          */
148         public function onSelectedIndexChanged($param)
149         {
150                 $value = $this->getRating();
151                 $value = TPropertyValue::ensureInteger($value);
152                 $this->setRating($value);
153                 parent::onSelectedIndexChanged($param);
154         }
155
156         /**
157          * @return string control or html element ID for displaying a caption.
158          */
159         public function getCaptionID()
160         {
161                 return $this->getViewState('CaptionID', '');
162         }
163
164         /**
165          * @param string control or html element ID for displaying a caption.
166          */
167         public function setCaptionID($value)
168         {
169                 $this->setViewState('CaptionID', $value, '');
170         }
171
172         protected function getCaptionControl()
173         {
174                 if(($id=$this->getCaptionID())!=='')
175                 {
176                         if($control=$this->getPage()->findControl($id))
177                                 return $control;
178                         if($control=$this->getNamingContainer()->findControl($id))
179                                 return $control;
180                 }
181                 throw new TInvalidDataValueException(
182                         'ratinglist_invalid_caption_id',$id,$this->getID());
183         }
184
185         /**
186          * @return string caption text. Default is "Rate It:".
187          */
188         public function getCaption()
189         {
190                 return $this->getCaptionControl()->getText();
191         }
192
193         /**
194          * @return TRatingListStyle current rating style
195          */
196         public function setCaption($value)
197         {
198                 $this->getCaptionControl()->setText($value);
199         }
200
201         /**
202          * @param string set the rating style, default is "default"
203          */
204         public function setRatingStyle($value)
205         {
206            $this->setViewState('RatingStyle', $value, 'default');
207         }
208
209         /**
210          * @return TRatingListStyle current rating style
211          */
212         public function getRatingStyle()
213         {
214            return $this->getViewState('RatingStyle', 'default');
215         }
216
217         /**
218          * @return string rating style css class name.
219          */
220         protected function getRatingStyleCssClass()
221         {
222                 return 'TRatingList_'.$this->getRatingStyle();
223         }
224
225         /**
226          * Sets the interval such that those rating values within the interval
227          * will be considered as a half star rating.
228          * @param array rating display half value interval, default is array(0.3, 0.7);
229          */
230         public function setHalfRatingInterval($value)
231         {
232                 $this->setViewState('HalfRating',
233                                 TPropertyValue::ensureArray($value), array(0.3, 0.7));
234         }
235
236         /**
237          * @return array rating display half value interval, default is array(0.3, 0.7);
238          */
239         public function getHalfRatingInterval()
240         {
241                 return $this->getViewState('HalfRating', array(0.3, 0.7));
242         }
243
244         /**
245          * @return array list of post back options.
246          */
247         protected function getPostBackOptions()
248         {
249                 $options = parent::getPostBackOptions();
250                 $options['AutoPostBack'] = $this->getAutoPostBack();
251                 $options['ReadOnly'] = $this->getReadOnly();
252                 $options['Style'] = $this->getRatingStyleCssClass();
253                 $options['CaptionID'] = $this->getCaptionControlID();
254                 $options['SelectedIndex'] = $this->getSelectedIndex();
255                 $options['Rating'] = $this->getRating();
256                 $options['HalfRating'] = $this->getHalfRatingInterval();
257                 return $options;
258         }
259
260         /**
261          * @return string find the client ID of the caption control.
262          */
263         protected function getCaptionControlID()
264         {
265                 if(($id=$this->getCaptionID())!=='')
266                 {
267                         if($control=$this->getParent()->findControl($id))
268                         {
269                                 if($control->getVisible(true))
270                                         return $control->getClientID();
271                         }
272                         else
273                                 return $id;
274                 }
275                 return '';
276         }
277
278         /**
279          * Publish the the rating style css file and rating image files.
280          */
281         public function onPreRender($param)
282         {
283                 parent::onPreRender($param);
284                 $this->publishStyle($this->getRatingStyle());
285                 $this->_ratingImages = $this->publishImages($this->getRatingStyle());
286                 $this->registerClientScript();
287         }
288
289         /**
290          * @param string rating style name
291          * @return string URL of the css style file
292          */
293         protected function publishStyle($style)
294         {
295                 $cs = $this->getPage()->getClientScript();
296                 $url = $this->getAssetUrl($style.'.css');
297                 if(!$cs->isStyleSheetFileRegistered($url))
298                         $cs->registerStyleSheetFile($url, $url);
299                 return $url;
300         }
301
302         /**
303          * @param string rating style name
304          * @param string rating image file extension, default is '.gif'
305          * @return array URL of publish the rating images
306          */
307         protected function publishImages($style, $fileExt='.gif')
308         {
309                 $types = array('blank', 'selected', 'half', 'combined');
310                 $files = array();
311                 foreach($types as $type)
312                         $files[$type] = $this->getAssetUrl("{$style}_{$type}{$fileExt}");
313                 return $files;
314         }
315
316         /**
317          * Registers the relevant JavaScript.
318          */
319         protected function registerClientScript()
320         {
321                 $cs=$this->getPage()->getClientScript();
322                 $cs->registerPradoScript('ratings');
323         }
324
325         /**
326          * @param string asset file in the self::SCRIPT_PATH directory.
327          * @return string asset file url.
328          */
329         protected function getAssetUrl($file='')
330         {
331                 $base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl();
332                 return $base.'/'.self::SCRIPT_PATH.'/'.$file;
333         }
334
335         /**
336          * Add rating style class name to the class attribute
337          * when {@link setReadOnly ReadOnly} property is true and when the
338          * {@link setCssClass CssClass} property is empty.
339          * @param THtmlWriter renderer
340          */
341         public function render($writer)
342         {
343                 $writer->addAttribute('id',$this->getClientID());
344                 $this->getPage()->getClientScript()->registerPostBackControl(
345                         $this->getClientClassName(), $this->getPostBackOptions());
346                 parent::render($writer);
347         }
348
349         /**
350          * Gets the name of the javascript class responsible for performing postback for this control.
351          * This method overrides the parent implementation.
352          * @return string the javascript class name
353          */
354         protected function getClientClassName()
355         {
356                 return 'Prado.WebUI.TRatingList';
357         }
358 }
359