CMS  Version 3.9
TaxonomyManager Class Reference

TaxonomyManager provides the internal API for working with taxonomies, facets and bindings. More...

Public Member Functions

 __construct ()
 Create a new TaxonomyManager instance. More...
 

Static Public Member Functions

static setDefaults ()
 
static getTabs ($key)
 
static getRegisteredClasses ()
 Retrieved the list of DataItem classes that have registered as supporting taxonomy term associations. More...
 
static getRegisteredClassOptions ()
 Retrieves the list of DataItem classes that have registered as supporting taxonomy term associations in an associated array format suitable for use with a SelectFieldRenderer or CheckListFieldRenderer. More...
 
static getBindableClasses ()
 
static registerBindableClass ($class, $format, $constraint="", $fieldRenderer=BindingTargetFieldRenderer, $facetFilter=BindingTargetFacetFilter)
 
static getBindableClassOptions ()
 
static getBindingOptions ($bindingTarget)
 
static getBindingOptionFormat ($bindingTarget)
 
static getBindingFieldRendererClass ($bindingTarget)
 
static getBindingFacetFilterClass ($bindingTarget)
 
static addTaxonomyFieldRenderers ($form, $showSelectAll=true, $group=null, $limitTo=null)
 
static addBindingFieldRenderers ($form, $showSelectAll=true, $group=null)
 
static getAssociatedTaxonomies ($classes, $facet_filter_only=false)
 
static getBindingTargets ($classes, $facet_filter_only=false)
 
static importTerms ($taxonomy, $file)
 
static addFacets ($manager, $classes, $dropdown=true, $max_width="200px", $width="200px", $height="120px")
 
static getRequestTerms ($classes)
 
static getRequestFilterConstraint ($class)
 
static cloneTaxonomy ($form)
 
static getBoundItems ($source, $class, $constraint="")
 Retrieve items of the specified class that are bound to the specified source object. More...
 
static getBoundItemsReverse ($target, $class, $constraint="")
 Same as TaxonomyManager::getBoundItems, but with the reverse relationship. More...
 
static countBoundItems ($target, $class, $constraint="")
 Returns the number of items of the specified class that are bound to the specified source object. More...
 
static countBoundItemsReverse ($target, $class, $constraint="")
 Same as TaxonomyManager::countBoundItems, but with the reverse relationship. More...
 
static generateSearchConstraint ($text, $class, $placeholder=false)
 Generates a search constraint matching the supplied text against taxonomy terms that might be associated with the given class. More...
 
static upgradeComponent ($version)
 
static registerSerializationHandler ()
 

Static Public Attributes

static $bindableClasses = null
 

Detailed Description

TaxonomyManager provides the internal API for working with taxonomies, facets and bindings.

These methods are designed to bridge the gap between the user-extensible taxonomy schemas and the defined items in your data model. TaxonomyManager also provides event handlers and common functionality for the taxonomy component.

Definition at line 90 of file taxonomy_manager.inc.

Constructor & Destructor Documentation

◆ __construct()

TaxonomyManager::__construct ( )

Create a new TaxonomyManager instance.

Generally not required as all methods are static.

Definition at line 98 of file taxonomy_manager.inc.

99  {
100 
101  }

Member Function Documentation

◆ addBindingFieldRenderers()

static TaxonomyManager::addBindingFieldRenderers (   $form,
  $showSelectAll = true,
  $group = null 
)
static

Definition at line 259 of file taxonomy_manager.inc.

260  {
262 
263  $class = get_class($form->data);
264 
265  $targets = Query::create(BindingTarget, "")->execute();
266 
267  trace("### Found ".count($targets)." binding targets", 3);
268  $renderers = array();
269 
270  foreach($targets as $target)
271  {
272  trace("## Checking {$target->target_class} - {$target->associated_classes}", 3);
273 
274  $classes = explode(",", $target->associated_classes);
275  if (array_search($class, $classes) !== FALSE)
276  {
277  trace("## Found a match", 3);
278 
280 
281  $renderer = new $fieldRenderer($form, $target);
282  $renderer->setSize(400, 125);
283  $renderer->resizable = true;
284  $renderer->showSelectAll = $showSelectAll;
285 
286  if ($group)
287  {
288  $form->group($group, $class);
289  }
290 
291  $renderers[] = $renderer;
292  }
293  }
294 
295  return $renderers;
296  }
$form
static getBindableClasses()
static getBindingFieldRendererClass($bindingTarget)
$group
Definition: group_form.inc:43
$renderer

◆ addFacets()

static TaxonomyManager::addFacets (   $manager,
  $classes,
  $dropdown = true,
  $max_width = "200px",
  $width = "200px",
  $height = "120px" 
)
static

Definition at line 418 of file taxonomy_manager.inc.

419  {
420  if (!is_array($classes) && $classes) $classes = array($classes);
421 
422  $bindingTargets = TaxonomyManager::getBindingTargets($classes, true);
423  foreach($bindingTargets as $target)
424  {
426 
427  $facet = $manager->addFacet($target->target_class, new $facetFilter($target));
428  $facet->dropdown = $dropdown;
429  $facet->dropdownMaxWidth = $max_width;
430  $facet->width = $width;
431  $facet->height = $height;
432  }
433 
435  foreach($taxonomies as $taxonomy)
436  {
437  $facet = $manager->addFacet($taxonomy->taxonomy_name, new TaxonomyFacetFilter($taxonomy));
438  $facet->dropdown = $dropdown;
439  $facet->dropdownMaxWidth = $max_width;
440  $facet->width = $width;
441  $facet->height = $height;
442  }
443  }
$taxonomies
Definition: taxonomies.inc:39
static getAssociatedTaxonomies($classes, $facet_filter_only=false)
static getBindingFacetFilterClass($bindingTarget)
static getBindingTargets($classes, $facet_filter_only=false)
$height
Definition: cover.inc:38
$width
Definition: cover.inc:37

◆ addTaxonomyFieldRenderers()

static TaxonomyManager::addTaxonomyFieldRenderers (   $form,
  $showSelectAll = true,
  $group = null,
  $limitTo = null 
)
static

Definition at line 217 of file taxonomy_manager.inc.

218  {
219  $renderers = TaxonomyManager::addBindingFieldRenderers($form, $showSelectAll, $group, $limitTo);
220 
221  $class = get_class($form->data);
222 
223  $taxonomies = Query::create(Taxonomy, "")->execute();
224 
225  foreach($taxonomies as $taxonomy)
226  {
227  $classes = explode(",", $taxonomy->associated_classes);
228  if (array_search($class, $classes) !== FALSE)
229  {
230  if (is_array($limitTo) && !array_search($taxonomy->identifier, $limitTo))
231  {
232  continue;
233  }
234 
235  trace("Adding TaxonomyTermFieldRenderer for {$taxonomy->identifier}", 3);
237  $renderer->setSize(400, 125);
238  $renderer->resizable = true;
239  $renderer->showSelectAll = $showSelectAll;
240 
241  if ($group)
242  {
243  $form->group($group, $taxonomy->identifier);
244  }
245 
246  if ($form instanceof SearchForm)
247  {
248  $form->params->setHandler($taxonomy->identifier, new TaxonomySearchParameterHandler($class));
249  $form->setMatchingMode("member", $taxonomy->identifier);
250  }
251 
252  $renderers[] = $renderer;
253  }
254  }
255 
256  return $renderers;
257  }
static addBindingFieldRenderers($form, $showSelectAll=true, $group=null)
Field renderer for handling taxonomy term relationships.

◆ cloneTaxonomy()

static TaxonomyManager::cloneTaxonomy (   $form)
static

Definition at line 499 of file taxonomy_manager.inc.

500  {
501  $tx = new DataTransaction();
502 
503  try
504  {
505  $clone = $form->data;
506 
507  $clone->joinTransaction($tx);
508 
509  $clone->filter = null;
510  $name = $clone->taxonomy_name;
511  $ident = $clone->identifier;
512 
513  $taxonomy_id = checkNumeric($_GET["taxonomy_id"]);
514  $src = new Taxonomy($taxonomy_id);
515  $clone->copy($src);
516  unset($clone->taxonomy_id);
517  $clone->taxonomy_name = $name;
518  $clone->identifier = $ident;
519  $clone->published = false;
520  $clone->save();
521 
522  $terms = $src->Terms();
523 
524  foreach($terms as $term)
525  {
526  $cloneTerm = new TaxonomyTerm();
527  $cloneTerm->copy($term);
528  $cloneTerm->taxonomy_id = $clone->taxonomy_id;
529  unset($cloneTerm->term_id);
530  $cloneTerm->joinTransaction($tx);
531  $cloneTerm->save();
532  }
533 
534  $tx->commit();
535  return true;
536  }
537  catch (Exception $e)
538  {
539  $tx->rollback();
540  $form->msg = $e->getMessage();
541  return false;
542  }
543  }
$src
Definition: page.inc:37
$name
Definition: upload.inc:54
$clone
$term
Definition: term_dialog.inc:46

◆ countBoundItems()

static TaxonomyManager::countBoundItems (   $target,
  $class,
  $constraint = "" 
)
static

Returns the number of items of the specified class that are bound to the specified source object.

Parameters
DataItem$sourcethe source item
string$classthe class of bound items to retrieve
string$constraintoptional extra constraint or ordering
Returns
array any items of the specified class that are bound to the target

Definition at line 597 of file taxonomy_manager.inc.

598  {
599  $constraint = preg_replace("/^\\s*WHERE/i", "AND", $constraint);
600  $obj = new $class;
601  $pk = $obj->getPrimaryKey();
602 
603  $items = Query::create($class, "WHERE $pk IN (SELECT target_id FROM binding WHERE id=:source_id and class=:source_class AND target_class=:target_class) $constraint")
604  ->bind(":source_id", $source->get($source->getPrimaryKey()),
605  ":target_class", $class,
606  ":source_class", get_class($source))
607  ->executeValue("COUNT(1)");
608 
609  return $items;
610  }
$constraint

◆ countBoundItemsReverse()

static TaxonomyManager::countBoundItemsReverse (   $target,
  $class,
  $constraint = "" 
)
static

Same as TaxonomyManager::countBoundItems, but with the reverse relationship.

Parameters
DataItem$targetthe target item
string$classthe class of bound items to retrieve
string$constraintoptional extra constraint or ordering
Returns
array any items of the specified class that are bound to the target

Definition at line 619 of file taxonomy_manager.inc.

620  {
621  $constraint = preg_replace("/^\\s*WHERE/i", "AND", $constraint);
622  $obj = new $class;
623  $pk = $obj->getPrimaryKey();
624 
625  $items = Query::create($class, "WHERE $pk IN (SELECT id FROM binding WHERE target_id=:target_id and class=:source_class AND target_class=:target_class) $constraint")
626  ->bind(":target_id", $target->get($target->getPrimaryKey()),
627  ":target_class", get_class($target),
628  ":source_class", $class)
629  ->executeValue("COUNT(1)");
630 
631  return $items;
632 
633  }

◆ generateSearchConstraint()

static TaxonomyManager::generateSearchConstraint (   $text,
  $class,
  $placeholder = false 
)
static

Generates a search constraint matching the supplied text against taxonomy terms that might be associated with the given class.

Parameters
string$textthe search text
string$classthe DataItem class that is being search
string$placeholdertrue if $text is a placeholder variable, false,
Returns
string a SQL constraint that can be used to perform the taxonomy text match

Definition at line 643 of file taxonomy_manager.inc.

644  {
645  $obj = new $class;
646  $pk = $obj->getPrimaryKey();
647  $text = $placeholder ? $text : "'$text'";
648 
649  return "$pk in (select id from taxonomy_term_association a, taxonomy_term t where t.term like {$text} and a.term_id=t.term_id and a.class='{$class}')";
650  }

◆ getAssociatedTaxonomies()

static TaxonomyManager::getAssociatedTaxonomies (   $classes,
  $facet_filter_only = false 
)
static

Definition at line 298 of file taxonomy_manager.inc.

299  {
300  if (!is_array($classes))
301  {
302  $classes = array($classes);
303  }
304 
305  $matched = array();
306 
307  $taxonomies = Query::create(Taxonomy, "")->execute();
308 
309  foreach($taxonomies as $taxonomy)
310  {
311  if ($facet_filter_only and !$taxonomy->enable_facet_filter) continue;
312 
313  $taxonomy_classes = explode(",", $taxonomy->associated_classes);
314  foreach($classes as $class)
315  {
316  if (array_search($class, $taxonomy_classes) !== FALSE)
317  {
318  $matched[] = $taxonomy;
319  break;
320  }
321  }
322  }
323 
324  return $matched;
325  }

◆ getBindableClasses()

static TaxonomyManager::getBindableClasses ( )
static

Definition at line 150 of file taxonomy_manager.inc.

151  {
153  {
154  $bindableClasses = array();
155  ComponentManager::fireEvent("RegisterBindableClasses");
156  }
157 
159  }
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.

◆ getBindableClassOptions()

static TaxonomyManager::getBindableClassOptions ( )
static

Definition at line 167 of file taxonomy_manager.inc.

168  {
170 
171  $options = array();
172 
173  foreach($classes as $class => $format)
174  {
175  $proto = new $class;
176  $options[$class] = $proto->prettifyClassName();
177  }
178 
179  return $options;
180  }

◆ getBindingFacetFilterClass()

static TaxonomyManager::getBindingFacetFilterClass (   $bindingTarget)
static

Definition at line 211 of file taxonomy_manager.inc.

212  {
214  return $classes[$bindingTarget->target_class]["facet_filter"];
215  }
$bindingTarget

◆ getBindingFieldRendererClass()

static TaxonomyManager::getBindingFieldRendererClass (   $bindingTarget)
static

Definition at line 205 of file taxonomy_manager.inc.

206  {
208  return $classes[$bindingTarget->target_class]["field_renderer"];
209  }

◆ getBindingOptionFormat()

static TaxonomyManager::getBindingOptionFormat (   $bindingTarget)
static

Definition at line 199 of file taxonomy_manager.inc.

200  {
202  return $classes[$bindingTarget->target_class]["format"];
203  }

◆ getBindingOptions()

static TaxonomyManager::getBindingOptions (   $bindingTarget)
static

Definition at line 182 of file taxonomy_manager.inc.

183  {
185 
186  $class = is_object($bindingTarget) ? get_class($bindingTarget) : $bindingTarget;
187  $bindingDetails = TaxonomyManager::$bindableClasses[$class];
188  if (!$bindingDetails)
189  {
190  trace("## $class is not registered as a binding target", 1);
191  return array();
192  }
193 
194  $targets = Query::create($class, $bindingDetails["constraint"])->execute();
195 
196  return $targets;
197  }

◆ getBindingTargets()

static TaxonomyManager::getBindingTargets (   $classes,
  $facet_filter_only = false 
)
static

Definition at line 327 of file taxonomy_manager.inc.

328  {
329  if (!is_array($classes))
330  {
331  $classes = array($classes);
332  }
333 
334  $matched = array();
335 
336  $bindingTargets = Query::create(BindingTarget, "")->execute();
337 
338  foreach($bindingTargets as $bindingTarget)
339  {
340  if ($facet_filter_only and !$bindingTarget->enable_facet_filter) continue;
341 
342  $assoc_classes = explode(",", $bindingTarget->associated_classes);
343  foreach($classes as $class)
344  {
345  if (array_search($class, $assoc_classes) !== FALSE)
346  {
347  $matched[] = $bindingTarget;
348  break;
349  }
350  }
351  }
352 
353  return $matched;
354  }

◆ getBoundItems()

static TaxonomyManager::getBoundItems (   $source,
  $class,
  $constraint = "" 
)
static

Retrieve items of the specified class that are bound to the specified source object.

Parameters
DataItem$sourcethe source item
string$classthe class of bound items to retrieve
string$constraintoptional extra constraint or ordering
Returns
array any items of the specified class that are bound to the target

Definition at line 552 of file taxonomy_manager.inc.

553  {
554  $constraint = preg_replace("/^\\s*WHERE/i", "AND", $constraint);
555  $obj = new $class;
556  $pk = $obj->getPrimaryKey();
557 
558  $items = Query::create($class, "WHERE $pk IN (SELECT target_id FROM binding WHERE id=:source_id and class=:source_class AND target_class=:target_class) $constraint")
559  ->bind(":source_id", $source->get($source->getPrimaryKey()),
560  ":target_class", $class,
561  ":source_class", get_class($source))
562  ->execute();
563 
564  return $items;
565  }

◆ getBoundItemsReverse()

static TaxonomyManager::getBoundItemsReverse (   $target,
  $class,
  $constraint = "" 
)
static

Same as TaxonomyManager::getBoundItems, but with the reverse relationship.

Parameters
DataItem$targetthe target item
string$classthe class of bound items to retrieve
string$constraintoptional extra constraint or ordering
Returns
array any items of the specified class that are bound to the target

Definition at line 574 of file taxonomy_manager.inc.

575  {
576  $constraint = preg_replace("/^\\s*WHERE/i", "AND", $constraint);
577  $obj = new $class;
578  $pk = $obj->getPrimaryKey();
579 
580  $items = Query::create($class, "WHERE $pk IN (SELECT id FROM binding WHERE target_id=:target_id and class=:source_class AND target_class=:target_class) $constraint")
581  ->bind(":target_id", $target->get($target->getPrimaryKey()),
582  ":target_class", get_class($target),
583  ":source_class", $class)
584  ->execute();
585 
586  return $items;
587 
588  }

◆ getRegisteredClasses()

static TaxonomyManager::getRegisteredClasses ( )
static

Retrieved the list of DataItem classes that have registered as supporting taxonomy term associations.

Returns
array of class names

Definition at line 121 of file taxonomy_manager.inc.

122  {
123  $registeredClasses = array();
124 
125  $registeredClasses = ComponentManager::fireEvent("RegisterTaxonomyClasses", $registeredClasses);
126 
127  return $registeredClasses;
128  }

◆ getRegisteredClassOptions()

static TaxonomyManager::getRegisteredClassOptions ( )
static

Retrieves the list of DataItem classes that have registered as supporting taxonomy term associations in an associated array format suitable for use with a SelectFieldRenderer or CheckListFieldRenderer.

Returns
array

Definition at line 135 of file taxonomy_manager.inc.

136  {
138 
139  $options = array();
140 
141  foreach($classes as $class)
142  {
143  $proto = new $class;
144  $options[$class] = $proto->prettifyClassName(true);
145  }
146 
147  return $options;
148  }
static getRegisteredClasses()
Retrieved the list of DataItem classes that have registered as supporting taxonomy term associations.

◆ getRequestFilterConstraint()

static TaxonomyManager::getRequestFilterConstraint (   $class)
static

Definition at line 471 of file taxonomy_manager.inc.

472  {
474 
475  $obj = new $class;
476  $pk = $obj->getPrimaryKey();
477 
478  $clauses = array();
479 
480  foreach($taxonomies as $taxonomy)
481  {
482  if (isset($_REQUEST[$taxonomy->identifier]))
483  {
484  $term_ids = $_REQUEST[$taxonomy->identifier];
485 
486  if (!$term_ids) continue;
487  if (!preg_match("/^[\\d,]+/", $term_ids))
488  {
489  throw new FakoliException("Invalid facet filter terms");
490  }
491 
492  $clauses[] = "$pk IN (SELECT id FROM taxonomy_term_association WHERE class='$class' AND term_id IN ($term_ids))";
493  }
494  }
495 
496  return implode(" AND ", $clauses);
497  }
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53

◆ getRequestTerms()

static TaxonomyManager::getRequestTerms (   $classes)
static

Definition at line 445 of file taxonomy_manager.inc.

446  {
447  $ids = array();
448 
449  if (!is_array($classes) && $classes) $classes = array($classes);
450 
452  foreach($taxonomies as $taxonomy)
453  {
454  if (isset($_REQUEST[$taxonomy->identifier]))
455  {
456  $term_ids = explode(",", $_REQUEST[$taxonomy->identifier]);
457  checkNumeric($term_ids);
458  foreach($term_ids as $term_id)
459  {
460  if ($term_id) $ids[] = $term_id;
461  }
462  }
463  }
464 
465  if (count($ids) == 0) return array();
466  $idString = implode(", ", $ids);
467 
468  return Query::create(TaxonomyTerm, "WHERE term_id IN ($idString)")->execute();
469  }
$term_id
Definition: blog.inc:40

◆ getTabs()

static TaxonomyManager::getTabs (   $key)
static

Definition at line 108 of file taxonomy_manager.inc.

109  {
110  $tabs = array( "Taxonomy Definition" => "/admin/taxonomy_form",
111  "Terms" => "/admin/taxonomy_terms");
112 
113  $qs = ($key) ? "taxonomy_id=$key" : "";
114  return new TabBar("tabs", $tabs, $qs);
115  }
$tabs

◆ importTerms()

static TaxonomyManager::importTerms (   $taxonomy,
  $file 
)
static

Definition at line 356 of file taxonomy_manager.inc.

357  {
358  $indexedTerms = reindexList($taxonomy->Terms(), "term");
359 
360  $fp = fopen($file, "r");
361 
362  $fields = fgetcsv($fp);
363 
364  $fileIdx = -1;
365 
366  foreach($fields as $field)
367  {
368  if ($field == "term")
369  {
370  $fileIdx = $i;
371  break;
372  }
373 
374  $i++;
375  }
376 
377  if ($fileIdx < 0)
378  {
379  throw new FakoliException("'term' column not present");
380  }
381 
382  while($values = fgetcsv($fp))
383  {
384  $term = $values[$fileIdx];
385 
386  trace("Importing term", 3);
387 
388  $i = 0;
389 
390  if (array_key_exists($term, $indexedTerms))
391  {
392  $import = $indexedTerms[$term];
393  foreach($fields as $field)
394  {
395  $import->set($field, $values[$i]);
396  $i++;
397  }
398 
399  $import->save();
400  }
401  else
402  {
403  $import = new TaxonomyTerm();
404  foreach($fields as $field)
405  {
406  $import->set($field, $values[$i]);
407  $i++;
408  }
409 
410  $import->taxonomy_id = $taxonomy->taxonomy_id;
411  $import->save();
412  }
413  }
414 
415  fclose($fp);
416  }
$file
Definition: delete.inc:47

◆ registerBindableClass()

static TaxonomyManager::registerBindableClass (   $class,
  $format,
  $constraint = "",
  $fieldRenderer = BindingTargetFieldRenderer,
  $facetFilter = BindingTargetFacetFilter 
)
static

Definition at line 161 of file taxonomy_manager.inc.

162  {
163  TaxonomyManager::$bindableClasses[$class] = array("format" => $format, "constraint" => $constraint,
164  "field_renderer" => $fieldRenderer, "facet_filter" => $facetFilter);
165  }

◆ registerSerializationHandler()

static TaxonomyManager::registerSerializationHandler ( )
static

Definition at line 658 of file taxonomy_manager.inc.

659  {
660  SerializationManager::registerHandler("taxonomy", "Taxonomies and Terms", new TaxonomySerializationHandler());
661  SerializationManager::registerHandler("taxonomy_associations", "Taxonomy Term Associations", new SimpleSerializationHandler(TaxonomyTermAssociation));
662  return true;
663  }
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.
Provides a simple implementation of a SerializationHandler that can serialize a single DataItem class...

◆ setDefaults()

static TaxonomyManager::setDefaults ( )
static

Definition at line 103 of file taxonomy_manager.inc.

104  {
105  //TODO: Set default configuration parameters here
106  }

◆ upgradeComponent()

static TaxonomyManager::upgradeComponent (   $version)
static

Definition at line 652 of file taxonomy_manager.inc.

653  {
655  $mgr->upgrade($version);
656  }

Member Data Documentation

◆ $bindableClasses

TaxonomyManager::$bindableClasses = null
static

Definition at line 92 of file taxonomy_manager.inc.


The documentation for this class was generated from the following file: