Framework  3.9
facet_manager.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7 Copyright (c) 2007-2013 Sonjara, Inc
8 
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without
12 restriction, including without limitation the rights to use,
13 copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the
15 Software is furnished to do so, subject to the following
16 conditions:
17 
18 The above copyright notice and this permission notice shall be
19 included in all copies or substantial portions of the Software.
20 
21 Except as contained in this notice, the name(s) of the above
22 copyright holders shall not be used in advertising or otherwise
23 to promote the sale, use or other dealings in this Software
24 without prior written authorization.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
37 interface FacetFilterable
38 {
39  public function getID();
40  public function addFacetTaggingHandler($handler);
41 }
42 
43 abstract class AbstractFacetFilter
44 {
45  abstract function onTagItem($item, $attrs);
46  abstract function writeScript();
47  abstract function drawFacet();
48 }
49 
51 {
52  var $id;
53  var $target;
54  var $facets;
55 
56  function __construct($id, $target)
57  {
58  $this->id = $id;
59  $this->target = $target;
60  $this->facets = array();
61 
62  $this->target->addFacetTaggingHandler(array($this, onTagItem));
63  }
64 
65  function addFacet($title, $facet)
66  {
67  $this->facets[$title] = $facet;
68  $facet->manager = $this;
69 
70  return $facet;
71  }
72 
73  function onTagItem($item, $attrs)
74  {
75  if ($item instanceof AbstractSearchResult)
76  {
77  $item = $item->item;
78  }
79 
80  foreach ($this->facets as $title => $facet)
81  {
82  $attrs = $facet->onTagItem($item, $attrs);
83  }
84 
85  return $attrs;
86  }
87 
88  function writeScript()
89  {
90  $script = "<script type=\"text/javascript\">\nwindow.addEvent('domready', function()\n{\n\tvar {$this->target->getID()}_facet_manager = new FacetManager('{$this->target->getID()}');\n";
91 
92  foreach($this->facets as $facet)
93  {
94  $script .= $facet->writeScript();
95  }
96 
97  $script .= "\n});\n</script>";
98  return $script;
99  }
100 
101  function drawForm()
102  {
103 ?>
104 <div id='<?echo $this->id?>' class='facet_form'>
105 <?
106  foreach($this->facets as $title => $facet)
107  {
108 ?>
109 <span style='display: inline-block'><label for='<?echo $facet->getID()?>'><?echo $title?></label>
110 <?
111  $facet->drawFacet();
112 ?>
113 </span>
114 <?
115  }
116 ?>
117 </div>
118 <?
119  }
120 }
121 
123 {
124  var $id;
125  var $target;
126  var $facets;
127 
129  {
130  $this->id = $target->getID()."_facet_manager";
131  $this->target = $target;
132  $this->facets = array();
133  }
134 
135  function addFacet($title, $facet)
136  {
137  $this->facets[$title] = $facet;
138  $facet->manager = $this;
139 
140  return $facet;
141  }
142 
143  function writeScript()
144  {
145  $mgr = $this->target->getID()."_facet_manager";
146  $panel = $this->target->getID();
147 
148  $script = <<<ENDSCRIPT
149 <script type="text/javascript">
150  window.addEvent('domready', function()
151  {
152  var {$mgr} = new FacetManager(null,
153  {
154  onFilterChanged: function()
155  {
156  document.id('{$panel}').loadPanel('{$this->target->handler}?' + {$mgr}.getQueryString());
157  }
158  });
159 
160 ENDSCRIPT;
161 
162  foreach($this->facets as $facet)
163  {
164  $script .= $facet->writeScript();
165  }
166 
167  $script .= "\n});\n</script>";
168  return $script;
169  }
170 
171  function drawForm()
172  {
173 ?>
174 <div id='<?echo $this->id?>' class='facet_form'>
175 <?
176  foreach($this->facets as $title => $facet)
177  {
178 ?>
179 <span style='display: inline-block'><label for='<?echo $facet->getID()?>'><?echo $title?></label>&nbsp;
180 <?
181  $facet->drawFacet();
182 ?>
183 </span>
184 <?
185  }
186 ?>
187 </div>
188 <?
189  }
190 }
191 
193 {
194  var $field;
195 
196  function __construct($field)
197  {
198  $this->field = $field;
199  }
200 
201  function getID()
202  {
203  return $this->field;
204  }
205 
206  function onTagItem($item, $attrs)
207  {
208  if ($item->hasField($this->field))
209  {
210  $attrs["data-{$this->field}"] = strtolower($item->format("{".$this->field.":xml}"));
211  }
212 
213  return $attrs;
214  }
215 
216  function writeScript()
217  {
218  ob_start();
219 ?>
220  var <?echo $this->getID()?>_handler = new StringFacetHandler('<?echo $this->getID()?>', '<?echo $this->getID()?>_facet', <?echo $this->manager->target->getID()?>_facet_manager);
221 <?
222  $script = ob_get_contents();
223  ob_end_clean();
224  return $script;
225  }
226 
227  function drawFacet()
228  {
229 ?>
230 <input type='text' name='<?echo $this->field?>' id='<?echo $this->getID()?>_facet' value='' autocomplete='off'/>
231 <?
232  }
233 }
234 
236 {
237  var $field;
239 
240  function __construct($field, $expression)
241  {
242  $this->field = $field;
243  $this->expression = $expression;
244  }
245 
246  function getID()
247  {
248  return $this->field;
249  }
250 
251  function onTagItem($item, $attrs)
252  {
253  $attrs["data-{$this->field}"] = strtolower($item->format($this->expression));
254 
255  return $attrs;
256  }
257 
258  function writeScript()
259  {
260  ob_start();
261 ?>
262  var <?echo $this->getID()?>_handler = new StringFacetHandler('<?echo $this->getID()?>', '<?echo $this->getID()?>_facet', <?echo $this->manager->target->getID()?>_facet_manager);
263 <?
264  $script = ob_get_contents();
265  ob_end_clean();
266  return $script;
267  }
268 
269  function drawFacet()
270  {
271  ?>
272 <input type='text' name='<?echo $this->field?>' id='<?echo $this->getID()?>_facet' value='' autocomplete='off'/>
273 <?
274  }
275 }
276 
278 {
280  var $id;
281 
282  var $width = "200px";
283  var $height = "100px";
284  var $dropdown = true;
285  var $dropdownMaxWidth = "200px";
286  var $dropdownMessage = "Click to Select Terms";
287 
288  function __construct($id, $formatAdapters)
289  {
290  $this->id = $id;
291  $this->formatAdapters = $formatAdapters;
292  }
293 
294  function getID()
295  {
296  return $this->id;
297  }
298 
299  function onTagItem($item, $attrs)
300  {
301  $cl = get_class($item);
302 
303  if (array_key_exists($cl, $this->formatAdapters))
304  {
305  $value = $item->format($this->formatAdapters[$cl]);
306  }
307 
308  if ($value)
309  {
310  $attrs["data-{$this->id}"] = $value;
311  }
312 
313  return $attrs;
314  }
315 
316  function writeScript()
317  {
318  ob_start();
319 
320  if ($this->dropdown)
321  {
322  ?>
323  var <?echo $this->id?>_select = new MultiSelect('<?echo $this->id?>_container', {maxWidth: '<?echo $this->dropdownMaxWidth?>', message: '<?echo jsSafe($this->dropdownMessage)?>'});
324  var <?echo $this->id?>_handler = new MultiSelectFacetHandler('<?echo $this->id?>', <?echo $this->id?>_select, <?echo $this->manager->target->getID()?>_facet_manager);
325  <?
326  }
327 
328  $script = ob_get_contents();
329  ob_end_clean();
330  return $script;
331 
332  }
333 
334  function drawFacet()
335  {
336  if ($this->width && $this->height)
337  {
338  $resizable = (!$this->dropdown && $this->resizable) ? " resizable": "";
339  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
340  echo "<div id='{$this->id}_container' class='scrollbox{$resizable}{$dropdown}' style='width: {$this->width}; height: {$this->height}; overflow: auto'></div>\n";
341  }
342  }
343 }
344 
346 {
347  var $id;
348  var $format;
350  var $related;
351 
352  var $width = "200px";
353  var $height = "100px";
354  var $dropdown = true;
355  var $dropdownMaxWidth = "200px";
356  var $dropdownMessage = "Click to Select Terms";
357 
358  function __construct($id, $xrefAdapters, $format)
359  {
360  $this->id = $id;
361  $this->xrefAdapters = $xrefAdapters;
362  $this->format = $format;
363  $this->related = array();
364  }
365 
366  function getID()
367  {
368  return $this->id;
369  }
370 
371  function onTagItem($item, $attrs)
372  {
373  $cl = get_class($item);
374 
375  if (array_key_exists($cl, $this->xrefAdapters))
376  {
377  $adapter = $this->xrefAdapters[$cl];
378  $related = $adapter($item);
379  if (!is_array($related)) $related = array($related);
380  $value = formatItems($related, "{getPrimaryKeyValue()}", ",");
381  foreach($related as $r)
382  {
383  $this->related[$r->getPrimaryKeyValue()] = $r;
384  }
385  }
386 
387  if ($value)
388  {
389  $attrs["data-{$this->id}"] = $value;
390  }
391 
392  return $attrs;
393  }
394 
395  function writeScript()
396  {
397  ob_start();
398 
399  if ($this->dropdown)
400  {
401  ?>
402  var <?echo $this->id?>_select = new MultiSelect('<?echo $this->id?>_container', {maxWidth: '<?echo $this->dropdownMaxWidth?>', message: '<?echo jsSafe($this->dropdownMessage)?>'});
403  var <?echo $this->id?>_handler = new CrossReferenceFacetHandler('<?echo $this->id?>', <?echo $this->id?>_select, <?echo $this->manager->target->getID()?>_facet_manager);
404  <?
405  }
406 
407  $script = ob_get_contents();
408  ob_end_clean();
409  return $script;
410 
411  }
412 
413  function renderCheckboxes()
414  {
415  $options = array();
416  foreach($this->related as $pk => $r)
417  {
418  $options[$pk] = $r->format($this->format);
419  }
420 
421  asort($options);
422 
423  foreach($options as $id => $option)
424  {
425  echo "<label for='checkbox_{$this->id}_{$id}'><input type='checkbox' class='checkbox' name='checkbox_{$this->id}_{$id}' id='checkbox_{$this->id}_{$id}' value='{$id}'";
426  echo ">".$option."</label>";
427  }
428  }
429 
430  function drawFacet()
431  {
432  if ($this->width && $this->height)
433  {
434  $resizable = (!$this->dropdown && $this->resizable) ? " resizable": "";
435  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
436  echo "<div id='{$this->id}_container' class='scrollbox{$resizable}{$dropdown}' style='width: {$this->width}; height: {$this->height}; overflow: auto'>";
437  echo $this->renderCheckboxes();
438  echo"</div>\n";
439  }
440  }
441 }
442 
444 {
445  var $id;
446  var $options;
447  var $related;
448 
449  var $width = "200px";
450  var $height = "100px";
451  var $dropdown = true;
452  var $dropdownMaxWidth = "200px";
453  var $dropdownMessage = "Click to Select Terms";
454 
455  function __construct($id, $options)
456  {
457  $this->id = $id;
458  $this->options = $options;
459  $this->related = array();
460  }
461 
462  function getID()
463  {
464  return $this->id;
465  }
466 
467  function onTagItem($item, $attrs)
468  {
469  $cl = get_class($item);
470 
471  $value = $item->get($this->id);
472  if ($value)
473  {
474  $attrs["data-{$this->id}"] = $value;
475  }
476 
477  return $attrs;
478  }
479 
480  function writeScript()
481  {
482  ob_start();
483 
484  if ($this->dropdown)
485  {
486  ?>
487  var <?echo $this->id?>_select = new MultiSelect('<?echo $this->id?>_container', {maxWidth: '<?echo $this->dropdownMaxWidth?>', message: '<?echo jsSafe($this->dropdownMessage)?>'});
488  var <?echo $this->id?>_handler = new CheckListFacetHandler('<?echo $this->id?>', <?echo $this->id?>_select, <?echo $this->manager->target->getID()?>_facet_manager);
489  <?
490  }
491 
492  $script = ob_get_contents();
493  ob_end_clean();
494  return $script;
495 
496  }
497 
498  function renderCheckboxes()
499  {
500  foreach($this->options as $id => $option)
501  {
502  echo "<label for='checkbox_{$this->id}_{$id}'><input type='checkbox' class='checkbox' name='checkbox_{$this->id}_{$id}' id='checkbox_{$this->id}_{$id}' value='{$id}'";
503  echo ">".$option."</label>";
504  }
505  }
506 
507  function drawFacet()
508  {
509  if ($this->width && $this->height)
510  {
511  $resizable = (!$this->dropdown && $this->resizable) ? " resizable": "";
512  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
513  echo "<div id='{$this->id}_container' class='scrollbox{$resizable}{$dropdown}' style='width: {$this->width}; height: {$this->height}; overflow: auto'>";
514  echo $this->renderCheckboxes();
515  echo"</div>\n";
516  }
517  }
518 }
519 
onTagItem($item, $attrs)
__construct($id, $options)
onTagItem($item, $attrs)
__construct($id, $xrefAdapters, $format)
__construct($field, $expression)
onTagItem($item, $attrs)
addFacet($title, $facet)
onTagItem($item, $attrs)
__construct($id, $target)
onTagItem($item, $attrs)
__construct($id, $formatAdapters)
onTagItem($item, $attrs)
addFacet($title, $facet)
formatItems($items, $template, $separator="")
Format a list of DataItems using the specified templated.
Definition: data_item.inc:2176
addFacetTaggingHandler($handler)