CWIS Developer Documentation
ClassificationFactory.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: ClassificationFactory.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2004-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
15 {
16 
17  # ---- PUBLIC INTERFACE --------------------------------------------------
18 
23  public function __construct($FieldId = NULL)
24  {
25  # set up item factory base class
26  parent::__construct("Classification", "Classifications",
27  "ClassificationId", "ClassificationName", FALSE,
28  ($FieldId ? "FieldId = ".intval($FieldId) : NULL));
29  $this->FieldId = (!is_null($FieldId)) ? intval($FieldId) : NULL;
30  }
31 
37  public function GetChildIds($ClassId)
38  {
39  static $DB;
40 
41  # retrieve IDs of all children
42  if (!isset($DB)) { $DB = new Database(); }
43  $DB->Query("SELECT ClassificationId FROM Classifications"
44  ." WHERE ParentId = ".intval($ClassId));
45  $ChildIds = $DB->FetchColumn("ClassificationId");
46 
47  # for each child
48  $Ids = $ChildIds;
49  foreach ($Ids as $Id)
50  {
51  # retrieve IDs of any children of child
52  $ChildChildIds = $this->GetChildIds($Id);
53 
54  # add retrieved IDs to child ID list
55  $ChildIds = array_merge($ChildIds, $ChildChildIds);
56  }
57 
58  # return child ID list to caller
59  return $ChildIds;
60  }
61 
65  public function RecalculateAllResourceCounts()
66  {
67  # queue a task to recalculate the resource counts for each
68  # classification
69  foreach ($this->GetItemIds() as $Id)
70  {
71  $GLOBALS["AF"]->QueueUniqueTask(
72  array(__CLASS__, "RecalculateResourceCount"),
73  array(intval($Id)),
75  "Recalculate the resource counts for a classification");
76  }
77  }
78 
88  public function FindMatchingRecentlyUsedValues($SearchString, $NumberOfResults=5,
89  $IdExclusions=array(), $ValueExclusions=array() )
90  {
91  # return no results if empty search string passed in
92  if (!strlen(trim($SearchString))) { return array(); }
93 
94  $IdExclusionSql = (count($IdExclusions) >0) ?
95  "AND ClassificationId NOT IN ("
96  . implode(',', array_map('intval', $IdExclusions) ) . ")" :
97  "" ;
98 
99  $ValueExclusionSql = (count($ValueExclusions) > 0)
100  ? "AND ClassificationName NOT IN ("
101  .implode(',', array_map(
102  function($v){ return "'".addslashes($v)."'"; },
103  $ValueExclusions) ).")"
104  : "";
105 
106  # mark all search elements as required
107  $SearchString = preg_replace("%\S+%", "+\$0", $SearchString);
108 
109  $QueryString =
110  "SELECT ClassificationId, ClassificationName"
111  ." FROM Classifications"
112  ." WHERE LastAssigned IS NOT NULL"
113  .(($this->FieldId !== NULL) ? " AND FieldId = "
114  .intval($this->FieldId) : "")
115  ." AND MATCH(ClassificationName) AGAINST ('"
116  .addslashes(trim($SearchString))."' IN BOOLEAN MODE)"
117  ." ".$IdExclusionSql
118  ." ".$ValueExclusionSql
119  ." ORDER BY LastAssigned DESC LIMIT ".$NumberOfResults;
120 
121  $this->DB->Query($QueryString);
122 
123  $Names = $this->DB->FetchColumn("ClassificationName", "ClassificationId");
124 
125  return $Names;
126 
127  }
134  public static function RecalculateResourceCount($Id)
135  {
136  # only recalculate the counts if the classification appears valid
137  try
138  {
139  $Classification = new Classification($Id);
140  }
141  catch (Exception $e)
142  {
143  return;
144  }
145 
146  $Classification->RecalcResourceCount();
147  }
148 
156  public static function PruneClassificationsToTopLevel($Classes)
157  {
158  # for each area
159  $ClassNames = array();
160  foreach ($Classes as $Id => $Value)
161  {
162  # pare down area so that it is top-level
163  $Class = new Classification($Id);
164  while ($Class->Depth() > 0)
165  {
166  $Class = new Classification($Class->ParentId());
167  }
168 
169  # add area to list
170  $ClassNames[$Class->Id()] = $Class->FullName();
171  }
172 
173  # return list of subject area names to caller
174  return $ClassNames;
175  }
176 
177 
178  # ---- PRIVATE INTERFACE -------------------------------------------------
179 
180  private $FieldId;
181 }
static RecalculateResourceCount($Id)
Callback to recalculate the resource count for a single classification by its ID. ...
FullName()
Get full classification name (all segments).
SQL database abstraction object with smart query caching.
Definition: Database.php:22
GetChildIds($ClassId)
Get IDs of all children of specified classification.
__construct($FieldId=NULL)
Class constructor.
FindMatchingRecentlyUsedValues($SearchString, $NumberOfResults=5, $IdExclusions=array(), $ValueExclusions=array())
Retrieve recently used items matching a search string.
RecalculateAllResourceCounts()
Queue tasks to recalculate resource counts for all classifications.
static PruneClassificationsToTopLevel($Classes)
Prune list of classifications back to just their top level.
Common factory class for item manipulation.
Definition: ItemFactory.php:17
Factory for producing and manipulating Classification objects.
Metadata type representing hierarchical ("Tree") controlled vocabulary values.
GetItemIds($Condition=NULL, $IncludeTempItems=FALSE, $SortField=NULL, $SortAscending=TRUE)
Return array of item IDs.
const PRIORITY_BACKGROUND
Lowest priority.