3 # FILE: Classification.php 5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2002-2016 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 15 # ---- PUBLIC INTERFACE -------------------------------------------------- 31 public static function Create($Name, $FieldId, $ParentId = NULL)
35 # initialize state for creation 36 self::$SegmentsCreated = 0;
38 # if parent class supplied 40 if ($ParentId !== NULL)
42 # error out if parent ID is invalid 43 if ($ParentId != self::NOPARENT)
45 if (
$DB->Query(
"SELECT COUNT(*) AS NumberFound" 46 .
" FROM Classifications" 47 .
" WHERE ClassificationId = ".intval($ParentId),
50 throw new InvalidArgumentException(
"Invalid parent ID" 51 .
" specified (".$ParentId.
").");
55 # error out if name already exists 57 $Count =
$DB->Query(
"SELECT COUNT(*) AS NumberFound" 58 .
" FROM Classifications" 59 .
" WHERE ParentId = ".intval($ParentId)
60 .
" AND FieldId = ".intval($FieldId)
61 .
" AND LOWER(SegmentName) = '" 62 .addslashes(strtolower($Name)).
"'",
66 throw new Exception(
"Duplicate name specified for" 67 .
" new classification (".$Name.
").");
70 # determine full name and depth for new classification 71 if ($ParentId == self::NOPARENT)
78 $DB->Query(
"SELECT ClassificationName, Depth" 79 .
" FROM Classifications" 80 .
" WHERE ClassificationId = ".intval($ParentId));
81 $ParentInfo =
$DB->FetchRow();
82 $NewName = $ParentInfo[
"ClassificationName"].
" -- ".$Name;
83 $NewDepth = $ParentInfo[
"Depth"] + 1;
86 # add classification to database 87 $InitialValues = array(
88 "FieldId" => $FieldId,
89 "ParentId" => $ParentId,
90 "SegmentName" => $Name,
93 "ClassificationName" => $NewName);
94 $NewItem = parent::CreateWithValues($InitialValues);
98 # parse classification name into separate segments 99 $Segments = preg_split(
"/--/", $Name);
101 # start out with top as parent 102 $ParentId = self::NOPARENT;
106 $CurrentFullName =
"";
107 foreach ($Segments as $Segment)
109 # track segment depth and full classification name for use 110 # in adding new entries 111 $Segment = trim($Segment);
113 $CurrentFullName .= (($CurrentFullName ==
"") ?
"" :
" -- ").$Segment;
115 # if we have added classifications 116 $Segment = addslashes($Segment);
117 if (self::$SegmentsCreated)
119 # we know that current segment will not be found 124 # look up classification with current parent and segment name 125 if (!isset($IdCache[$FieldId][$ParentId][$Segment]))
127 if ($ParentId == self::NOPARENT)
129 $IdCache[$FieldId][$ParentId][$Segment] =
$DB->Query(
130 "SELECT ClassificationId FROM Classifications" 131 .
" WHERE ParentId = ".self::NOPARENT
132 .
" AND SegmentName = '".addslashes($Segment).
"'" 133 .
" AND FieldId = ".intval($FieldId),
138 $IdCache[$FieldId][$ParentId][$Segment] =
$DB->Query(
139 "SELECT ClassificationId FROM Classifications " 140 .
"WHERE ParentId = ".intval($ParentId)
141 .
" AND SegmentName = '".addslashes($Segment).
"'",
145 $ClassId = $IdCache[$FieldId][$ParentId][$Segment];
148 # if classification not found 149 if ($ClassId === NULL)
151 # add new classification 152 $InitialValues = array(
153 "FieldId" => $FieldId,
154 "ParentId" => $ParentId,
155 "SegmentName" => $Segment,
156 "ResourceCount" => 0,
157 "Depth" => $CurrentDepth,
158 "ClassificationName" => $CurrentFullName);
159 $NewItem = parent::CreateWithValues($InitialValues);
160 $ClassId = $NewItem->Id();
161 $IdCache[$FieldId][$ParentId][$Segment] = $ClassId;
163 # track total number of new classification segments created 164 self::$SegmentsCreated++;
167 # set parent to created or found class 168 $ParentId = $ClassId;
172 # return new classification to caller 173 return new self($NewItem->Id());
191 return $this->ValueCache[
"ClassificationName"];
218 return $this->ValueCache[
"Depth"];
228 return $this->ValueCache[
"ResourceCount"];
239 return $this->ValueCache[
"FullResourceCount"];
248 return self::$SegmentsCreated;
257 return $this->ValueCache[
"ParentId"];
267 return $this->
UpdateValue(
"SegmentName", $NewValue);
279 return $this->
UpdateValue(
"LinkString", $NewValue);
290 return $this->
UpdateValue(
"QualifierId", $NewValue);
311 # if new qualifier supplied 314 # set new qualifier ID 317 # use new qualifier for return value 318 $Qualifier = $NewValue;
322 # if qualifier is available 325 # create qualifier object using stored ID 330 # return NULL to indicate no qualifier 335 # return qualifier to caller 346 # start with full classification name set to our segment name 347 $FullClassName = $this->ValueCache[
"SegmentName"];
349 # assume to begin with that we're at the top of the hierarchy 352 # while parent available 353 $ParentId = $this->ValueCache[
"ParentId"];
354 while ($ParentId != self::NOPARENT)
356 # retrieve classification information 357 $this->DB->Query(
"SELECT SegmentName, ParentId " 358 .
"FROM Classifications " 359 .
"WHERE ClassificationId=".$ParentId);
360 $Record = $this->DB->FetchRow();
362 # prepend segment name to full classification name 363 $FullClassName = $Record[
"SegmentName"].
" -- ".$FullClassName;
365 # increment depth value 368 # move to parent of current classification 369 $ParentId = $Record[
"ParentId"];
373 $this->DB->Query(
"SELECT ClassificationId FROM Classifications" 374 .
" WHERE ParentId = ".intval($this->
Id));
375 while ($Record = $this->DB->FetchRow())
377 # perform depth and name recalc 379 $Child->RecalcDepthAndFullName();
382 # save new depth and full classification name 384 $this->
UpdateValue(
"ClassificationName", $FullClassName);
392 $this->DB->Query(
"UPDATE Classifications SET LastAssigned=NOW() " 393 .
"WHERE ClassificationId=".intval($this->
Id));
405 $IdsUpdated = array();
407 # if we don't have a skip list or we aren't in the skip list 408 if (!$IdsToSkip || !in_array($this->
Id, $IdsToSkip))
410 # retrieve new count of resources directly associated with class 411 $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount" 412 .
" FROM ResourceClassInts, Resources" 413 .
" WHERE ClassificationId=".intval($this->
Id)
414 .
" AND ResourceClassInts.ResourceId = Resources.ResourceId" 415 .
" AND Resources.ResourceId > 0" 416 .
" AND ReleaseFlag = 1");
417 $Record = $this->DB->FetchRow();
418 $ResourceCount = $Record[
"ResourceCount"];
420 # add on resources associated with all children 421 $ResourceCount += $this->DB->Query(
422 "SELECT SUM(ResourceCount) AS ResourceCountTotal " 423 .
"FROM Classifications " 424 .
"WHERE ParentId = ".intval($this->
Id),
425 "ResourceCountTotal");
428 $this->
UpdateValue(
"ResourceCount", $ResourceCount);
430 # add our ID to list of IDs that have been recalculated 434 # update resource count for our parent (if any) 435 if (($this->ValueCache[
"ParentId"] != self::NOPARENT)
436 && (!$IdsToSkip || !in_array($this->ValueCache[
"ParentId"], $IdsToSkip)) )
439 $IdsUpdated = array_merge($IdsUpdated, $Class->RecalcResourceCount());
442 # retrieve new count of all resources directly associated with class 443 $FullCount = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount" 444 .
" FROM ResourceClassInts I, Resources R" 445 .
" WHERE I.ClassificationId = ".intval($this->
Id)
446 .
" AND R.ResourceId > 0" 447 .
" AND I.ResourceId = R.ResourceId",
450 # add on resources associated with all children 451 $FullCount += $this->DB->Query(
452 "SELECT SUM(ResourceCount) AS ResourceCountTotal" 453 .
" FROM Classifications" 454 .
" WHERE ParentId = ".intval($this->
Id),
455 "ResourceCountTotal");
457 # save new full count 458 $this->
UpdateValue(
"FullResourceCount", $ResourceCount);
460 # return list of IDs of updated classifications to caller 470 # return count of classifications that have this one as parent 471 return $this->DB->Query(
"SELECT COUNT(*) AS ClassCount " 472 .
"FROM Classifications " 473 .
"WHERE ParentId=".intval($this->
Id),
484 $ChildList = array();
486 $this->DB->Query(
"SELECT ClassificationId " 487 .
"FROM Classifications " 488 .
"WHERE ParentId=".intval($this->
Id));
490 while ($Entry = $this->DB->FetchRow())
492 $ChildList[] = $Entry[
"ClassificationId"];
494 if($Child->ChildCount() > 0)
496 $GrandChildList = $Child->ChildList();
497 $ChildList = array_merge($GrandChildList, $ChildList);
500 return array_unique($ChildList);
516 public function Delete($DeleteParents = FALSE,
517 $DeleteIfHasResources = FALSE, $DeleteIfHasChildren = FALSE)
519 # if no resources or okay to delete with resources 520 # and no children or okay to delete with children 523 && ($DeleteIfHasChildren || ($this->
ChildCount() == 0)))
527 $this->DB->Query(
"DELETE FROM ResourceClassInts" 528 .
" WHERE ClassificationId = ".intval($this->
Id));
532 # delete this classification 536 # delete parent classification (if requested) 537 $ParentId = $this->ValueCache[
"ParentId"];
538 if (($DeleteParents) && ($ParentId != self::NOPARENT))
541 $DeleteCount += $Parent->Delete(
542 TRUE, $DeleteIfHasResources, $DeleteIfHasChildren);
546 # return total number of classifications deleted to caller 551 # ---- PRIVATE INTERFACE ------------------------------------------------- 553 static private $SegmentsCreated;
LinkString($NewValue=DB_NOVALUE)
Get or set the stored link string for the Classification.
static SegmentsCreated()
Get number of new segments (Classifications) generated when creating a new Classification with a full...
FullName()
Get full classification name (all segments).
FullResourceCount()
Get number of all resources (minus temporary ones) having this classification assigned to them...
UpdateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database->UpdateValue().
SQL database abstraction object with smart query caching.
static Create($Name, $FieldId, $ParentId=NULL)
Add new classification to the hierarchy.
FieldId($NewValue=DB_NOVALUE)
Get or set the ID of the MetadataField for the Classification.
VariantName()
Get variant name of classification, if any.
Qualifier($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with the Classification.
ChildCount()
Get number of classifications that have this Classification as their direct parent.
RecalcResourceCount($IdsToSkip=NULL)
Recalculate number of resources assigned to class and any parent classes.
const NOPARENT
Parent value for classifications with no parent.
Name()
Get name of classification segment.
ParentId()
Get ID of parent Classification.
RecalcDepthAndFullName()
Rebuild classification full name and recalculate depth in hierarchy.
Common base class for persistent items store in database.
Depth()
Get depth of classification in hierarchy.
ResourceCount()
Get number of released resources having this classification assigned to them.
Delete($DeleteParents=FALSE, $DeleteIfHasResources=FALSE, $DeleteIfHasChildren=FALSE)
Remove Classification (and accompanying associations) from database.
UpdateLastAssigned()
Update the LastAssigned timestamp for this classification.
Metadata type representing hierarchical ("Tree") controlled vocabulary values.
QualifierId($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with the Classification by ID.
ChildList()
Get list of IDs of Classifications that have this class as an "ancestor" (parent, grandparent...
SegmentName($NewValue=DB_NOVALUE)
Get or set the segment name.
Id()
Get Classification ID.