4 # FILE: ControlledName.php
6 # Part of the Collection Workflow Integration System
7 # Copyright 2001-2009 Edward Almasy and Internet Scout
8 # http://scout.wisc.edu
17 # ---- PUBLIC INTERFACE --------------------------------------------------
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(
"ControlledNames");
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 ($this->DBFields[
"ControlledNameId"] != $this->
Id)
120 $this->ErrorStatus = self::STATUS_INVALID_ID;
128 function Status() {
return $this->ErrorStatus; }
134 function Id() {
return $this->Id; }
142 {
return $this->UpdateValue(
"ControlledName", $NewValue); }
150 {
return $this->UpdateVariantValue(
"VariantName", $NewValue); }
158 {
return $this->UpdateValue(
"FieldId", $NewValue); }
166 {
return $this->UpdateValue(
"QualifierId", $NewValue); }
183 # if new qualifier supplied
186 # set new qualifier ID
189 # use new qualifier for return value
190 $Qualifier = $NewValue;
194 # if qualifier is available
197 # create qualifier object using stored ID
200 # if ID was zero and no name available for qualifieR
201 # (needed because some controlled name records in DB
202 # have 0 instead of NULL when no controlled name assigned)
203 # (NOTE: this is problematic if there is a qualifier with an
205 if (($this->
QualifierId() == 0) && !strlen($Qualifier->Name()))
207 # return NULL to indicate no qualifier
213 # return NULL to indicate no qualifier
218 # return qualifier to caller
230 $DB->Query(
"SELECT ControlledNameId FROM ".
231 "ControlledNames WHERE FieldId=".intval($FieldId).
232 " AND ControlledName='".addslashes($ControlledName).
"'");
235 while ($Row = $DB->FetchRow())
237 $rc []= $Row[
"ControlledNameId"];
248 return $this->DB->Query(
"SELECT COUNT(*) AS Count FROM ".
249 "ResourceNameInts WHERE ControlledNameId = ".$this->
Id,
"Count");
259 # Get a list of resources associated with the new name
260 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts "
261 .
"WHERE ControlledNameId = ".intval($NewNameId));
262 $NewNameResources = array();
263 while ($Row = $this->DB->FetchRow())
265 $NewNameResources[$Row[
"ResourceId"]]=1;
268 # Get a list of resources associated with the old name
269 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts "
270 .
"WHERE ControlledNameId = ".intval($this->
Id));
271 $OldNameResources = array();
272 while ($Row = $this->DB->FetchRow())
274 $OldNameResources []= $Row[
"ResourceId"];
277 # Foreach of the old name resources, check to see if it's already
278 # associated with the new name. If not, associate it.
279 foreach ($OldNameResources as $ResourceId)
281 if (!isset($NewNameResources[$ResourceId]))
283 $this->DB->Query(
"INSERT INTO ResourceNameInts "
284 .
"(ResourceId, ControlledNameId) VALUES "
285 .
"(".intval($ResourceId).
",".intval($NewNameId).
")");
289 # Clear out all the associations to the old name
290 $this->DB->Query(
"DELETE FROM ResourceNameInts WHERE ControlledNameId = ".intval($this->
Id));
299 function Delete($DeleteIfHasResources = FALSE)
303 if ($DeleteIfHasResources || !$this->
InUse())
305 # delete this controlled name
306 $DB->Query(
"DELETE FROM ControlledNames WHERE ControlledNameId=".
309 # delete associated variant name
310 $DB->Query(
"DELETE FROM VariantNames WHERE ControlledNameId=".
313 if ($DeleteIfHasResources)
315 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ".
316 "ControlledNameId=".$this->
Id);
321 # ---- PRIVATE INTERFACE -------------------------------------------------
326 private $ErrorStatus;
328 # convenience function to supply parameters to Database->UpdateValue()
329 private function UpdateValue($FieldName, $NewValue)
331 return $this->DB->UpdateValue(
"ControlledNames", $FieldName,
332 $NewValue,
"ControlledNameId = ".$this->
Id,
333 $this->DBFields, TRUE);
336 # convenience function for VariantNames table
337 private function UpdateVariantValue($FieldName, $NewValue)
339 if (!empty($NewValue))
341 # see if variant name exists for the controlled Name
342 $this->DB->Query(
"SELECT * from VariantNames WHERE ".
343 "ControlledNameId = ".$this->
Id);
345 # variant name exists so do an update
346 if ($this->DB->NumRowsSelected() > 0)
348 return $this->DB->UpdateValue(
"VariantNames",
349 $FieldName, $NewValue,
350 "ControlledNameId = ".$this->
Id,
351 $this->DBFields, TRUE);
353 # no variant name so do an insert
356 $this->DB->Query(
"INSERT into VariantNames ".
357 "(VariantName, ControlledNameId) VALUES ".
358 "('".addslashes($NewValue).
"', ".$this->
Id.
")");
361 # delete variant name
364 $this->DB->Query(
"Delete from VariantNames where ".
365 "ControlledNameId = ".$this->
Id);