3 # FILE: ControlledName.php 5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2001-2016 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 17 # ---- PUBLIC INTERFACE -------------------------------------------------- 40 public function __construct($NameId, $Name = NULL, $FieldId = NULL,
41 $QualifierId =
"NULL", $VariantName = NULL)
43 # assume everything will turn out okay 44 $this->ErrorStatus = self::STATUS_OK;
46 # create DB handle for our use 50 # remove whitespace padding 52 $VariantName = trim($VariantName);
54 # look for passed in name and type 55 if (!empty($Name) && !empty($FieldId))
57 $DB->Query(
"SELECT * FROM ControlledNames".
58 " WHERE ControlledName = \"".addslashes($Name).
"\"".
59 " AND FieldId = ".intval($FieldId));
61 while ($this->DBFields = $DB->FetchRow())
63 # this controlled name already exists 64 if ($this->DBFields[
"ControlledName"] == $Name)
66 $this->ErrorStatus = self::STATUS_EXISTS;
67 $NameId = $this->DBFields[
"ControlledNameId"];
69 # cache the variant name separately 70 $VN = $DB->Query(
"SELECT VariantName FROM VariantNames".
71 " WHERE ControlledNameId = ".
72 $this->DBFields[
"ControlledNameId"],
"VariantName");
74 $this->DBFields[
"VariantName"] = $VN;
78 # controlled name not found, create it 79 if ($this->ErrorStatus == self::STATUS_OK)
81 # add new controlled name 82 $DB->Query(
"INSERT INTO ControlledNames ".
83 "(FieldId, ControlledName, QualifierId)".
84 " VALUES (".intval($FieldId).
", '".addslashes($Name)
85 .
"', ".intval($QualifierId).
")");
87 # get name ID for new controlled name 88 $NameId = $DB->LastInsertId();
91 if (!empty($VariantName))
93 $DB->Query(
"INSERT INTO VariantNames ".
94 "(ControlledNameId, VariantName) ".
95 "VALUES (".intval($NameId).
", '" 96 .addslashes($VariantName).
"') ");
100 # Name Id passed in, look it up 101 if (!empty($NameId) && $NameId != -1)
103 $DB->Query(
"SELECT * FROM ControlledNames".
104 " WHERE ControlledNameId = ".intval($NameId));
105 $this->DBFields = $DB->FetchRow();
107 # cache the variant name separately 108 $VN = $DB->Query(
"SELECT VariantName FROM VariantNames".
109 " WHERE ControlledNameId = ".intval($NameId),
"VariantName");
111 $this->DBFields[
"VariantName"] = $VN;
114 # save supplied or generated controlled name ID 115 $this->
Id = intval($NameId);
117 # set error status if controlled name info not loaded 118 if (!array_key_exists(
"ControlledNameId", $this->DBFields)
119 || ($this->DBFields[
"ControlledNameId"] != $this->
Id))
121 $this->ErrorStatus = self::STATUS_INVALID_ID;
131 return $this->ErrorStatus;
150 return $this->UpdateValue(
"ControlledName", $NewValue);
160 return $this->UpdateVariantValue(
"VariantName", $NewValue);
170 return $this->UpdateValue(
"FieldId", $NewValue);
180 return $this->UpdateValue(
"QualifierId", $NewValue);
200 # if new qualifier supplied 203 # set new qualifier ID 206 # use new qualifier for return value 207 $Qualifier = $NewValue;
211 # if qualifier is available 214 # create qualifier object using stored ID 217 # if ID was zero and no name available for qualifieR 218 # (needed because some controlled name records in DB 219 # have 0 instead of NULL when no controlled name assigned) 220 # (NOTE: this is problematic if there is a qualifier with an 222 if (($this->
QualifierId() == 0) && !strlen($Qualifier->Name()))
224 # return NULL to indicate no qualifier 230 # return NULL to indicate no qualifier 235 # return qualifier to caller 249 # query for the controlled name 251 SELECT ControlledNameId FROM 252 ControlledNames WHERE FieldId='".addslashes($FieldId).
"' 253 AND ControlledName='".addslashes($ControlledName).
"'");
255 # return the controlled name IDs found, if any 256 return $Database->FetchColumn(
"ControlledNameId");
265 return $this->DB->Query(
"SELECT COUNT(*) AS Count FROM ".
266 "ResourceNameInts WHERE ControlledNameId = ".$this->
Id,
"Count");
276 "SELECT ResourceId FROM ResourceNameInts " 277 .
"WHERE ControlledNameId = ".$this->
Id);
279 return $this->DB->FetchColumn(
"ResourceId");
289 # Get a list of resources associated with the new name 290 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts " 291 .
"WHERE ControlledNameId = ".intval($NewNameId));
292 $NewNameResources = array();
293 while ($Row = $this->DB->FetchRow())
295 $NewNameResources[$Row[
"ResourceId"]]=1;
298 # Get a list of resources associated with the old name 299 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts " 300 .
"WHERE ControlledNameId = ".intval($this->
Id));
301 $OldNameResources = array();
302 while ($Row = $this->DB->FetchRow())
304 $OldNameResources[]= $Row[
"ResourceId"];
307 # Foreach of the old name resources, check to see if it's already 308 # associated with the new name. If not, associate it. 309 foreach ($OldNameResources as $ResourceId)
311 if (!isset($NewNameResources[$ResourceId]))
313 $this->DB->Query(
"INSERT INTO ResourceNameInts " 314 .
"(ResourceId, ControlledNameId) VALUES " 315 .
"(".intval($ResourceId).
",".intval($NewNameId).
")");
319 # Clear out all the associations to the old name 320 $this->DB->Query(
"DELETE FROM ResourceNameInts WHERE ControlledNameId = " 329 $this->DB->Query(
"UPDATE ControlledNames SET LastAssigned=NOW() " 330 .
"WHERE ControlledNameId=".intval($this->
Id));
339 public function Delete($DeleteIfHasResources = FALSE)
343 if ($DeleteIfHasResources || !$this->
InUse())
345 # delete this controlled name 346 $DB->Query(
"DELETE FROM ControlledNames WHERE ControlledNameId=".
349 # delete associated variant name 350 $DB->Query(
"DELETE FROM VariantNames WHERE ControlledNameId=".
353 if ($DeleteIfHasResources)
355 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ".
356 "ControlledNameId=".$this->
Id);
361 # ---- PRIVATE INTERFACE ------------------------------------------------- 366 private $ErrorStatus;
375 private function UpdateValue($FieldName, $NewValue)
377 return $this->DB->UpdateValue(
"ControlledNames", $FieldName,
378 $NewValue,
"ControlledNameId = ".$this->
Id,
379 $this->DBFields, TRUE);
389 private function UpdateVariantValue($FieldName, $NewValue)
391 if (!empty($NewValue))
393 # see if variant name exists for the controlled Name 394 $this->DB->Query(
"SELECT * from VariantNames WHERE ".
395 "ControlledNameId = ".$this->
Id);
397 # variant name exists so do an update 398 if ($this->DB->NumRowsSelected() > 0)
400 return $this->DB->UpdateValue(
"VariantNames",
401 $FieldName, $NewValue,
402 "ControlledNameId = ".$this->
Id,
403 $this->DBFields, TRUE);
405 # no variant name so do an insert 408 $this->DB->Query(
"INSERT into VariantNames ".
409 "(VariantName, ControlledNameId) VALUES ".
410 "('".addslashes($NewValue).
"', ".$this->
Id.
")");
413 # delete variant name 416 $this->DB->Query(
"Delete from VariantNames where ".
417 "ControlledNameId = ".$this->
Id);
RemapTo($NewNameId)
Change all currently associated Resources to be instead associated with another ControlledName.
__construct($NameId, $Name=NULL, $FieldId=NULL, $QualifierId="NULL", $VariantName=NULL)
Class constructor.
Name($NewValue=DB_NOVALUE)
Get or set the controlled vocabulary term.
GetAssociatedResources()
Get resourceIds associated with this ControlledName.
Delete($DeleteIfHasResources=FALSE)
Remove ControlledName (and any accompanying associations from database.
static SearchForControlledName($ControlledName, $FieldId)
Check if the given controlled name already exists for a given field ID.
SQL database abstraction object with smart query caching.
VariantName($NewValue=DB_NOVALUE)
Get or set any variant terms for this controlled name .
Metadata type representing non-hierarchical controlled vocabulary values.
Qualifier($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with this term via object.
const STATUS_OK
Successful execution.
FieldId($NewValue=DB_NOVALUE)
Get or set the MetadataField associated with this term.
Variant($NewValue=DB_NOVALUE)
Get or set the controlled name variant.
const STATUS_INVALID_ID
No ControlledName exists with specified ID.
QualifierId($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with this term via ID.
const STATUS_EXISTS
ControlledName already exists with this term.
InUse()
See if ControlledName is currently associated with any Resources.
Status()
Check success of constructor.
UpdateLastAssigned()
Update the LastAssigned timestamp for this classification.