Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- File List
- File Members

# MetadataField.php

Go to the documentation of this file.

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