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

CWIS logo doxygen
Copyright 2010 Internet Scout