Search:

CWIS Developers Documentation

  • Main Page
  • Classes
  • Files
  • File List
  • File Members

MetadataField.php

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

CWIS logo doxygen
Copyright 2010 Internet Scout