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
231 # query for the controlled name
233 SELECT ControlledNameId FROM
234 ControlledNames WHERE FieldId='".addslashes($FieldId).
"'
235 AND ControlledName='".addslashes($ControlledName).
"'");
237 # return the controlled name IDs found, if any
238 return $Database->FetchColumn(
"ControlledNameId");
246 return $this->DB->Query(
"SELECT COUNT(*) AS Count FROM ".
247 "ResourceNameInts WHERE ControlledNameId = ".$this->
Id,
"Count");
257 # Get a list of resources associated with the new name
258 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts "
259 .
"WHERE ControlledNameId = ".intval($NewNameId));
260 $NewNameResources = array();
261 while ($Row = $this->DB->FetchRow())
263 $NewNameResources[$Row[
"ResourceId"]]=1;
266 # Get a list of resources associated with the old name
267 $this->DB->Query(
"SELECT ResourceId FROM ResourceNameInts "
268 .
"WHERE ControlledNameId = ".intval($this->
Id));
269 $OldNameResources = array();
270 while ($Row = $this->DB->FetchRow())
272 $OldNameResources []= $Row[
"ResourceId"];
275 # Foreach of the old name resources, check to see if it's already
276 # associated with the new name. If not, associate it.
277 foreach ($OldNameResources as $ResourceId)
279 if (!isset($NewNameResources[$ResourceId]))
281 $this->DB->Query(
"INSERT INTO ResourceNameInts "
282 .
"(ResourceId, ControlledNameId) VALUES "
283 .
"(".intval($ResourceId).
",".intval($NewNameId).
")");
287 # Clear out all the associations to the old name
288 $this->DB->Query(
"DELETE FROM ResourceNameInts WHERE ControlledNameId = ".intval($this->
Id));
297 function Delete($DeleteIfHasResources = FALSE)
301 if ($DeleteIfHasResources || !$this->
InUse())
303 # delete this controlled name
304 $DB->Query(
"DELETE FROM ControlledNames WHERE ControlledNameId=".
307 # delete associated variant name
308 $DB->Query(
"DELETE FROM VariantNames WHERE ControlledNameId=".
311 if ($DeleteIfHasResources)
313 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ".
314 "ControlledNameId=".$this->
Id);
319 # ---- PRIVATE INTERFACE -------------------------------------------------
324 private $ErrorStatus;
326 # convenience function to supply parameters to Database->UpdateValue()
327 private function UpdateValue($FieldName, $NewValue)
329 return $this->DB->UpdateValue(
"ControlledNames", $FieldName,
330 $NewValue,
"ControlledNameId = ".$this->
Id,
331 $this->DBFields, TRUE);
334 # convenience function for VariantNames table
335 private function UpdateVariantValue($FieldName, $NewValue)
337 if (!empty($NewValue))
339 # see if variant name exists for the controlled Name
340 $this->DB->Query(
"SELECT * from VariantNames WHERE ".
341 "ControlledNameId = ".$this->
Id);
343 # variant name exists so do an update
344 if ($this->DB->NumRowsSelected() > 0)
346 return $this->DB->UpdateValue(
"VariantNames",
347 $FieldName, $NewValue,
348 "ControlledNameId = ".$this->
Id,
349 $this->DBFields, TRUE);
351 # no variant name so do an insert
354 $this->DB->Query(
"INSERT into VariantNames ".
355 "(VariantName, ControlledNameId) VALUES ".
356 "('".addslashes($NewValue).
"', ".$this->
Id.
")");
359 # delete variant name
362 $this->DB->Query(
"Delete from VariantNames where ".
363 "ControlledNameId = ".$this->
Id);