Search:

CWIS Developers Documentation

  • Main Page
  • Classes
  • Files
  • File List
  • File Members

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         # look for passed in name and type
00051         if (!empty($Name) && !empty($FieldId))
00052         {
00053             $DB->Query("SELECT * FROM ControlledNames".
00054                    " WHERE ControlledName = \"".addslashes($Name)."\"".
00055                    " AND FieldId = ".intval($FieldId));
00056 
00057             while ($this->DBFields = $DB->FetchRow())
00058             {
00059                 # this controlled name already exists
00060                 if ($this->DBFields["ControlledName"] == $Name)
00061                 {
00062                     $this->ErrorStatus = self::STATUS_EXISTS;
00063                     $NameId = $this->DBFields["ControlledNameId"];
00064                 
00065                     # cache the variant name separately
00066                     $VN = $DB->Query("SELECT VariantName FROM VariantNames".
00067                         " WHERE ControlledNameId = ".
00068                         $this->DBFields["ControlledNameId"], "VariantName"); 
00069 
00070                     $this->DBFields["VariantName"] = $VN;
00071                     break;
00072                 }
00073             }
00074             # controlled name not found, create it
00075             if ($this->ErrorStatus == self::STATUS_OK) 
00076             {
00077                 # add new controlled name
00078                 $DB->Query("INSERT INTO ControlledNames ".
00079                         "(FieldId, ControlledName, QualifierId)".
00080                         " VALUES (".intval($FieldId).", '".addslashes($Name)
00081                         ."', ".intval($QualifierId).")");
00082 
00083                 # get name ID  for new controlled name
00084                 $NameId = $DB->LastInsertId("ControlledNames");
00085 
00086                 # check for Variant
00087                 if (!empty($VariantName))
00088                 {
00089                     $DB->Query("INSERT INTO VariantNames ".
00090                         "(ControlledNameId, VariantName) ".
00091                         "VALUES (".intval($NameId).", '"
00092                         .addslashes($VariantName)."') ");
00093                 }
00094             }
00095         }
00096         # Name Id passed in, look it up
00097         if (!empty($NameId) && $NameId != -1)
00098         {
00099             $DB->Query("SELECT * FROM ControlledNames".
00100                    " WHERE ControlledNameId = ".intval($NameId));
00101             $this->DBFields = $DB->FetchRow();
00102 
00103             # cache the variant name separately
00104             $VN = $DB->Query("SELECT VariantName FROM VariantNames".
00105                    " WHERE ControlledNameId = ".intval($NameId), "VariantName"); 
00106 
00107             $this->DBFields["VariantName"] = $VN;
00108         }
00109 
00110         # save supplied or generated controlled name ID
00111         $this->Id = intval($NameId);
00112 
00113         # set error status if controlled name info not loaded
00114         if ($this->DBFields["ControlledNameId"] != $this->Id)
00115         {
00116             $this->ErrorStatus = self::STATUS_INVALID_ID;
00117         }
00118     }
00119     
00124     function Status()   {  return $this->ErrorStatus;  }
00125 
00130     function Id()       {  return $this->Id;  }
00131 
00137     function Name($NewValue = DB_NOVALUE)
00138             {  return $this->UpdateValue("ControlledName", $NewValue);  }
00139 
00145     function VariantName($NewValue = DB_NOVALUE)
00146             {  return $this->UpdateVariantValue("VariantName", $NewValue);  }
00147 
00153     function FieldId($NewValue = DB_NOVALUE)
00154             {  return $this->UpdateValue("FieldId", $NewValue);  }
00155 
00161     function QualifierId($NewValue = DB_NOVALUE)
00162             {  return $this->UpdateValue("QualifierId", $NewValue);  }
00163 
00169     function Variant($NewValue = DB_NOVALUE)
00170             {  return $this->VariantName($NewValue);  }
00171 
00177     function Qualifier($NewValue = DB_NOVALUE)
00178     {
00179         # if new qualifier supplied
00180         if ($NewValue !== DB_NOVALUE)
00181         {
00182             # set new qualifier ID
00183             $this->QualifierId($NewValue->Id());
00184 
00185             # use new qualifier for return value
00186             $Qualifier = $NewValue;
00187         }
00188         else
00189         {
00190             # if qualifier is available
00191             if ($this->QualifierId() !== NULL)
00192             {
00193                 # create qualifier object using stored ID
00194                 $Qualifier = new Qualifier($this->QualifierId());
00195 
00196                 # if ID was zero and no name available for qualifieR
00197                 # (needed because some controlled name records in DB
00198                 #       have 0 instead of NULL when no controlled name assigned)
00199                 # (NOTE:  this is problematic if there is a qualifier with an
00200                 #       ID value of 0!!!)
00201                 if (($this->QualifierId() == 0) && !strlen($Qualifier->Name()))
00202                 {
00203                     # return NULL to indicate no qualifier
00204                     $Qualifier = NULL;
00205                 }
00206             }
00207             else
00208             {
00209                 # return NULL to indicate no qualifier
00210                 $Qualifier = NULL;
00211             }
00212         }
00213 
00214         # return qualifier to caller
00215         return $Qualifier;
00216     }
00217 
00222     function InUse()
00223     {
00224         return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
00225                 "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
00226     }
00227 
00233     function RemapTo($NewNameId)
00234     {
00235         $this->DB->Query("UPDATE ResourceNameInts SET ControlledNameId = "
00236                 .intval($NewNameId)." WHERE ControlledNameId = ".$this->Id);
00237     }
00238 
00245     function Delete($DeleteIfHasResources = FALSE)
00246     {
00247         $DB =& $this->DB;
00248 
00249         if ($DeleteIfHasResources || !$this->InUse())
00250         {
00251             # delete this controlled name
00252             $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
00253                 $this->Id);
00254 
00255             # delete associated variant name
00256             $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
00257                 $this->Id);
00258 
00259             if ($DeleteIfHasResources)
00260             {
00261                 $DB->Query("DELETE FROM ResourceNameInts WHERE ".
00262                    "ControlledNameId=".$this->Id);
00263             }
00264         }
00265     }
00266 
00267     # ---- PRIVATE INTERFACE -------------------------------------------------
00268 
00269     private $DB;
00270     private $DBFields;
00271     private $Id;
00272     private $ErrorStatus;
00273 
00274     # convenience function to supply parameters to Database->UpdateValue()
00275     private function UpdateValue($FieldName, $NewValue)
00276     {
00277         return $this->DB->UpdateValue("ControlledNames", $FieldName, 
00278                 $NewValue, "ControlledNameId = ".$this->Id,
00279                 $this->DBFields, TRUE);
00280     }
00281     
00282     # convenience function for VariantNames table
00283     private function UpdateVariantValue($FieldName, $NewValue)
00284     {
00285         if (!empty($NewValue))
00286         {
00287             # see if variant name exists for the controlled Name
00288             $this->DB->Query("SELECT * from VariantNames WHERE ".
00289                         "ControlledNameId = ".$this->Id);
00290 
00291             # variant name exists so do an update
00292             if ($this->DB->NumRowsSelected() > 0)
00293             {
00294                 return $this->DB->UpdateValue("VariantNames", 
00295                             $FieldName, $NewValue,
00296                             "ControlledNameId = ".$this->Id,
00297                             $this->DBFields, TRUE);
00298             }
00299             # no variant name so do an insert
00300             else if ($NewValue != DB_NOVALUE)
00301             {
00302                 $this->DB->Query("INSERT into VariantNames ".
00303                         "(VariantName, ControlledNameId) VALUES ".
00304                         "('".addslashes($NewValue)."', ".$this->Id.")");
00305             }
00306         }
00307         # delete variant name
00308         else
00309         {
00310             $this->DB->Query("Delete from VariantNames where ".
00311                 "ControlledNameId = ".$this->Id);
00312         }
00313     }
00314 }
00315 
00316 ?>
CWIS logo doxygen
Copyright 2009 Internet Scout