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 
14 class ControlledName extends Item
15 {
16 
17  # ---- PUBLIC INTERFACE --------------------------------------------------
18 
29  public static function Create($Term, $FieldId)
30  {
31  $DB = new Database();
32 
33  # check if this controlledname is already present
34  $DB->Query("SELECT * FROM ControlledNames".
35  " WHERE ControlledName = '".addslashes($Term).
36  "' AND FieldId = ".intval($FieldId));
37  if ($Row = $DB->FetchRow())
38  {
39  $NameId = $Row["ControlledNameId"];
40  }
41  else
42  {
43  # add new controlled name
44  $DB->Query("INSERT INTO ControlledNames ".
45  "(FieldId, ControlledName) VALUES (".
46  intval($FieldId).", '".addslashes($Term)."')");
47 
48  $NameId = $DB->LastInsertId();
49  }
50  # instantiate new controlledname and return
51  $NewCN = new ControlledName(intval($NameId));
52  return $NewCN;
53  }
54 
64  public static function ControlledNameExists($Term, $FieldId)
65  {
66  $DB = new Database();
67 
68  $DB->Query("SELECT * FROM ControlledNames".
69  " WHERE ControlledName = '".addslashes($Term).
70  "' AND FieldId = ".intval($FieldId));
71  return $DB->NumRowsSelected();
72  }
73 
74 
80  public function Name($NewValue = DB_NOVALUE)
81  {
82  return $this->UpdateValue("ControlledName", $NewValue);
83  }
84 
94  public function VariantName($NewValue = DB_NOVALUE)
95  {
96  # unset any variant name attached if asked
97  if ($NewValue === FALSE)
98  {
99  $this->DB->Query("DELETE FROM VariantNames WHERE "
100  ."ControlledNameId = ".$this->Id);
101  $this->VNCache = NULL;
102  }
103 
104  else if ($NewValue != DB_NOVALUE)
105  {
106  $this->VNCache = $NewValue;
107 
108  # try to load variant name and update cache
109  $this->DB->Query("SELECT VariantName FROM VariantNames WHERE "
110  ."ControlledNameId = ".$this->Id);
111 
112  # variant name exists so do an update
113  if ($this->DB->NumRowsSelected() > 0)
114  {
115  $this->DB->Query("UPDATE VariantNames SET VariantName = '"
116  .addslashes($NewValue)."' WHERE ControlledNameId = "
117  .$this->Id);
118  }
119  # no variant name so do an insert
120  else
121  {
122  $this->DB->Query("INSERT INTO VariantNames ".
123  "(VariantName, ControlledNameId) VALUES ".
124  "('".addslashes($NewValue)."', ".$this->Id.")");
125  }
126  }
127 
128  # if variant name is not cached
129  else if (!isset($this->VNCache))
130  {
131  $this->VNCache = $this->DB->Query("SELECT VariantName FROM VariantNames".
132  " WHERE ControlledNameId = ".$this->Id, "VariantName");
133  }
134 
135  return $this->VNCache;
136  }
137 
143  public function FieldId($NewValue = DB_NOVALUE)
144  {
145  return $this->UpdateValue("FieldId", $NewValue);
146  }
147 
153  public function QualifierId($NewValue = DB_NOVALUE)
154  {
155  return $this->UpdateValue("QualifierId", $NewValue);
156  }
157 
163  public function Qualifier($NewValue = DB_NOVALUE)
164  {
165  # if new qualifier supplied
166  if ($NewValue !== DB_NOVALUE)
167  {
168  # set new qualifier ID
169  $this->QualifierId($NewValue->Id());
170 
171  # use new qualifier for return value
172  $Qualifier = $NewValue;
173  }
174  else
175  {
176  # if qualifier is available
177  if ($this->QualifierId() !== NULL
178  && Qualifier::ItemExists($this->QualifierId()))
179  {
180  # create qualifier object using stored ID
181  $Qualifier = new Qualifier($this->QualifierId());
182 
183  # if ID was zero and no name available for qualifieR
184  # (needed because some controlled name records in DB
185  # have 0 instead of NULL when no controlled name assigned)
186  # (NOTE: this is problematic if there is a qualifier with an
187  # ID value of 0!!!)
188  if ($this->QualifierId() == 0 && !strlen($Qualifier->Name()))
189  {
190  # return NULL to indicate no qualifier
191  $Qualifier = NULL;
192  }
193  }
194  else
195  {
196  # return NULL to indicate no qualifier
197  $Qualifier = NULL;
198  }
199  }
200 
201  # return qualifier to caller
202  return $Qualifier;
203  }
204 
211  static public function SearchForControlledName($ControlledName, $FieldId)
212  {
213  $Database = new Database();
214 
215  # query for the controlled name
216  $Database->Query("
217  SELECT ControlledNameId FROM
218  ControlledNames WHERE FieldId='".addslashes($FieldId)."'
219  AND ControlledName='".addslashes($ControlledName)."'");
220 
221  # return the controlled name IDs found, if any
222  return $Database->FetchColumn("ControlledNameId");
223  }
224 
229  public function InUse()
230  {
231  return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
232  "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
233  }
234 
239  public function GetAssociatedResources()
240  {
241  $this->DB->Query(
242  "SELECT ResourceId FROM ResourceNameInts "
243  ."WHERE ControlledNameId = ".$this->Id);
244 
245  return $this->DB->FetchColumn("ResourceId");
246  }
247 
253  public function RemapTo($NewNameId)
254  {
255  # Get a list of resources associated with the new name
256  $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
257  ."WHERE ControlledNameId = ".intval($NewNameId));
258  $NewNameResources = array();
259  while ($Row = $this->DB->FetchRow())
260  {
261  $NewNameResources[$Row["ResourceId"]]=1;
262  }
263 
264  # Get a list of resources associated with the old name
265  $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
266  ."WHERE ControlledNameId = ".intval($this->Id));
267  $OldNameResources = array();
268  while ($Row = $this->DB->FetchRow())
269  {
270  $OldNameResources[]= $Row["ResourceId"];
271  }
272 
273  # Foreach of the old name resources, check to see if it's already
274  # associated with the new name. If not, associate it.
275  foreach ($OldNameResources as $ResourceId)
276  {
277  if (!isset($NewNameResources[$ResourceId]))
278  {
279  $this->DB->Query("INSERT INTO ResourceNameInts "
280  ."(ResourceId, ControlledNameId) VALUES "
281  ."(".intval($ResourceId).",".intval($NewNameId).")");
282  }
283  }
284 
285  # Clear out all the associations to the old name
286  $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = "
287  .intval($this->Id));
288  }
289 
293  public function UpdateLastAssigned()
294  {
295  $this->DB->Query("UPDATE ControlledNames SET LastAssigned=NOW() "
296  ."WHERE ControlledNameId=".intval($this->Id));
297  }
298 
305  public function Delete($DeleteIfHasResources = FALSE)
306  {
307  $DB = &$this->DB;
308 
309  if ($DeleteIfHasResources || !$this->InUse())
310  {
311  # delete this controlled name
312  $this->Destroy();
313 
314  # delete associated variant name
315  $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
316  $this->Id);
317 
318  if ($DeleteIfHasResources)
319  {
320  $DB->Query("DELETE FROM ResourceNameInts WHERE ".
321  "ControlledNameId=".$this->Id);
322  }
323  }
324  }
325 
326  private $VNCache;
327 }
static ControlledNameExists($Term, $FieldId)
Check if there exists a controlledname with a ControlledName and FieldId same as given.
RemapTo($NewNameId)
Change all currently associated Resources to be instead associated with another ControlledName.
Id()
Get item ID.
Definition: Item.php:81
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.
static Create($Term, $FieldId)
Create a new empty ControlledName if it&#39;s not already present.
UpdateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database::UpdateValue().
Definition: Item.php:284
SQL database abstraction object with smart query caching.
Definition: Database.php:22
$DB
Definition: Item.php:210
VariantName($NewValue=DB_NOVALUE)
Get, set, or clear any variant terms for this controlled name .
Destroy()
Destroy item.
Definition: Item.php:60
Metadata type representing non-hierarchical controlled vocabulary values.
Qualifier($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with this term via object.
Common base class for persistent items store in database.
Definition: Item.php:13
const DB_NOVALUE
Definition: Database.php:1738
FieldId($NewValue=DB_NOVALUE)
Get or set the MetadataField associated with this term.
QualifierId($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with this term via ID.
static ItemExists($Id)
Check whether an item exists with the specified ID.
Definition: Item.php:162
InUse()
See if ControlledName is currently associated with any Resources.
UpdateLastAssigned()
Update the LastAssigned timestamp for this classification.