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_USER             => "User",
    00666             MetadataSchema::MDFTYPE_IMAGE            => "Still Image",
    00667             MetadataSchema::MDFTYPE_FILE             => "File",
    00668             MetadataSchema::MDFTYPE_URL              => "Url",
    00669             MetadataSchema::MDFTYPE_POINT            => "Point"
    00670             );
    00671     public static $FieldTypePHPEnums = array(
    00672             "Text"                   => MetadataSchema::MDFTYPE_TEXT,
    00673             "Paragraph"              => MetadataSchema::MDFTYPE_PARAGRAPH,
    00674             "Number"                 => MetadataSchema::MDFTYPE_NUMBER,
    00675             "Date"                   => MetadataSchema::MDFTYPE_DATE,
    00676             "TimeStamp"              => MetadataSchema::MDFTYPE_TIMESTAMP,
    00677             "Flag"                   => MetadataSchema::MDFTYPE_FLAG,
    00678             "Tree"                   => MetadataSchema::MDFTYPE_TREE,
    00679             "ControlledName"         => MetadataSchema::MDFTYPE_CONTROLLEDNAME,
    00680             "Option"                 => MetadataSchema::MDFTYPE_OPTION,
    00681             "User"                   => MetadataSchema::MDFTYPE_USER,
    00682             "Still Image"            => MetadataSchema::MDFTYPE_IMAGE,
    00683             "File"                   => MetadataSchema::MDFTYPE_FILE,
    00684             "Url"                    => MetadataSchema::MDFTYPE_URL,
    00685             "Point"                  => MetadataSchema::MDFTYPE_POINT
    00686             );
    00687
    00688     public static $UpdateTypes = array(
    00689         MetadataField::UPDATEMETHOD_NOAUTOUPDATE   => "Do not update automatically",
    00690         MetadataField::UPDATEMETHOD_ONRECORDCREATE => "Update on record creation",
    00691         MetadataField::UPDATEMETHOD_BUTTON         => "Provide an update button",
    00692         MetadataField::UPDATEMETHOD_ONRECORDEDIT   => "Update when record is edited",
    00693         MetadataField::UPDATEMETHOD_ONRECORDCHANGE => "Update when record is changed"
    00694         );
    00695
    00696
    00697     # object constructor (only for use by MetadataSchema object)
    00698     function MetadataField($FieldId, $FieldName = NULL, $FieldType = NULL,
    00699                            $Optional = TRUE, $DefaultValue = NULL)
    00700     {
    00701         # assume everything will be okay
    00702         $this->ErrorStatus = MetadataSchema::MDFSTAT_OK;
    00703
    00704         # grab our own database handle
    00705         $this->DB = new Database();
    00706         $DB = $this->DB;
    00707
    00708         # if field ID supplied
    00709         if ($FieldId != NULL)
    00710         {
    00711             # look up field in database
    00712             $DB->Query("SELECT * FROM MetadataFields WHERE FieldId = ".intval($FieldId));
    00713             $Record = $DB->FetchRow();
    00714         }
    00715
    00716         # if no field ID supplied or if record not found in database
    00717         if (($FieldId == NULL) || ($Record == NULL))
    00718         {
    00719             # error out if valid field type not supplied
    00720             if (empty(MetadataField::$FieldTypeDBEnums[$FieldType]))
    00721             {
    00722                 $this->ErrorStatus = MetadataSchema::MDFSTAT_FIELDDOESNOTEXIST;
    00723                 return;
    00724             }
    00725
    00726             # if field name supplied
    00727             $FieldName = trim($FieldName);
    00728             if (strlen($FieldName) > 0)
    00729             {
    00730                 # error out if field name is duplicate
    00731                 $DuplicateCount = $DB->Query(
    00732                         "SELECT COUNT(*) AS RecordCount FROM MetadataFields "
    00733                             ."WHERE FieldName = '".addslashes($FieldName)."'",
    00734                         "RecordCount");
    00735                 if ($DuplicateCount > 0)
    00736                 {
    00737                     $this->ErrorStatus = MetadataSchema::MDFSTAT_DUPLICATENAME;
    00738                     return;
    00739                 }
    00740             }
    00741
    00742             # grab current user ID
    00743             global $G_User;
    00744             $UserId = $G_User->Get("UserId");
    00745
    00746             # lock DB tables and get next temporary field ID
    00747             $Schema = new MetadataSchema();
    00748             $DB->Query("LOCK TABLES MetadataFields WRITE");
    00749             $FieldId = $Schema->GetNextTempItemId();
    00750
    00751             # add field to MDF table in database
    00752             $DB->Query("INSERT INTO MetadataFields "
    00753                   ."(FieldId, FieldName, FieldType, Optional, DefaultValue, LastModifiedById) VALUES "
    00754                   ."(".intval($FieldId).", "
    00755                   ."'".addslashes($FieldName)."', "
    00756                   ."'".MetadataField::$FieldTypeDBEnums[$FieldType]."', "
    00757                   .($Optional ? 1 : 0).", "
    00758                   ."'".addslashes($DefaultValue)."',"
    00759                   ."'".$UserId."')");
    00760
    00761             # release DB tables
    00762             $DB->Query("UNLOCK TABLES");
    00763
    00764             # re-read record from database
    00765             $DB->Query("SELECT * FROM MetadataFields WHERE FieldId = "
    00766                     .intval($FieldId));
    00767             $this->DBFields = $DB->FetchRow();
    00768             $this->DBFields["DBFieldName"] =
    00769                     $this->NormalizeFieldNameForDB($this->DBFields["FieldName"]);
    00770
    00771             # set field order values for new field
    00772             $FieldCount = $DB->Query("SELECT COUNT(*) AS FieldCount FROM MetadataFields", "FieldCount");
    00773             $this->OrderPosition(MetadataSchema::MDFORDER_DISPLAY,
    00774                                  ($FieldCount + 1));
    00775             $this->OrderPosition(MetadataSchema::MDFORDER_EDITING,
    00776                                  ($FieldCount + 1));
    00777         }
    00778         else
    00779         {
    00780             # save values locally
    00781             $this->DBFields = $Record;
    00782             $this->DBFields["DBFieldName"] =
    00783                     $this->NormalizeFieldNameForDB($Record["FieldName"]);
    00784         }
    00785     }
    00786
    00787     # remove field from database (only for use by MetadataSchema object)
    00788     function Drop()
    00789     {
    00790         # clear other database entries as appropriate for field type
    00791         $DB = $this->DB;
    00792         $DBFieldName = $this->DBFields["DBFieldName"];
    00793         switch (MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]])
    00794         {
    00795             case MetadataSchema::MDFTYPE_TEXT:
    00796             case MetadataSchema::MDFTYPE_PARAGRAPH:
    00797             case MetadataSchema::MDFTYPE_NUMBER:
    00798             case MetadataSchema::MDFTYPE_USER:
    00799             case MetadataSchema::MDFTYPE_IMAGE:
    00800             case MetadataSchema::MDFTYPE_TIMESTAMP:
    00801             case MetadataSchema::MDFTYPE_URL:
    00802                 # remove field from resources table
    00803                 if ($DB->FieldExists("Resources", $DBFieldName))
    00804                 {
    00805                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."`");
    00806                 }
    00807                 break;
    00808
    00809             case MetadataSchema::MDFTYPE_POINT:
    00810                 if ($DB->FieldExists("Resources", $DBFieldName."X"))
    00811                 {
    00812                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."X`");
    00813                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Y`");
    00814                 }
    00815                 break;
    00816
    00817             case MetadataSchema::MDFTYPE_FLAG:
    00818                 # remove field from resources table
    00819                 if ($DB->FieldExists("Resources", $DBFieldName))
    00820                 {
    00821                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."`");
    00822                 }
    00823                 break;
    00824
    00825             case MetadataSchema::MDFTYPE_DATE:
    00826                 # remove fields from resources table
    00827                 if ($DB->FieldExists("Resources", $DBFieldName."Begin"))
    00828                 {
    00829                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Begin`");
    00830                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."End`");
    00831                     $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Precision`");
    00832                 }
    00833                 break;
    00834
    00835             case MetadataSchema::MDFTYPE_TREE:
    00836                 $DB->Query("SELECT ClassificationId FROM Classifications "
    00837                            ."WHERE FieldId = ".$this->Id());
    00838                 $TempDB = new SPTDatabase();
    00839                 while ($ClassificationId = $DB->FetchField("ClassificationId"))
    00840                 {
    00841                     # remove any resource / name intersections
    00842                     $TempDB->Query("DELETE FROM ResourceClassInts WHERE "
    00843                                    ."ClassificationId = ".$ClassificationId);
    00844
    00845                     # remove controlled name
    00846                     $TempDB->Query("DELETE FROM Classifications WHERE "
    00847                                    ."ClassificationId = ".$ClassificationId);
    00848                 }
    00849                 break;
    00850
    00851             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    00852             case MetadataSchema::MDFTYPE_OPTION:
    00853                 $DB->Query("SELECT ControlledNameId FROM ControlledNames "
    00854                            ."WHERE FieldId = ".$this->Id());
    00855                 $TempDB = new SPTDatabase();
    00856                 while ($ControlledNameId = $DB->FetchField("ControlledNameId"))
    00857                 {
    00858                     # remove any resource / name intersections
    00859                     $TempDB->Query("DELETE FROM ResourceNameInts WHERE "
    00860                                    ."ControlledNameId = ".$ControlledNameId);
    00861
    00862                     # remove any variant names
    00863                     $TempDB->Query("DELETE FROM VariantNames WHERE "
    00864                                    ."ControlledNameId = ".$ControlledNameId);
    00865
    00866                     # remove controlled name
    00867                     $TempDB->Query("DELETE FROM ControlledNames WHERE "
    00868                                    ."ControlledNameId = ".$ControlledNameId);
    00869                 }
    00870                 break;
    00871
    00872             case MetadataSchema::MDFTYPE_FILE:
    00873                 # for each file associated with this field
    00874                 $DB->Query("SELECT FileId FROM Files WHERE FieldId = '".$this->Id()."'");
    00875                 while ($FileId = $DB->FetchRow())
    00876                 {
    00877                     # delete file
    00878                     $File = new File(intval($FileId));
    00879                     $File->Delete();
    00880                 }
    00881                 break;
    00882         }
    00883
    00884         # remove field from database
    00885         $DB->Query("DELETE FROM MetadataFields "
    00886                    ."WHERE FieldId = '".$this->DBFields["FieldId"]."'");
    00887
    00888         # remove any qualifier associations
    00889         $DB->Query("DELETE FROM FieldQualifierInts WHERE MetadataFieldId = '"
    00890                    .$this->DBFields["FieldId"]."'");
    00891     }
    00892
    00893     # modify any database fields
    00894     function ModifyField($NewName = NULL, $NewType = NULL)
    00895     {
    00896         # grab old DB field name
    00897         $OldDBFieldName = $this->DBFields["DBFieldName"];
    00898         $OldFieldType = NULL;
    00899
    00900         # if new field name supplied
    00901         if ($NewName != NULL)
    00902         {
    00903             # cache the old name for options and controllednames below
    00904             $OldName = $this->DBFields["FieldName"];
    00905
    00906             # store new name
    00907             $this->UpdateValue("FieldName", $NewName);
    00908
    00909             # determine new DB field name
    00910             $NewDBFieldName = $this->NormalizeFieldNameForDB($NewName);
    00911
    00912             # store new database field name
    00913             $this->DBFields["DBFieldName"] = $NewDBFieldName;
    00914         }
    00915         else
    00916         {
    00917             # set new field name equal to old field name
    00918             $NewDBFieldName = $OldDBFieldName;
    00919         }
    00920
    00921         # if new type supplied
    00922         if ($NewType != NULL)
    00923         {
    00924             # grab old field type
    00925             $OldFieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
    00926
    00927             # store new field type
    00928             $this->UpdateValue("FieldType", MetadataField::$FieldTypeDBEnums[$NewType]);
    00929         }
    00930
    00931         # if this is not a temporary field
    00932         if ($this->Id() >= 0)
    00933         {
    00934             # modify field in DB as appropriate for field type
    00935             $DB = $this->DB;
    00936             $FieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
    00937             switch ($FieldType)
    00938             {
    00939                 case MetadataSchema::MDFTYPE_TEXT:
    00940                 case MetadataSchema::MDFTYPE_PARAGRAPH:
    00941                 case MetadataSchema::MDFTYPE_URL:
    00942                     # alter field declaration in Resources table
    00943                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00944                                .$OldDBFieldName."` `"
    00945                                .$NewDBFieldName."` TEXT "
    00946                                .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    00947                     break;
    00948
    00949                 case MetadataSchema::MDFTYPE_NUMBER:
    00950                 case MetadataSchema::MDFTYPE_USER:
    00951                     # alter field declaration in Resources table
    00952                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00953                                .$OldDBFieldName."` `"
    00954                                .$NewDBFieldName."` INT "
    00955                                .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    00956                     break;
    00957
    00958                 case MetadataSchema::MDFTYPE_POINT:
    00959                     $Precision = $this->UpdateValue("PointPrecision",
    00960                                                     DB_NOVALUE);
    00961                     $Digits    = $this->UpdateValue("PointDecimalDigits",
    00962                                                     DB_NOVALUE);
    00963                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
    00964                                ."`".$OldDBFieldName."X` "
    00965                                ."`".$NewDBFieldName."X`".
    00966                                " DECIMAL(".$Precision.",".$Digits.")");
    00967                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
    00968                                ."`".$OldDBFieldName."Y` "
    00969                                ."`".$NewDBFieldName."Y`".
    00970                                " DECIMAL(".$Precision.",".$Digits.")");
    00971                     break;
    00972
    00973                 case MetadataSchema::MDFTYPE_FILE:
    00974                     # if DB field name has changed
    00975                     if ($NewDBFieldName != $OldDBFieldName)
    00976                     {
    00977                         # alter field declaration in Resources table
    00978                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00979                                    .$OldDBFieldName."` `"
    00980                                    .$NewDBFieldName."` TEXT");
    00981                     }
    00982                     break;
    00983
    00984                 case MetadataSchema::MDFTYPE_IMAGE:
    00985                     # if DB field name has changed
    00986                     if ($NewDBFieldName != $OldDBFieldName)
    00987                     {
    00988                         # alter field declaration in Resources table
    00989                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00990                                    .$OldDBFieldName."` `"
    00991                                    .$NewDBFieldName."` INT");
    00992                     }
    00993                     break;
    00994
    00995                 case MetadataSchema::MDFTYPE_FLAG:
    00996                     # alter field declaration in Resources table
    00997                     $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    00998                                .$OldDBFieldName."` `"
    00999                                .$NewDBFieldName."` INT"
    01000                                ." DEFAULT ".intval($this->DefaultValue()));
    01001
    01002                     # set any unset values to default
    01003                     $DB->Query("UPDATE Resources SET `".$NewDBFieldName
    01004                             ."` = ".intval($this->DefaultValue())
    01005                             ." WHERE `".$NewDBFieldName."` IS NULL");
    01006                     break;
    01007
    01008                 case MetadataSchema::MDFTYPE_DATE:
    01009                     # if new type supplied and new type is different from old
    01010                     if (($NewType != NULL) && ($NewType != $OldFieldType))
    01011                     {
    01012                         # if old type was time stamp
    01013                         if ($OldFieldType == MetadataSchema::MDFTYPE_TIMESTAMP)
    01014                         {
    01015                             # change time stamp field in resources table to begin date
    01016                             $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01017                                        .$OldDBFieldName."` `"
    01018                                        .$NewDBFieldName."Begin` DATE "
    01019                                        .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01020
    01021                             # add end date and precision fields
    01022                             $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."End"
    01023                                        ."` DATE");
    01024                             $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."Precision`"
    01025                                        ." INT ".($Optional ? "" : "NOT NULL"));
    01026
    01027                             # set precision to reflect time stamp content
    01028                             $DB->Query("UPDATE Resources SET `".$NewDBFieldName."Precision` = "
    01029                                        .(DATEPRE_BEGINYEAR|DATEPRE_BEGINMONTH|DATEPRE_BEGINDAY));
    01030                         }
    01031                         else
    01032                         {
    01033                             exit("<br>ERROR:  Attempt to convert metadata field to date from type other than timestamp<br>\n");
    01034                         }
    01035                     }
    01036                     else
    01037                     {
    01038                         # change name of fields
    01039                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01040                                    .$OldDBFieldName."Begin` `"
    01041                                    .$NewDBFieldName."Begin` DATE "
    01042                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01043                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01044                                    .$OldDBFieldName."End` `"
    01045                                    .$NewDBFieldName."End` DATE "
    01046                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01047                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01048                                    .$OldDBFieldName."Precision` `"
    01049                                    .$NewDBFieldName."Precision` INT "
    01050                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01051                     }
    01052                     break;
    01053
    01054                 case MetadataSchema::MDFTYPE_TIMESTAMP:
    01055                     # if new type supplied and new type is different from old
    01056                     if (($NewType != NULL) && ($NewType != $OldFieldType))
    01057                     {
    01058                         # if old type was date
    01059                         if ($OldFieldType == MetadataSchema::MDFTYPE_DATE)
    01060                         {
    01061                             # change begin date field in resource table to time stamp
    01062                             $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01063                                        .$OldDBFieldName."Begin` `"
    01064                                        .$NewDBFieldName."` DATETIME "
    01065                                        .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01066
    01067                             # drop end date and precision fields
    01068                             $DB->Query("ALTER TABLE Resources DROP COLUMN `"
    01069                                        .$OldDBFieldName."End`");
    01070                             $DB->Query("ALTER TABLE Resources DROP COLUMN `"
    01071                                        .$OldDBFieldName."Precision`");
    01072                         }
    01073                         else
    01074                         {
    01075                             exit("<br>ERROR:  Attempt to convert metadata field to time stamp from type other than date<br>\n");
    01076                         }
    01077                     }
    01078                     else
    01079                     {
    01080                         # change name of field
    01081                         $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01082                                    .$OldDBFieldName."` `"
    01083                                    .$NewDBFieldName."` DATETIME "
    01084                                    .($this->DBFields["Optional"] ? "" : "NOT NULL"));
    01085                     }
    01086                     break;
    01087
    01088                 case MetadataSchema::MDFTYPE_TREE:
    01089                 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    01090                 case MetadataSchema::MDFTYPE_OPTION:
    01091                     break;
    01092             }
    01093
    01094             # if qualifier DB field exists
    01095             if ($DB->FieldExists("Resources", $OldDBFieldName."Qualifier"))
    01096             {
    01097                 # rename qualifier DB field
    01098                 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
    01099                            .$OldDBFieldName."Qualifier` `"
    01100                            .$NewDBFieldName."Qualifier` INT ");
    01101             }
    01102         }
    01103     }
    01104
    01105     # convenience function to supply parameters to Database->UpdateValue()
    01106     function UpdateValue($FieldName, $NewValue)
    01107     {
    01108         return $this->DB->UpdateValue("MetadataFields", $FieldName, $NewValue,
    01109                                "FieldId = ".intval($this->DBFields["FieldId"]),
    01110                                $this->DBFields);
    01111     }
    01112
    01113     # normalize field name for use as database field name
    01114     function NormalizeFieldNameForDB($Name)
    01115     {
    01116         return preg_replace("/[^a-z0-9]/i", "", $Name);
    01117     }
    01118
    01119     # add any needed database fields and/or entries
    01120     function AddDatabaseFields()
    01121     {
    01122         # grab values for common use
    01123         $DB = $this->DB;
    01124         $FieldName = $this->Name();
    01125         $DBFieldName = $this->DBFieldName();
    01126         $Optional = $this->Optional();
    01127         $DefaultValue = $this->DefaultValue();
    01128
    01129         # set up field(s) based on field type
    01130         switch ($this->Type())
    01131         {
    01132             case MetadataSchema::MDFTYPE_TEXT:
    01133             case MetadataSchema::MDFTYPE_PARAGRAPH:
    01134             case MetadataSchema::MDFTYPE_URL:
    01135                 # add field to resources table (if not already present)
    01136                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01137                 {
    01138                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01139                                ."` TEXT ".($Optional ? "" : "NOT NULL"));
    01140                 }
    01141
    01142                 # if default value supplied
    01143                 if ($DefaultValue != NULL)
    01144                 {
    01145                     # set all existing records to default value
    01146                     $DB->Query("UPDATE Resources SET `"
    01147                                .$DBFieldName."` = '".addslashes($DefaultValue)."'");
    01148                 }
    01149                 break;
    01150
    01151             case MetadataSchema::MDFTYPE_NUMBER:
    01152                 # add field to resources table (if not already present)
    01153                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01154                 {
    01155                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01156                                ."` INT ".($Optional ? "" : "NOT NULL"));
    01157                 }
    01158
    01159                 # if default value supplied
    01160                 if ($DefaultValue != NULL)
    01161                 {
    01162                     # set all existing records to default value
    01163                     $DB->Query("UPDATE Resources SET `"
    01164                                .$DBFieldName."` = '".addslashes($DefaultValue)."'");
    01165                 }
    01166                 break;
    01167
    01168             case MetadataSchema::MDFTYPE_POINT:
    01169                 if (!$DB->FieldExists("Resources", $DBFieldName."X"))
    01170                 {
    01171                     $Precision = $this->UpdateValue("PointPrecision",
    01172                                                     DB_NOVALUE);
    01173                     $Digits    = $this->UpdateValue("PointDecimalDigits",
    01174                                                     DB_NOVALUE);
    01175
    01176                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01177                                .$DBFieldName."X`".
    01178                                " DECIMAL(".$Precision.",".$Digits.")");
    01179                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01180                                .$DBFieldName."Y`".
    01181                                " DECIMAL(".$Precision.",".$Digits.")");
    01182                 }
    01183
    01184                 break;
    01185             case MetadataSchema::MDFTYPE_FLAG:
    01186                 # if field is not already present in database
    01187                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01188                 {
    01189                     # add field to resources table
    01190                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01191                                ."` INT DEFAULT ".intval($DefaultValue));
    01192
    01193                     # set all existing records to default value
    01194                     $DB->Query("UPDATE Resources SET `"
    01195                                .$DBFieldName."` = ".intval($DefaultValue));
    01196                 }
    01197                 break;
    01198
    01199             case MetadataSchema::MDFTYPE_USER:
    01200                 # add field to resources table (if not already present)
    01201                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01202                 {
    01203                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01204                                ."` INT ".($Optional ? "" : "NOT NULL"));
    01205                 }
    01206                 break;
    01207
    01208             case MetadataSchema::MDFTYPE_FILE:
    01209                 # add fields to resources table (if not already present)
    01210                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01211                 {
    01212                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01213                                .$DBFieldName."` TEXT");
    01214                 }
    01215                 break;
    01216
    01217             case MetadataSchema::MDFTYPE_IMAGE:
    01218                 # add fields to resources table (if not already present)
    01219                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01220                 {
    01221                     $DB->Query("ALTER TABLE Resources ADD COLUMN `"
    01222                                .$DBFieldName."` INT");
    01223                 }
    01224                 break;
    01225
    01226             case MetadataSchema::MDFTYPE_DATE:
    01227                 # add fields to resources table (if not already present)
    01228                 if (!$DB->FieldExists("Resources", $DBFieldName."Begin"))
    01229                 {
    01230                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Begin`"
    01231                                ." DATE ".($Optional ? "" : "NOT NULL"));
    01232                 }
    01233                 if (!$DB->FieldExists("Resources", $DBFieldName."End"))
    01234                 {
    01235                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."End`"
    01236                                ." DATE");
    01237                 }
    01238                 if (!$DB->FieldExists("Resources", $DBFieldName."Precision"))
    01239                 {
    01240                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Precision`"
    01241                                ." INT ".($Optional ? "" : "NOT NULL"));
    01242                 }
    01243                 break;
    01244
    01245             case MetadataSchema::MDFTYPE_TIMESTAMP:
    01246                 # add fields to resources table (if not already present)
    01247                 if (!$DB->FieldExists("Resources", $DBFieldName))
    01248                 {
    01249                     $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
    01250                                ."` DATETIME ".($Optional ? "" : "NOT NULL"));
    01251                 }
    01252                 break;
    01253
    01254             case MetadataSchema::MDFTYPE_TREE:
    01255             case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
    01256             case MetadataSchema::MDFTYPE_OPTION:
    01257                 break;
    01258
    01259             default:
    01260                 exit("<br>ERROR:  Attempt to add database fields for illegal metadata field type<br>\n");
    01261                 break;
    01262         }
    01263     }
    01264
    01265     # get/set field order positions
    01266     function OrderPosition($OrderType, $NewValue = DB_NOVALUE)
    01267     {
    01268         switch ($OrderType)
    01269         {
    01270             case MetadataSchema::MDFORDER_DISPLAY:
    01271                 return $this->UpdateValue("DisplayOrderPosition", $NewValue);
    01272                 break;
    01273
    01274             case MetadataSchema::MDFORDER_EDITING:
    01275                 return $this->UpdateValue("EditingOrderPosition", $NewValue);
    01276                 break;
    01277
    01278             default:
    01279                 exit("invalid order type passed to MetadataField::OrderPosition");
    01280                 break;
    01281         }
    01282     }
    01283 }
    01284
    01285
    01286 ?>

Copyright 2010 Internet Scout