| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TRatingList class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TRatingList.php 2562 2008-11-08 17:41:48Z Christophe.Boulain $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Includes TRadioButtonList class
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Web.UI.WebControls.TRadioButtonList');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TRatingList class.
|
|
|
20 |
*
|
|
|
21 |
* This class is EXPERIMENTAL.
|
|
|
22 |
*
|
|
|
23 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
24 |
* @author Bradley Booms <bradley[dot]booms[at]gmail[dot]com>
|
|
|
25 |
* @version $Id: TRatingList.php 2562 2008-11-08 17:41:48Z Christophe.Boulain $
|
|
|
26 |
* @package System.Web.UI.WebControls
|
|
|
27 |
* @since 3.0
|
|
|
28 |
*/
|
|
|
29 |
class TRatingList extends TRadioButtonList
|
|
|
30 |
{
|
|
|
31 |
/**
|
|
|
32 |
* Script path relative to the TClientScriptManager::SCRIPT_PATH
|
|
|
33 |
*/
|
|
|
34 |
const SCRIPT_PATH='prado/ratings';
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* @var array list of published rating images.
|
|
|
38 |
*/
|
|
|
39 |
private $_ratingImages = array();
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Sets the default repeat direction to horizontal.
|
|
|
43 |
*/
|
|
|
44 |
public function __construct()
|
|
|
45 |
{
|
|
|
46 |
parent::__construct();
|
|
|
47 |
$this->setRepeatDirection(TRepeatDirection::Horizontal);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @return boolean whether the items in the column can be edited. Defaults to false.
|
|
|
52 |
*/
|
|
|
53 |
public function getReadOnly()
|
|
|
54 |
{
|
|
|
55 |
return $this->getViewState('ReadOnly',false);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* @param boolean whether the items in the column can be edited
|
|
|
60 |
*/
|
|
|
61 |
public function setReadOnly($value)
|
|
|
62 |
{
|
|
|
63 |
$this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Wrapper for {@link setReadOnly ReadOnly} property.
|
|
|
68 |
* @return boolean whether the rating list can be edited. Defaults to true.
|
|
|
69 |
*/
|
|
|
70 |
public function getAllowInput()
|
|
|
71 |
{
|
|
|
72 |
return !$this->getReadOnly();
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Wrapper for {@link setReadOnly ReadOnly} property.
|
|
|
77 |
* @param boolean whether the rating list can be edited
|
|
|
78 |
*/
|
|
|
79 |
public function setAllowInput($value)
|
|
|
80 |
{
|
|
|
81 |
$this->setReadOnly(!TPropertyValue::ensureBoolean($value));
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Wrapper for {@link setReadOnly ReadOnly} property.
|
|
|
86 |
* @param boolean whether the rating list can be edited
|
|
|
87 |
*/
|
|
|
88 |
public function setEnabled($value)
|
|
|
89 |
{
|
|
|
90 |
$this->setReadOnly(!TPropertyValue::ensureBoolean($value));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* The repeat layout must be Table.
|
|
|
95 |
* @param string repeat layout type
|
|
|
96 |
* @throws TInvaliddataValueException when repeat layout is not Table.
|
|
|
97 |
*/
|
|
|
98 |
public function setRepeatLayout($value)
|
|
|
99 |
{
|
|
|
100 |
if($value!==TRepeatLayout::Table)
|
|
|
101 |
throw new TInvalidDataValueException('ratinglist_table_layout_only');
|
|
|
102 |
else
|
|
|
103 |
parent::setRepeatLayout($value);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* @return float rating value.
|
|
|
108 |
*/
|
|
|
109 |
public function getRating()
|
|
|
110 |
{
|
|
|
111 |
$rating = $this->getViewState('Rating', null);
|
|
|
112 |
if ($rating === null)
|
|
|
113 |
return $this->getSelectedIndex()+1;
|
|
|
114 |
else
|
|
|
115 |
return $rating;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* @param float rating value, also sets the selected Index
|
|
|
120 |
*/
|
|
|
121 |
public function setRating($value)
|
|
|
122 |
{
|
|
|
123 |
$value = TPropertyValue::ensureFloat($value);
|
|
|
124 |
$this->setViewState('Rating', $value, null);
|
|
|
125 |
$index = $this->getRatingIndex($value);
|
|
|
126 |
parent::setSelectedIndex($index);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
public function setSelectedIndex($value)
|
|
|
130 |
{
|
|
|
131 |
$this->setRating($value+1);
|
|
|
132 |
parent::setSelectedIndex($value);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* @param float rating value
|
|
|
137 |
* @return int rating as integer
|
|
|
138 |
*/
|
|
|
139 |
protected function getRatingIndex($rating)
|
|
|
140 |
{
|
|
|
141 |
$interval = $this->getHalfRatingInterval();
|
|
|
142 |
$base = intval($rating)-1;
|
|
|
143 |
$remainder = $rating-$base-1;
|
|
|
144 |
return $remainder > $interval[1] ? $base+1 : $base;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* @param int change the rating selection index
|
|
|
149 |
*/
|
|
|
150 |
public function onSelectedIndexChanged($param)
|
|
|
151 |
{
|
|
|
152 |
$value = $this->getRating();
|
|
|
153 |
$value = TPropertyValue::ensureInteger($value);
|
|
|
154 |
$this->setRating($value);
|
|
|
155 |
parent::onSelectedIndexChanged($param);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* @return string control or html element ID for displaying a caption.
|
|
|
160 |
*/
|
|
|
161 |
public function getCaptionID()
|
|
|
162 |
{
|
|
|
163 |
return $this->getViewState('CaptionID', '');
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* @param string control or html element ID for displaying a caption.
|
|
|
168 |
*/
|
|
|
169 |
public function setCaptionID($value)
|
|
|
170 |
{
|
|
|
171 |
$this->setViewState('CaptionID', $value, '');
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
protected function getCaptionControl()
|
|
|
175 |
{
|
|
|
176 |
if(($id=$this->getCaptionID())!=='')
|
|
|
177 |
{
|
|
|
178 |
if($control=$this->getParent()->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 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* @param string rating style name
|
|
|
290 |
* @return string URL of the css style file
|
|
|
291 |
*/
|
|
|
292 |
protected function publishStyle($style)
|
|
|
293 |
{
|
|
|
294 |
$cs = $this->getPage()->getClientScript();
|
|
|
295 |
$url = $this->getAssetUrl($style.'.css');
|
|
|
296 |
if(!$cs->isStyleSheetFileRegistered($url))
|
|
|
297 |
$cs->registerStyleSheetFile($url, $url);
|
|
|
298 |
return $url;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* @param string rating style name
|
|
|
303 |
* @param string rating image file extension, default is '.gif'
|
|
|
304 |
* @return array URL of publish the rating images
|
|
|
305 |
*/
|
|
|
306 |
protected function publishImages($style, $fileExt='.gif')
|
|
|
307 |
{
|
|
|
308 |
$types = array('blank', 'selected', 'half', 'combined');
|
|
|
309 |
$files = array();
|
|
|
310 |
foreach($types as $type)
|
|
|
311 |
$files[$type] = $this->getAssetUrl("{$style}_{$type}{$fileExt}");
|
|
|
312 |
return $files;
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
/**
|
|
|
316 |
* @param string asset file in the self::SCRIPT_PATH directory.
|
|
|
317 |
* @return string asset file url.
|
|
|
318 |
*/
|
|
|
319 |
protected function getAssetUrl($file='')
|
|
|
320 |
{
|
|
|
321 |
$base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl();
|
|
|
322 |
return $base.'/'.self::SCRIPT_PATH.'/'.$file;
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
/**
|
|
|
326 |
* Add rating style class name to the class attribute
|
|
|
327 |
* when {@link setReadOnly ReadOnly} property is true and when the
|
|
|
328 |
* {@link setCssClass CssClass} property is empty.
|
|
|
329 |
* @param THtmlWriter renderer
|
|
|
330 |
*/
|
|
|
331 |
public function render($writer)
|
|
|
332 |
{
|
|
|
333 |
$writer->addAttribute('id',$this->getClientID());
|
|
|
334 |
$this->getPage()->getClientScript()->registerPostBackControl(
|
|
|
335 |
$this->getClientClassName(), $this->getPostBackOptions());
|
|
|
336 |
parent::render($writer);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* Gets the name of the javascript class responsible for performing postback for this control.
|
|
|
341 |
* This method overrides the parent implementation.
|
|
|
342 |
* @return string the javascript class name
|
|
|
343 |
*/
|
|
|
344 |
protected function getClientClassName()
|
|
|
345 |
{
|
|
|
346 |
return 'Prado.WebUI.TRatingList';
|
|
|
347 |
}
|
|
|
348 |
}
|
|
|
349 |
|