CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
14 class ClassificationFactory extends ItemFactory
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)),
74 ApplicationFramework::PRIORITY_BACKGROUND,
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 }
ClassificationFactory\RecalculateResourceCount
static RecalculateResourceCount($Id)
Callback to recalculate the resource count for a single classification by its ID. ...
Definition: ClassificationFactory.php:134
Classification\FullName
FullName()
Get full classification name (all segments).
Definition: Classification.php:200
Database
SQL database abstraction object with smart query caching.
Definition: Database.php:22
ClassificationFactory\GetChildIds
GetChildIds($ClassId)
Get IDs of all children of specified classification.
Definition: ClassificationFactory.php:37
ClassificationFactory\__construct
__construct($FieldId=NULL)
Class constructor.
Definition: ClassificationFactory.php:23
ClassificationFactory\FindMatchingRecentlyUsedValues
FindMatchingRecentlyUsedValues($SearchString, $NumberOfResults=5, $IdExclusions=array(), $ValueExclusions=array())
Retrieve recently used items matching a search string.
Definition: ClassificationFactory.php:88
ItemFactory\$DB
$DB
Definition: ItemFactory.php:1004
PHP
PHP
Definition: RSSClient--Test.php:12
ClassificationFactory\RecalculateAllResourceCounts
RecalculateAllResourceCounts()
Queue tasks to recalculate resource counts for all classifications.
Definition: ClassificationFactory.php:65
ClassificationFactory\PruneClassificationsToTopLevel
static PruneClassificationsToTopLevel($Classes)
Prune list of classifications back to just their top level.
Definition: ClassificationFactory.php:156
ItemFactory
Common factory class for item manipulation.
Definition: ItemFactory.php:17
ClassificationFactory
Factory for producing and manipulating Classification objects.
Definition: ClassificationFactory.php:14
Classification
Metadata type representing hierarchical ("Tree") controlled vocabulary values.
Definition: Classification.php:13
ItemFactory\GetItemIds
GetItemIds($Condition=NULL, $IncludeTempItems=FALSE, $SortField=NULL, $SortAscending=TRUE)
Return array of item IDs.
Definition: ItemFactory.php:238
ApplicationFramework\PRIORITY_BACKGROUND
const PRIORITY_BACKGROUND
Lowest priority.
Definition: ApplicationFramework.php:2905