CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
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 ($Qualifier->Status() == Qualifier::STATUS_OK &&
223 ($this->QualifierId() == 0) && !strlen($Qualifier->Name()))
224 {
225 # return NULL to indicate no qualifier
226 $Qualifier = NULL;
227 }
228 }
229 else
230 {
231 # return NULL to indicate no qualifier
232 $Qualifier = NULL;
233 }
234 }
235
236 # return qualifier to caller
237 return $Qualifier;
238 }
239
246 static public function SearchForControlledName($ControlledName, $FieldId)
247 {
248 $Database = new Database();
249
250 # query for the controlled name
251 $Database->Query("
252 SELECT ControlledNameId FROM
253 ControlledNames WHERE FieldId='".addslashes($FieldId)."'
254 AND ControlledName='".addslashes($ControlledName)."'");
255
256 # return the controlled name IDs found, if any
257 return $Database->FetchColumn("ControlledNameId");
258 }
259
264 public function InUse()
265 {
266 return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
267 "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
268 }
269
274 public function GetAssociatedResources()
275 {
276 $this->DB->Query(
277 "SELECT ResourceId FROM ResourceNameInts "
278 ."WHERE ControlledNameId = ".$this->Id);
279
280 return $this->DB->FetchColumn("ResourceId");
281 }
282
288 public function RemapTo($NewNameId)
289 {
290 # Get a list of resources associated with the new name
291 $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
292 ."WHERE ControlledNameId = ".intval($NewNameId));
293 $NewNameResources = array();
294 while ($Row = $this->DB->FetchRow())
295 {
296 $NewNameResources[$Row["ResourceId"]]=1;
297 }
298
299 # Get a list of resources associated with the old name
300 $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
301 ."WHERE ControlledNameId = ".intval($this->Id));
302 $OldNameResources = array();
303 while ($Row = $this->DB->FetchRow())
304 {
305 $OldNameResources[]= $Row["ResourceId"];
306 }
307
308 # Foreach of the old name resources, check to see if it's already
309 # associated with the new name. If not, associate it.
310 foreach ($OldNameResources as $ResourceId)
311 {
312 if (!isset($NewNameResources[$ResourceId]))
313 {
314 $this->DB->Query("INSERT INTO ResourceNameInts "
315 ."(ResourceId, ControlledNameId) VALUES "
316 ."(".intval($ResourceId).",".intval($NewNameId).")");
317 }
318 }
319
320 # Clear out all the associations to the old name
321 $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = "
322 .intval($this->Id));
323 }
324
328 public function UpdateLastAssigned()
329 {
330 $this->DB->Query("UPDATE ControlledNames SET LastAssigned=NOW() "
331 ."WHERE ControlledNameId=".intval($this->Id));
332 }
333
340 public function Delete($DeleteIfHasResources = FALSE)
341 {
342 $DB =& $this->DB;
343
344 if ($DeleteIfHasResources || !$this->InUse())
345 {
346 # delete this controlled name
347 $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
348 $this->Id);
349
350 # delete associated variant name
351 $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
352 $this->Id);
353
354 if ($DeleteIfHasResources)
355 {
356 $DB->Query("DELETE FROM ResourceNameInts WHERE ".
357 "ControlledNameId=".$this->Id);
358 }
359 }
360 }
361
362 # ---- PRIVATE INTERFACE -------------------------------------------------
363
364 private $DB;
365 private $DBFields;
366 private $Id;
367 private $ErrorStatus;
368
376 private function UpdateValue($FieldName, $NewValue)
377 {
378 return $this->DB->UpdateValue("ControlledNames", $FieldName,
379 $NewValue, "ControlledNameId = ".$this->Id,
380 $this->DBFields, TRUE);
381 }
382
390 private function UpdateVariantValue($FieldName, $NewValue)
391 {
392 if (!empty($NewValue))
393 {
394 # see if variant name exists for the controlled Name
395 $this->DB->Query("SELECT * from VariantNames WHERE ".
396 "ControlledNameId = ".$this->Id);
397
398 # variant name exists so do an update
399 if ($this->DB->NumRowsSelected() > 0)
400 {
401 return $this->DB->UpdateValue("VariantNames",
402 $FieldName, $NewValue,
403 "ControlledNameId = ".$this->Id,
404 $this->DBFields, TRUE);
405 }
406 # no variant name so do an insert
407 else if ($NewValue != DB_NOVALUE)
408 {
409 $this->DB->Query("INSERT into VariantNames ".
410 "(VariantName, ControlledNameId) VALUES ".
411 "('".addslashes($NewValue)."', ".$this->Id.")");
412 }
413 }
414 # delete variant name
415 else
416 {
417 $this->DB->Query("Delete from VariantNames where ".
418 "ControlledNameId = ".$this->Id);
419 }
420 }
421 }
ControlledName\RemapTo
RemapTo($NewNameId)
Change all currently associated Resources to be instead associated with another ControlledName.
Definition: ControlledName.php:288
ControlledName\__construct
__construct($NameId, $Name=NULL, $FieldId=NULL, $QualifierId="NULL", $VariantName=NULL)
Class constructor.
Definition: ControlledName.php:40
ControlledName\Name
Name($NewValue=DB_NOVALUE)
Get or set the controlled vocabulary term.
Definition: ControlledName.php:148
ControlledName\GetAssociatedResources
GetAssociatedResources()
Get resourceIds associated with this ControlledName.
Definition: ControlledName.php:274
ControlledName\Delete
Delete($DeleteIfHasResources=FALSE)
Remove ControlledName (and any accompanying associations from database.
Definition: ControlledName.php:340
ControlledName\SearchForControlledName
static SearchForControlledName($ControlledName, $FieldId)
Check if the given controlled name already exists for a given field ID.
Definition: ControlledName.php:246
Database
SQL database abstraction object with smart query caching.
Definition: Database.php:22
ControlledName\VariantName
VariantName($NewValue=DB_NOVALUE)
Get or set any variant terms for this controlled name .
Definition: ControlledName.php:158
ControlledName
Metadata type representing non-hierarchical controlled vocabulary values.
Definition: ControlledName.php:14
ControlledName\Qualifier
Qualifier($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with this term via object.
Definition: ControlledName.php:198
PHP
PHP
Definition: RSSClient--Test.php:12
DB_NOVALUE
const DB_NOVALUE
Definition: Database.php:1706
ControlledName\Id
Id()
Get ID.
Definition: ControlledName.php:138
ControlledName\STATUS_OK
const STATUS_OK
Successful execution.
Definition: ControlledName.php:21
ControlledName\FieldId
FieldId($NewValue=DB_NOVALUE)
Get or set the MetadataField associated with this term.
Definition: ControlledName.php:168
ControlledName\Variant
Variant($NewValue=DB_NOVALUE)
Get or set the controlled name variant.
Definition: ControlledName.php:188
ControlledName\STATUS_INVALID_ID
const STATUS_INVALID_ID
No ControlledName exists with specified ID.
Definition: ControlledName.php:23
ControlledName\QualifierId
QualifierId($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with this term via ID.
Definition: ControlledName.php:178
ControlledName\STATUS_EXISTS
const STATUS_EXISTS
ControlledName already exists with this term.
Definition: ControlledName.php:25
ControlledName\InUse
InUse()
See if ControlledName is currently associated with any Resources.
Definition: ControlledName.php:264
Qualifier\STATUS_OK
const STATUS_OK
Status code used for an okay, valid qualifier.
Definition: Qualifier.php:18
ControlledName\Status
Status()
Check success of constructor.
Definition: ControlledName.php:129
ControlledName\UpdateLastAssigned
UpdateLastAssigned()
Update the LastAssigned timestamp for this classification.
Definition: ControlledName.php:328