Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- Search for |

- File List
- File Members

# MetadataField.php

Go to the documentation of this file.

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