ControlledName.php
Go to the documentation of this file.00001 <?PHP
00002
00003 #
00004 # FILE: ControlledName.php
00005 #
00006 # Part of the Collection Workflow Integration System
00007 # Copyright 2001-2009 Edward Almasy and Internet Scout
00008 # http://scout.wisc.edu
00009 #
00010
00015 class ControlledName {
00016
00017 # ---- PUBLIC INTERFACE --------------------------------------------------
00018
00021 const STATUS_OK = 0;
00023 const STATUS_INVALID_ID = 1;
00025 const STATUS_EXISTS = 2;
00040 function ControlledName($NameId, $Name = NULL, $FieldId = NULL,
00041 $QualifierId = "NULL", $VariantName = NULL)
00042 {
00043 # assume everything will turn out okay
00044 $this->ErrorStatus = self::STATUS_OK;
00045
00046 # create DB handle for our use
00047 $this->DB = new SPTDatabase();
00048 $DB =& $this->DB;
00049
00050 # remove whitespace padding
00051 $Name = trim($Name);
00052 $VariantName = trim($VariantName);
00053
00054 # look for passed in name and type
00055 if (!empty($Name) && !empty($FieldId))
00056 {
00057 $DB->Query("SELECT * FROM ControlledNames".
00058 " WHERE ControlledName = \"".addslashes($Name)."\"".
00059 " AND FieldId = ".intval($FieldId));
00060
00061 while ($this->DBFields = $DB->FetchRow())
00062 {
00063 # this controlled name already exists
00064 if ($this->DBFields["ControlledName"] == $Name)
00065 {
00066 $this->ErrorStatus = self::STATUS_EXISTS;
00067 $NameId = $this->DBFields["ControlledNameId"];
00068
00069 # cache the variant name separately
00070 $VN = $DB->Query("SELECT VariantName FROM VariantNames".
00071 " WHERE ControlledNameId = ".
00072 $this->DBFields["ControlledNameId"], "VariantName");
00073
00074 $this->DBFields["VariantName"] = $VN;
00075 break;
00076 }
00077 }
00078 # controlled name not found, create it
00079 if ($this->ErrorStatus == self::STATUS_OK)
00080 {
00081 # add new controlled name
00082 $DB->Query("INSERT INTO ControlledNames ".
00083 "(FieldId, ControlledName, QualifierId)".
00084 " VALUES (".intval($FieldId).", '".addslashes($Name)
00085 ."', ".intval($QualifierId).")");
00086
00087 # get name ID for new controlled name
00088 $NameId = $DB->LastInsertId("ControlledNames");
00089
00090 # check for Variant
00091 if (!empty($VariantName))
00092 {
00093 $DB->Query("INSERT INTO VariantNames ".
00094 "(ControlledNameId, VariantName) ".
00095 "VALUES (".intval($NameId).", '"
00096 .addslashes($VariantName)."') ");
00097 }
00098 }
00099 }
00100 # Name Id passed in, look it up
00101 if (!empty($NameId) && $NameId != -1)
00102 {
00103 $DB->Query("SELECT * FROM ControlledNames".
00104 " WHERE ControlledNameId = ".intval($NameId));
00105 $this->DBFields = $DB->FetchRow();
00106
00107 # cache the variant name separately
00108 $VN = $DB->Query("SELECT VariantName FROM VariantNames".
00109 " WHERE ControlledNameId = ".intval($NameId), "VariantName");
00110
00111 $this->DBFields["VariantName"] = $VN;
00112 }
00113
00114 # save supplied or generated controlled name ID
00115 $this->Id = intval($NameId);
00116
00117 # set error status if controlled name info not loaded
00118 if ($this->DBFields["ControlledNameId"] != $this->Id)
00119 {
00120 $this->ErrorStatus = self::STATUS_INVALID_ID;
00121 }
00122 }
00123
00128 function Status() { return $this->ErrorStatus; }
00129
00134 function Id() { return $this->Id; }
00135
00141 function Name($NewValue = DB_NOVALUE)
00142 { return $this->UpdateValue("ControlledName", $NewValue); }
00143
00149 function VariantName($NewValue = DB_NOVALUE)
00150 { return $this->UpdateVariantValue("VariantName", $NewValue); }
00151
00157 function FieldId($NewValue = DB_NOVALUE)
00158 { return $this->UpdateValue("FieldId", $NewValue); }
00159
00165 function QualifierId($NewValue = DB_NOVALUE)
00166 { return $this->UpdateValue("QualifierId", $NewValue); }
00167
00173 function Variant($NewValue = DB_NOVALUE)
00174 { return $this->VariantName($NewValue); }
00175
00181 function Qualifier($NewValue = DB_NOVALUE)
00182 {
00183 # if new qualifier supplied
00184 if ($NewValue !== DB_NOVALUE)
00185 {
00186 # set new qualifier ID
00187 $this->QualifierId($NewValue->Id());
00188
00189 # use new qualifier for return value
00190 $Qualifier = $NewValue;
00191 }
00192 else
00193 {
00194 # if qualifier is available
00195 if ($this->QualifierId() !== NULL)
00196 {
00197 # create qualifier object using stored ID
00198 $Qualifier = new Qualifier($this->QualifierId());
00199
00200 # if ID was zero and no name available for qualifieR
00201 # (needed because some controlled name records in DB
00202 # have 0 instead of NULL when no controlled name assigned)
00203 # (NOTE: this is problematic if there is a qualifier with an
00204 # ID value of 0!!!)
00205 if (($this->QualifierId() == 0) && !strlen($Qualifier->Name()))
00206 {
00207 # return NULL to indicate no qualifier
00208 $Qualifier = NULL;
00209 }
00210 }
00211 else
00212 {
00213 # return NULL to indicate no qualifier
00214 $Qualifier = NULL;
00215 }
00216 }
00217
00218 # return qualifier to caller
00219 return $Qualifier;
00220 }
00221
00227 static function SearchForControlledName($ControlledName, $FieldId)
00228 {
00229 global $DB;
00230 $DB->Query("SELECT ControlledNameId FROM ".
00231 "ControlledNames WHERE FieldId=".intval($FieldId).
00232 " AND ControlledName='".addslashes($ControlledName)."'");
00233
00234 $rc = array();
00235 while ($Row = $DB->FetchRow())
00236 {
00237 $rc []= $Row["ControlledNameId"];
00238 }
00239 return $rc;
00240
00241 }
00246 function InUse()
00247 {
00248 return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
00249 "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
00250 }
00251
00257 function RemapTo($NewNameId)
00258 {
00259 # Get a list of resources associated with the new name
00260 $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
00261 ."WHERE ControlledNameId = ".intval($NewNameId));
00262 $NewNameResources = array();
00263 while ($Row = $this->DB->FetchRow())
00264 {
00265 $NewNameResources[$Row["ResourceId"]]=1;
00266 }
00267
00268 # Get a list of resources associated with the old name
00269 $this->DB->Query("SELECT ResourceId FROM ResourceNameInts "
00270 ."WHERE ControlledNameId = ".intval($this->Id));
00271 $OldNameResources = array();
00272 while ($Row = $this->DB->FetchRow())
00273 {
00274 $OldNameResources []= $Row["ResourceId"];
00275 }
00276
00277 # Foreach of the old name resources, check to see if it's already
00278 # associated with the new name. If not, associate it.
00279 foreach ($OldNameResources as $ResourceId)
00280 {
00281 if (!isset($NewNameResources[$ResourceId]))
00282 {
00283 $this->DB->Query("INSERT INTO ResourceNameInts "
00284 ."(ResourceId, ControlledNameId) VALUES "
00285 ."(".intval($ResourceId).",".intval($NewNameId).")");
00286 }
00287 }
00288
00289 # Clear out all the associations to the old name
00290 $this->DB->Query("DELETE FROM ResourceNameInts WHERE ControlledNameId = ".intval($this->Id));
00291 }
00292
00299 function Delete($DeleteIfHasResources = FALSE)
00300 {
00301 $DB =& $this->DB;
00302
00303 if ($DeleteIfHasResources || !$this->InUse())
00304 {
00305 # delete this controlled name
00306 $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
00307 $this->Id);
00308
00309 # delete associated variant name
00310 $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
00311 $this->Id);
00312
00313 if ($DeleteIfHasResources)
00314 {
00315 $DB->Query("DELETE FROM ResourceNameInts WHERE ".
00316 "ControlledNameId=".$this->Id);
00317 }
00318 }
00319 }
00320
00321 # ---- PRIVATE INTERFACE -------------------------------------------------
00322
00323 private $DB;
00324 private $DBFields;
00325 private $Id;
00326 private $ErrorStatus;
00327
00328 # convenience function to supply parameters to Database->UpdateValue()
00329 private function UpdateValue($FieldName, $NewValue)
00330 {
00331 return $this->DB->UpdateValue("ControlledNames", $FieldName,
00332 $NewValue, "ControlledNameId = ".$this->Id,
00333 $this->DBFields, TRUE);
00334 }
00335
00336 # convenience function for VariantNames table
00337 private function UpdateVariantValue($FieldName, $NewValue)
00338 {
00339 if (!empty($NewValue))
00340 {
00341 # see if variant name exists for the controlled Name
00342 $this->DB->Query("SELECT * from VariantNames WHERE ".
00343 "ControlledNameId = ".$this->Id);
00344
00345 # variant name exists so do an update
00346 if ($this->DB->NumRowsSelected() > 0)
00347 {
00348 return $this->DB->UpdateValue("VariantNames",
00349 $FieldName, $NewValue,
00350 "ControlledNameId = ".$this->Id,
00351 $this->DBFields, TRUE);
00352 }
00353 # no variant name so do an insert
00354 else if ($NewValue != DB_NOVALUE)
00355 {
00356 $this->DB->Query("INSERT into VariantNames ".
00357 "(VariantName, ControlledNameId) VALUES ".
00358 "('".addslashes($NewValue)."', ".$this->Id.")");
00359 }
00360 }
00361 # delete variant name
00362 else
00363 {
00364 $this->DB->Query("Delete from VariantNames where ".
00365 "ControlledNameId = ".$this->Id);
00366 }
00367 }
00368 }
00369
00370 ?>