3 # FILE: ControlledName.php 5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2001-2013 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 16 # ---- PUBLIC INTERFACE -------------------------------------------------- 39 public function __construct($NameId, $Name = NULL, $FieldId = NULL,
40 $QualifierId =
"NULL", $VariantName = NULL)
42 # assume everything will turn out okay 43 $this->ErrorStatus = self::STATUS_OK;
45 # create DB handle for our use 49 # remove whitespace padding 51 $VariantName = trim($VariantName);
53 # look for passed in name and type 54 if (!empty($Name) && !empty($FieldId))
56 $DB->Query(
"SELECT * FROM ControlledNames".
57 " WHERE ControlledName = \"".addslashes($Name).
"\"".
58 " AND FieldId = ".intval($FieldId));
60 while ($this->DBFields = $DB->FetchRow())
62 # this controlled name already exists 63 if ($this->DBFields[
"ControlledName"] == $Name)
65 $this->ErrorStatus = self::STATUS_EXISTS;
66 $NameId = $this->DBFields[
"ControlledNameId"];
68 # cache the variant name separately 69 $VN = $DB->Query(
"SELECT VariantName FROM VariantNames".
70 " WHERE ControlledNameId = ".
71 $this->DBFields[
"ControlledNameId"],
"VariantName");
73 $this->DBFields[
"VariantName"] = $VN;
77 # controlled name not found, create it 78 if ($this->ErrorStatus == self::STATUS_OK)
80 # add new controlled name 81 $DB->Query(
"INSERT INTO ControlledNames ".
82 "(FieldId, ControlledName, QualifierId)".
83 " VALUES (".intval($FieldId).
", '".addslashes($Name)
84 .
"', ".intval($QualifierId).
")");
86 # get name ID for new controlled name 87 $NameId = $DB->LastInsertId();
90 if (!empty($VariantName))
92 $DB->Query(
"INSERT INTO VariantNames ".
93 "(ControlledNameId, VariantName) ".
94 "VALUES (".intval($NameId).
", '" 95 .addslashes($VariantName).
"') ");
99 # Name Id passed in, look it up 100 if (!empty($NameId) && $NameId != -1)
102 $DB->Query(
"SELECT * FROM ControlledNames".
103 " WHERE ControlledNameId = ".intval($NameId));
104 $this->DBFields = $DB->FetchRow();
106 # cache the variant name separately 107 $VN = $DB->Query(
"SELECT VariantName FROM VariantNames".
108 " WHERE ControlledNameId = ".intval($NameId),
"VariantName");
110 $this->DBFields[
"VariantName"] = $VN;
113 # save supplied or generated controlled name ID 114 $this->
Id = intval($NameId);
116 # set error status if controlled name info not loaded 117 if (!array_key_exists(
"ControlledNameId", $this->DBFields)
118 || ($this->DBFields[
"ControlledNameId"] != $this->
Id))
120 $this->ErrorStatus = self::STATUS_INVALID_ID;
130 return $this->ErrorStatus;
149 return $this->UpdateValue(
"ControlledName", $NewValue);
159 return $this->UpdateVariantValue(
"VariantName", $NewValue);
169 return $this->UpdateValue(
"FieldId", $NewValue);
179 return $this->UpdateValue(
"QualifierId", $NewValue);
199 # if new qualifier supplied 202 # set new qualifier ID 205 # use new qualifier for return value 206 $Qualifier = $NewValue;
210 # if qualifier is available 213 # create qualifier object using stored ID 216 # if ID was zero and no name available for qualifieR 217 # (needed because some controlled name records in DB 218 # have 0 instead of NULL when no controlled name assigned) 219 # (NOTE: this is problematic if there is a qualifier with an 221 if (($this->
QualifierId() == 0) && !strlen($Qualifier->Name()))
223 # return NULL to indicate no qualifier 229 # return NULL to indicate no qualifier 234 # return qualifier to caller 248 # query for the controlled name 250 SELECT ControlledNameId FROM 251 ControlledNames WHERE FieldId='".addslashes($FieldId).
"' 252 AND ControlledName='".addslashes($ControlledName).
"'");
254 # return the controlled name IDs found, if any 255 return $Database->FetchColumn(
"ControlledNameId");
264 return $this->DB->Query(
"SELECT COUNT(*) AS Count FROM ".
265 "ResourceNameInts WHERE ControlledNameId = ".$this->
Id,
"Count");
275 "SELECT ResourceId FROM ResourceNameInts " 276 .
"WHERE ControlledNameId = ".$this->
Id);
278 return $this->DB->FetchColumn(
"ResourceId");
288 # Get a list of resources associated with the new name 289 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts " 290 .
"WHERE ControlledNameId = ".intval($NewNameId));
291 $NewNameResources = array();
292 while ($Row = $this->DB->FetchRow())
294 $NewNameResources[$Row[
"ResourceId"]]=1;
297 # Get a list of resources associated with the old name 298 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts " 299 .
"WHERE ControlledNameId = ".intval($this->
Id));
300 $OldNameResources = array();
301 while ($Row = $this->DB->FetchRow())
303 $OldNameResources[]= $Row[
"ResourceId"];
306 # Foreach of the old name resources, check to see if it's already 307 # associated with the new name. If not, associate it. 308 foreach ($OldNameResources as $ResourceId)
310 if (!isset($NewNameResources[$ResourceId]))
312 $this->DB->Query(
"INSERT INTO ResourceNameInts " 313 .
"(ResourceId, ControlledNameId) VALUES " 314 .
"(".intval($ResourceId).
",".intval($NewNameId).
")");
318 # Clear out all the associations to the old name 319 $this->DB->Query(
"DELETE FROM ResourceNameInts WHERE ControlledNameId = " 328 $this->DB->Query(
"UPDATE ControlledNames SET LastAssigned=NOW() " 329 .
"WHERE ControlledNameId=".intval($this->
Id));
338 public function Delete($DeleteIfHasResources = FALSE)
342 if ($DeleteIfHasResources || !$this->
InUse())
344 # delete this controlled name 345 $DB->Query(
"DELETE FROM ControlledNames WHERE ControlledNameId=".
348 # delete associated variant name 349 $DB->Query(
"DELETE FROM VariantNames WHERE ControlledNameId=".
352 if ($DeleteIfHasResources)
354 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ".
355 "ControlledNameId=".$this->
Id);
360 # ---- PRIVATE INTERFACE ------------------------------------------------- 365 private $ErrorStatus;
374 private function UpdateValue($FieldName, $NewValue)
376 return $this->DB->UpdateValue(
"ControlledNames", $FieldName,
377 $NewValue,
"ControlledNameId = ".$this->
Id,
378 $this->DBFields, TRUE);
388 private function UpdateVariantValue($FieldName, $NewValue)
390 if (!empty($NewValue))
392 # see if variant name exists for the controlled Name 393 $this->DB->Query(
"SELECT * from VariantNames WHERE ".
394 "ControlledNameId = ".$this->
Id);
396 # variant name exists so do an update 397 if ($this->DB->NumRowsSelected() > 0)
399 return $this->DB->UpdateValue(
"VariantNames",
400 $FieldName, $NewValue,
401 "ControlledNameId = ".$this->
Id,
402 $this->DBFields, TRUE);
404 # no variant name so do an insert 407 $this->DB->Query(
"INSERT into VariantNames ".
408 "(VariantName, ControlledNameId) VALUES ".
409 "('".addslashes($NewValue).
"', ".$this->
Id.
")");
412 # delete variant name 415 $this->DB->Query(
"Delete from VariantNames where ".
416 "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.