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 
83  public function VariantName($NewValue = DB_NOVALUE)
84  {
85  # unset any variant name attached if asked
86  if ($NewValue === FALSE)
87  {
88  $this->DB->Query("DELETE FROM VariantNames WHERE "
89  ."ControlledNameId = ".$this->Id);
90  $this->VNCache = NULL;
91  }
92 
93  else if ($NewValue != DB_NOVALUE)
94  {
95  $this->VNCache = $NewValue;
96 
97  # try to load variant name and update cache
98  $this->DB->Query("SELECT VariantName FROM VariantNames WHERE "
99  ."ControlledNameId = ".$this->Id);
100 
101  # variant name exists so do an update
102  if ($this->DB->NumRowsSelected() > 0)
103  {
104  $this->DB->Query("UPDATE VariantNames SET VariantName = '"
105  .addslashes($NewValue)."' WHERE ControlledNameId = "
106  .$this->Id);
107  }
108  # no variant name so do an insert
109  else
110  {
111  $this->DB->Query("INSERT INTO VariantNames ".
112  "(VariantName, ControlledNameId) VALUES ".
113  "('".addslashes($NewValue)."', ".$this->Id.")");
114  }
115  }
116 
117  # if variant name is not cached
118  else if (!isset($this->VNCache))
119  {
120  $this->VNCache = $this->DB->Query("SELECT VariantName FROM VariantNames".
121  " WHERE ControlledNameId = ".$this->Id, "VariantName");
122  }
123 
124  return $this->VNCache;
125  }
126 
132  public function FieldId($NewValue = DB_NOVALUE)
133  {
134  return $this->UpdateValue("FieldId", $NewValue);
135  }
136 
142  public function QualifierId($NewValue = DB_NOVALUE)
143  {
144  return $this->UpdateValue("QualifierId", $NewValue);
145  }
146 
152  public function Qualifier($NewValue = DB_NOVALUE)
153  {
154  # if new qualifier supplied
155  if ($NewValue !== DB_NOVALUE)
156  {
157  # set new qualifier ID
158  $this->QualifierId($NewValue->Id());
159 
160  # use new qualifier for return value
161  $Qualifier = $NewValue;
162  }
163  else
164  {
165  # if qualifier is available
166  if ($this->QualifierId() !== NULL
167  && Qualifier::ItemExists($this->QualifierId()))
168  {
169  # create qualifier object using stored ID
170  $Qualifier = new Qualifier($this->QualifierId());
171 
172  # if ID was zero and no name available for qualifieR
173  # (needed because some controlled name records in DB
174  # have 0 instead of NULL when no controlled name assigned)
175  # (NOTE: this is problematic if there is a qualifier with an
176  # ID value of 0!!!)
177  if ($this->QualifierId() == 0 && !strlen($Qualifier->Name()))
178  {
179  # return NULL to indicate no qualifier
180  $Qualifier = NULL;
181  }
182  }
183  else
184  {
185  # return NULL to indicate no qualifier
186  $Qualifier = NULL;
187  }
188  }
189 
190  # return qualifier to caller
191  return $Qualifier;
192  }
193 
200  static public function SearchForControlledName($ControlledName, $FieldId)
201  {
202  $Database = new Database();
203 
204  # query for the controlled name
205  $Database->Query("
206  SELECT ControlledNameId FROM
207  ControlledNames WHERE FieldId='".addslashes($FieldId)."'
208  AND ControlledName='".addslashes($ControlledName)."'");
209 
210  # return the controlled name IDs found, if any
211  return $Database->FetchColumn("ControlledNameId");
212  }
213 
218  public function InUse()
219  {
220  return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
221  "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
222  }
223 
228  public function GetAssociatedResources()
229  {
230  $this->DB->Query(
231  "SELECT ResourceId FROM ResourceNameInts "
232  ."WHERE ControlledNameId = ".$this->Id);
233 
234  return $this->DB->FetchColumn("ResourceId");
235  }
236 
242  public function RemapTo($NewNameId)
243  {
244  # Get a list of resources associated with the new name
245  $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
246  ."WHERE ControlledNameId = ".intval($NewNameId));
247  $NewNameResources = array();
248  while ($Row = $this->DB->FetchRow())
249  {
250  $NewNameResources[$Row["ResourceId"]]=1;
251  }
252 
253  # Get a list of resources associated with the old name
254  $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
255  ."WHERE ControlledNameId = ".intval($this->Id));
256  $OldNameResources = array();
257  while ($Row = $this->DB->FetchRow())
258  {
259  $OldNameResources[]= $Row["ResourceId"];
260  }
261 
262  # Foreach of the old name resources, check to see if it's already
263  # associated with the new name. If not, associate it.
264  foreach ($OldNameResources as $ResourceId)
265  {
266  if (!isset($NewNameResources[$ResourceId]))
267  {
268  $this->DB->Query("INSERT INTO ResourceNameInts "
269  ."(ResourceId, ControlledNameId) VALUES "
270  ."(".intval($ResourceId).",".intval($NewNameId).")");
271  }
272  }
273 
274  # Clear out all the associations to the old name
275  $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = "
276  .intval($this->Id));
277  }
278 
282  public function UpdateLastAssigned()
283  {
284  $this->DB->Query("UPDATE ControlledNames SET LastAssigned=NOW() "
285  ."WHERE ControlledNameId=".intval($this->Id));
286  }
287 
294  public function Delete($DeleteIfHasResources = FALSE)
295  {
296  $DB = &$this->DB;
297 
298  if ($DeleteIfHasResources || !$this->InUse())
299  {
300  # delete this controlled name
301  $this->Destroy();
302 
303  # delete associated variant name
304  $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
305  $this->Id);
306 
307  if ($DeleteIfHasResources)
308  {
309  $DB->Query("DELETE FROM ResourceNameInts WHERE ".
310  "ControlledNameId=".$this->Id);
311  }
312  }
313  }
314 
321  static protected function SetDatabaseAccessValues($ClassName)
322  {
323  parent::SetDatabaseAccessValues($ClassName);
324  self::$ItemNameColumnNames[$ClassName] = "ControlledName";
325  }
326 
327  private $VNCache;
328 }
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
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:285
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:1784
static SetDatabaseAccessValues($ClassName)
Set the database access values (table name, ID column name, name column name) for specified class...
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.