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 223 ($this->
QualifierId() == 0) && !strlen($Qualifier->Name()))
225 # return NULL to indicate no qualifier 231 # return NULL to indicate no qualifier 236 # return qualifier to caller 250 # query for the controlled name 252 SELECT ControlledNameId FROM 253 ControlledNames WHERE FieldId='".addslashes($FieldId).
"' 254 AND ControlledName='".addslashes($ControlledName).
"'");
256 # return the controlled name IDs found, if any 257 return $Database->FetchColumn(
"ControlledNameId");
266 return $this->DB->Query(
"SELECT COUNT(*) AS Count FROM ".
267 "ResourceNameInts WHERE ControlledNameId = ".$this->
Id,
"Count");
277 "SELECT ResourceId FROM ResourceNameInts " 278 .
"WHERE ControlledNameId = ".$this->
Id);
280 return $this->DB->FetchColumn(
"ResourceId");
290 # Get a list of resources associated with the new name 291 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts " 292 .
"WHERE ControlledNameId = ".intval($NewNameId));
293 $NewNameResources = array();
294 while ($Row = $this->DB->FetchRow())
296 $NewNameResources[$Row[
"ResourceId"]]=1;
299 # Get a list of resources associated with the old name 300 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts " 301 .
"WHERE ControlledNameId = ".intval($this->
Id));
302 $OldNameResources = array();
303 while ($Row = $this->DB->FetchRow())
305 $OldNameResources[]= $Row[
"ResourceId"];
308 # Foreach of the old name resources, check to see if it's already 309 # associated with the new name. If not, associate it. 310 foreach ($OldNameResources as $ResourceId)
312 if (!isset($NewNameResources[$ResourceId]))
314 $this->DB->Query(
"INSERT INTO ResourceNameInts " 315 .
"(ResourceId, ControlledNameId) VALUES " 316 .
"(".intval($ResourceId).
",".intval($NewNameId).
")");
320 # Clear out all the associations to the old name 321 $this->DB->Query(
"DELETE FROM ResourceNameInts WHERE ControlledNameId = " 330 $this->DB->Query(
"UPDATE ControlledNames SET LastAssigned=NOW() " 331 .
"WHERE ControlledNameId=".intval($this->
Id));
340 public function Delete($DeleteIfHasResources = FALSE)
344 if ($DeleteIfHasResources || !$this->
InUse())
346 # delete this controlled name 347 $DB->Query(
"DELETE FROM ControlledNames WHERE ControlledNameId=".
350 # delete associated variant name 351 $DB->Query(
"DELETE FROM VariantNames WHERE ControlledNameId=".
354 if ($DeleteIfHasResources)
356 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ".
357 "ControlledNameId=".$this->
Id);
362 # ---- PRIVATE INTERFACE ------------------------------------------------- 367 private $ErrorStatus;
376 private function UpdateValue($FieldName, $NewValue)
378 return $this->DB->UpdateValue(
"ControlledNames", $FieldName,
379 $NewValue,
"ControlledNameId = ".$this->
Id,
380 $this->DBFields, TRUE);
390 private function UpdateVariantValue($FieldName, $NewValue)
392 if (!empty($NewValue))
394 # see if variant name exists for the controlled Name 395 $this->DB->Query(
"SELECT * from VariantNames WHERE ".
396 "ControlledNameId = ".$this->
Id);
398 # variant name exists so do an update 399 if ($this->DB->NumRowsSelected() > 0)
401 return $this->DB->UpdateValue(
"VariantNames",
402 $FieldName, $NewValue,
403 "ControlledNameId = ".$this->
Id,
404 $this->DBFields, TRUE);
406 # no variant name so do an insert 409 $this->DB->Query(
"INSERT into VariantNames ".
410 "(VariantName, ControlledNameId) VALUES ".
411 "('".addslashes($NewValue).
"', ".$this->
Id.
")");
414 # delete variant name 417 $this->DB->Query(
"Delete from VariantNames where ".
418 "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.
const STATUS_OK
Status code used for an okay, valid qualifier.
Status()
Check success of constructor.
UpdateLastAssigned()
Update the LastAssigned timestamp for this classification.