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         # 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
    00226     function InUse()
    00227     {
    00228         return $this->DB->Query("SELECT COUNT(*) AS Count FROM ".
    00229                 "ResourceNameInts WHERE ControlledNameId = ".$this->Id, "Count");
    00230     }
    00231
    00237     function RemapTo($NewNameId)
    00238     {
    00239         $this->DB->Query("UPDATE ResourceNameInts SET ControlledNameId = "
    00240                 .intval($NewNameId)." WHERE ControlledNameId = ".$this->Id);
    00241     }
    00242
    00249     function Delete($DeleteIfHasResources = FALSE)
    00250     {
    00251         $DB =& $this->DB;
    00252
    00253         if ($DeleteIfHasResources || !$this->InUse())
    00254         {
    00255             # delete this controlled name
    00256             $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
    00257                 $this->Id);
    00258
    00259             # delete associated variant name
    00260             $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
    00261                 $this->Id);
    00262
    00263             if ($DeleteIfHasResources)
    00264             {
    00265                 $DB->Query("DELETE FROM ResourceNameInts WHERE ".
    00266                    "ControlledNameId=".$this->Id);
    00267             }
    00268         }
    00269     }
    00270
    00271     # ---- PRIVATE INTERFACE -------------------------------------------------
    00272
    00273     private $DB;
    00274     private $DBFields;
    00275     private $Id;
    00276     private $ErrorStatus;
    00277
    00278     # convenience function to supply parameters to Database->UpdateValue()
    00279     private function UpdateValue($FieldName, $NewValue)
    00280     {
    00281         return $this->DB->UpdateValue("ControlledNames", $FieldName,
    00282                 $NewValue, "ControlledNameId = ".$this->Id,
    00283                 $this->DBFields, TRUE);
    00284     }
    00285
    00286     # convenience function for VariantNames table
    00287     private function UpdateVariantValue($FieldName, $NewValue)
    00288     {
    00289         if (!empty($NewValue))
    00290         {
    00291             # see if variant name exists for the controlled Name
    00292             $this->DB->Query("SELECT * from VariantNames WHERE ".
    00293                         "ControlledNameId = ".$this->Id);
    00294
    00295             # variant name exists so do an update
    00296             if ($this->DB->NumRowsSelected() > 0)
    00297             {
    00298                 return $this->DB->UpdateValue("VariantNames",
    00299                             $FieldName, $NewValue,
    00300                             "ControlledNameId = ".$this->Id,
    00301                             $this->DBFields, TRUE);
    00302             }
    00303             # no variant name so do an insert
    00304             else if ($NewValue != DB_NOVALUE)
    00305             {
    00306                 $this->DB->Query("INSERT into VariantNames ".
    00307                         "(VariantName, ControlledNameId) VALUES ".
    00308                         "('".addslashes($NewValue)."', ".$this->Id.")");
    00309             }
    00310         }
    00311         # delete variant name
    00312         else
    00313         {
    00314             $this->DB->Query("Delete from VariantNames where ".
    00315                 "ControlledNameId = ".$this->Id);
    00316         }
    00317     }
    00318 }
    00319
    00320 ?>