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