4 # FILE: Classification.php
5 # AUTHOR: Edward Almasy
7 # Part of the Scout Portal Toolkit
8 # Copyright 2002-2003 Internet Scout Project
9 # http://scout.wisc.edu
17 # ---- PUBLIC INTERFACE --------------------------------------------------
36 function Classification($ClassId, $Name = NULL, $FieldId = NULL, $ParentId = NULL)
40 # assume everything will turn out okay
43 # create DB handle for our use
47 # if class ID not given (indicating class must be created)
48 if ($ClassId === NULL)
50 # if parent class supplied
51 if ($ParentId !== NULL)
53 # if parent ID was invalid
55 && ($DB->Query(
"SELECT COUNT(*) AS NumberFound"
56 .
" FROM Classifications"
57 .
" WHERE ClassificationId = ".intval($ParentId),
60 # set error code for bad parent ID
65 # if name already exists
67 if ($FieldId === NULL)
69 # If we know what field we're trying to add a classifcation for,
70 # Check just within that field
71 $Count = $DB->Query(
"SELECT COUNT(*) AS NumberFound FROM Classifications"
72 .
" WHERE ParentId = ".intval($ParentId)
73 .
" AND LOWER(SegmentName) = '"
74 .addslashes(strtolower($Name)).
"'",
79 # Otherwise, check all classifications for all fields
80 $Count = $DB->Query(
"SELECT COUNT(*) AS NumberFound FROM Classifications"
81 .
" WHERE ParentId = ".intval($ParentId)
82 .
" AND FieldId = ".intval($FieldId)
83 .
" AND LOWER(SegmentName) = '"
84 .addslashes(strtolower($Name)).
"'",
90 # set error code for duplicate class name
95 # add class to database
96 $ParentId = intval($ParentId);
104 $DB->Query(
"SELECT ClassificationName, Depth"
105 .
" FROM Classifications"
106 .
" WHERE ClassificationId = ".$ParentId);
107 $ParentInfo = $DB->FetchRow();
108 $NewName = $ParentInfo[
"ClassificationName"].
" -- ".$Name;
109 $NewDepth = $ParentInfo[
"Depth"] + 1;
111 $DB->Query(
"INSERT INTO Classifications"
112 .
" (FieldId, ParentId, SegmentName, ResourceCount,"
113 .
" Depth, ClassificationName) VALUES"
114 .
" (".intval($FieldId).
", ".$ParentId.
","
115 .
" '".addslashes($Name).
"', 0, "
116 .$NewDepth.
", '".addslashes($NewName).
"')");
118 # retrieve ID of new class
119 $this->
Id = $DB->LastInsertId(
"Classifications");
125 # parse classification name into separate segments
126 $Segments = preg_split(
"/--/", $Name);
128 # start out with top as parent
133 $CurrentFullName =
"";
134 foreach ($Segments as $Segment)
136 # track segment depth and full classification name for use in adding new entries
137 $Segment = trim($Segment);
139 $CurrentFullName .= (($CurrentFullName ==
"") ?
"" :
" -- ").$Segment;
141 # if we have added classifications
142 $Segment = addslashes($Segment);
145 # we know that current segment will not be found
150 # look up classification with current parent and segment name
151 if (!isset($IdCache[$FieldId][$ParentId][$Segment]))
155 $IdCache[$FieldId][$ParentId][$Segment] = $DB->Query(
156 "SELECT ClassificationId FROM Classifications"
157 .
" WHERE ParentId = -1"
158 .
" AND SegmentName = '".addslashes($Segment).
"'"
159 .
" AND FieldId = ".intval($FieldId),
164 $IdCache[$FieldId][$ParentId][$Segment] = $DB->Query(
165 "SELECT ClassificationId FROM Classifications "
166 .
"WHERE ParentId = ".intval($ParentId)
167 .
" AND SegmentName = '".addslashes($Segment).
"'",
171 $ClassId = $IdCache[$FieldId][$ParentId][$Segment];
174 # if classification not found
175 if ($ClassId === NULL)
177 # add new classification
178 $DB->Query(
"INSERT INTO Classifications "
179 .
"(FieldId, ParentId, SegmentName,"
180 .
" ClassificationName, Depth, ResourceCount) "
181 .
"VALUES (".intval($FieldId).
", "
182 .intval($ParentId).
", "
183 .
"'".addslashes($Segment).
"', "
184 .
"'".addslashes($CurrentFullName).
"', "
185 .intval($CurrentDepth).
", 0)");
186 $ClassId = $DB->LastInsertId(
"Classifications");
187 $IdCache[$FieldId][$ParentId][$Segment] = $ClassId;
189 # track total number of new classification segments created
193 # set parent to created or found class
194 $PreviousParentId = $ParentId;
195 $ParentId = $ClassId;
198 # our class ID is the one that was last found
199 $this->
Id = $ClassId;
204 # our class ID is the one that was supplied by caller
205 $this->
Id = intval($ClassId);
208 # if no error encountered
211 # load in attributes from database
212 $DB->Query(
"SELECT * FROM Classifications"
213 .
" WHERE ClassificationId = ".intval($this->
Id));
214 $this->DBFields = $DB->NumRowsSelected()>0 ? $DB->FetchRow() : NULL ;
216 # set error status if class info not loaded
217 if ($this->DBFields === NULL ||
218 $this->DBFields[
"ClassificationId"] != $this->
Id)
229 function Status() {
return $this->ErrorStatus; }
235 function Id() {
return $this->Id; }
241 function FullName() {
return stripslashes($this->DBFields[
"ClassificationName"]); }
259 function Depth() {
return $this->DBFields[
"Depth"]; }
278 function ParentId() {
return $this->DBFields[
"ParentId"]; }
286 return stripslashes($this->UpdateValue(
"SegmentName", $NewValue)); }
296 return stripslashes($this->UpdateValue(
"LinkString", $NewValue)); }
305 return $this->UpdateValue(
"QualifierId", $NewValue); }
313 return $this->UpdateValue(
"FieldId", $NewValue); }
323 # if new qualifier supplied
326 # set new qualifier ID
329 # use new qualifier for return value
330 $Qualifier = $NewValue;
334 # if qualifier is available
337 # create qualifier object using stored ID
342 # return NULL to indicate no qualifier
347 # return qualifier to caller
360 # start with full classification name set to our segment name
361 $FullClassName = $this->DBFields[
"SegmentName"];
363 # assume to begin with that we're at the top of the hierarchy
366 # while parent available
367 $ParentId = $this->DBFields[
"ParentId"];
368 while ($ParentId != -1)
370 # retrieve classification information
371 $DB->Query(
"SELECT SegmentName, ParentId "
372 .
"FROM Classifications "
373 .
"WHERE ClassificationId=".$ParentId);
374 $Record = $DB->FetchRow();
376 # prepend segment name to full classification name
377 $FullClassName = stripslashes($Record[
"SegmentName"])
378 .
" -- ".$FullClassName;
380 # increment depth value
383 # move to parent of current classification
384 $ParentId = $Record[
"ParentId"];
388 $DB->Query(
"SELECT ClassificationId "
389 .
"FROM Classifications "
390 .
"WHERE ParentId=".intval($this->
Id));
391 while ($Record = $DB->FetchRow())
393 # perform depth and name recalc
395 $Child->RecalcDepthAndFullName();
398 # save new depth and full classification name
399 $DB->Query(
"UPDATE Classifications SET "
400 .
"Depth=".intval($Depth).
", "
401 .
"ClassificationName='".addslashes($FullClassName).
"' "
402 .
"WHERE ClassificationId=".intval($this->
Id));
403 $this->DBFields[
"ClassificationName"] = $FullClassName;
404 $this->DBFields[
"Depth"] = $Depth;
416 $IdsUpdated = array();
418 # if we don't have a skip list or we aren't in the skip list
419 if (!$IdsToSkip || !in_array($this->
Id, $IdsToSkip))
421 # retrieve new count of resources directly associated with class
422 $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
423 .
" FROM ResourceClassInts, Resources"
424 .
" WHERE ClassificationId=".intval($this->
Id)
425 .
" AND ResourceClassInts.ResourceId = Resources.ResourceId"
426 .
" AND ReleaseFlag = 1");
427 $Record = $this->DB->FetchRow();
428 $ResourceCount = $Record[
"ResourceCount"];
430 # add on resources associated with all children
431 $ResourceCount += $this->DB->Query(
432 "SELECT SUM(ResourceCount) AS ResourceCountTotal "
433 .
"FROM Classifications "
434 .
"WHERE ParentId = ".intval($this->
Id),
435 "ResourceCountTotal");
437 # save new count to database
438 $this->DB->Query(
"UPDATE Classifications SET "
439 .
"ResourceCount=".$ResourceCount.
" "
440 .
"WHERE ClassificationId=".intval($this->
Id));
442 # save new count to our local cache
443 $this->DBFields[
"ResourceCount"] = $ResourceCount;
445 # add our ID to list of IDs that have been recalculated
446 $IdsUpdated[] = $this->Id;
449 # update resource count for our parent (if any)
450 if (($this->DBFields[
"ParentId"] != -1)
451 && (!$IdsToSkip || !in_array($this->DBFields[
"ParentId"], $IdsToSkip)) )
456 $IdsUpdated = array_merge($IdsUpdated, $Class->RecalcResourceCount());
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),
482 # this also returns grandchildren, great grandchildren, etc.
485 $ChildList = array();
487 $this->DB->Query(
"SELECT ClassificationId "
488 .
"FROM Classifications "
489 .
"WHERE ParentId=".intval($this->
Id));
491 while ($Entry = $this->DB->FetchRow())
493 $ChildList[] = $Entry[
"ClassificationId"];
495 if($Child->ChildCount() > 0)
497 $GrandChildList = $Child->ChildList();
498 $ChildList = array_merge($GrandChildList, $ChildList);
511 $DeleteIfHasResources = FALSE, $DeleteIfHasChildren = FALSE)
515 # if no resources or okay to delete with resources
516 # and no children or okay to delete with children
518 && ($DeleteIfHasChildren || ($this->
ChildCount() == 0)))
520 $ParentId = $this->DBFields[
"ParentId"];
522 if ($DeleteIfHasResources)
524 $DB->Query(
"DELETE FROM ResourceClassInts "
525 .
"WHERE ClassificationId=".intval($this->
Id));
528 # delete this classification
529 $DB->Query(
"DELETE FROM Classifications "
530 .
"WHERE ClassificationId=".intval($this->
Id));
532 # delete parent classification (if requested)
533 if (($DeleteParents) && ($this->DBFields[
"ParentId"] != -1))
537 TRUE, $DeleteIfHasResources, $DeleteIfHasChildren);
543 # ---- PRIVATE INTERFACE -------------------------------------------------
548 private $ErrorStatus;
549 private $SegmentsCreated;
551 # convenience function to supply parameters to Database->UpdateValue()
552 private function UpdateValue($FieldName, $NewValue)
554 return $this->DB->UpdateValue(
"Classifications", $FieldName, $NewValue,
555 "ClassificationId = ".intval($this->
Id),
556 $this->DBFields, TRUE);