1 <?php
2 /**
3 * ItemListField.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This class describes the field displayed in a smart list.
13 * All properties of an instance of this class can be scalar, or a function taking as arguments :
14 * - $value The value of the cell
15 * - $field The field itself
16 * - $line All the values in the list results line
17 *
18 * @package List
19 */
20 class ItemListField {
21 /**
22 * The list of the properties that can be either scalar or callable
23 */
24 private static $callableProperties = array(
25 'class',
26 'title',
27 'href',
28 'target',
29 'onclick',
30 'style',
31 'unit',
32 'display',
33 );
34
35
36 /**
37 * The name of the field in the list. Must be unique for each field in a list
38 */
39 public $name,
40
41 /**
42 * The field name in the search table
43 *
44 * @var string
45 */
46 $field = null,
47
48 /**
49 * The class attribute to add to the result cell
50 *
51 * @var string|callable
52 */
53 $class = null,
54
55 /**
56 * The 'title' attribute on hover on the cell
57 *
58 * @var string|callable
59 */
60 $title = null,
61
62 /**
63 * This property, if set, will permit to open the set URL in the target defined by the property $target on a click event
64 *
65 * @var string|callable
66 */
67 $href = null,
68
69 /**
70 * The target where to open the URL defined in $href property
71 *
72 * @var string|callable
73 */
74 $target = null,
75
76 /**
77 * The 'onclick' attribute
78 *
79 * @var string|callable
80 */
81 $onclick = null,
82
83 /**
84 * The 'style' attribute
85 *
86 * @var string|callable
87 */
88 $style = null,
89
90 /**
91 * A unit to add after the value of the cell
92 *
93 * @var string|callable
94 */
95 $unit = null,
96
97 /**
98 * Define if you want a specific displaying for this cell
99 *
100 * @var string|callable
101 */
102 $display = null,
103
104 /**
105 * Display the widgets to sort the list by this field values
106 *
107 * @var boolean
108 */
109 $sort = true,
110
111 /**
112 * The sort value
113 *
114 * @var string (ASC or DESC)
115 */
116 $sortValue = null,
117
118 /**
119 * Displays the serach input for this field
120 *
121 * @var boolean
122 */
123 $search = true,
124
125 /**
126 * The search value
127 *
128 * @var string
129 */
130 $searchValue = null,
131
132 /**
133 * If set to true, this field will not be searched in the database
134 *
135 * @var boolean
136 */
137 $independant = false,
138
139 /**
140 * The label to display in the list header
141 *
142 * @var string
143 */
144 $label = null,
145
146 /**
147 * If set to true, this field will appear in the DOM, but wille be not visible
148 *
149 * @var boolean
150 */
151 $hidden = false,
152
153 /**
154 * The list the field is associated with
155 *
156 * @var ItemList
157 */
158 $list = null;
159
160 /**
161 * Constructor
162 *
163 * @param string $name The field name
164 * @param array $param The field parameters
165 * @param ItemList $list The list the field is associated with
166 */
167 public function __construct($name, $param, ItemList $list){
168 $this->name = $name;
169 foreach($param as $key => $value){
170 $this->$key = $value;
171 }
172
173 if(!$this->field) {
174 $this->field = $this->name;
175 }
176
177 $this->list = $list;
178 }
179
180 /**
181 * Get the Search SQL expression on this field
182 *
183 * @param array $binds The binded values, passe by reference that will be filled
184 *
185 * @return string The SQL expression for the search on this field
186 */
187 public function getSearchCondition(&$binds){
188 if($this->searchValue !== null) {
189 return DBExample::make(
190 array(
191 $this->field => array(
192 '$like' => '%' . $this->getInput()->dbvalue() . '%'
193 )
194 ),
195 $binds
196 );
197 }
198 }
199
200
201 /**
202 * Get the input corresponding to the field
203 *
204 * @return FormInput the input instance
205 */
206 public function getInput(){
207 if(!is_array($this->search)) {
208 $this->search = array(
209 'type' => 'text'
210 );
211 }
212
213 switch($this->search['type']){
214 case 'select' :
215 $input = new SelectInput(array(
216 'options' => $this->search['options'],
217 'invitation' => isset($this->search['invitation']) ? $this->search['invitation'] : null,
218 'emptyValue' => isset($this->search['emptyValue']) ? $this->search['emptyValue'] : null,
219 'attributes' => array(
220 'ko-value' => 'search',
221 'ko-class' => "search() ? 'alert-info not-empty' : 'empty'"
222 )
223 ));
224 break;
225
226 case 'checkbox' :
227 $input = new CheckboxInput(array(
228 'attributes' => array(
229 'ko-checked' => 'search'
230 )
231 ));
232 break;
233
234 case 'date' :
235 $input = new DatetimeInput(array(
236 'id' => uniqid(),
237 'after' => Icon::make(array(
238 'icon' => 'times-circle',
239 'class' => 'clean-search',
240 'ko-click' => 'function(data){ data.search(null); }',
241 'ko-visible' => 'search()'
242 )),
243 'attributes' => array(
244 'ko-value' => 'search',
245 'ko-class' => "search() ? 'alert-info not-empty' : 'empty'"
246 )
247 ));
248 break;
249
250
251 case 'text' :
252 default :
253 $input = new TextInput(array(
254 'after' => Icon::make(array(
255 'icon' => 'times-circle',
256 'class' => 'clean-search',
257 'ko-click' => 'function(data){ data.search(null); }',
258 'ko-visible' => 'search()'
259 )),
260 'attributes' => array(
261 'ko-textInput' => 'search',
262 'ko-class' => "search() ? 'alert-info not-empty' : 'empty'"
263 )
264 ));
265 break;
266 }
267 $input->attributes['data-field'] = $this->name;
268 $input->class = ' list-search-input';
269 $input->value = $this->searchValue;
270
271 return $input;
272 }
273
274
275 /**
276 * Display the search field in the list header
277 *
278 * @return string The HTML result to display
279 */
280 public function displaySearchInput(){
281 if($this->search) {
282 $input = $this->getInput();
283
284 return $input->__toString();
285 }
286 else{
287 return '';
288 }
289 }
290
291 /**
292 * Display the field header
293 *
294 * @return string The HTML result to display
295 */
296 public function displayHeader(){
297 return View::make(Theme::getSelected()->getView('item-list/field-header.tpl'), array(
298 'field' => $this
299 ));
300 }
301
302 /**
303 * Get the displayed value
304 *
305 * @param array $lineIndex The index of the line in the list results to display
306 *
307 * @return string The HTML result to display
308 */
309 public function displayCell($lineIndex){
310 $line = $this->list->results[$lineIndex];
311 $name = $this->name;
312
313 $cell = new \StdClass;
314 foreach(self::$callableProperties as $prop){
315 if(! is_null($this->$prop) && is_callable($this->$prop)) {
316 $func = $this->$prop;
317 $cell->$prop = $func(isset($line->$name) ? $line->$name : null, $this, $line);
318 }
319 else{
320 $cell->$prop = $this->$prop;
321 }
322 }
323
324 // Compute the cell content
325 if($cell->display) {
326 $cell->content = $cell->display;
327 }
328 else{
329 $cell->content = isset($line->$name) ? $line->$name : '';
330 }
331
332 // Add a unit to the displayed value
333 if($cell->unit && !$cell->content) {
334 $cell->content .= ' ' . $cell->unit;
335 }
336
337 return View::make(Theme::getSelected()->getView('item-list/result-cell.tpl'), array(
338 'cell' => $cell,
339 'field' => $this
340 ));
341 }
342
343 }
344