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
    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         $this->DB->Query("UPDATE ResourceNameInts SET ControlledNameId = "
    00260                 .intval($NewNameId)." WHERE ControlledNameId = ".$this->Id);
    00261     }
    00262
    00269     function Delete($DeleteIfHasResources = FALSE)
    00270     {
    00271         $DB =& $this->DB;
    00272
    00273         if ($DeleteIfHasResources || !$this->InUse())
    00274         {
    00275             # delete this controlled name
    00276             $DB->Query("DELETE FROM ControlledNames WHERE ControlledNameId=".
    00277                 $this->Id);
    00278
    00279             # delete associated variant name
    00280             $DB->Query("DELETE FROM VariantNames WHERE ControlledNameId=".
    00281                 $this->Id);
    00282
    00283             if ($DeleteIfHasResources)
    00284             {
    00285                 $DB->Query("DELETE FROM ResourceNameInts WHERE ".
    00286                    "ControlledNameId=".$this->Id);
    00287             }
    00288         }
    00289     }
    00290
    00291     # ---- PRIVATE INTERFACE -------------------------------------------------
    00292
    00293     private $DB;
    00294     private $DBFields;
    00295     private $Id;
    00296     private $ErrorStatus;
    00297
    00298     # convenience function to supply parameters to Database->UpdateValue()
    00299     private function UpdateValue($FieldName, $NewValue)
    00300     {
    00301         return $this->DB->UpdateValue("ControlledNames", $FieldName,
    00302                 $NewValue, "ControlledNameId = ".$this->Id,
    00303                 $this->DBFields, TRUE);
    00304     }
    00305
    00306     # convenience function for VariantNames table
    00307     private function UpdateVariantValue($FieldName, $NewValue)
    00308     {
    00309         if (!empty($NewValue))
    00310         {
    00311             # see if variant name exists for the controlled Name
    00312             $this->DB->Query("SELECT * from VariantNames WHERE ".
    00313                         "ControlledNameId = ".$this->Id);
    00314
    00315             # variant name exists so do an update
    00316             if ($this->DB->NumRowsSelected() > 0)
    00317             {
    00318                 return $this->DB->UpdateValue("VariantNames",
    00319                             $FieldName, $NewValue,
    00320                             "ControlledNameId = ".$this->Id,
    00321                             $this->DBFields, TRUE);
    00322             }
    00323             # no variant name so do an insert
    00324             else if ($NewValue != DB_NOVALUE)
    00325             {
    00326                 $this->DB->Query("INSERT into VariantNames ".
    00327                         "(VariantName, ControlledNameId) VALUES ".
    00328                         "('".addslashes($NewValue)."', ".$this->Id.")");
    00329             }
    00330         }
    00331         # delete variant name
    00332         else
    00333         {
    00334             $this->DB->Query("Delete from VariantNames where ".
    00335                 "ControlledNameId = ".$this->Id);
    00336         }
    00337     }
    00338 }
    00339
    00340 ?>

Copyright 2010 Internet Scout