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-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
14 class ControlledName {
15
16 # ---- PUBLIC INTERFACE --------------------------------------------------
17 /*@{*/
20 const STATUS_OK = 0;
22 const STATUS_INVALID_ID = 1;
24 const STATUS_EXISTS = 2;
39 function ControlledName($NameId, $Name = NULL, $FieldId = NULL,
40 $QualifierId = "NULL", $VariantName = NULL)
41 {
42 # assume everything will turn out okay
43 $this->ErrorStatus = self::STATUS_OK;
44
45 # create DB handle for our use
46 $this->DB = new Database();
47 $DB =& $this->DB;
48
49 # remove whitespace padding
50 $Name = trim($Name);
51 $VariantName = trim($VariantName);
52
53 # look for passed in name and type
54 if (!empty($Name) && !empty($FieldId))
55 {
56 $DB->Query("SELECT * FROM ControlledNames".
57 " WHERE ControlledName = \"".addslashes($Name)."\"".
58 " AND FieldId = ".intval($FieldId));
59
60 while ($this->DBFields = $DB->FetchRow())
61 {
62 # this controlled name already exists
63 if ($this->DBFields["ControlledName"] == $Name)
64 {
65 $this->ErrorStatus = self::STATUS_EXISTS;
66 $NameId = $this->DBFields["ControlledNameId"];
67
68 # cache the variant name separately
69 $VN = $DB->Query("SELECT VariantName FROM VariantNames".
70 " WHERE ControlledNameId = ".
71 $this->DBFields["ControlledNameId"], "VariantName");
72
73 $this->DBFields["VariantName"] = $VN;
74 break;
75 }
76 }
77 # controlled name not found, create it
78 if ($this->ErrorStatus == self::STATUS_OK)
79 {
80 # add new controlled name
81 $DB->Query("INSERT INTO ControlledNames ".
82 "(FieldId, ControlledName, QualifierId)".
83 " VALUES (".intval($FieldId).", '".addslashes($Name)
84 ."', ".intval($QualifierId).")");
85
86 # get name ID for new controlled name
87 $NameId = $DB->LastInsertId();
88
89 # check for Variant
90 if (!empty($VariantName))
91 {
92 $DB->Query("INSERT INTO VariantNames ".
93 "(ControlledNameId, VariantName) ".
94 "VALUES (".intval($NameId).", '"
95 .addslashes($VariantName)."') ");
96 }
97 }
98 }
99 # Name Id passed in, look it up
100 if (!empty($NameId) && $NameId != -1)
101 {
102 $DB->Query("SELECT * FROM ControlledNames".
103 " WHERE ControlledNameId = ".intval($NameId));
104 $this->DBFields = $DB->FetchRow();
105
106 # cache the variant name separately
107 $VN = $DB->Query("SELECT VariantName FROM VariantNames".
108 " WHERE ControlledNameId = ".intval($NameId), "VariantName");
109
110 $this->DBFields["VariantName"] = $VN;
111 }
112
113 # save supplied or generated controlled name ID
114 $this->Id = intval($NameId);
115
116 # set error status if controlled name info not loaded
117 if (!array_key_exists("ControlledNameId", $this->DBFields)
118 || ($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
228 static function SearchForControlledName($ControlledName, $FieldId)
229 {
230 $Database = new Database();
231
232 # query for the controlled name
233 $Database->Query("
234 SELECT ControlledNameId FROM
235 ControlledNames WHERE FieldId='".addslashes($FieldId)."'
236 AND ControlledName='".addslashes($ControlledName)."'");
237
238 # return the controlled name IDs found, if any
239 return $Database->FetchColumn("ControlledNameId");
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
256 function GetAssociatedResources()
257 {
258 $this->DB->Query(
259 "SELECT ResourceId FROM ResourceNameInts "
260 ."WHERE ControlledNameId = ".$this->Id);
261
262 return $this->DB->FetchColumn("ResourceId");
263 }
264
270 function RemapTo($NewNameId)
271 {
272 # Get a list of resources associated with the new name
273 $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
274 ."WHERE ControlledNameId = ".intval($NewNameId));
275 $NewNameResources = array();
276 while ($Row = $this->DB->FetchRow())
277 {
278 $NewNameResources[$Row["ResourceId"]]=1;
279 }
280
281 # Get a list of resources associated with the old name
282 $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
283 ."WHERE ControlledNameId = ".intval($this->Id));
284 $OldNameResources = array();
285 while ($Row = $this->DB->FetchRow())
286 {
287 $OldNameResources []= $Row["ResourceId"];
288 }
289
290 # Foreach of the old name resources, check to see if it's already
291 # associated with the new name. If not, associate it.
292 foreach ($OldNameResources as $ResourceId)
293 {
294 if (!isset($NewNameResources[$ResourceId]))
295 {
296 $this->DB->Query("INSERT INTO ResourceNameInts "
297 ."(ResourceId, ControlledNameId) VALUES "
298 ."(".intval($ResourceId).",".intval($NewNameId).")");
299 }
300 }
301
302 # Clear out all the associations to the old name
303 $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = "
304 .intval($this->Id));
305 }
306
310 function UpdateLastAssigned()
311 {
312 $this->DB->Query("UPDATE ControlledNames SET LastAssigned=NOW() "
313 ."WHERE ControlledNameId=".intval($this->Id));
314 }
315
322 function Delete($DeleteIfHasResources = FALSE)
323 {
324 $DB =& $this->DB;
325
326 if ($DeleteIfHasResources || !$this->InUse())
327 {
328 # delete this controlled name
329 $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
330 $this->Id);
331
332 # delete associated variant name
333 $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
334 $this->Id);
335
336 if ($DeleteIfHasResources)
337 {
338 $DB->Query("DELETE FROM ResourceNameInts WHERE ".
339 "ControlledNameId=".$this->Id);
340 }
341 }
342 }
343
344 # ---- PRIVATE INTERFACE -------------------------------------------------
345
346 private $DB;
347 private $DBFields;
348 private $Id;
349 private $ErrorStatus;
350
358 private function UpdateValue($FieldName, $NewValue)
359 {
360 return $this->DB->UpdateValue("ControlledNames", $FieldName,
361 $NewValue, "ControlledNameId = ".$this->Id,
362 $this->DBFields, TRUE);
363 }
364
372 private function UpdateVariantValue($FieldName, $NewValue)
373 {
374 if (!empty($NewValue))
375 {
376 # see if variant name exists for the controlled Name
377 $this->DB->Query("SELECT * from VariantNames WHERE ".
378 "ControlledNameId = ".$this->Id);
379
380 # variant name exists so do an update
381 if ($this->DB->NumRowsSelected() > 0)
382 {
383 return $this->DB->UpdateValue("VariantNames",
384 $FieldName, $NewValue,
385 "ControlledNameId = ".$this->Id,
386 $this->DBFields, TRUE);
387 }
388 # no variant name so do an insert
389 else if ($NewValue != DB_NOVALUE)
390 {
391 $this->DB->Query("INSERT into VariantNames ".
392 "(VariantName, ControlledNameId) VALUES ".
393 "('".addslashes($NewValue)."', ".$this->Id.")");
394 }
395 }
396 # delete variant name
397 else
398 {
399 $this->DB->Query("Delete from VariantNames where ".
400 "ControlledNameId = ".$this->Id);
401 }
402 }
403 }
ControlledName\RemapTo
RemapTo($NewNameId)
Change all currently associated Resources to be instead associated with another ControlledName.
Definition: ControlledName.php:270
ControlledName\Name
Name($NewValue=DB_NOVALUE)
Get or set the controlled vocabulary term.
Definition: ControlledName.php:141
ControlledName\GetAssociatedResources
GetAssociatedResources()
Get resourceIds associated with this ControlledName.
Definition: ControlledName.php:256
ControlledName\ControlledName
ControlledName($NameId, $Name=NULL, $FieldId=NULL, $QualifierId="NULL", $VariantName=NULL)
Class constructor.
Definition: ControlledName.php:39
ControlledName\Delete
Delete($DeleteIfHasResources=FALSE)
Remove ControlledName (and any accompanying associations from database.
Definition: ControlledName.php:322
ControlledName\SearchForControlledName
static SearchForControlledName($ControlledName, $FieldId)
Check if the given controlled name already exists for a given field ID.
Definition: ControlledName.php:228
Database
SQL database abstraction object with smart query caching.
Definition: Axis--Database.php:22
DB_NOVALUE
const DB_NOVALUE
Definition: Axis--Database.php:1331
ControlledName\VariantName
VariantName($NewValue=DB_NOVALUE)
Get or set any variant terms for this controlled name .
Definition: ControlledName.php:149
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:181
PHP
PHP
Definition: RSSClient--Test.php:12
ControlledName\Id
Id()
Get ID.
Definition: ControlledName.php:134
ControlledName\STATUS_OK
const STATUS_OK
Successful execution.
Definition: ControlledName.php:20
ControlledName\FieldId
FieldId($NewValue=DB_NOVALUE)
Get or set the MetadataField associated with this term.
Definition: ControlledName.php:157
ControlledName\Variant
Variant($NewValue=DB_NOVALUE)
Get or set the controlled name variant.
Definition: ControlledName.php:173
ControlledName\STATUS_INVALID_ID
const STATUS_INVALID_ID
No ControlledName exists with specified ID.
Definition: ControlledName.php:22
ControlledName\QualifierId
QualifierId($NewValue=DB_NOVALUE)
Get or set the Qualifier associated with this term via ID.
Definition: ControlledName.php:165
ControlledName\STATUS_EXISTS
const STATUS_EXISTS
ControlledName already exists with this term.
Definition: ControlledName.php:24
ControlledName\InUse
InUse()
See if ControlledName is currently associated with any Resources.
Definition: ControlledName.php:246
ControlledName\Status
Status()
Check success of constructor.
Definition: ControlledName.php:128
ControlledName\UpdateLastAssigned
UpdateLastAssigned()
Update the LastAssigned timestamp for this classification.
Definition: ControlledName.php:310