CWIS Developer Documentation
ControlledName.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: ControlledName.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2001-2016 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
15 {
16 
17  # ---- PUBLIC INTERFACE --------------------------------------------------
18  /*@{*/
21  const STATUS_OK = 0;
23  const STATUS_INVALID_ID = 1;
25  const STATUS_EXISTS = 2;
40  public function __construct($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();
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 (!array_key_exists("ControlledNameId", $this->DBFields)
119  || ($this->DBFields["ControlledNameId"] != $this->Id))
120  {
121  $this->ErrorStatus = self::STATUS_INVALID_ID;
122  }
123  }
124 
129  public function Status()
130  {
131  return $this->ErrorStatus;
132  }
133 
138  public function Id()
139  {
140  return $this->Id;
141  }
142 
148  public function Name($NewValue = DB_NOVALUE)
149  {
150  return $this->UpdateValue("ControlledName", $NewValue);
151  }
152 
158  public function VariantName($NewValue = DB_NOVALUE)
159  {
160  return $this->UpdateVariantValue("VariantName", $NewValue);
161  }
162 
168  public function FieldId($NewValue = DB_NOVALUE)
169  {
170  return $this->UpdateValue("FieldId", $NewValue);
171  }
172 
178  public function QualifierId($NewValue = DB_NOVALUE)
179  {
180  return $this->UpdateValue("QualifierId", $NewValue);
181  }
182 
188  public function Variant($NewValue = DB_NOVALUE)
189  {
190  return $this->VariantName($NewValue);
191  }
192 
198  public function Qualifier($NewValue = DB_NOVALUE)
199  {
200  # if new qualifier supplied
201  if ($NewValue !== DB_NOVALUE)
202  {
203  # set new qualifier ID
204  $this->QualifierId($NewValue->Id());
205 
206  # use new qualifier for return value
207  $Qualifier = $NewValue;
208  }
209  else
210  {
211  # if qualifier is available
212  if ($this->QualifierId() !== NULL)
213  {
214  # create qualifier object using stored ID
215  $Qualifier = new Qualifier($this->QualifierId());
216 
217  # if ID was zero and no name available for qualifieR
218  # (needed because some controlled name records in DB
219  # have 0 instead of NULL when no controlled name assigned)
220  # (NOTE: this is problematic if there is a qualifier with an
221  # ID value of 0!!!)
222  if (($this->QualifierId() == 0) && !strlen($Qualifier->Name()))
223  {
224  # return NULL to indicate no qualifier
225  $Qualifier = NULL;
226  }
227  }
228  else
229  {
230  # return NULL to indicate no qualifier
231  $Qualifier = NULL;
232  }
233  }
234 
235  # return qualifier to caller
236  return $Qualifier;
237  }
238 
245  static public function SearchForControlledName($ControlledName, $FieldId)
246  {
247  $Database = new Database();
248 
249  # query for the controlled name
250  $Database->Query("
251  SELECT ControlledNameId FROM
252  ControlledNames WHERE FieldId='".addslashes($FieldId)."'
253  AND ControlledName='".addslashes($ControlledName)."'");
254 
255  # return the controlled name IDs found, if any
256  return $Database->FetchColumn("ControlledNameId");
257  }
258 
263  public function InUse()
264  {
265  return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
266  "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
267  }
268 
273  public function GetAssociatedResources()
274  {
275  $this->DB->Query(
276  "SELECT ResourceId FROM ResourceNameInts "
277  ."WHERE ControlledNameId = ".$this->Id);
278 
279  return $this->DB->FetchColumn("ResourceId");
280  }
281 
287  public function RemapTo($NewNameId)
288  {
289  # Get a list of resources associated with the new name
290  $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
291  ."WHERE ControlledNameId = ".intval($NewNameId));
292  $NewNameResources = array();
293  while ($Row = $this->DB->FetchRow())
294  {
295  $NewNameResources[$Row["ResourceId"]]=1;
296  }
297 
298  # Get a list of resources associated with the old name
299  $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
300  ."WHERE ControlledNameId = ".intval($this->Id));
301  $OldNameResources = array();
302  while ($Row = $this->DB->FetchRow())
303  {
304  $OldNameResources[]= $Row["ResourceId"];
305  }
306 
307  # Foreach of the old name resources, check to see if it's already
308  # associated with the new name. If not, associate it.
309  foreach ($OldNameResources as $ResourceId)
310  {
311  if (!isset($NewNameResources[$ResourceId]))
312  {
313  $this->DB->Query("INSERT INTO ResourceNameInts "
314  ."(ResourceId, ControlledNameId) VALUES "
315  ."(".intval($ResourceId).",".intval($NewNameId).")");
316  }
317  }
318 
319  # Clear out all the associations to the old name
320  $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = "
321  .intval($this->Id));
322  }
323 
327  public function UpdateLastAssigned()
328  {
329  $this->DB->Query("UPDATE ControlledNames SET LastAssigned=NOW() "
330  ."WHERE ControlledNameId=".intval($this->Id));
331  }
332 
339  public function Delete($DeleteIfHasResources = FALSE)
340  {
341  $DB =& $this->DB;
342 
343  if ($DeleteIfHasResources || !$this->InUse())
344  {
345  # delete this controlled name
346  $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
347  $this->Id);
348 
349  # delete associated variant name
350  $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
351  $this->Id);
352 
353  if ($DeleteIfHasResources)
354  {
355  $DB->Query("DELETE FROM ResourceNameInts WHERE ".
356  "ControlledNameId=".$this->Id);
357  }
358  }
359  }
360 
361  # ---- PRIVATE INTERFACE -------------------------------------------------
362 
363  private $DB;
364  private $DBFields;
365  private $Id;
366  private $ErrorStatus;
367 
375  private function UpdateValue($FieldName, $NewValue)
376  {
377  return $this->DB->UpdateValue("ControlledNames", $FieldName,
378  $NewValue, "ControlledNameId = ".$this->Id,
379  $this->DBFields, TRUE);
380  }
381 
389  private function UpdateVariantValue($FieldName, $NewValue)
390  {
391  if (!empty($NewValue))
392  {
393  # see if variant name exists for the controlled Name
394  $this->DB->Query("SELECT * from VariantNames WHERE ".
395  "ControlledNameId = ".$this->Id);
396 
397  # variant name exists so do an update
398  if ($this->DB->NumRowsSelected() > 0)
399  {
400  return $this->DB->UpdateValue("VariantNames",
401  $FieldName, $NewValue,
402  "ControlledNameId = ".$this->Id,
403  $this->DBFields, TRUE);
404  }
405  # no variant name so do an insert
406  else if ($NewValue != DB_NOVALUE)
407  {
408  $this->DB->Query("INSERT into VariantNames ".
409  "(VariantName, ControlledNameId) VALUES ".
410  "('".addslashes($NewValue)."', ".$this->Id.")");
411  }
412  }
413  # delete variant name
414  else
415  {
416  $this->DB->Query("Delete from VariantNames where ".
417  "ControlledNameId = ".$this->Id);
418  }
419  }
420 }
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.
Definition: Database.php:22
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 DB_NOVALUE
Definition: Database.php:1541
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.