Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- Search for |

- File List
- File Members

# MetadataField.php

Go to the documentation of this file.

    00001 <?PHP
    00002
    00003 #
    00004 #   FILE:  SPT--MetadataField.php
    00005 #
    00006 #   METHODS PROVIDED:
    00007 #       Type($NewValue = DB_NOVALUE)
    00008 #           - get/set type of field as enumerated value
    00009 #       TypeAsName()
    00010 #           - get type of field as type name (string)
    00011 #       GetAllowedConversionTypes()
    00012 #           - get associative array (enumeration => string) containing field types we can convert to
    00013 #       Name($NewValue = DB_NOVALUE)
    00014 #           - get/set name of field
    00015 #       HasItemLevelQualifiers($NewValue = DB_NOVALUE)
    00016 #           - get/set whether field uses item-level qualifiers
    00017 #       AssociatedQualifierList()
    00018 #           - get list of qualifiers associated with field
    00019 #       UnassociatedQualifierList()
    00020 #           - get list of qualifiers not associated with field
    00021 #       AssociateWithQualifier($QualifierIdOrObject)
    00022 #           - add qualifier association
    00023 #       UnassociateWithQualifier($QualifierIdOrObject)
    00024 #           - delete qualifier association
    00025 #       Status()
    00026 #           - get current error status of object
    00027 #       Id()
    00028 #       DBFieldName()
    00029 #           - get field attributes
    00030 #       Description($NewValue = DB_NOVALUE)
    00031 #       RequiredBySPT($NewValue = DB_NOVALUE)
    00032 #       Enabled($NewValue = DB_NOVALUE)
    00033 #       Optional($NewValue = DB_NOVALUE)
    00034 #       Viewable($NewValue = DB_NOVALUE)
    00035 #       AllowMultiple($NewValue = DB_NOVALUE)
    00036 #       IncludeInKeywordSearch($NewValue = DB_NOVALUE)
    00037 #       IncludeInAdvancedSearch($NewValue = DB_NOVALUE)
    00038 #       TextFieldSize($NewValue = DB_NOVALUE)
    00039 #       MaxLength($NewValue = DB_NOVALUE)
    00040 #       ParagraphRows($NewValue = DB_NOVALUE)
    00041 #       ParagraphCols($NewValue = DB_NOVALUE)
    00042 #       DefaultValue($NewValue = DB_NOVALUE)
    00043 #       MinValue($NewValue = DB_NOVALUE)
    00044 #       MaxValue($NewValue = DB_NOVALUE)
    00045 #       FlagOnLabel($NewValue = DB_NOVALUE)
    00046 #       FlagOffLabel($NewValue = DB_NOVALUE)
    00047 #       DateFormat($NewValue = DB_NOVALUE)
    00048 #       SearchWeight($NewValue = DB_NOVALUE)
    00049 #       MaxHeight($NewValue = DB_NOVALUE)
    00050 #       MaxWidth($NewValue = DB_NOVALUE)
    00051 #       MaxPreviewHeight($NewValue = DB_NOVALUE)
    00052 #       MaxPreviewWidth($NewValue = DB_NOVALUE)
    00053 #       MaxThumbnailHeight($NewValue = DB_NOVALUE)
    00054 #       MaxThumbnailWidth($NewValue = DB_NOVALUE)
    00055 #       DefaultAltText($NewValue = DB_NOVALUE)
    00056 #       UsesQualifiers($NewValue = DB_NOVALUE)
    00057 #       DefaultQualifier($NewValue = DB_NOVALUE)
    00058 #           - get/set field attributes
    00059 #
    00060 #   AUTHOR:  Edward Almasy
    00061 #
    00062 #   Part of the Scout Portal Toolkit
    00063 #   Copyright 2002-2003 Internet Scout Project
    00064 #   http://scout.wisc.edu
    00065 #
    00066
    00067 class MetadataField {
    00068
    00069     # ---- PUBLIC INTERFACE --------------------------------------------------
    00070
    00071     # get current error status of object
    00072     function Status() {  return $this->ErrorStatus;  }
    00073
    00074     # get/set type of field as enumerated value
    00075     function Type($NewValue = DB_NOVALUE)
    00076     {
    00077         # if new value supplied
    00078         if (($NewValue != DB_NOVALUE)
    00079              && ($NewValue != MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]]))
    00080         {
    00081             # update database fields and store new type
    00082             $this->ModifyField(NULL, $NewValue);
    00083         }
    00084
    00085         # return type to caller
    00086         return MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
    00087     }
    00088
    00089     # get type of field as type name (string)
    00090     function TypeAsName()
    00091     {
    00092         return $this->DBFields["FieldType"];
    00093     }
    00094
    00095     # get/set name of field
    00096     function Name($NewName = DB_NOVALUE)
    00097     {
    00098         # if new name specified
    00099         if (($NewName != DB_NOVALUE)
    00100             && (trim($NewName) != $this->DBFields["FieldName"]))
    00101         {
    00102             # if field name is invalid
    00103             $NewName = trim($NewName);
    00104             if (strlen($this->NormalizeFieldNameForDB($NewName)) < 1)
    00105             {
    00106                 # set error status to indicate illegal name
    00107                 $this->ErrorStatus = MetadataSchema::MDFSTAT_ILLEGALNAME;
    00108             }
    00109             else
    00110             {
    00111                 # check for duplicate name
    00112                 $DuplicateCount = $this->DB->Query("SELECT COUNT(*) AS RecordCount FROM MetadataFields "
    00113                                              ."WHERE FieldName = '".addslashes($NewName)."'",
    00114                                              "RecordCount");
    00115
    00116                 # if field name is duplicate
    00117                 if ($DuplicateCount > 0)
    00118                 {
    00119                     # set error status to indicate duplicate name
    00120                     $this->ErrorStatus = MetadataSchema::MDFSTAT_DUPLICATENAME;
    00121                 }
    00122                 else
    00123                 {
    00124                     # modify database declaration to reflect new field name
    00125                     $this->ErrorStatus = MetadataSchema::MDFSTAT_OK;
    00126                     $this->ModifyField($NewName);
    00127                 }
    00128             }
    00129         }
    00130
    00131         # return value to caller
    00132         return $this->DBFields["FieldName"];
    00133     }
    00134
    00135     # get associative array (enumeration => string) containing field types we can convert to
    00136     function GetAllowedConversionTypes()
    00137     {
    00138         # determine type list based on our type
    00139         switch ($this->Type())
    00140         {
    00141         case MetadataSchema::MDFTYPE_TEXT:
    00142         case MetadataSchema::MDFTYPE_PARAGRAPH:
    00143         case MetadataSchema::MDFTYPE_NUMBER:
    00144         case MetadataSchema::MDFTYPE_FLAG:
    00145         case MetadataSchema::MDFTYPE_URL:
    00146             $AllowedTypes = array(
    00147                 MetadataSchema::MDFTYPE_TEXT       => "Text",
    00148                 MetadataSchema::MDFTYPE_PARAGRAPH  => "Paragraph",
    00149                 MetadataSchema::MDFTYPE_NUMBER     => "Number",
    00150                 MetadataSchema::MDFTYPE_FLAG       => "Flag",
    00151                 MetadataSchema::MDFTYPE_URL        => "Url"
    00152                 );
    00153             break;
    00154
    00155         case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00156         case MetadataSchema::MDFTYPE_OPTION:
    00157             $AllowedTypes = array(
    00158                 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "ControlledName",
    00159                 MetadataSchema::MDFTYPE_OPTION         => "Option",
    00160                 );
    00161             break;
    00162
    00163         case MetadataSchema::MDFTYPE_DATE:
    00164             $AllowedTypes = array(
    00165                 MetadataSchema::MDFTYPE_TEXT  => "Text",
    00166                 MetadataSchema::MDFTYPE_DATE  => "Date",
    00167                 );
    00168             break;
    00169
    00170         case MetadataSchema::MDFTYPE_IMAGE:
    00171             $AllowedTypes = array(
    00172                 MetadataSchema::MDFTYPE_TEXT  => "Text",
    00173                 MetadataSchema::MDFTYPE_IMAGE => "Still Image",
    00174                 );
    00175             break;
    00176
    00177         case MetadataSchema::MDFTYPE_TIMESTAMP:
    00178         case MetadataSchema::MDFTYPE_TREE:
    00179         case MetadataSchema::MDFTYPE_USER:
    00180         case MetadataSchema::MDFTYPE_FILE:
    00181         default:
    00182             $AllowedTypes = array();
    00183             break;
    00184         }
    00185
    00186         # return type list to caller
    00187         return $AllowedTypes;
    00188     }
    00189
    00190
    00191     # get/set whether item is temporary instance
    00192     function IsTempItem($NewSetting = NULL)
    00193     {
    00194         $ItemTableName = "MetadataFields";
    00195         $ItemIdFieldName = "FieldId";
    00196         $ItemFactoryObjectName = "MetadataSchema";
    00197         $ItemAssociationTables = array(
    00198                 "FieldQualifierInts",
    00199                 );
    00200         $ItemAssociationFieldName = "MetadataFieldId";
    00201
    00202         # if new temp item setting supplied
    00203         if ($NewSetting !== NULL)
    00204         {
    00205             # if caller requested to switch
    00206             if ((($this->Id() < 0) && ($NewSetting == FALSE))
    00207                     || (($this->Id() >= 0) && ($NewSetting == TRUE)))
    00208             {
    00209                 # if field name is invalid
    00210                 if (strlen($this->NormalizeFieldNameForDB($this->Name())) < 1)
    00211                 {
    00212                     # set error status to indicate illegal name
    00213                     $this->ErrorStatus = MetadataSchema::MDFSTAT_ILLEGALNAME;
    00214                 }
    00215                 else
    00216                 {
    00217                     # lock DB tables to prevent next ID from being grabbed
    00218                     $DB = $this->DB;
    00219                     $DB->Query("LOCK TABLES ".$ItemTableName." WRITE,".
    00220                             "APSessions WRITE, APSessionData WRITE");
    00221
    00222                     # get next temp item ID
    00223                     $OldItemId = $this->Id();
    00224                     $Factory = new $ItemFactoryObjectName();
    00225                     if ($NewSetting == TRUE)
    00226                     {
    00227                         $this->Id = $Factory->GetNextTempItemId();
    00228                     }
    00229                     else
    00230                     {
    00231                         $this->Id = $Factory->GetNextItemId();
    00232                     }
    00233
    00234                     # change item ID
    00235                     $DB->Query("UPDATE ".$ItemTableName." SET ".$ItemIdFieldName." = ".
    00236                         $this->Id.  " WHERE ".$ItemIdFieldName." = ".$OldItemId);
    00237
    00238                     # release DB tables
    00239                     $DB->Query("UNLOCK TABLES");
    00240
    00241                     # change associations
    00242                     foreach ($ItemAssociationTables as $TableName)
    00243                     {
    00244                         $DB->Query("UPDATE ".$TableName." SET ".$ItemAssociationFieldName." = ".
    00245                                 $this->Id.  " WHERE ".$ItemAssociationFieldName." = ".$OldItemId);
    00246                     }
    00247
    00248                     # if changing item from temp to non-temp
    00249                     if ($NewSetting == FALSE)
    00250                     {
    00251                         # add any needed database fields and/or entries
    00252                         $this->AddDatabaseFields();
    00253                     }
    00254                 }
    00255             }
    00256         }
    00257
    00258         # report to caller whether we are a temp item
    00259         return ($this->Id() < 0) ? TRUE : FALSE;
    00260     }
    00261
    00262     # get field attributes
    00263     function Id() {  return $this->DBFields["FieldId"];  }
    00264     function DBFieldName() {  return $this->DBFields["DBFieldName"];  }
    00265
    00266     # get/set field attributes
    00267     function Description($NewValue = DB_NOVALUE) {  return $this->UpdateValue("Description", $NewValue);  }
    00268     function RequiredBySPT($NewValue = DB_NOVALUE) {  return $this->UpdateValue("RequiredBySPT", $NewValue);  }
    00269     function Enabled($NewValue = DB_NOVALUE) {  return $this->UpdateValue("Enabled", $NewValue);  }
    00270     function Optional($NewValue = DB_NOVALUE) {  return $this->UpdateValue("Optional", $NewValue);  }
    00271     function Viewable($NewValue = DB_NOVALUE) {  return $this->UpdateValue("Viewable", $NewValue);  }
    00272     function AllowMultiple($NewValue = DB_NOVALUE) {  return $this->UpdateValue("AllowMultiple", $NewValue);  }
    00273     function IncludeInKeywordSearch($NewValue = DB_NOVALUE) {  return $this->UpdateValue("IncludeInKeywordSearch", $NewValue);  }
    00274     function IncludeInAdvancedSearch($NewValue = DB_NOVALUE) {  return $this->UpdateValue("IncludeInAdvancedSearch", $NewValue);  }
    00275     function IncludeInRecommenderSystem($NewValue = DB_NOVALUE) {  return $this->UpdateValue("IncludeInRecommenderSystem", $NewValue);  }
    00276     function TextFieldSize($NewValue = DB_NOVALUE) {  return $this->UpdateValue("TextFieldSize", $NewValue);  }
    00277     function MaxLength($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxLength", $NewValue);  }
    00278     function ParagraphRows($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ParagraphRows", $NewValue);  }
    00279     function ParagraphCols($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ParagraphCols", $NewValue);  }
    00280     function MinValue($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MinValue", $NewValue);  }
    00281     function MaxValue($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxValue", $NewValue);  }
    00282     function FlagOnLabel($NewValue = DB_NOVALUE) {  return $this->UpdateValue("FlagOnLabel", $NewValue);  }
    00283     function FlagOffLabel($NewValue = DB_NOVALUE) {  return $this->UpdateValue("FlagOffLabel", $NewValue);  }
    00284     function DateFormat($NewValue = DB_NOVALUE) {  return $this->UpdateValue("DateFormat", $NewValue);  }
    00285     function SearchWeight($NewValue = DB_NOVALUE) {  return $this->UpdateValue("SearchWeight", $NewValue);  }
    00286     function RecommenderWeight($NewValue = DB_NOVALUE) {  return $this->UpdateValue("RecommenderWeight", $NewValue);  }
    00287     function MaxHeight($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxHeight", $NewValue);  }
    00288     function MaxWidth($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxWidth", $NewValue);  }
    00289     function MaxPreviewHeight($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxPreviewHeight", $NewValue);  }
    00290     function MaxPreviewWidth($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxPreviewWidth", $NewValue);  }
    00291     function MaxThumbnailHeight($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxThumbnailHeight", $NewValue);  }
    00292     function MaxThumbnailWidth($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MaxThumbnailWidth", $NewValue);  }
    00293     function DefaultAltText($NewValue = DB_NOVALUE) {  return $this->UpdateValue("DefaultAltText", $NewValue);  }
    00294     function UsesQualifiers($NewValue = DB_NOVALUE) {  return $this->UpdateValue("UsesQualifiers", $NewValue);  }
    00295     function DefaultQualifier($NewValue = DB_NOVALUE) {  return $this->UpdateValue("DefaultQualifier", $NewValue);  }
    00296     function UseForOaiSets($NewValue = DB_NOVALUE) {  return $this->UpdateValue("UseForOaiSets", $NewValue);  }
    00297     function ViewingPrivilege($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ViewingPrivilege", $NewValue);  }
    00298     function AuthoringPrivilege($NewValue = DB_NOVALUE) {  return $this->UpdateValue("AuthoringPrivilege", $NewValue);  }
    00299     function EditingPrivilege($NewValue = DB_NOVALUE) {  return $this->UpdateValue("EditingPrivilege", $NewValue);  }
    00300
    00301     function PointPrecision($NewValue = DB_NOVALUE)
    00302     {
    00303         if ($NewValue !== DB_NOVALUE && $this->Id() >= 0)
    00304         {
    00305             $OldValue = $this->UpdateValue("PointPrecision", DB_NOVALUE);
    00306
    00307             if ($NewValue != $OldValue)
    00308             {
    00309                 $Decimals  = $this->UpdateValue("PointDecimalDigits", DB_NOVALUE);
    00310
    00311                 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
    00312                            ."`".$this->DBFields["DBFieldName"]."X` "
    00313                            ."DECIMAL(".$NewValue.",".$Decimals.")");
    00314                 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
    00315                            ."`".$this->DBFields["DBFieldName"]."Y` "
    00316                            ."DECIMAL(".$NewValue.",".$Decimals.")");
    00317             }
    00318         }
    00319
    00320         return $this->UpdateValue("PointPrecision", $NewValue);
    00321     }
    00322
    00323     function PointDecimalDigits($NewValue = DB_NOVALUE)
    00324     {
    00325         if ($NewValue !== DB_NOVALUE && $this->Id() >= 0)
    00326         {
    00327             $OldValue = $this->UpdateValue("PointDecimalDigits", DB_NOVALUE);
    00328
    00329             if ($NewValue != $OldValue)
    00330             {
    00331                 $Precision = $this->UpdateValue("PointPrecision", DB_NOVALUE);
    00332
    00333                 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
    00334                            ."`".$this->DBFields["DBFieldName"]."X` "
    00335                            ."DECIMAL(".$Precision.",".$NewValue.")");
    00336                 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
    00337                            ."`".$this->DBFields["DBFieldName"]."Y` "
    00338                            ."DECIMAL(".$Precision.",".$NewValue.")");
    00339             }
    00340         }
    00341
    00342         return $this->UpdateValue("PointDecimalDigits", $NewValue);
    00343     }
    00344
    00345     function DefaultValue($NewValue = DB_NOVALUE) {
    00346         if ($this->Type() == MetadataSchema::MDFTYPE_POINT)
    00347         {
    00348             if ($NewValue !== DB_NOVALUE &&
    00349                 isset($NewValue["X"]) && isset($NewValue["Y"]))
    00350             {
    00351                 $NewValue = $NewValue["X"].",".$NewValue["Y"];
    00352             }
    00353
    00354             $tmp = explode(",", $this->UpdateValue("DefaultValue", $NewValue));
    00355
    00356             if (count($tmp)==2)
    00357             {
    00358                 $rc = array("X" => $tmp[0], "Y" => $tmp[1]);
    00359             }
    00360             else
    00361             {
    00362                 $rc = array("X" => NULL, "Y" => NULL);
    00363             }
    00364         }
    00365         else
    00366         {
    00367             $rc = $this->UpdateValue("DefaultValue", $NewValue);
    00368         }
    00369         return $rc;
    00370     }
    00371
    00372     # get possible values (only meaningful for Trees, Controlled Names, Options, Flags)
    00373     # (index for returned array is IDs for values)
    00374     function GetPossibleValues($MaxNumberOfValues = NULL)
    00375     {
    00376         # retrieve values based on field type
    00377         switch ($this->Type())
    00378         {
    00379             case MetadataSchema::MDFTYPE_TREE:
    00380                 $QueryString = "SELECT ClassificationId,ClassificationName"
    00381                         ." FROM Classifications WHERE FieldId = ".$this->Id()
    00382                         ." ORDER BY ClassificationName";
    00383                 if ($MaxNumberOfValues)
    00384                         {  $QueryString .= " LIMIT ".intval($MaxNumberOfValues);  }
    00385                 $this->DB->Query($QueryString);
    00386                 $PossibleValues = $this->DB->FetchColumn(
    00387                         "ClassificationName", "ClassificationId");
    00388                 break;
    00389
    00390             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00391             case MetadataSchema::MDFTYPE_OPTION:
    00392                 $QueryString = "SELECT ControlledNameId,ControlledName"
    00393                         ." FROM ControlledNames WHERE FieldId = ".$this->Id()
    00394                         ." ORDER BY ControlledName";
    00395                 if ($MaxNumberOfValues)
    00396                         {  $QueryString .= " LIMIT ".intval($MaxNumberOfValues);  }
    00397                 $this->DB->Query($QueryString);
    00398                 $PossibleValues = $this->DB->FetchColumn(
    00399                         "ControlledName", "ControlledNameId");
    00400                 break;
    00401
    00402             case MetadataSchema::MDFTYPE_FLAG:
    00403                 $PossibleValues[0] = $this->FlagOffLabel();
    00404                 $PossibleValues[1] = $this->FlagOnLabel();
    00405                 break;
    00406
    00407             default:
    00408                 # for everything else return an empty array
    00409                 $PossibleValues = array();
    00410                 break;
    00411         }
    00412
    00413         # return array of possible values to caller
    00414         return $PossibleValues;
    00415     }
    00416
    00417     # get count of possible values (only meaningful for Trees, Controlled Names, Options)
    00418     function GetCountOfPossibleValues()
    00419     {
    00420         # retrieve values based on field type
    00421         switch ($this->Type())
    00422         {
    00423             case MetadataSchema::MDFTYPE_TREE:
    00424                 $Count = $this->DB->Query("SELECT count(*) AS ValueCount"
    00425                         ." FROM Classifications WHERE FieldId = ".$this->Id(),
    00426                         "ValueCount");
    00427                 break;
    00428
    00429             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00430             case MetadataSchema::MDFTYPE_OPTION:
    00431                 $Count = $this->DB->Query("SELECT count(*) AS ValueCount"
    00432                         ." FROM ControlledNames WHERE FieldId = ".$this->Id(),
    00433                         "ValueCount");
    00434                 break;
    00435
    00436             case MetadataSchema::MDFTYPE_FLAG:
    00437                 $Count = 2;
    00438                 break;
    00439
    00440             default:
    00441                 # for everything else return an empty array
    00442                 $Count = 0;
    00443                 break;
    00444         }
    00445
    00446         # return count of possible values to caller
    00447         return $Count;
    00448     }
    00449
    00450     # get ID for specified value (only meaningful for Trees / Controlled Names / Options)
    00451     # (returns NULL if value not found)
    00452     function GetIdForValue($Value)
    00453     {
    00454         # retrieve ID based on field type
    00455         switch ($this->Type())
    00456         {
    00457             case MetadataSchema::MDFTYPE_TREE:
    00458                 $Id = $this->DB->Query("SELECT ClassificationId FROM Classifications"
    00459                         ." WHERE ClassificationName = '".addslashes($Value)."'"
    00460                         ." AND FieldId = ".$this->Id(),
    00461                         "ClassificationId");
    00462                 break;
    00463
    00464             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00465             case MetadataSchema::MDFTYPE_OPTION:
    00466                 $Id = $this->DB->Query("SELECT ControlledNameId FROM ControlledNames"
    00467                         ." WHERE ControlledName = '".addslashes($Value)."'"
    00468                         ." AND FieldId = ".$this->Id(),
    00469                         "ControlledNameId");
    00470                 break;
    00471
    00472             default:
    00473                 # for everything else return NULL
    00474                 $Id = NULL;
    00475                 break;
    00476         }
    00477
    00478         # return ID for value to caller
    00479         return $Id;
    00480     }
    00481
    00482     # get value for specified ID (only meaningful for Trees / Controlled Names / Options)
    00483     # (returns NULL if ID not found)
    00484     function GetValueForId($Id)
    00485     {
    00486         # retrieve ID based on field type
    00487         switch ($this->Type())
    00488         {
    00489             case MetadataSchema::MDFTYPE_TREE:
    00490                 $Value = $this->DB->Query("SELECT ClassificationName FROM Classifications"
    00491                         ." WHERE ClassificationId = '".intval($Id)."'"
    00492                         ." AND FieldId = ".$this->Id(),
    00493                         "ClassificationName");
    00494                 break;
    00495
    00496             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00497             case MetadataSchema::MDFTYPE_OPTION:
    00498                 $Value = $this->DB->Query("SELECT ControlledName FROM ControlledNames"
    00499                         ." WHERE ControlledNameId = '".intval($Id)."'"
    00500                         ." AND FieldId = ".$this->Id(),
    00501                         "ControlledName");
    00502                 break;
    00503
    00504             default:
    00505                 # for everything else return NULL
    00506                 $Value = NULL;
    00507                 break;
    00508         }
    00509
    00510         # return ID for value to caller
    00511         return $Value;
    00512     }
    00513
    00514
    00515
    00516     # get/set whether field uses item-level qualifiers
    00517     function HasItemLevelQualifiers($NewValue = DB_NOVALUE)
    00518     {
    00519         # if value provided different from present value
    00520         if (($NewValue != DB_NOVALUE)
    00521             && ($NewValue != $this->DBFields["HasItemLevelQualifiers"]))
    00522         {
    00523             # check if qualifier column currently exists
    00524             $QualColName = $this->DBFieldName()."Qualifier";
    00525             $QualColExists = $this->DB->FieldExists("Resources", $QualColName);
    00526
    00527             # if new value indicates qualifiers should now be used
    00528             if ($NewValue == TRUE)
    00529             {
    00530                 # if qualifier column does not exist in DB for this field
    00531                 if ($QualColExists == FALSE)
    00532                 {
    00533                     # add qualifier column in DB for this field
    00534                     $this->DB->Query("ALTER TABLE Resources ADD COLUMN `"
    00535                                      .$QualColName."` INT");
    00536                 }
    00537             }
    00538             else
    00539             {
    00540                 # if qualifier column exists in DB for this field
    00541                 if ($QualColExists == TRUE)
    00542                 {
    00543                     # remove qualifier column from DB for this field
    00544                     $this->DB->Query("ALTER TABLE Resources DROP COLUMN `"
    00545                                      .$QualColName."`");
    00546                 }
    00547             }
    00548         }
    00549
    00550         return $this->UpdateValue("HasItemLevelQualifiers", $NewValue);
    00551     }
    00552
    00553     # get list of qualifiers associated with field
    00554     function AssociatedQualifierList()
    00555     {
    00556         # start with empty list
    00557         $List = array();
    00558
    00559         # for each associated qualifier
    00560         $this->DB->Query("SELECT QualifierId FROM FieldQualifierInts"
    00561                      ." WHERE MetadataFieldId = ".$this->DBFields["FieldId"]);
    00562         while ($Record = $this->DB->FetchRow())
    00563         {
    00564             # load qualifier object
    00565             $Qual = new Qualifier($Record["QualifierId"]);
    00566
    00567             # add qualifier ID and name to list
    00568             $List[$Qual->Id()] = $Qual->Name();
    00569         }
    00570
    00571         # return list to caller
    00572         return $List;
    00573     }
    00574
    00575     # get list of qualifiers not associated with field
    00576     function UnassociatedQualifierList()
    00577     {
    00578         # grab list of associated qualifiers
    00579         $AssociatedQualifiers = $this->AssociatedQualifierList();
    00580
    00581         # get list of all qualifiers
    00582         $QFactory = new QualifierFactory();
    00583         $AllQualifiers = $QFactory->QualifierList();
    00584
    00585         # return list of unassociated qualifiers
    00586         return array_diff($AllQualifiers, $AssociatedQualifiers);
    00587     }
    00588
    00589     # add qualifier association
    00590     function AssociateWithQualifier($QualifierIdOrObject)
    00591     {
    00592         # if qualifier object passed in
    00593         if (is_object($QualifierIdOrObject))
    00594         {
    00595             # grab qualifier ID from object
    00596             $QualifierIdOrObject = $QualifierIdOrObject->Id();
    00597         }
    00598
    00599         # if not already associated
    00600         $RecordCount = $this->DB->Query(
    00601             "SELECT COUNT(*) AS RecordCount FROM FieldQualifierInts"
    00602             ." WHERE QualifierId = ".$QualifierIdOrObject
    00603             ." AND MetadataFieldId = ".$this->Id(), "RecordCount");
    00604         if ($RecordCount < 1)
    00605         {
    00606             # associate field with qualifier
    00607             $this->DB->Query("INSERT INTO FieldQualifierInts SET"
    00608                              ." QualifierId = ".$QualifierIdOrObject.","
    00609                              ." MetadataFieldId = ".$this->Id());
    00610         }
    00611     }
    00612
    00613     # delete qualifier association
    00614     function UnassociateWithQualifier($QualifierIdOrObject)
    00615     {
    00616         # if qualifier object passed in
    00617         if (is_object($QualifierIdOrObject))
    00618         {
    00619             # grab qualifier ID from object
    00620             $QualifierIdOrObject = $QualifierIdOrObject->Id();
    00621         }
    00622
    00623         # delete intersection record from database
    00624         $this->DB->Query("DELETE FROM FieldQualifierInts WHERE QualifierId = "
    00625                          .$QualifierIdOrObject." AND MetadataFieldId = ".
    00626                          $this->Id());
    00627     }
    00628
    00629     # retrieve item factory object for this field
    00630     function GetFactory()
    00631     {
    00632         switch ($this->Type())
    00633         {
    00634             case MetadataSchema::MDFTYPE_TREE:
    00635                 $Factory = new ClassificationFactory($this->Id());
    00636                 break;
    00637
    00638             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00639             case MetadataSchema::MDFTYPE_OPTION:
    00640                 $Factory = new ControlledNameFactory($this->Id());
    00641                 break;
    00642
    00643             default:
    00644                 $Factory = NULL;
    00645                 break;
    00646         }
    00647
    00648         return $Factory;
    00649     }
    00650
    00651
    00652     # ---- PRIVATE INTERFACE -------------------------------------------------
    00653
    00654     private $DB;
    00655     private $DBFields;
    00656     private $ErrorStatus;
    00657
    00658     # field type DB/PHP enum translations
    00659     public static $FieldTypeDBEnums = array(
    00660             MetadataSchema::MDFTYPE_TEXT             => "Text",
    00661             MetadataSchema::MDFTYPE_PARAGRAPH        => "Paragraph",
    00662             MetadataSchema::MDFTYPE_NUMBER           => "Number",
    00663             MetadataSchema::MDFTYPE_DATE             => "Date",
    00664             MetadataSchema::MDFTYPE_TIMESTAMP        => "TimeStamp",
    00665             MetadataSchema::MDFTYPE_FLAG             => "Flag",
    00666             MetadataSchema::MDFTYPE_TREE             => "Tree",
    00667             MetadataSchema::MDFTYPE_CONTROLLEDNAME   => "ControlledName",
    00668             MetadataSchema::MDFTYPE_OPTION           => "Option",
    00669             MetadataSchema::MDFTYPE_USER             => "User",
    00670             MetadataSchema::MDFTYPE_IMAGE            => "Still Image",
    00671             MetadataSchema::MDFTYPE_FILE             => "File",
    00672             MetadataSchema::MDFTYPE_URL              => "Url",
    00673             MetadataSchema::MDFTYPE_POINT            => "Point"
    00674             );
    00675     public static $FieldTypeDBAllowedEnums = array(
    00676             MetadataSchema::MDFTYPE_TEXT             => "Text",
    00677             MetadataSchema::MDFTYPE_PARAGRAPH        => "Paragraph",
    00678             MetadataSchema::MDFTYPE_NUMBER           => "Number",
    00679             MetadataSchema::MDFTYPE_DATE             => "Date",
    00680             MetadataSchema::MDFTYPE_FLAG             => "Flag",
    00681             MetadataSchema::MDFTYPE_TREE             => "Tree",
    00682             MetadataSchema::MDFTYPE_CONTROLLEDNAME   => "ControlledName",
    00683             MetadataSchema::MDFTYPE_OPTION           => "Option",
    00684             MetadataSchema::MDFTYPE_IMAGE            => "Still Image",
    00685             MetadataSchema::MDFTYPE_FILE             => "File",
    00686             MetadataSchema::MDFTYPE_URL              => "Url",
    00687             MetadataSchema::MDFTYPE_POINT            => "Point"
    00688             );
    00689     public static $FieldTypePHPEnums = array(
    00690             "Text"                   => MetadataSchema::MDFTYPE_TEXT,
    00691             "Paragraph"              => MetadataSchema::MDFTYPE_PARAGRAPH,
    00692             "Number"                 => MetadataSchema::MDFTYPE_NUMBER,
    00693             "Date"                   => MetadataSchema::MDFTYPE_DATE,
    00694             "TimeStamp"              => MetadataSchema::MDFTYPE_TIMESTAMP,
    00695             "Flag"                   => MetadataSchema::MDFTYPE_FLAG,
    00696             "Tree"                   => MetadataSchema::MDFTYPE_TREE,
    00697             "ControlledName"         => MetadataSchema::MDFTYPE_CONTROLLEDNAME,
    00698             "Option"                 => MetadataSchema::MDFTYPE_OPTION,
    00699             "User"                   => MetadataSchema::MDFTYPE_USER,
    00700             "Still Image"            => MetadataSchema::MDFTYPE_IMAGE,
    00701             "File"                   => MetadataSchema::MDFTYPE_FILE,
    00702             "Url"                    => MetadataSchema::MDFTYPE_URL,
    00703             "Point"                  => MetadataSchema::MDFTYPE_POINT
    00704             );
    00705
    00706
    00707     # object constructor (only for use by MetadataSchema object)
    00708     function MetadataField($FieldId, $FieldName = NULL, $FieldType = NULL,
    00709                            $Optional = TRUE, $DefaultValue = NULL)
    00710     {
    00711         # assume everything will be okay
    00712         $this->ErrorStatus = MetadataSchema::MDFSTAT_OK;
    00713
    00714         # grab our own database handle
    00715         $this->DB = new Database();
    00716         $DB = $this->DB;
    00717
    00718         # if field ID supplied
    00719         if ($FieldId != NULL)
    00720         {
    00721             # look up field in database
    00722             $DB->Query("SELECT * FROM MetadataFields WHERE FieldId = ".intval($FieldId));
    00723             $Record = $DB->FetchRow();
    00724         }
    00725
    00726         # if no field ID supplied or if record not found in database
    00727         if (($FieldId == NULL) || ($Record == NULL))
    00728         {
    00729             # error out if valid field type not supplied
    00730             if (empty(MetadataField::$FieldTypeDBEnums[$FieldType]))
    00731             {
    00732                 $this->ErrorStatus = MetadataSchema::MDFSTAT_FIELDDOESNOTEXIST;
    00733                 return;
    00734             }
    00735
    00736             # if field name supplied
    00737             $FieldName = trim($FieldName);
    00738             if (strlen($FieldName) > 0)
    00739             {
    00740                 # error out if field name is duplicate
    00741                 $DuplicateCount = $DB->Query(
    00742                         "SELECT COUNT(*) AS RecordCount FROM MetadataFields "
    00743                             ."WHERE FieldName = '".addslashes($FieldName)."'",
    00744                         "RecordCount");
    00745                 if ($DuplicateCount > 0)
    00746                 {
    00747                     $this->ErrorStatus = MetadataSchema::MDFSTAT_DUPLICATENAME;
    00748                     return;
    00749                 }
    00750             }
    00751
    00752             # grab current user ID
    00753             global $G_User;
    00754             $UserId = $G_User->Get("UserId");
    00755
    00756             # lock DB tables and get next temporary field ID
    00757             $Schema = new MetadataSchema();
    00758             $DB->Query("LOCK TABLES MetadataFields WRITE");
    00759             $FieldId = $Schema->GetNextTempItemId();
    00760
    00761             # add field to MDF table in database
    00762             $DB->Query("INSERT INTO MetadataFields "
    00763                   ."(FieldId, FieldName, FieldType, Optional, DefaultValue, LastModifiedById) VALUES "
    00764                   ."(".intval($FieldId).", "
    00765                   ."'".addslashes($FieldName)."', "
    00766                   ."'".MetadataField::$FieldTypeDBEnums[$FieldType]."', "
    00767                   .($Optional ? 1 : 0).", "
    00768                   ."'".addslashes($DefaultValue)."',"
    00769                   ."'".$UserId."')");
    00770
    00771             # release DB tables
    00772             $DB->Query("UNLOCK TABLES");
    00773
    00774             # re-read record from database
    00775             $DB->Query("SELECT * FROM MetadataFields WHERE FieldId = "
    00776                     .intval($FieldId));
    00777             $this->DBFields = $DB->FetchRow();
    00778             $this->DBFields["DBFieldName"] =
    00779                     $this->NormalizeFieldNameForDB($this->DBFields["FieldName"]);
    00780
    00781             # set field order values for new field
    00782             $FieldCount = $DB->Query("SELECT COUNT(*) AS FieldCount FROM MetadataFields", "FieldCount");
    00783             $this->OrderPosition(MetadataSchema::MDFORDER_DISPLAY,
    00784                                  ($FieldCount + 1));
    00785             $this->OrderPosition(MetadataSchema::MDFORDER_EDITING,
    00786                                  ($FieldCount + 1));
    00787         }
    00788         else
    00789         {
    00790             # save values locally
    00791             $this->DBFields = $Record;
    00792             $this->DBFields["DBFieldName"] =
    00793                     $this->NormalizeFieldNameForDB($Record["FieldName"]);
    00794         }
    00795     }
    00796
    00797     # remove field from database (only for use by MetadataSchema object)
    00798     function Drop()
    00799     {
    00800         # clear other database entries as appropriate for field type
    00801         $DB = $this->DB;
    00802         $DBFieldName = $this->DBFields["DBFieldName"];
    00803         switch (MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]])
    00804         {
    00805             case MetadataSchema::MDFTYPE_TEXT:
    00806             case MetadataSchema::MDFTYPE_PARAGRAPH:
    00807             case MetadataSchema::MDFTYPE_NUMBER:
    00808             case MetadataSchema::MDFTYPE_USER:
    00809             case MetadataSchema::MDFTYPE_IMAGE:
    00810             case MetadataSchema::MDFTYPE_TIMESTAMP:
    00811             case MetadataSchema::MDFTYPE_URL:
    00812                 # remove field from resources table
    00813                 if ($DB->FieldExists("Resources", $DBFieldName))
    00814                 {
    00815                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."`");
    00816                 }
    00817                 break;
    00818
    00819             case MetadataSchema::MDFTYPE_POINT:
    00820                 if ($DB->FieldExists("Resources", $DBFieldName."X"))
    00821                 {
    00822                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."X`");
    00823                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Y`");
    00824                 }
    00825                 break;
    00826
    00827             case MetadataSchema::MDFTYPE_FLAG:
    00828                 # remove field from resources table
    00829                 if ($DB->FieldExists("Resources", $DBFieldName))
    00830                 {
    00831                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."`");
    00832                 }
    00833
    00834                 # remove field from saved user searches table
    00835                 if ($DB->FieldExists("UserSearch", $DBFieldName))
    00836                 {
    00837                     $DB->Query("ALTER TABLE UserSearch DROP COLUMN `".$DBFieldName."`");
    00838                 }
    00839                 break;
    00840
    00841             case MetadataSchema::MDFTYPE_DATE:
    00842                 # remove fields from resources table
    00843                 if ($DB->FieldExists("Resources", $DBFieldName."Begin"))
    00844                 {
    00845                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Begin`");
    00846                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."End`");
    00847                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Precision`");
    00848                 }
    00849                 break;
    00850
    00851             case MetadataSchema::MDFTYPE_TREE:
    00852                 $DB->Query("SELECT ClassificationId FROM Classifications "
    00853                            ."WHERE FieldId = ".$this->Id());
    00854                 $TempDB = new SPTDatabase();
    00855                 while ($ClassificationId = $DB->FetchField("ClassificationId"))
    00856                 {
    00857                     # remove any resource / name intersections
    00858                     $TempDB->Query("DELETE FROM ResourceClassInts WHERE "
    00859                                    ."ClassificationId = ".$ClassificationId);
    00860
    00861                     # remove controlled name
    00862                     $TempDB->Query("DELETE FROM Classifications WHERE "
    00863                                    ."ClassificationId = ".$ClassificationId);
    00864                 }
    00865                 break;
    00866
    00867             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00868             case MetadataSchema::MDFTYPE_OPTION:
    00869                 $DB->Query("SELECT ControlledNameId FROM ControlledNames "
    00870                            ."WHERE FieldId = ".$this->Id());
    00871                 $TempDB = new SPTDatabase();
    00872                 while ($ControlledNameId = $DB->FetchField("ControlledNameId"))
    00873                 {
    00874                     # remove any resource / name intersections
    00875                     $TempDB->Query("DELETE FROM ResourceNameInts WHERE "
    00876                                    ."ControlledNameId = ".$ControlledNameId);
    00877
    00878                     # remove any variant names
    00879                     $TempDB->Query("DELETE FROM VariantNames WHERE "
    00880                                    ."ControlledNameId = ".$ControlledNameId);
    00881
    00882                     # remove controlled name
    00883                     $TempDB->Query("DELETE FROM ControlledNames WHERE "
    00884                                    ."ControlledNameId = ".$ControlledNameId);
    00885                 }
    00886
    00887                 # drop entry from saved user searches table (if type is option)
    00888                 if ($DB->FieldExists("UserSearch", $DBFieldName))
    00889                 {
    00890                     $DB->Query("ALTER TABLE UserSearch DROP COLUMN `".$DBFieldName."`");
    00891                 }
    00892                 break;
    00893
    00894             case MetadataSchema::MDFTYPE_FILE:
    00895                 # for each file associated with this field
    00896                 $DB->Query("SELECT FileId FROM Files WHERE FieldId = '".$this->Id()."'");
    00897                 while ($FileId = $DB->FetchRow())
    00898                 {
    00899                     # delete file
    00900                     $File = new File(intval($FileId));
    00901                     $File->Delete();
    00902                 }
    00903                 break;
    00904         }
    00905
    00906         # remove field from database
    00907         $DB->Query("DELETE FROM MetadataFields "
    00908                    ."WHERE FieldId = '".$this->DBFields["FieldId"]."'");
    00909
    00910         # remove any qualifier associations
    00911         $DB->Query("DELETE FROM FieldQualifierInts WHERE MetadataFieldId = '"
    00912                    .$this->DBFields["FieldId"]."'");
    00913     }
    00914
    00915     # modify any database fields
    00916     function ModifyField($NewName = NULL, $NewType = NULL)
    00917     {
    00918         # grab old DB field name
    00919         $OldDBFieldName = $this->DBFields["DBFieldName"];
    00920         $OldFieldType = NULL;
    00921
    00922         # if new field name supplied
    00923         if ($NewName != NULL)
    00924         {
    00925             # cache the old name for options and controllednames below
    00926             $OldName = $this->DBFields["FieldName"];
    00927
    00928             # store new name
    00929             $this->UpdateValue("FieldName", $NewName);
    00930
    00931             # determine new DB field name
    00932             $NewDBFieldName = $this->NormalizeFieldNameForDB($NewName);
    00933
    00934             # store new database field name
    00935             $this->DBFields["DBFieldName"] = $NewDBFieldName;
    00936         }
    00937         else
    00938         {
    00939             # set new field name equal to old field name
    00940             $NewDBFieldName = $OldDBFieldName;
    00941         }
    00942
    00943         # if new type supplied
    00944         if ($NewType != NULL)
    00945         {
    00946             # grab old field type
    00947             $OldFieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
    00948
    00949             # store new field type
    00950             $this->UpdateValue("FieldType", MetadataField::$FieldTypeDBEnums[$NewType]);
    00951         }
    00952
    00953         # if this is not a temporary field
    00954         if ($this->Id() >= 0)
    00955         {
    00956             # modify field in DB as appropriate for field type
    00957             $DB = $this->DB;
    00958             $FieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
    00959             switch ($FieldType)
    00960             {
    00961                 case MetadataSchema::MDFTYPE_TEXT:
    00962                 case MetadataSchema::MDFTYPE_PARAGRAPH:
    00963                 case MetadataSchema::MDFTYPE_URL:
    00964                     # alter field declaration in Resources table
    00965                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00966                                .$OldDBFieldName."` `"
    00967                                .$NewDBFieldName."` TEXT "
    00968                                .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    00969                     break;
    00970
    00971                 case MetadataSchema::MDFTYPE_NUMBER:
    00972                 case MetadataSchema::MDFTYPE_USER:
    00973                     # alter field declaration in Resources table
    00974                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00975                                .$OldDBFieldName."` `"
    00976                                .$NewDBFieldName."` INT "
    00977                                .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    00978                     break;
    00979
    00980                 case MetadataSchema::MDFTYPE_POINT:
    00981                     $Precision = $this->UpdateValue("PointPrecision",
    00982                                                     DB_NOVALUE);
    00983                     $Digits    = $this->UpdateValue("PointDecimalDigits",
    00984                                                     DB_NOVALUE);
    00985
    00986                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
    00987                                ."`".$OldDBFieldName."X` "
    00988                                ."`".$NewDBFieldName."X`".
    00989                                " DECIMAL(".$Precision.",".$Digits.")");
    00990                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
    00991                                ."`".$OldDBFieldName."Y` "
    00992                                ."`".$NewDBFieldName."Y`".
    00993                                " DECIMAL(".$Precision.",".$Digits.")");
    00994                     break;
    00995
    00996                 case MetadataSchema::MDFTYPE_FILE:
    00997                     # alter field declaration in Resources table
    00998                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00999                                .$OldDBFieldName."` `"
    01000                                .$NewDBFieldName."` TEXT");
    01001                     break;
    01002
    01003                 case MetadataSchema::MDFTYPE_IMAGE:
    01004                     # alter field declaration in Resources table
    01005                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01006                                .$OldDBFieldName."` `"
    01007                                .$NewDBFieldName."` INT");
    01008                     break;
    01009
    01010                 case MetadataSchema::MDFTYPE_FLAG:
    01011                     # alter field declaration in Resources table
    01012                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01013                                .$OldDBFieldName."` `"
    01014                                .$NewDBFieldName."` INT "
    01015                                .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01016
    01017                     # if old type was flag
    01018                     if ($OldFieldType == MetadataSchema::MDFTYPE_FLAG)
    01019                     {
    01020                         # alter field declaration in saved user searches table
    01021                         $DB->Query("ALTER TABLE UserSearch CHANGE COLUMN `"
    01022                                    .$OldDBFieldName."` `"
    01023                                    .$NewDBFieldName."` INT "
    01024                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01025                     }
    01026                     break;
    01027
    01028                 case MetadataSchema::MDFTYPE_DATE:
    01029                     # if new type supplied and new type is different from old
    01030                     if (($NewType != NULL) && ($NewType != $OldFieldType))
    01031                     {
    01032                         # if old type was time stamp
    01033                         if ($OldFieldType == MetadataSchema::MDFTYPE_TIMESTAMP)
    01034                         {
    01035                             # change time stamp field in resources table to begin date
    01036                             $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01037                                        .$OldDBFieldName."` `"
    01038                                        .$NewDBFieldName."Begin` DATE "
    01039                                        .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01040
    01041                             # add end date and precision fields
    01042                             $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."End"
    01043                                        ."` DATE");
    01044                             $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."Precision`"
    01045                                        ." INT ".($Optional ? "" : "NOT NULL"));
    01046
    01047                             # set precision to reflect time stamp content
    01048                             $DB->Query("UPDATE Resources SET `".$NewDBFieldName."Precision` = "
    01049                                        .(DATEPRE_BEGINYEAR|DATEPRE_BEGINMONTH|DATEPRE_BEGINDAY));
    01050                         }
    01051                         else
    01052                         {
    01053                             exit("<br>ERROR:  Attempt to convert metadata field to date from type other than timestamp<br>\n");
    01054                         }
    01055                     }
    01056                     else
    01057                     {
    01058                         # change name of fields
    01059                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01060                                    .$OldDBFieldName."Begin` `"
    01061                                    .$NewDBFieldName."Begin` DATE "
    01062                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01063                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01064                                    .$OldDBFieldName."End` `"
    01065                                    .$NewDBFieldName."End` DATE "
    01066                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01067                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01068                                    .$OldDBFieldName."Precision` `"
    01069                                    .$NewDBFieldName."Precision` INT "
    01070                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01071                     }
    01072                     break;
    01073
    01074                 case MetadataSchema::MDFTYPE_TIMESTAMP:
    01075                     # if new type supplied and new type is different from old
    01076                     if (($NewType != NULL) && ($NewType != $OldFieldType))
    01077                     {
    01078                         # if old type was date
    01079                         if ($OldFieldType == MetadataSchema::MDFTYPE_DATE)
    01080                         {
    01081                             # change begin date field in resource table to time stamp
    01082                             $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01083                                        .$OldDBFieldName."Begin` `"
    01084                                        .$NewDBFieldName."` DATETIME "
    01085                                        .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01086
    01087                             # drop end date and precision fields
    01088                             $DB->Query("ALTER TABLE Resources DROP COLUMN `"
    01089                                        .$OldDBFieldName."End`");
    01090                             $DB->Query("ALTER TABLE Resources DROP COLUMN `"
    01091                                        .$OldDBFieldName."Precision`");
    01092                         }
    01093                         else
    01094                         {
    01095                             exit("<br>ERROR:  Attempt to convert metadata field to time stamp from type other than date<br>\n");
    01096                         }
    01097                     }
    01098                     else
    01099                     {
    01100                         # change name of field
    01101                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01102                                    .$OldDBFieldName."` `"
    01103                                    .$NewDBFieldName."` DATETIME "
    01104                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01105                     }
    01106                     break;
    01107
    01108                 case MetadataSchema::MDFTYPE_TREE:
    01109                     break;
    01110
    01111                 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    01112                     if ($OldFieldType == MetadataSchema::MDFTYPE_OPTION)
    01113                     {
    01114                         $DB->Query("ALTER TABLE UserSearch DROP COLUMN `".
    01115                             $OldDBFieldName."`");
    01116                     }
    01117                     break;
    01118
    01119                 case MetadataSchema::MDFTYPE_OPTION:
    01120                     if ($OldFieldType == MetadataSchema::MDFTYPE_CONTROLLEDNAME)
    01121                     {
    01122                         if (!$DB->FieldExists("UserSearch", $NewDBFieldName))
    01123                         {
    01124                             $DB->Query("ALTER TABLE UserSearch ADD COLUMN `".
    01125                                 $NewDBFieldName."` TEXT ".
    01126                                 ($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01127                         }
    01128                     }
    01129                     else
    01130                     {
    01131                         # alter field declaration in saved user searches table
    01132                         $DB->Query("ALTER TABLE UserSearch CHANGE COLUMN `"
    01133                            .$OldDBFieldName."` `"
    01134                            .$NewDBFieldName."` TEXT "
    01135                            .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01136                         break;
    01137                     }
    01138             }
    01139
    01140             # if qualifier DB field exists
    01141             if ($DB->FieldExists("Resources", $OldDBFieldName."Qualifier"))
    01142             {
    01143                 # rename qualifier DB field
    01144                 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01145                            .$OldDBFieldName."Qualifier` `"
    01146                            .$NewDBFieldName."Qualifier` INT ");
    01147             }
    01148         }
    01149     }
    01150
    01151     # convenience function to supply parameters to Database->UpdateValue()
    01152     function UpdateValue($FieldName, $NewValue)
    01153     {
    01154         return $this->DB->UpdateValue("MetadataFields", $FieldName, $NewValue,
    01155                                "FieldId = ".$this->DBFields["FieldId"],
    01156                                $this->DBFields);
    01157     }
    01158
    01159     # normalize field name for use as database field name
    01160     function NormalizeFieldNameForDB($Name)
    01161     {
    01162         return preg_replace("/[^a-z0-9]/i", "", $Name);
    01163     }
    01164
    01165     # add any needed database fields and/or entries
    01166     function AddDatabaseFields()
    01167     {
    01168         # grab values for common use
    01169         $DB = $this->DB;
    01170         $FieldName = $this->Name();
    01171         $DBFieldName = $this->DBFieldName();
    01172         $Optional = $this->Optional();
    01173         $DefaultValue = $this->DefaultValue();
    01174
    01175         # set up field(s) based on field type
    01176         switch ($this->Type())
    01177         {
    01178             case MetadataSchema::MDFTYPE_TEXT:
    01179             case MetadataSchema::MDFTYPE_PARAGRAPH:
    01180             case MetadataSchema::MDFTYPE_URL:
    01181                 # add field to resources table (if not already present)
    01182                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01183                 {
    01184                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01185                                ."` TEXT ".($Optional ? "" : "NOT NULL"));
    01186                 }
    01187
    01188                 # if default value supplied
    01189                 if ($DefaultValue != NULL)
    01190                 {
    01191                     # set all existing records to default value
    01192                     $DB->Query("UPDATE Resources SET `"
    01193                                .$DBFieldName."` = '".addslashes($DefaultValue)."'");
    01194                 }
    01195                 break;
    01196
    01197             case MetadataSchema::MDFTYPE_NUMBER:
    01198                 # add field to resources table (if not already present)
    01199                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01200                 {
    01201                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01202                                ."` INT ".($Optional ? "" : "NOT NULL"));
    01203                 }
    01204
    01205                 # if default value supplied
    01206                 if ($DefaultValue != NULL)
    01207                 {
    01208                     # set all existing records to default value
    01209                     $DB->Query("UPDATE Resources SET `"
    01210                                .$DBFieldName."` = '".addslashes($DefaultValue)."'");
    01211                 }
    01212                 break;
    01213
    01214             case MetadataSchema::MDFTYPE_POINT:
    01215                 if (!$DB->FieldExists("Resources", $DBFieldName."X"))
    01216                 {
    01217                     $Precision = $this->UpdateValue("PointPrecision",
    01218                                                     DB_NOVALUE);
    01219                     $Digits    = $this->UpdateValue("PointDecimalDigits",
    01220                                                     DB_NOVALUE);
    01221
    01222                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01223                                .$DBFieldName."X`".
    01224                                " DECIMAL(".$Precision.",".$Digits.")");
    01225                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01226                                .$DBFieldName."Y`".
    01227                                " DECIMAL(".$Precision.",".$Digits.")");
    01228                 }
    01229
    01230                 break;
    01231             case MetadataSchema::MDFTYPE_FLAG:
    01232                 # add field to resources table (if not already present)
    01233                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01234                 {
    01235                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01236                                ."` INT ".($Optional ? "" : "NOT NULL"));
    01237                 }
    01238
    01239                 # add field to saved user searches table (if not already present)
    01240                 if (!$DB->FieldExists("UserSearch", $DBFieldName))
    01241                 {
    01242                     $DB->Query("ALTER TABLE UserSearch ADD COLUMN `".$DBFieldName
    01243                                ."` INT ".($Optional ? "" : "NOT NULL"));
    01244                 }
    01245
    01246                 # if default value supplied
    01247                 if ($DefaultValue != NULL)
    01248                 {
    01249                     # set all existing records to default value
    01250                     $DB->Query("UPDATE Resources SET `"
    01251                                .$DBFieldName."` = '".addslashes($DefaultValue)."'");
    01252                 }
    01253                 break;
    01254
    01255             case MetadataSchema::MDFTYPE_USER:
    01256                 # add field to resources table (if not already present)
    01257                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01258                 {
    01259                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01260                                ."` INT ".($Optional ? "" : "NOT NULL"));
    01261                 }
    01262                 break;
    01263
    01264             case MetadataSchema::MDFTYPE_FILE:
    01265                 # add fields to resources table (if not already present)
    01266                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01267                 {
    01268                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01269                                .$DBFieldName."` TEXT");
    01270                 }
    01271                 break;
    01272
    01273             case MetadataSchema::MDFTYPE_IMAGE:
    01274                 # add fields to resources table (if not already present)
    01275                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01276                 {
    01277                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01278                                .$DBFieldName."` INT");
    01279                 }
    01280                 break;
    01281
    01282             case MetadataSchema::MDFTYPE_DATE:
    01283                 # add fields to resources table (if not already present)
    01284                 if (!$DB->FieldExists("Resources", $DBFieldName."Begin"))
    01285                 {
    01286                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Begin`"
    01287                                ." DATE ".($Optional ? "" : "NOT NULL"));
    01288                 }
    01289                 if (!$DB->FieldExists("Resources", $DBFieldName."End"))
    01290                 {
    01291                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."End`"
    01292                                ." DATE");
    01293                 }
    01294                 if (!$DB->FieldExists("Resources", $DBFieldName."Precision"))
    01295                 {
    01296                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Precision`"
    01297                                ." INT ".($Optional ? "" : "NOT NULL"));
    01298                 }
    01299                 break;
    01300
    01301             case MetadataSchema::MDFTYPE_TIMESTAMP:
    01302                 # add fields to resources table (if not already present)
    01303                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01304                 {
    01305                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01306                                ."` DATETIME ".($Optional ? "" : "NOT NULL"));
    01307                 }
    01308                 break;
    01309
    01310             case MetadataSchema::MDFTYPE_TREE:
    01311                 break;
    01312
    01313             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    01314                 break;
    01315
    01316             case MetadataSchema::MDFTYPE_OPTION:
    01317                 # add field to saved user searches table (if not already present)
    01318                 if (!$DB->FieldExists("UserSearch", $DBFieldName))
    01319                 {
    01320                     $DB->Query("ALTER TABLE UserSearch ADD COLUMN `".$DBFieldName
    01321                                ."` TEXT ".($Optional ? "" : "NOT NULL"));
    01322                 }
    01323                 break;
    01324
    01325             default:
    01326                 exit("<br>ERROR:  Attempt to add database fields for illegal metadata field type<br>\n");
    01327                 break;
    01328         }
    01329     }
    01330
    01331     # get/set field order positions
    01332     function OrderPosition($OrderType, $NewValue = DB_NOVALUE)
    01333     {
    01334         switch ($OrderType)
    01335         {
    01336             case MetadataSchema::MDFORDER_DISPLAY:
    01337                 return $this->UpdateValue("DisplayOrderPosition", $NewValue);
    01338                 break;
    01339
    01340             case MetadataSchema::MDFORDER_EDITING:
    01341                 return $this->UpdateValue("EditingOrderPosition", $NewValue);
    01342                 break;
    01343
    01344             default:
    01345                 exit("invalid order type passed to MetadataField::OrderPosition");
    01346                 break;
    01347         }
    01348     }
    01349 }
    01350
    01351
    01352 ?>

Copyright 2009 Internet Scout