CMS  Version 3.9
taxonomy_term_field_renderer.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2014 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::usingFeature("field_renderers");
40 
44 class TaxonomyTermFieldRenderer extends FieldRenderer
45 {
46  var $field;
47  var $label;
48  var $taxonomy;
49  var $dropdown;
50  var $dropdownMaxWidth = "340px";
51  var $dropdownMessage = "Click to Select Terms";
52  var $options;
54  var $resizable = true;
55 
56  static $readOnlyTermSeparator = "<br>";
57 
59  {
60  $this->taxonomy = $taxonomy;
61  $this->field = $taxonomy->identifier;
62  $this->label = $taxonomy->taxonomy_name;
63  $this->options = $taxonomy->Terms("ORDER BY sort_order, term");
64  $this->dropdown = true;
65 
66  $this->showSelectAll = true;
67 
68  $this->FieldRenderer($form);
69 
70  // JDG 7/31/2011 - allow override
71  if ($form->data->hasField($this->field))
72  {
73  $form->override($this->field, $label, $this);
74  }
75  else
76  {
77  $form->add($this, $this->field);
78  $form->overrides[$this->field]['label'] = $label;
79  }
80  }
81 
82  function setSize($width, $height)
83  {
84  $this->width = $width;
85  $this->height = $height;
86  }
87 
88  function groupBy($groupBy, $format = "")
89  {
90  $this->groupBy = $groupBy;
91  $this->groupByFormat = $format;
92  }
93 
94  function renderScript($field)
95  {
96  if ($this->showSelectAll)
97  {
98 ?>
99 <script type="text/javascript">
100 
101 function <?echo $this->parent->id?>_<?echo $this->field ?>_toggleSelectAll(link)
102 {
103  link = document.id(link);
104 
105  var value = (link.innerHTML == "Select All");
106 
107  var container = document.id('<?echo $this->parent->id?>_<?echo $this->field?>_container');
108 
109  container.getElements("input[type=checkbox]").each(function(cbox)
110  {
111  cbox.set('checked', value);
112  });
113 
114  link.innerHTML = (value) ? "Deselect All" : "Select All";
115  if (container.multiSelect) container.multiSelect.selectionChanged();
116  return false;
117 }
118 
119 </script>
120 <?
121  }
122 
123  if ($this->dropdown)
124  {
125 ?>
126 <script type='text/javascript'>
127 window.addEvent('domready', function()
128 {
129  new MultiSelect('<?echo $this->parent->id?>_<?echo $this->field?>_container', {maxWidth: '<?echo $this->dropdownMaxWidth?>', message: '<?echo jsSafe($this->dropdownMessage)?>'});
130 });
131 </script>
132 <?
133  }
134  }
135 
137  {
138 
139  if ($this->dropdown)
140  {
141  ?>
142  <script type='text/javascript'>
143  window.addEvent('domready', function()
144  {
145  new MultiSelect('<?echo $this->parent->id?>_<?echo $this->field?>_container', {maxWidth: '<?echo $this->dropdownMaxWidth?>', message: '<?echo jsSafe($this->dropdownMessage)?>'});
146  });
147  </script>
148  <?
149  }
150  }
151  function getMatchingPK($xref, $obj)
152  {
153  $pks = $obj->getPrimaryKeyList();
154  foreach($pks as $pk)
155  {
156  if ($xref->hasField($pk)) return $pk;
157  }
158 
159  throw new DataItemException("No matching foreign key in xref table");
160  }
161 
162  function renderField($field = "")
163  {
164  if (count($this->options) == 0) return;
165 
167 
168  $this->_startField($field);
169 
170  $obj = $this->parent->data;
171 
172  if ($this->showSelectAll)
173  {
174  echo "<a href='#' onclick='return {$this->parent->id}_{$field}_toggleSelectAll(this);'>Select All</a><br/>";
175  }
176  if ($this->width && $this->height)
177  {
178  $width = is_numeric($this->width) ? "{$this->width}px" : $this->width;
179  $height = is_numeric($this->height) ? "{$this->height}px" : $this->height;
180  $padding = ($width == "auto") ? "padding-right: 20px; overflow-x: hidden" : "";
181 
182  $resizable = ($this->resizable && !$this->dropdown) ? " resizable": "";
183  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
184  echo "<div id='{$this->parent->id}_{$field}_container' class='scrollbox{$resizable}{$dropdown}' style='width: {$width}; height: {$height};overflow: auto; $padding'>\n";
185  }
186 
187  $countOptions = count($this->options);
188 
189  $class = get_class($obj);
190  $pk = $obj->get($obj->getPrimaryKey());
191 
192  $selected = IndexedQuery::create(TaxonomyTermAssociation, "WHERE taxonomy_id=:id AND class=:class AND id=:pk", "term_id")
193  ->bind(":id", $this->taxonomy->taxonomy_id, ":class", $class, ":pk", $pk)
194  ->execute();
195 
196  $this->renderCheckBoxes($field, $this->options, $selected);
197 
198  if ($this->width && $this->height)
199  {
200  echo "</div>\n";
201  }
202 
203  // Save this count to loop through checkboxes on required validation
204  echo "<input type=\"hidden\" name=\"count_{$field}\" value=\"$countOptions\">";
205 
206  $this->_endField($field);
207  }
208 
209  function renderCheckboxes($field, $items, $selected, $mode = null)
210  {
211  foreach($items as $item)
212  {
213  $p = $item->term_id;
214 
215  $name = $mode ? "{$field}:{$mode}" : $field;
216  $val = $mode ? $p : "1";
217 
218  echo "<label for='checkbox_{$field}_$p'><input type='checkbox' class='checkbox' name='{$name}[$p]' id='checkbox_{$field}_$p' value='{$val}'";
219  if (array_key_exists($p, $selected))
220  {
221  echo " checked";
222  }
223  echo ">";
224  echo $item->term."</label>";
225  }
226  }
227 
228  function renderReadOnly($field = "")
229  {
230  if (count($this->options) == 0) return;
231 
233 
234  $obj = $this->parent->data;
235 
236  $class = get_class($obj);
237  $pk = $obj->get($obj->getPrimaryKey());
238 
239  $selected = IndexedQuery::create(TaxonomyTermAssociation, "WHERE taxonomy_id=:id AND class=:class AND id=:pk", "term_id")
240  ->bind(":id", $this->taxonomy->taxonomy_id, ":class", $class, ":pk", $pk)
241  ->execute();
242 
243  foreach($this->options as $option)
244  {
245  $p = $option->term_id;
246 
247  if (array_key_exists($p, $selected))
248  {
249  $answers[] = $option->term;
250  }
251  }
252 
256  if(count($answers))
257  {
258  $this->_startField($field);
260  $this->_endField($field);
261  }
262  }
263 
264 
266  {
267  $this->_startField($field);
268 
269 
270  $obj = $this->parent->data;
271 
272  if ($this->width && $this->height)
273  {
274  $width = is_numeric($this->width) ? "{$this->width}px" : $this->width;
275  $height = is_numeric($this->height) ? "{$this->height}px" : $this->height;
276  $padding = ($width == "auto") ? "padding-right: 20px; overflow-x: hidden" : "";
277 
278  $resizable = ($this->resizable && !$this->dropdown) ? " resizable": "";
279  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
280  echo "<div id='{$this->parent->id}_{$field}_container' class='scrollbox{$resizable}{$dropdown}' style='width: {$width}; height: {$height};overflow: auto; $padding'>\n";
281  }
282 
283  $countOptions = count($this->options);
284 
285  $class = get_class($obj);
286  $pk = $obj->get($obj->getPrimaryKey());
287 
288  $selected = IndexedQuery::create(TaxonomyTermAssociation, "WHERE taxonomy_id=:id AND class=:class AND id=:pk", "term_id")
289  ->bind(":id", $this->taxonomy->taxonomy_id, ":class", $class, ":pk", $pk)
290  ->execute();
291 
292  $this->renderCheckBoxes($field, $this->options, $selected, $mode);
293 
294  if ($this->width && $this->height)
295  {
296  echo "</div>\n";
297  }
298 
299  // Save this count to loop through checkboxes on required validation
300  echo "<input type=\"hidden\" name=\"count_{$field}\" value=\"$countOptions\">";
301 
302  $this->_endField($field);
303  }
304 
305  function preProcess($field = "")
306  {
307  if (count($this->options) == 0) return;
308 
309  $obj = $this->parent->data;
310  if ($obj->hasField($this->field) && is_array($_POST[$this->field]))
311  {
312  // If overriding a field, unpack the terms
313  $terms = array();
314  foreach($_POST[$this->field] as $term_id => $dummy)
315  {
316  $terms[] = new TaxonomyTerm($term_id);
317  }
318 
319  $obj->set($this->field, formatItems($terms, "{term}", ","));
320  }
321 
322  return true;
323  }
324 
325  function postProcess($field = "")
326  {
327  if (count($this->options) == 0) return;
328 
329  $obj = $this->parent->data;
330  $class = get_class($obj);
331  $pk = $obj->get($obj->getPrimaryKey());
332 
333  $assoc = new TaxonomyTermAssociation();
334  $assoc->delete("WHERE class='$class' AND taxonomy_id={$this->taxonomy->taxonomy_id} AND id=$pk");
335 
336  if (isset($_POST[$this->field]))
337  {
338  foreach($_POST[$this->field] as $term_id => $dummy)
339  {
340  $assoc = new TaxonomyTermAssociation();
341  $assoc->class = $class;
342  $assoc->taxonomy_id = $this->taxonomy->taxonomy_id;
343  $assoc->id = $pk;
344  $assoc->term_id = $term_id;
345  $assoc->save();
346  }
347  }
348  }
349 }
350 
351 
352 
353 
357 class BindingTargetFieldRenderer extends FieldRenderer
358 {
359  var $field;
360  var $label;
363  var $dropdownMaxWidth = "340px";
364  var $dropdownMessage = "Click to Select Terms";
365  var $options;
367  var $resizable = true;
368 
369  static $readOnlyTermSeparator = "<br>";
370 
372  {
373  trace("## Creating BindingTargetFieldRenderer for {$bindingTarget->target_class}", 3);
374 
375  $this->bindingTarget = $bindingTarget;
376  $this->field = $bindingTarget->target_class;
377  $proto = new $bindingTarget->target_class;
378  $this->label = $proto->prettifyClassName(true);
379 
380  $this->options = TaxonomyManager::getBindingOptions($bindingTarget->target_class);
381  $this->format = TaxonomyManager::$bindableClasses[$bindingTarget->target_class]["format"];
382 
383  trace("## Found ".count($this->options)," options");
384 
385  $this->dropdown = true;
386 
387  $this->showSelectAll = true;
388 
389  $this->FieldRenderer($form);
390 
393 
394  // JDG 7/31/2011 - allow override
395  if ($form->data->hasField($field))
396  {
397  $form->override($field, $label, $this);
398  }
399  else
400  {
401  $form->add($this, $field);
402  $form->overrides[$field]['label'] = $label;
403  }
404  }
405 
406  function setSize($width, $height)
407  {
408  $this->width = $width;
409  $this->height = $height;
410  }
411 
412  function renderScript($field)
413  {
414  if ($this->showSelectAll)
415  {
416  ?>
417 <script type="text/javascript">
418 
419 function <?echo $this->parent->id?>_<?echo $this->field ?>_toggleSelectAll(link)
420 {
421  link = document.id(link);
422 
423  var value = (link.innerHTML == "Select All");
424 
425  var container = document.id('<?echo $this->parent->id?>_<?echo $this->field?>_container');
426 
427  container.getElements("input[type=checkbox]").each(function(cbox)
428  {
429  cbox.set('checked', value);
430  });
431 
432  link.innerHTML = (value) ? "Deselect All" : "Select All";
433  if (container.multiSelect) container.multiSelect.selectionChanged();
434  return false;
435 }
436 
437 </script>
438 <?
439  }
440 
441  if ($this->dropdown)
442  {
443 ?>
444 <script type='text/javascript'>
445 window.addEvent('domready', function()
446 {
447  new MultiSelect('<?echo $this->parent->id?>_<?echo $this->field?>_container', {maxWidth: '<?echo $this->dropdownMaxWidth?>', message: '<?echo jsSafe($this->dropdownMessage)?>'});
448 });
449 </script>
450 <?
451  }
452  }
453 
454  function getMatchingPK($xref, $obj)
455  {
456  $pks = $obj->getPrimaryKeyList();
457  foreach($pks as $pk)
458  {
459  if ($xref->hasField($pk)) return $pk;
460  }
461 
462  throw new DataItemException("No matching foreign key in xref table");
463  }
464 
465  function renderField($field = "")
466  {
467  if (count($this->options) == 0) return;
468 
470 
471  $this->_startField($field);
472 
473  $obj = $this->parent->data;
474 
475  if ($this->showSelectAll)
476  {
477  echo "<a href='#' onclick='return {$this->parent->id}_{$field}_toggleSelectAll(this);'>Select All</a><br/>";
478  }
479  if ($this->width && $this->height)
480  {
481  $width = is_numeric($this->width) ? "{$this->width}px" : $this->width;
482  $height = is_numeric($this->height) ? "{$this->height}px" : $this->height;
483  $padding = ($width == "auto") ? "padding-right: 20px; overflow-x: hidden" : "";
484 
485  $resizable = ($this->resizable && !$this->dropdown) ? " resizable": "";
486  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
487  echo "<div id='{$this->parent->id}_{$field}_container' class='scrollbox{$resizable}{$dropdown}' style='width: {$width}; height: {$height};overflow: auto; $padding'>\n";
488  }
489 
490  $countOptions = count($this->options);
491 
492  $class = get_class($obj);
493  $pk = $obj->get($obj->getPrimaryKey());
494 
495  $selected = IndexedQuery::create(BindingRecord, "WHERE target_class=:tc AND class=:class AND id=:pk", "target_id")
496  ->bind(":tc", $this->bindingTarget->target_class, ":class", $class, ":pk", $pk)
497  ->execute();
498 
499  $this->renderCheckBoxes($field, $this->options, $selected);
500 
501  if ($this->width && $this->height)
502  {
503  echo "</div>\n";
504  }
505 
506  // Save this count to loop through checkboxes on required validation
507  echo "<input type=\"hidden\" name=\"count_{$field}\" value=\"$countOptions\">";
508 
509  $this->_endField($field);
510  }
511 
512  function renderCheckboxes($field, $items, $selected)
513  {
514  foreach($items as $item)
515  {
516  $p = $item->get($item->getPrimaryKey());
517 
518  echo "<label for='checkbox_{$field}_$p'><input type='checkbox' class='checkbox' name='{$field}[$p]' id='checkbox_{$field}_$p' value='1'";
519  if (array_key_exists($p, $selected))
520  {
521  echo " checked";
522  }
523  echo ">";
524  echo $item->format($this->format)."</label>";
525  }
526  }
527 
528  function renderReadOnly($field = "")
529  {
530  if (count($this->options) == 0) return;
531 
533 
534  $obj = $this->parent->data;
535 
536  $class = get_class($obj);
537  $pk = $obj->get($obj->getPrimaryKey());
538 
539  $selected = IndexedQuery::create(BindingRecord, "WHERE target_class=:tc AND class=:class AND id=:pk", "target_id")
540  ->bind(":tc", $this->bindingTarget->target_class, ":class", $class, ":pk", $pk)
541  ->execute();
542 
543  foreach($this->options as $option)
544  {
545  $p = $option->get($option->getPrimaryKey());
546 
547  if (array_key_exists($p, $selected))
548  {
549  $answers[] = $option->format($this->format);
550  }
551  }
552 
556  if(count($answers))
557  {
558  $this->_startField($field);
560  $this->_endField($field);
561  }
562  }
563 
564  function postProcess($field = "")
565  {
566  if (count($this->options) == 0) return;
567 
568  $obj = $this->parent->data;
569  $class = get_class($obj);
570  $pk = $obj->get($obj->getPrimaryKey());
571 
572  $assoc = new BindingRecord();
573  $assoc->delete("WHERE target_class='{$this->bindingTarget->target_class}' AND class='$class' AND id=$pk");
574 
575  if (isset($_POST[$this->field]))
576  {
577  foreach($_POST[$this->field] as $target_id => $dummy)
578  {
579  $assoc = new BindingRecord();
580  $assoc->class = $class;
581  $assoc->target_class = $this->bindingTarget->target_class;
582  $assoc->id = $pk;
583  $assoc->target_id = $target_id;
584  $assoc->save();
585  }
586  }
587  }
588 }?>
$form
$_POST["owner_id"]
Definition: blog_form.inc:54
$dummy
Definition: data_import.inc:7
$columnSelect dropdown
$helpTree width
Definition: tree.inc:45
$name
Definition: upload.inc:54
Field renderer for handling taxonomy term relationships.
BindingTargetFieldRenderer(&$form, $bindingTarget)
renderCheckboxes($field, $items, $selected)
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
static getBindingOptions($bindingTarget)
Field renderer for handling taxonomy term relationships.
renderCheckboxes($field, $items, $selected, $mode=null)
$height
Definition: cover.inc:38
$width
Definition: cover.inc:37
$desc height
Definition: event_edit.inc:64
$mode
$openData field
$term_id
Definition: blog.inc:40
$answers
$email message