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  global $DB;
230  $DB->Query("SELECT ControlledNameId FROM ".
231  "ControlledNames WHERE FieldId=".intval($FieldId).
232  " AND ControlledName='".addslashes($ControlledName)."'");
233 
234  $rc = array();
235  while ($Row = $DB->FetchRow())
236  {
237  $rc []= $Row["ControlledNameId"];
238  }
239  return $rc;
240 
241  }
246  function InUse()
247  {
248  return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
249  "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
250  }
251 
257  function RemapTo($NewNameId)
258  {
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())
264  {
265  $NewNameResources[$Row["ResourceId"]]=1;
266  }
267 
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())
273  {
274  $OldNameResources []= $Row["ResourceId"];
275  }
276 
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)
280  {
281  if (!isset($NewNameResources[$ResourceId]))
282  {
283  $this->DB->Query("INSERT INTO ResourceNameInts "
284  ."(ResourceId, ControlledNameId) VALUES "
285  ."(".intval($ResourceId).",".intval($NewNameId).")");
286  }
287  }
288 
289  # Clear out all the associations to the old name
290  $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = ".intval($this->Id));
291  }
292 
299  function Delete($DeleteIfHasResources = FALSE)
300  {
301  $DB =& $this->DB;
302 
303  if ($DeleteIfHasResources || !$this->InUse())
304  {
305  # delete this controlled name
306  $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
307  $this->Id);
308 
309  # delete associated variant name
310  $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
311  $this->Id);
312 
313  if ($DeleteIfHasResources)
314  {
315  $DB->Query("DELETE FROM ResourceNameInts WHERE ".
316  "ControlledNameId=".$this->Id);
317  }
318  }
319  }
320 
321  # ---- PRIVATE INTERFACE -------------------------------------------------
322 
323  private $DB;
324  private $DBFields;
325  private $Id;
326  private $ErrorStatus;
327 
328  # convenience function to supply parameters to Database->UpdateValue()
329  private function UpdateValue($FieldName, $NewValue)
330  {
331  return $this->DB->UpdateValue("ControlledNames", $FieldName,
332  $NewValue, "ControlledNameId = ".$this->Id,
333  $this->DBFields, TRUE);
334  }
335 
336  # convenience function for VariantNames table
337  private function UpdateVariantValue($FieldName, $NewValue)
338  {
339  if (!empty($NewValue))
340  {
341  # see if variant name exists for the controlled Name
342  $this->DB->Query("SELECT * from VariantNames WHERE ".
343  "ControlledNameId = ".$this->Id);
344 
345  # variant name exists so do an update
346  if ($this->DB->NumRowsSelected() > 0)
347  {
348  return $this->DB->UpdateValue("VariantNames",
349  $FieldName, $NewValue,
350  "ControlledNameId = ".$this->Id,
351  $this->DBFields, TRUE);
352  }
353  # no variant name so do an insert
354  else if ($NewValue != DB_NOVALUE)
355  {
356  $this->DB->Query("INSERT into VariantNames ".
357  "(VariantName, ControlledNameId) VALUES ".
358  "('".addslashes($NewValue)."', ".$this->Id.")");
359  }
360  }
361  # delete variant name
362  else
363  {
364  $this->DB->Query("Delete from VariantNames where ".
365  "ControlledNameId = ".$this->Id);
366  }
367  }
368 }
369 
370 ?>