CWIS Developer Documentation
ControlledName.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: ControlledName.php
5 #
6 # Part of the Collection Workflow Integration System
7 # Copyright 2001-2009 Edward Almasy and Internet Scout
8 # http://scout.wisc.edu
9 #
10 
16 
17  # ---- PUBLIC INTERFACE --------------------------------------------------
18  /*@{*/
21  const STATUS_OK = 0;
23  const STATUS_INVALID_ID = 1;
25  const STATUS_EXISTS = 2;
40  function ControlledName($NameId, $Name = NULL, $FieldId = NULL,
41  $QualifierId = "NULL", $VariantName = NULL)
42  {
43  # assume everything will turn out okay
44  $this->ErrorStatus = self::STATUS_OK;
45 
46  # create DB handle for our use
47  $this->DB = new Database();
48  $DB =& $this->DB;
49 
50  # remove whitespace padding
51  $Name = trim($Name);
52  $VariantName = trim($VariantName);
53 
54  # look for passed in name and type
55  if (!empty($Name) && !empty($FieldId))
56  {
57  $DB->Query("SELECT * FROM ControlledNames".
58  " WHERE ControlledName = \"".addslashes($Name)."\"".
59  " AND FieldId = ".intval($FieldId));
60 
61  while ($this->DBFields = $DB->FetchRow())
62  {
63  # this controlled name already exists
64  if ($this->DBFields["ControlledName"] == $Name)
65  {
66  $this->ErrorStatus = self::STATUS_EXISTS;
67  $NameId = $this->DBFields["ControlledNameId"];
68 
69  # cache the variant name separately
70  $VN = $DB->Query("SELECT VariantName FROM VariantNames".
71  " WHERE ControlledNameId = ".
72  $this->DBFields["ControlledNameId"], "VariantName");
73 
74  $this->DBFields["VariantName"] = $VN;
75  break;
76  }
77  }
78  # controlled name not found, create it
79  if ($this->ErrorStatus == self::STATUS_OK)
80  {
81  # add new controlled name
82  $DB->Query("INSERT INTO ControlledNames ".
83  "(FieldId, ControlledName, QualifierId)".
84  " VALUES (".intval($FieldId).", '".addslashes($Name)
85  ."', ".intval($QualifierId).")");
86 
87  # get name ID for new controlled name
88  $NameId = $DB->LastInsertId("ControlledNames");
89 
90  # check for Variant
91  if (!empty($VariantName))
92  {
93  $DB->Query("INSERT INTO VariantNames ".
94  "(ControlledNameId, VariantName) ".
95  "VALUES (".intval($NameId).", '"
96  .addslashes($VariantName)."') ");
97  }
98  }
99  }
100  # Name Id passed in, look it up
101  if (!empty($NameId) && $NameId != -1)
102  {
103  $DB->Query("SELECT * FROM ControlledNames".
104  " WHERE ControlledNameId = ".intval($NameId));
105  $this->DBFields = $DB->FetchRow();
106 
107  # cache the variant name separately
108  $VN = $DB->Query("SELECT VariantName FROM VariantNames".
109  " WHERE ControlledNameId = ".intval($NameId), "VariantName");
110 
111  $this->DBFields["VariantName"] = $VN;
112  }
113 
114  # save supplied or generated controlled name ID
115  $this->Id = intval($NameId);
116 
117  # set error status if controlled name info not loaded
118  if ($this->DBFields["ControlledNameId"] != $this->Id)
119  {
120  $this->ErrorStatus = self::STATUS_INVALID_ID;
121  }
122  }
123 
128  function Status() { return $this->ErrorStatus; }
129 
134  function Id() { return $this->Id; }
135 
141  function Name($NewValue = DB_NOVALUE)
142  { return $this->UpdateValue("ControlledName", $NewValue); }
143 
149  function VariantName($NewValue = DB_NOVALUE)
150  { return $this->UpdateVariantValue("VariantName", $NewValue); }
151 
157  function FieldId($NewValue = DB_NOVALUE)
158  { return $this->UpdateValue("FieldId", $NewValue); }
159 
165  function QualifierId($NewValue = DB_NOVALUE)
166  { return $this->UpdateValue("QualifierId", $NewValue); }
167 
173  function Variant($NewValue = DB_NOVALUE)
174  { return $this->VariantName($NewValue); }
175 
181  function Qualifier($NewValue = DB_NOVALUE)
182  {
183  # if new qualifier supplied
184  if ($NewValue !== DB_NOVALUE)
185  {
186  # set new qualifier ID
187  $this->QualifierId($NewValue->Id());
188 
189  # use new qualifier for return value
190  $Qualifier = $NewValue;
191  }
192  else
193  {
194  # if qualifier is available
195  if ($this->QualifierId() !== NULL)
196  {
197  # create qualifier object using stored ID
198  $Qualifier = new Qualifier($this->QualifierId());
199 
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
204  # ID value of 0!!!)
205  if (($this->QualifierId() == 0) && !strlen($Qualifier->Name()))
206  {
207  # return NULL to indicate no qualifier
208  $Qualifier = NULL;
209  }
210  }
211  else
212  {
213  # return NULL to indicate no qualifier
214  $Qualifier = NULL;
215  }
216  }
217 
218  # return qualifier to caller
219  return $Qualifier;
220  }
221 
227  static function SearchForControlledName($ControlledName, $FieldId)
228  {
229  $Database = new Database();
230 
231  # query for the controlled name
232  $Database->Query("
233  SELECT ControlledNameId FROM
234  ControlledNames WHERE FieldId='".addslashes($FieldId)."'
235  AND ControlledName='".addslashes($ControlledName)."'");
236 
237  # return the controlled name IDs found, if any
238  return $Database->FetchColumn("ControlledNameId");
239  }
244  function InUse()
245  {
246  return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
247  "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
248  }
249 
255  function RemapTo($NewNameId)
256  {
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())
262  {
263  $NewNameResources[$Row["ResourceId"]]=1;
264  }
265 
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())
271  {
272  $OldNameResources []= $Row["ResourceId"];
273  }
274 
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)
278  {
279  if (!isset($NewNameResources[$ResourceId]))
280  {
281  $this->DB->Query("INSERT INTO ResourceNameInts "
282  ."(ResourceId, ControlledNameId) VALUES "
283  ."(".intval($ResourceId).",".intval($NewNameId).")");
284  }
285  }
286 
287  # Clear out all the associations to the old name
288  $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = ".intval($this->Id));
289  }
290 
297  function Delete($DeleteIfHasResources = FALSE)
298  {
299  $DB =& $this->DB;
300 
301  if ($DeleteIfHasResources || !$this->InUse())
302  {
303  # delete this controlled name
304  $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
305  $this->Id);
306 
307  # delete associated variant name
308  $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
309  $this->Id);
310 
311  if ($DeleteIfHasResources)
312  {
313  $DB->Query("DELETE FROM ResourceNameInts WHERE ".
314  "ControlledNameId=".$this->Id);
315  }
316  }
317  }
318 
319  # ---- PRIVATE INTERFACE -------------------------------------------------
320 
321  private $DB;
322  private $DBFields;
323  private $Id;
324  private $ErrorStatus;
325 
326  # convenience function to supply parameters to Database->UpdateValue()
327  private function UpdateValue($FieldName, $NewValue)
328  {
329  return $this->DB->UpdateValue("ControlledNames", $FieldName,
330  $NewValue, "ControlledNameId = ".$this->Id,
331  $this->DBFields, TRUE);
332  }
333 
334  # convenience function for VariantNames table
335  private function UpdateVariantValue($FieldName, $NewValue)
336  {
337  if (!empty($NewValue))
338  {
339  # see if variant name exists for the controlled Name
340  $this->DB->Query("SELECT * from VariantNames WHERE ".
341  "ControlledNameId = ".$this->Id);
342 
343  # variant name exists so do an update
344  if ($this->DB->NumRowsSelected() > 0)
345  {
346  return $this->DB->UpdateValue("VariantNames",
347  $FieldName, $NewValue,
348  "ControlledNameId = ".$this->Id,
349  $this->DBFields, TRUE);
350  }
351  # no variant name so do an insert
352  else if ($NewValue != DB_NOVALUE)
353  {
354  $this->DB->Query("INSERT into VariantNames ".
355  "(VariantName, ControlledNameId) VALUES ".
356  "('".addslashes($NewValue)."', ".$this->Id.")");
357  }
358  }
359  # delete variant name
360  else
361  {
362  $this->DB->Query("Delete from VariantNames where ".
363  "ControlledNameId = ".$this->Id);
364  }
365  }
366 }
367 
368 ?>