CWIS Developer Documentation

- Home
- Main Page
- Classes
- Files

- File List
- File Members

MetadataField.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: MetadataField.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2012-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
13 class MetadataField {
14
15 # ---- PUBLIC INTERFACE --------------------------------------------------
16
17 # Update methods for timestamp fields
18 const UPDATEMETHOD_NOAUTOUPDATE = "NoAutoUpdate";
19 const UPDATEMETHOD_ONRECORDCREATE = "OnRecordCreate";
20 const UPDATEMETHOD_BUTTON = "Button";
21 const UPDATEMETHOD_ONRECORDEDIT = "OnRecordEdit";
22 const UPDATEMETHOD_ONRECORDCHANGE = "OnRecordChange";
23
24 # values for the *UserIsValue fields
25 const USERISVALUE_OR = -1;
26 const USERISVALUE_UNSET = 0;
27 const USERISVALUE_AND = 1;
28
34 function Status() { return $this->ErrorStatus; }
35
42 function Type($NewValue = DB_NOVALUE)
43 {
44 # if new value supplied
45 if (($NewValue != DB_NOVALUE)
46 && ($NewValue != MetadataField::$FieldTypePHPEnums[
47 $this->DBFields["FieldType"]]))
48 {
49 # update database fields and store new type
50 $this->ModifyField(NULL, $NewValue);
51 }
52
53 # return type to caller
54 return MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
55 }
56
61 function TypeAsName()
62 {
63 return $this->DBFields["FieldType"];
64 }
65
70 function SchemaId()
71 {
72 return $this->DBFields["SchemaId"];
73 }
74
80 function GetDisplayName()
81 {
82 return strlen($this->Label()) ? $this->Label() : $this->Name();
83 }
84
91 function Name($NewName = DB_NOVALUE)
92 {
93 # if new name specified
94 if ($NewName != DB_NOVALUE
95 && trim($NewName) != $this->DBFields["FieldName"])
96 {
97 $NewName = trim($NewName);
98 $NormalizedName = $this->NormalizeFieldNameForDB(strtolower($NewName));
99
100 # if field name is invalid
101 if (!preg_match("/^[[:alnum:] \(\)]+$/", $NewName))
102 {
103 # set error status to indicate illegal name
104 $this->ErrorStatus = MetadataSchema::MDFSTAT_ILLEGALNAME;
105 }
106
107 # if the new name is a reserved word
108 else if ($NormalizedName == "resourceid" || $NormalizedName == "schemaid")
109 {
110 # set error status to indicate illegal name
111 $this->ErrorStatus = MetadataSchema::MDFSTAT_ILLEGALNAME;
112 }
113
114 # the name is okay but might be a duplicate
115 else
116 {
117 # check for duplicate name
118 $DuplicateCount = $this->DB->Query(
119 "SELECT COUNT(*) AS RecordCount FROM MetadataFields"
120 ." WHERE FieldName = '".addslashes($NewName)."'"
121 ." AND SchemaId = ".intval($this->DBFields["SchemaId"]),
122 "RecordCount");
123
124 # if field name is duplicate
125 if ($DuplicateCount > 0)
126 {
127 # set error status to indicate duplicate name
128 $this->ErrorStatus = MetadataSchema::MDFSTAT_DUPLICATENAME;
129 }
130 else
131 {
132 # modify database declaration to reflect new field name
133 $this->ErrorStatus = MetadataSchema::MDFSTAT_OK;
134 $this->ModifyField($NewName);
135 }
136 }
137 }
138
139 # return value to caller
140 return $this->DBFields["FieldName"];
141 }
142
148 function Label($NewLabel = DB_NOVALUE)
149 {
150 $ValidValueExp = '/^[[:alnum:] ]*$/';
151 $Value = $this->DBFields["Label"];
152
153 # if a new label was specified
154 if ($NewLabel !== DB_NOVALUE && trim($NewLabel) != $Value)
155 {
156 $NewLabel = trim($NewLabel);
157
158 # if field label is valid
159 if (preg_match($ValidValueExp, $NewLabel))
160 {
161 $this->UpdateValue("Label", $NewLabel);
162 $Value = $NewLabel;
163 }
164 # the field label is invalid
165 else
166 {
167 $this->ErrorStatus = MetadataSchema::MDFSTAT_ILLEGALLABEL;
168 }
169 }
170
171 return $Value;
172 }
173
179 function GetAllowedConversionTypes()
180 {
181 # determine type list based on our type
182 switch ($this->Type())
183 {
184 case MetadataSchema::MDFTYPE_TEXT:
185 case MetadataSchema::MDFTYPE_PARAGRAPH:
186 case MetadataSchema::MDFTYPE_NUMBER:
187 case MetadataSchema::MDFTYPE_FLAG:
188 case MetadataSchema::MDFTYPE_URL:
189 $AllowedTypes = array(
190 MetadataSchema::MDFTYPE_TEXT => "Text",
191 MetadataSchema::MDFTYPE_PARAGRAPH => "Paragraph",
192 MetadataSchema::MDFTYPE_NUMBER => "Number",
193 MetadataSchema::MDFTYPE_FLAG => "Flag",
194 MetadataSchema::MDFTYPE_URL => "Url"
195 );
196 break;
197
198 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
199 case MetadataSchema::MDFTYPE_OPTION:
200 $AllowedTypes = array(
201 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "ControlledName",
202 MetadataSchema::MDFTYPE_OPTION => "Option",
203 );
204 break;
205
206 case MetadataSchema::MDFTYPE_DATE:
207 $AllowedTypes = array(
208 MetadataSchema::MDFTYPE_TEXT => "Text",
209 MetadataSchema::MDFTYPE_DATE => "Date",
210 );
211 break;
212
213 case MetadataSchema::MDFTYPE_IMAGE:
214 $AllowedTypes = array(
215 MetadataSchema::MDFTYPE_TEXT => "Text",
216 MetadataSchema::MDFTYPE_IMAGE => "Still Image",
217 );
218 break;
219
220 case MetadataSchema::MDFTYPE_TIMESTAMP:
221 case MetadataSchema::MDFTYPE_TREE:
222 case MetadataSchema::MDFTYPE_USER:
223 case MetadataSchema::MDFTYPE_FILE:
224 case MetadataSchema::MDFTYPE_REFERENCE:
225 default:
226 $AllowedTypes = array();
227 break;
228 }
229
230 # return type list to caller
231 return $AllowedTypes;
232 }
233
241 function IsTempItem($NewSetting = NULL)
242 {
243 $Schema = new MetadataSchema($this->SchemaId());
244 $ItemTableName = "MetadataFields";
245 $ItemIdFieldName = "FieldId";
246 $ItemFactoryObjectName = "MetadataSchema";
247 $ItemAssociationTables = array(
248 "FieldQualifierInts",
249 );
250 $ItemAssociationFieldName = "MetadataFieldId";
251
252 # if new temp item setting supplied
253 if (!is_null($NewSetting))
254 {
255 # if caller requested to switch
256 if (($this->Id() < 0 && $NewSetting == FALSE)
257 || ($this->Id() >= 0 && $NewSetting == TRUE))
258 {
259 # if field name is invalid
260 if (strlen($this->NormalizeFieldNameForDB($this->Name())) < 1)
261 {
262 # set error status to indicate illegal name
263 $this->ErrorStatus = MetadataSchema::MDFSTAT_ILLEGALNAME;
264 }
265 else
266 {
267 # lock DB tables to prevent next ID from being grabbed
268 $DB = $this->DB;
269 $DB->Query("
270 LOCK TABLES ".$ItemTableName." WRITE,
271 APSessions WRITE, APSessionData WRITE,
272 MetadataSchemas WRITE");
273
274 # get next temp item ID
275 $OldItemId = $this->Id();
276 $Factory = new $ItemFactoryObjectName();
277 if ($NewSetting == TRUE)
278 {
279 $NewId = $Factory->GetNextTempItemId();
280 }
281 else
282 {
283 $NewId = $Factory->GetNextItemId();
284 }
285
286 # change item ID
287 $DB->Query("UPDATE ".$ItemTableName." SET ".$ItemIdFieldName." = ".
288 $NewId. " WHERE ".$ItemIdFieldName." = ".$OldItemId);
289
290 # release DB tables
291 $DB->Query("UNLOCK TABLES");
292
293 # change associations
294 foreach ($ItemAssociationTables as $TableName)
295 {
296 $DB->Query("UPDATE ".$TableName." SET ".$ItemAssociationFieldName." = ".
297 $NewId. " WHERE ".$ItemAssociationFieldName." = ".$OldItemId);
298 }
299
300 # if changing item from temp to non-temp
301 if ($NewSetting == FALSE)
302 {
303 # add any needed database fields and/or entries
304 $this->AddDatabaseFields();
305
306 # set field order values for new field
307 $Schema->GetDisplayOrder()->AppendItem($NewId, "MetadataField");
308 $Schema->GetEditOrder()->AppendItem($NewId, "MetadataField");
309 }
310
311 # update metadata field id
312 $this->DBFields["FieldId"] = $NewId;
313 }
314 }
315 }
316
317 # report to caller whether we are a temp item
318 return ($this->Id() < 0) ? TRUE : FALSE;
319 }
320
326 function AuthoringPrivileges(PrivilegeSet $NewValue = NULL)
327 {
328 # if new privileges supplied
329 if ($NewValue !== NULL)
330 {
331 # store new privileges in database
332 $this->UpdateValue("AuthoringPrivileges", $NewValue->Data());
333 $this->AuthoringPrivileges = $NewValue;
334 }
335
336 # return current value to caller
337 return $this->AuthoringPrivileges;
338 }
339
345 function EditingPrivileges(PrivilegeSet $NewValue = NULL)
346 {
347 # if new privileges supplied
348 if ($NewValue !== NULL)
349 {
350 # store new privileges in database
351 $this->UpdateValue("EditingPrivileges", $NewValue->Data());
352 $this->EditingPrivileges = $NewValue;
353 }
354
355 # return current value to caller
356 return $this->EditingPrivileges;
357 }
358
364 function ViewingPrivileges(PrivilegeSet $NewValue = NULL)
365 {
366 # if new privileges supplied
367 if ($NewValue !== NULL)
368 {
369 # store new privileges in database
370 $this->UpdateValue("ViewingPrivileges", $NewValue->Data());
371 $this->ViewingPrivileges = $NewValue;
372 }
373
374 # return current value to caller
375 return $this->ViewingPrivileges;
376 }
377
383 function PreviewingPrivileges(PrivilegeSet $NewValue = NULL)
384 {
385 # if new privileges supplied
386 if ($NewValue !== NULL)
387 {
388 # store new privileges in database
389 $this->UpdateValue("PreviewingPrivileges", $NewValue->Data());
390 $this->PreviewingPrivileges = $NewValue;
391 }
392
393 # return current value to caller
394 return $this->PreviewingPrivileges;
395 }
396
401 function Id() { return $this->Id; }
402
408 function DBFieldName() { return $this->DBFields["DBFieldName"]; }
409
415 function Description($NewValue = DB_NOVALUE)
416 { return $this->UpdateValue("Description", $NewValue); }
417
423 function Instructions($NewValue = DB_NOVALUE)
424 { return $this->UpdateValue("Instructions", $NewValue); }
425
431 function Owner($NewValue = DB_NOVALUE)
432 { return $this->UpdateValue("Owner", $NewValue); }
433
440 function Enabled($NewValue = DB_NOVALUE)
441 { return $this->UpdateBoolValue("Enabled", $NewValue); }
442
449 function Optional($NewValue = DB_NOVALUE)
450 { return $this->UpdateBoolValue("Optional", $NewValue); }
451
458 function Editable($NewValue = DB_NOVALUE)
459 { return $this->UpdateBoolValue("Editable", $NewValue); }
460
467 function AllowMultiple($NewValue = DB_NOVALUE)
468 { return $this->UpdateBoolValue("AllowMultiple", $NewValue); }
469
476 function IncludeInKeywordSearch($NewValue = DB_NOVALUE)
477 { return $this->UpdateBoolValue("IncludeInKeywordSearch", $NewValue); }
478
485 function IncludeInAdvancedSearch($NewValue = DB_NOVALUE)
486 { return $this->UpdateBoolValue("IncludeInAdvancedSearch", $NewValue); }
487
494 function IncludeInFacetedSearch($NewValue = DB_NOVALUE)
495 { return $this->UpdateBoolValue("IncludeInFacetedSearch", $NewValue); }
496
503 function IncludeInSortOptions($NewValue = DB_NOVALUE)
504 { return $this->UpdateBoolValue("IncludeInSortOptions", $NewValue); }
505
512 function IncludeInRecommender($NewValue = DB_NOVALUE)
513 { return $this->UpdateBoolValue("IncludeInRecommender", $NewValue); }
514 function TextFieldSize($NewValue = DB_NOVALUE)
515 { return $this->UpdateIntValue("TextFieldSize", $NewValue); }
516 function MaxLength($NewValue = DB_NOVALUE)
517 { return $this->UpdateIntValue("MaxLength", $NewValue); }
518 function ParagraphRows($NewValue = DB_NOVALUE)
519 { return $this->UpdateIntValue("ParagraphRows", $NewValue); }
520 function ParagraphCols($NewValue = DB_NOVALUE)
521 { return $this->UpdateIntValue("ParagraphCols", $NewValue); }
522 function MinValue($NewValue = DB_NOVALUE)
523 { return $this->UpdateFloatValue("MinValue", $NewValue); }
524 function MaxValue($NewValue = DB_NOVALUE)
525 { return $this->UpdateFloatValue("MaxValue", $NewValue); }
526 function FlagOnLabel($NewValue = DB_NOVALUE)
527 { return $this->UpdateValue("FlagOnLabel", $NewValue); }
528 function FlagOffLabel($NewValue = DB_NOVALUE)
529 { return $this->UpdateValue("FlagOffLabel", $NewValue); }
530 function DateFormat($NewValue = DB_NOVALUE)
531 { return $this->UpdateValue("DateFormat", $NewValue); }
532 function SearchWeight($NewValue = DB_NOVALUE)
533 { return $this->UpdateIntValue("SearchWeight", $NewValue); }
534 function RecommenderWeight($NewValue = DB_NOVALUE)
535 { return $this->UpdateIntValue("RecommenderWeight", $NewValue); }
536 function MaxHeight($NewValue = DB_NOVALUE)
537 { return $this->UpdateIntValue("MaxHeight", $NewValue); }
538 function MaxWidth($NewValue = DB_NOVALUE)
539 { return $this->UpdateIntValue("MaxWidth", $NewValue); }
540 function MaxPreviewHeight($NewValue = DB_NOVALUE)
541 { return $this->UpdateIntValue("MaxPreviewHeight", $NewValue); }
542 function MaxPreviewWidth($NewValue = DB_NOVALUE)
543 { return $this->UpdateIntValue("MaxPreviewWidth", $NewValue); }
544 function MaxThumbnailHeight($NewValue = DB_NOVALUE)
545 { return $this->UpdateIntValue("MaxThumbnailHeight", $NewValue); }
546 function MaxThumbnailWidth($NewValue = DB_NOVALUE)
547 { return $this->UpdateIntValue("MaxThumbnailWidth", $NewValue); }
548 function DefaultAltText($NewValue = DB_NOVALUE)
549 { return $this->UpdateValue("DefaultAltText", $NewValue); }
550 function UsesQualifiers($NewValue = DB_NOVALUE)
551 { return $this->UpdateBoolValue("UsesQualifiers", $NewValue); }
552 function ShowQualifiers($NewValue = DB_NOVALUE)
553 { return $this->UpdateBoolValue("ShowQualifiers", $NewValue); }
554 function DefaultQualifier($NewValue = DB_NOVALUE)
555 { return $this->UpdateValue("DefaultQualifier", $NewValue); }
556 function AllowHTML($NewValue = DB_NOVALUE)
557 { return $this->UpdateBoolValue("AllowHTML", $NewValue); }
558 function UseWysiwygEditor($NewValue = DB_NOVALUE)
559 { return $this->UpdateBoolValue("UseWysiwygEditor", $NewValue); }
560 function UseForOaiSets($NewValue = DB_NOVALUE)
561 { return $this->UpdateBoolValue("UseForOaiSets", $NewValue); }
562 function OptionListThreshold($NewValue = DB_NOVALUE)
563 { return $this->UpdateIntValue("OptionListThreshold", $NewValue); }
564 function AjaxThreshold($NewValue = DB_NOVALUE)
565 { return $this->UpdateIntValue("AjaxThreshold", $NewValue); }
566 function NumAjaxResults($NewValue = DB_NOVALUE)
567 { return $this->UpdateIntValue("NumAjaxResults", $NewValue); }
568 function ViewingPrivilege($NewValue = DB_NOVALUE)
569 {
570 if ($NewValue === DB_NOVALUE)
571 return $this->ViewingPrivileges();
572 else
573 throw new Exception("Deprecated ".__METHOD__."() called -- ".
574 __METHOD__."s() should be used instead.");
575 }
576 function AuthoringPrivilege($NewValue = DB_NOVALUE)
577 {
578 if ($NewValue === DB_NOVALUE)
579 return $this->AuthoringPrivileges();
580 else
581 throw new Exception("Deprecated ".__METHOD__."() called -- ".
582 __METHOD__."s() should be used instead.");
583 }
584 function EditingPrivilege($NewValue = DB_NOVALUE)
585 {
586 if ($NewValue === DB_NOVALUE)
587 return $this->EditingPrivileges();
588 else
589 throw new Exception("Deprecated ".__METHOD__."() called -- ".
590 __METHOD__."s() should be used instead.");
591 }
592 function ImagePreviewPrivilege($NewValue = DB_NOVALUE)
593 { return $this->UpdateConstValue("ImagePreviewPrivilege", $NewValue); }
594 function TreeBrowsingPrivilege($NewValue = DB_NOVALUE)
595 {
596 if ($NewValue === DB_NOVALUE)
597 return $this->ViewingPrivileges();
598 else
599 throw new Exception("Deprecated ".__METHOD__."() called -- ".
600 "this should probably be using ViewingPrivileges() instead.");
601 }
602 function EnableOnOwnerReturn($NewValue = DB_NOVALUE)
603 { return $this->UpdateBoolValue("EnableOnOwnerReturn", $NewValue); }
604 function ViewingUserIsValue($NewValue = DB_NOVALUE)
605 { return $this->UpdateConstValue("ViewingUserIsValue", $NewValue, "MetadataField"); }
606 function AuthoringUserIsValue($NewValue = DB_NOVALUE)
607 { return $this->UpdateConstValue("AuthoringUserIsValue", $NewValue, "MetadataField"); }
608 function EditingUserIsValue($NewValue = DB_NOVALUE)
609 { return $this->UpdateConstValue("EditingUserIsValue", $NewValue, "MetadataField"); }
610 function ViewingUserValue($NewValue = DB_NOVALUE)
611 { return $this->UpdateIntValue("ViewingUserValue", $NewValue, "MetadataField"); }
612 function AuthoringUserValue($NewValue = DB_NOVALUE)
613 { return $this->UpdateIntValue("AuthoringUserValue", $NewValue, "MetadataField"); }
614 function EditingUserValue($NewValue = DB_NOVALUE)
615 { return $this->UpdateIntValue("EditingUserValue", $NewValue, "MetadataField"); }
616 function RequiredBySPT($NewValue = DB_NOVALUE)
617 { return $this->UpdateBoolValue("RequiredBySPT", $NewValue); }
618
619 function UserPrivilegeRestrictions($NewValue = DB_NOVALUE)
620 {
621 # new value
622 if ($NewValue != DB_NOVALUE)
623 {
624 $NewValue = serialize((array) $NewValue);
625 }
626
627 $Value = $this->UpdateValue("UserPrivilegeRestrictions", $NewValue);
628
629 # value set
630 if (strlen($Value))
631 {
632 $Value = (array) unserialize($Value);
633 }
634
635 # no value set, set it to an empty array
636 else
637 {
638 $Value = $this->UserPrivilegeRestrictions(array());
639 }
640
641 return $Value;
642 }
643
644 function PointPrecision($NewValue = DB_NOVALUE)
645 {
646 if ($NewValue !== DB_NOVALUE && $this->Id() >= 0
647 && $this->Type() == MetadataSchema::MDFTYPE_POINT)
648 {
649 $OldValue = $this->UpdateValue("PointPrecision", DB_NOVALUE);
650
651 if ($NewValue != $OldValue)
652 {
653 $Decimals = $this->UpdateValue("PointDecimalDigits", DB_NOVALUE);
654 $TotalDigits = $NewValue + $Decimals;
655
656 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
657 ."`".$this->DBFields["DBFieldName"]."X` "
658 ."DECIMAL(".$TotalDigits.",".$Decimals.")");
659 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
660 ."`".$this->DBFields["DBFieldName"]."Y` "
661 ."DECIMAL(".$TotalDigits.",".$Decimals.")");
662 }
663 }
664
665 return $this->UpdateValue("PointPrecision", $NewValue);
666 }
667
668 function PointDecimalDigits($NewValue = DB_NOVALUE)
669 {
670 if ($NewValue !== DB_NOVALUE && $this->Id() >= 0
671 && $this->Type() == MetadataSchema::MDFTYPE_POINT)
672 {
673 $OldValue = $this->UpdateValue("PointDecimalDigits", DB_NOVALUE);
674
675 if ($NewValue != $OldValue)
676 {
677 $Precision = $this->UpdateValue("PointPrecision", DB_NOVALUE);
678
679 $TotalDigits = $NewValue + $Precision;
680
681 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
682 ."`".$this->DBFields["DBFieldName"]."X` "
683 ."DECIMAL(".$TotalDigits.",".$NewValue.")");
684 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
685 ."`".$this->DBFields["DBFieldName"]."Y` "
686 ."DECIMAL(".$TotalDigits.",".$NewValue.")");
687 }
688 }
689
690 return $this->UpdateValue("PointDecimalDigits", $NewValue);
691 }
692
693 function DefaultValue($NewValue = DB_NOVALUE)
694 {
695 if ($this->Type() == MetadataSchema::MDFTYPE_POINT)
696 {
697 # valid value given
698 if ($NewValue !== DB_NOVALUE &&
699 isset($NewValue["X"]) && isset($NewValue["Y"]))
700 {
701 $NewValue = $NewValue["X"].",".$NewValue["Y"];
702 }
703
704 # invalid value given
705 else
706 {
707 $NewValue = DB_NOVALUE;
708 }
709
710 $Value = $this->UpdateValue("DefaultValue", $NewValue);
711
712 if (is_array($Value))
713 {
714 $tmp = explode(",", $Value);
715
716 if (count($tmp)==2)
717 {
718 return array("X" => $tmp[0], "Y" => $tmp[1]);
719 }
720 }
721
722 return array("X" => NULL, "Y" => NULL);
723 }
724
725 else if ($this->Type() == MetadataSchema::MDFTYPE_OPTION)
726 {
727 # multiple default values to set
728 if (is_array($NewValue))
729 {
730 # empty array
731 if (count($NewValue) == 0)
732 {
733 $NewValue = NULL;
734 }
735
736 # multiple defaults are allowed
737 else if ($this->AllowMultiple())
738 {
739 $NewValue = serialize($NewValue);
740 }
741
742 # only one default is allowed so get the first one
743 else
744 {
745 $NewValue = array_shift($NewValue);
746 }
747 }
748
749 $Result = $this->UpdateValue("DefaultValue", $NewValue);
750
751 return empty($Result) || is_numeric($Result) ?
752 $Result : unserialize($Result);
753 }
754
755 return $this->UpdateValue("DefaultValue", $NewValue);
756 }
757
763 function UpdateMethod($NewValue = DB_NOVALUE)
764 {
765 return $this->UpdateValue("UpdateMethod", $NewValue);
766 }
767
768 # get possible values (only meaningful for Trees, Controlled Names, Options,
769 # Flags, and Users)
770 # (index for returned array is IDs for values)
771 function GetPossibleValues($MaxNumberOfValues = NULL, $Offset=0)
772 {
773 # retrieve values based on field type
774 switch ($this->Type())
775 {
776 case MetadataSchema::MDFTYPE_TREE:
777 $QueryString = "SELECT ClassificationId, ClassificationName"
778 ." FROM Classifications WHERE FieldId = ".$this->Id()
779 ." ORDER BY ClassificationName";
780 if ($MaxNumberOfValues)
781 {
782 $QueryString .= " LIMIT ".intval($MaxNumberOfValues)." OFFSET "
783 .intval($Offset);
784 }
785 $this->DB->Query($QueryString);
786 $PossibleValues = $this->DB->FetchColumn(
787 "ClassificationName", "ClassificationId");
788 break;
789
790 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
791 case MetadataSchema::MDFTYPE_OPTION:
792 $QueryString = "SELECT ControlledNameId, ControlledName"
793 ." FROM ControlledNames WHERE FieldId = ".$this->Id()
794 ." ORDER BY ControlledName";
795 if ($MaxNumberOfValues)
796 {
797 $QueryString .= " LIMIT ".intval($MaxNumberOfValues)." OFFSET "
798 .intval($Offset);
799 }
800 $this->DB->Query($QueryString);
801 $PossibleValues = $this->DB->FetchColumn(
802 "ControlledName", "ControlledNameId");
803 break;
804
805 case MetadataSchema::MDFTYPE_FLAG:
806 $PossibleValues[0] = $this->FlagOffLabel();
807 $PossibleValues[1] = $this->FlagOnLabel();
808 break;
809
810 case MetadataSchema::MDFTYPE_USER:
811 $UserFactory = new CWUserFactory();
812 $Restrictions = $this->UserPrivilegeRestrictions();
813 $PossibleValues = array();
814
815 if (count($Restrictions))
816 {
817 $PossibleValues = call_user_func_array(
818 array($UserFactory, "GetUsersWithPrivileges"),
819 $Restrictions);
820 }
821
822 else
823 {
824 $Users = $UserFactory->GetMatchingUsers(".*.");
825
826 foreach ($Users as $Id => $Data)
827 {
828 $PossibleValues[$Id] = $Data["UserName"];
829 }
830 }
831
832 break;
833
834 default:
835 # for everything else return an empty array
836 $PossibleValues = array();
837 break;
838 }
839
840 # return array of possible values to caller
841 return $PossibleValues;
842 }
843
844 # get count of possible values (only meaningful for Trees, Controlled Names,
845 # Options, and Users)
846 function GetCountOfPossibleValues()
847 {
848 # retrieve values based on field type
849 switch ($this->Type())
850 {
851 case MetadataSchema::MDFTYPE_TREE:
852 $Count = $this->DB->Query("SELECT count(*) AS ValueCount"
853 ." FROM Classifications WHERE FieldId = ".$this->Id(),
854 "ValueCount");
855 break;
856
857 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
858 case MetadataSchema::MDFTYPE_OPTION:
859 $Count = $this->DB->Query("SELECT count(*) AS ValueCount"
860 ." FROM ControlledNames WHERE FieldId = ".$this->Id(),
861 "ValueCount");
862 break;
863
864 case MetadataSchema::MDFTYPE_FLAG:
865 $Count = 2;
866 break;
867
868 case MetadataSchema::MDFTYPE_USER:
869 $Count = count($this->GetPossibleValues());
870 break;
871
872 default:
873 # for everything else return an empty array
874 $Count = 0;
875 break;
876 }
877
878 # return count of possible values to caller
879 return $Count;
880 }
881
882 # get ID for specified value (only meaningful for Trees / Controlled Names / Options)
883 # (returns NULL if value not found)
884 function GetIdForValue($Value)
885 {
886 # retrieve ID based on field type
887 switch ($this->Type())
888 {
889 case MetadataSchema::MDFTYPE_TREE:
890 $Id = $this->DB->Query("SELECT ClassificationId FROM Classifications"
891 ." WHERE ClassificationName = '".addslashes($Value)."'"
892 ." AND FieldId = ".$this->Id(),
893 "ClassificationId");
894 break;
895
896 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
897 case MetadataSchema::MDFTYPE_OPTION:
898 $Id = $this->DB->Query("SELECT ControlledNameId FROM ControlledNames"
899 ." WHERE ControlledName = '".addslashes($Value)."'"
900 ." AND FieldId = ".$this->Id(),
901 "ControlledNameId");
902 break;
903
904 default:
905 # for everything else return NULL
906 $Id = NULL;
907 break;
908 }
909
910 # return ID for value to caller
911 return $Id;
912 }
913
914 # get value for specified ID (only meaningful for Trees / Controlled Names / Options)
915 # (returns NULL if ID not found)
916 function GetValueForId($Id)
917 {
918 # retrieve ID based on field type
919 switch ($this->Type())
920 {
921 case MetadataSchema::MDFTYPE_TREE:
922 $Value = $this->DB->Query("SELECT ClassificationName FROM Classifications"
923 ." WHERE ClassificationId = '".intval($Id)."'"
924 ." AND FieldId = ".$this->Id(),
925 "ClassificationName");
926 break;
927
928 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
929 case MetadataSchema::MDFTYPE_OPTION:
930 $Value = $this->DB->Query("SELECT ControlledName FROM ControlledNames"
931 ." WHERE ControlledNameId = '".intval($Id)."'"
932 ." AND FieldId = ".$this->Id(),
933 "ControlledName");
934 break;
935
936 default:
937 # for everything else return NULL
938 $Value = NULL;
939 break;
940 }
941
942 # return ID for value to caller
943 return $Value;
944 }
945
956 function ValueUseCount($Value)
957 {
958 # retrieve ID if object passed in
959 if (is_object($Value) && method_exists($Value, "Id"))
960 {
961 $Value = $Value->Id();
962 }
963
964 # check value based on field type
965 $DBFieldName = $this->DBFields["DBFieldName"];
966 switch ($this->Type())
967 {
968 case MetadataSchema::MDFTYPE_TEXT:
969 case MetadataSchema::MDFTYPE_PARAGRAPH:
970 case MetadataSchema::MDFTYPE_NUMBER:
971 case MetadataSchema::MDFTYPE_USER:
972 case MetadataSchema::MDFTYPE_IMAGE:
973 case MetadataSchema::MDFTYPE_TIMESTAMP:
974 case MetadataSchema::MDFTYPE_URL:
975 case MetadataSchema::MDFTYPE_FLAG:
976 case MetadataSchema::MDFTYPE_FILE:
977 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
978 ." FROM Resources"
979 ." WHERE `".$DBFieldName."` = '".addslashes($Value)."'"
980 ." AND SchemaId = ".intval($this->DBFields["SchemaId"]),
981 "UseCount");
982 break;
983
984 case MetadataSchema::MDFTYPE_TREE:
985 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
986 ." FROM ResourceClassInts"
987 ." WHERE ClassificationId = ".intval($Value),
988 "UseCount");
989 break;
990
991 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
992 case MetadataSchema::MDFTYPE_OPTION:
993 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
994 ." FROM ResourceNameInts"
995 ." WHERE ControlledNameId = ".intval($Value),
996 "UseCount");
997 break;
998
999 case MetadataSchema::MDFTYPE_POINT:
1000 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
1001 ." FROM Resources"
1002 ." WHERE `".$DBFieldName."X` = '".$Value["X"]."'"
1003 ." AND `".$DBFieldName."Y` = '".$Value["Y"]."'"
1004 ." AND SchemaId = ".intval($this->DBFields["SchemaId"]),
1005 "UseCount");
1006 break;
1007
1008 default:
1009 throw new Exception(__CLASS__."::".__METHOD__."() called for"
1010 ." unsupported field type (".$this->Type().").");
1011 break;
1012 }
1013
1014 # report use count to caller
1015 return $UseCount;
1016 }
1017
1018 # get/set whether field uses item-level qualifiers
1019 function HasItemLevelQualifiers($NewValue = DB_NOVALUE)
1020 {
1021 # if value provided different from present value
1022 if (($NewValue != DB_NOVALUE)
1023 && ($NewValue != $this->DBFields["HasItemLevelQualifiers"]))
1024 {
1025 # check if qualifier column currently exists
1026 $QualColName = $this->DBFieldName()."Qualifier";
1027 $QualColExists = $this->DB->FieldExists("Resources", $QualColName);
1028
1029 # if new value indicates qualifiers should now be used
1030 if ($NewValue == TRUE)
1031 {
1032 # if qualifier column does not exist in DB for this field
1033 if ($QualColExists == FALSE)
1034 {
1035 # add qualifier column in DB for this field
1036 $this->DB->Query("ALTER TABLE Resources ADD COLUMN `"
1037 .$QualColName."` INT");
1038 }
1039 }
1040 else
1041 {
1042 # if qualifier column exists in DB for this field
1043 if ($QualColExists == TRUE)
1044 {
1045 # remove qualifier column from DB for this field
1046 $this->DB->Query("ALTER TABLE Resources DROP COLUMN `"
1047 .$QualColName."`");
1048 }
1049 }
1050 }
1051
1052 return $this->UpdateValue("HasItemLevelQualifiers", $NewValue);
1053 }
1054
1055 # get list of qualifiers associated with field
1056 function AssociatedQualifierList()
1057 {
1058 # start with empty list
1059 $List = array();
1060
1061 # for each associated qualifier
1062 $this->DB->Query("SELECT QualifierId FROM FieldQualifierInts"
1063 ." WHERE MetadataFieldId = ".$this->DBFields["FieldId"]);
1064 while ($Record = $this->DB->FetchRow())
1065 {
1066 # load qualifier object
1067 $Qual = new Qualifier($Record["QualifierId"]);
1068
1069 # add qualifier ID and name to list
1070 $List[$Qual->Id()] = $Qual->Name();
1071 }
1072
1073 # return list to caller
1074 return $List;
1075 }
1076
1077 # get list of qualifiers not associated with field
1078 function UnassociatedQualifierList()
1079 {
1080 # grab list of associated qualifiers
1081 $AssociatedQualifiers = $this->AssociatedQualifierList();
1082
1083 # get list of all qualifiers
1084 $QFactory = new QualifierFactory();
1085 $AllQualifiers = $QFactory->GetItemNames();
1086
1087 # return list of unassociated qualifiers
1088 return array_diff($AllQualifiers, $AssociatedQualifiers);
1089 }
1090
1095 function AddQualifier($Qualifier)
1096 {
1097 # if qualifier object passed in
1098 if (is_object($Qualifier))
1099 {
1100 # grab qualifier ID from object
1101 $Qualifier = $Qualifier->Id();
1102 }
1103 # else if string passed in does not look like ID
1104 elseif (!preg_match('/^[0-9]+$/', $Qualifier))
1105 {
1106 # assume string passed in is name and use it to retrieve ID
1107 $QFact = new QualifierFactory();
1108 $Qualifier = $QFact->GetItemIdByName($Qualifier);
1109 }
1110
1111 # if not already associated
1112 $RecordCount = $this->DB->Query(
1113 "SELECT COUNT(*) AS RecordCount FROM FieldQualifierInts"
1114 ." WHERE QualifierId = ".$Qualifier
1115 ." AND MetadataFieldId = ".$this->Id(), "RecordCount");
1116 if ($RecordCount < 1)
1117 {
1118 # associate field with qualifier
1119 $this->DB->Query("INSERT INTO FieldQualifierInts SET"
1120 ." QualifierId = ".$Qualifier.","
1121 ." MetadataFieldId = ".$this->Id());
1122 }
1123 }
1129 function AssociateWithQualifier($Qualifier)
1130 { $this->AddQualifier($Qualifier); }
1131
1132 # delete qualifier association
1133 function UnassociateWithQualifier($QualifierIdOrObject)
1134 {
1135 # if qualifier object passed in
1136 if (is_object($QualifierIdOrObject))
1137 {
1138 # grab qualifier ID from object
1139 $QualifierIdOrObject = $QualifierIdOrObject->Id();
1140 }
1141
1142 # delete intersection record from database
1143 $this->DB->Query("DELETE FROM FieldQualifierInts WHERE QualifierId = "
1144 .$QualifierIdOrObject." AND MetadataFieldId = ".
1145 $this->Id());
1146 }
1147
1148 # retrieve item factory object for this field
1149 function GetFactory()
1150 {
1151 switch ($this->Type())
1152 {
1153 case MetadataSchema::MDFTYPE_TREE:
1154 $Factory = new ClassificationFactory($this->Id());
1155 break;
1156
1157 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1158 case MetadataSchema::MDFTYPE_OPTION:
1159 $Factory = new ControlledNameFactory($this->Id());
1160 break;
1161
1162 default:
1163 $Factory = NULL;
1164 break;
1165 }
1166
1167 return $Factory;
1168 }
1169
1174 function Viewable()
1175 {
1176 # the field should not be viewed if it is disabled
1177 if (!$this->Enabled())
1178 {
1179 return FALSE;
1180 }
1181
1182 $UserPrivs = $GLOBALS["G_User"]->Privileges();
1183
1184 # the user can view the field if they can edit it
1185 if ($UserPrivs->IsGreaterThan($this->EditingPrivileges()))
1186 {
1187 return TRUE;
1188 }
1189
1190 # if the user can view the field
1191 if ($UserPrivs->IsGreaterThan($this->ViewingPrivileges()))
1192 {
1193 return TRUE;
1194 }
1195
1196 # the user can't view the field
1197 return FALSE;
1198 }
1199
1200 # ---- PRIVATE INTERFACE -------------------------------------------------
1201
1202 private $DB;
1203 private $Id;
1204 private $DBFields;
1205 private $ErrorStatus;
1206 private $AuthoringPrivileges;
1207 private $EditingPrivileges;
1208 private $ViewingPrivileges;
1209 private $PreviewingPrivileges;
1210
1215 public static $FieldTypeHumanEnums = array(
1216 MetadataSchema::MDFTYPE_TEXT => "Text",
1217 MetadataSchema::MDFTYPE_PARAGRAPH => "Paragraph",
1218 MetadataSchema::MDFTYPE_NUMBER => "Number",
1219 MetadataSchema::MDFTYPE_DATE => "Date",
1220 MetadataSchema::MDFTYPE_TIMESTAMP => "Timestamp",
1221 MetadataSchema::MDFTYPE_FLAG => "Flag",
1222 MetadataSchema::MDFTYPE_TREE => "Tree",
1223 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "Controlled Name",
1224 MetadataSchema::MDFTYPE_OPTION => "Option",
1225 MetadataSchema::MDFTYPE_USER => "User",
1226 MetadataSchema::MDFTYPE_IMAGE => "Image",
1227 MetadataSchema::MDFTYPE_FILE => "File",
1228 MetadataSchema::MDFTYPE_URL => "URL",
1229 MetadataSchema::MDFTYPE_POINT => "Point",
1230 MetadataSchema::MDFTYPE_REFERENCE => "Reference");
1231
1232 # field type DB/PHP enum translations
1233 public static $FieldTypeDBEnums = array(
1234 MetadataSchema::MDFTYPE_TEXT => "Text",
1235 MetadataSchema::MDFTYPE_PARAGRAPH => "Paragraph",
1236 MetadataSchema::MDFTYPE_NUMBER => "Number",
1237 MetadataSchema::MDFTYPE_DATE => "Date",
1238 MetadataSchema::MDFTYPE_TIMESTAMP => "TimeStamp",
1239 MetadataSchema::MDFTYPE_FLAG => "Flag",
1240 MetadataSchema::MDFTYPE_TREE => "Tree",
1241 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "ControlledName",
1242 MetadataSchema::MDFTYPE_OPTION => "Option",
1243 MetadataSchema::MDFTYPE_USER => "User",
1244 MetadataSchema::MDFTYPE_IMAGE => "Still Image",
1245 MetadataSchema::MDFTYPE_FILE => "File",
1246 MetadataSchema::MDFTYPE_URL => "Url",
1247 MetadataSchema::MDFTYPE_POINT => "Point",
1248 MetadataSchema::MDFTYPE_REFERENCE => "Reference"
1249 );
1250 public static $FieldTypeDBAllowedEnums = array(
1251 MetadataSchema::MDFTYPE_TEXT => "Text",
1252 MetadataSchema::MDFTYPE_PARAGRAPH => "Paragraph",
1253 MetadataSchema::MDFTYPE_NUMBER => "Number",
1254 MetadataSchema::MDFTYPE_DATE => "Date",
1255 MetadataSchema::MDFTYPE_TIMESTAMP => "TimeStamp",
1256 MetadataSchema::MDFTYPE_FLAG => "Flag",
1257 MetadataSchema::MDFTYPE_TREE => "Tree",
1258 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "ControlledName",
1259 MetadataSchema::MDFTYPE_OPTION => "Option",
1260 MetadataSchema::MDFTYPE_USER => "User",
1261 MetadataSchema::MDFTYPE_IMAGE => "Still Image",
1262 MetadataSchema::MDFTYPE_FILE => "File",
1263 MetadataSchema::MDFTYPE_URL => "Url",
1264 MetadataSchema::MDFTYPE_POINT => "Point",
1265 MetadataSchema::MDFTYPE_REFERENCE => "Reference"
1266 );
1267 public static $FieldTypePHPEnums = array(
1268 "Text" => MetadataSchema::MDFTYPE_TEXT,
1269 "Paragraph" => MetadataSchema::MDFTYPE_PARAGRAPH,
1270 "Number" => MetadataSchema::MDFTYPE_NUMBER,
1271 "Date" => MetadataSchema::MDFTYPE_DATE,
1272 "TimeStamp" => MetadataSchema::MDFTYPE_TIMESTAMP,
1273 "Flag" => MetadataSchema::MDFTYPE_FLAG,
1274 "Tree" => MetadataSchema::MDFTYPE_TREE,
1275 "ControlledName" => MetadataSchema::MDFTYPE_CONTROLLEDNAME,
1276 "Option" => MetadataSchema::MDFTYPE_OPTION,
1277 "User" => MetadataSchema::MDFTYPE_USER,
1278 "Still Image" => MetadataSchema::MDFTYPE_IMAGE,
1279 "File" => MetadataSchema::MDFTYPE_FILE,
1280 "Url" => MetadataSchema::MDFTYPE_URL,
1281 "Point" => MetadataSchema::MDFTYPE_POINT,
1282 "Reference" => MetadataSchema::MDFTYPE_REFERENCE
1283 );
1284
1285 public static $UpdateTypes = array(
1286 MetadataField::UPDATEMETHOD_NOAUTOUPDATE => "Do not update automatically",
1287 MetadataField::UPDATEMETHOD_ONRECORDCREATE => "Update on record creation",
1288 MetadataField::UPDATEMETHOD_BUTTON => "Provide an update button",
1289 MetadataField::UPDATEMETHOD_ONRECORDEDIT => "Update when record is edited",
1290 MetadataField::UPDATEMETHOD_ONRECORDCHANGE => "Update when record is changed"
1291 );
1292
1306 static function Create($SchemaId, $FieldType, $FieldName,
1307 $Optional = NULL, $DefaultValue = NULL)
1308 {
1309 # error out if field type is bad
1310 if (empty(MetadataField::$FieldTypeDBEnums[$FieldType]))
1311 {
1312 throw new InvalidArgumentException("Bad field type (".$FieldType.").");
1313 }
1314
1315 # error out if field name is duplicate
1316 $DB = new Database();
1317 $FieldName = trim($FieldName);
1318 $DuplicateCount = $DB->Query(
1319 "SELECT COUNT(*) AS RecordCount FROM MetadataFields"
1320 ." WHERE FieldName = '".addslashes($FieldName)."'"
1321 ." AND SchemaId = ".intval($SchemaId),
1322 "RecordCount");
1323 if ($DuplicateCount > 0)
1324 {
1325 throw new InvalidArgumentException("Duplicate field name (".$FieldName.").");
1326 }
1327
1328 # grab current user ID
1329 $UserId = $GLOBALS["G_User"]->Get("UserId");
1330
1331 # normalize schema ID
1332 $Schema = new MetadataSchema($SchemaId);
1333 $SchemaId = $Schema->Id();
1334
1335 # use schema privileges as starting privilege values
1336 $AuthorPrivs = $Schema->AuthoringPrivileges();
1337 $EditPrivs = $Schema->EditingPrivileges();
1338 $ViewPrivs = $Schema->ViewingPrivileges();
1339 $PreviewPrivs = $Schema->ViewingPrivileges();
1340
1341 # lock DB tables and get next temporary field ID
1342 $DB->Query("LOCK TABLES MetadataFields WRITE");
1343 $FieldId = $Schema->GetNextTempItemId();
1344
1345 # add field to MDF table in database
1346 $DB->Query("INSERT INTO MetadataFields"
1347 ." (FieldId, SchemaId, FieldName, FieldType, LastModifiedById,"
1348 ." Optional, AuthoringPrivileges, EditingPrivileges,"
1349 ." ViewingPrivileges, PreviewingPrivileges)"
1350 ." VALUES ("
1351 .intval($FieldId).", "
1352 .intval($SchemaId).","
1353 ." '".addslashes($FieldName)."',"
1354 ." '".MetadataField::$FieldTypeDBEnums[$FieldType]."', "
1355 .intval($UserId).", "
1356 .($Optional ? "1" : "0").","
1357 ."'".mysql_escape_string($AuthorPrivs->Data())."',"
1358 ."'".mysql_escape_string($EditPrivs->Data())."',"
1359 ."'".mysql_escape_string($ViewPrivs->Data())."',"
1360 ."'".mysql_escape_string($PreviewPrivs->Data())."')");
1361
1362 # release DB tables
1363 $DB->Query("UNLOCK TABLES");
1364
1365 # load field object
1366 $Field = new MetadataField($FieldId);
1367
1368 # set field defaults
1369 $Field->SetDefaults();
1370
1371 # set the default value if specified
1372 if ($DefaultValue !== NULL)
1373 {
1374 $Field->DefaultValue($DefaultValue);
1375 }
1376
1377 # return newly-constructed field to caller
1378 return $Field;
1379 }
1380
1387 function __construct($FieldId)
1388 {
1389 # assume everything will be okay
1390 $this->ErrorStatus = MetadataSchema::MDFSTAT_OK;
1391
1392 # retrieve field info from database
1393 $this->DB = new Database();
1394 $Result = $this->DB->Query("SELECT * FROM MetadataFields"
1395 ." WHERE FieldId = ".intval($FieldId));
1396 if ($this->DB->NumRowsSelected() == 0)
1397 {
1398 throw new InvalidArgumentException("Invalid metadata field ID ("
1399 .$FieldId.")");
1400 }
1401 $Row = $this->DB->FetchRow();
1402 $this->DBFields = $Row;
1403 $this->Id = $FieldId;
1404
1405 # if privileges have not yet been initialized
1406 if (!strlen($this->DBFields["AuthoringPrivileges"]))
1407 {
1408 # set default values for privileges from metadata schema
1409 $Schema = new MetadataSchema($Row["SchemaId"]);
1410 $this->AuthoringPrivileges($Schema->AuthoringPrivileges());
1411 $this->EditingPrivileges($Schema->EditingPrivileges());
1412 $this->ViewingPrivileges($Schema->ViewingPrivileges());
1413 $this->PreviewingPrivileges($Schema->ViewingPrivileges());
1414 }
1415 else
1416 {
1417 # set privileges from stored values
1418 $this->AuthoringPrivileges = new PrivilegeSet(
1419 $Row["AuthoringPrivileges"]);
1420 $this->EditingPrivileges = new PrivilegeSet(
1421 $Row["EditingPrivileges"]);
1422 $this->ViewingPrivileges = new PrivilegeSet(
1423 $Row["ViewingPrivileges"]);
1424 $this->PreviewingPrivileges = new PrivilegeSet(
1425 $Row["PreviewingPrivileges"]);
1426 }
1427
1428 # set database column name
1429 $this->DBFields["DBFieldName"] =
1430 $this->NormalizeFieldNameForDB($this->DBFields["FieldName"]);
1431 }
1432
1437 public static $FixedDefaults = array(
1438 "Label" => NULL,
1439 "Description" => NULL,
1440 "Instructions" => NULL,
1441 "Enabled" => TRUE,
1442 "Optional" => TRUE,
1443 "Editable" => TRUE,
1444 "AllowMultiple" => FALSE,
1445 "IncludeInKeywordSearch" => FALSE,
1446 "IncludeInAdvancedSearch" => FALSE,
1447 "IncludeInFacetedSearch" => FALSE,
1448 "IncludeInSortOptions" => TRUE,
1449 "IncludeInRecommender" => FALSE,
1450 "ParagraphRows" => 4,
1451 "ParagraphCols" => 50,
1452 "MinValue" => 1,
1453 "FlagOnLabel" => "On",
1454 "FlagOffLabel" => "Off",
1455 "DateFormat" => NULL,
1456 "RecommenderWeight" => 1,
1457 "MaxHeight" => 500,
1458 "MaxWidth" => 500,
1459 "MaxPreviewHeight" => 100,
1460 "MaxPreviewWidth" => 100,
1461 "MaxThumbnailHeight" => 50,
1462 "MaxThumbnailWidth" => 50,
1463 "DefaultAltText" => NULL,
1464 "UsesQualifiers" => FALSE,
1465 "HasItemLevelQualifiers" => FALSE,
1466 "ShowQualifiers" => FALSE,
1467 "DefaultQualifier" => NULL,
1468 "AllowHTML" => FALSE,
1469 "UseWysiwygEditor" => FALSE,
1470 "UseForOaiSets" => FALSE,
1471 "OptionListThreshold" => 25,
1472 "AjaxThreshold" => 50,
1473 "NumAjaxResults" => 50,
1474 "PointPrecision" => 8,
1475 "PointDecimalDigits" => 5,
1476 "UserPrivilegeRestrictions" => array(),
1477 "UpdateMethod" => "NoAutoUpdate",
1478 # 9999 is the default max value because default number field length is 4
1479 "MaxValue" => 9999);
1480
1485 public static $TypeBasedDefaults = array(
1486 MetadataSchema::MDFTYPE_TEXT => array(
1487 "DefaultValue" => NULL,
1488 "SearchWeight" => 1,
1489 "TextFieldSize" => 50,
1490 "MaxLength" => 100),
1491 MetadataSchema::MDFTYPE_PARAGRAPH => array(
1492 "DefaultValue" => NULL,
1493 "SearchWeight" => 1,
1494 "TextFieldSize" => 50,
1495 "MaxLength" => 100),
1496 MetadataSchema::MDFTYPE_NUMBER => array(
1497 "DefaultValue" => NULL,
1498 "SearchWeight" => 1,
1499 "TextFieldSize" => 4,
1500 "MaxLength" => 100),
1501 MetadataSchema::MDFTYPE_DATE => array(
1502 "DefaultValue" => NULL,
1503 "SearchWeight" => 1,
1504 "TextFieldSize" => 10,
1505 "MaxLength" => 100),
1506 MetadataSchema::MDFTYPE_TIMESTAMP => array(
1507 "DefaultValue" => NULL,
1508 "SearchWeight" => 1,
1509 "TextFieldSize" => 50,
1510 "MaxLength" => 100),
1511 MetadataSchema::MDFTYPE_FLAG => array(
1512 "DefaultValue" => NULL,
1513 "SearchWeight" => 1,
1514 "TextFieldSize" => 50,
1515 "MaxLength" => 100),
1516 MetadataSchema::MDFTYPE_TREE => array(
1517 "DefaultValue" => NULL,
1518 "SearchWeight" => 1,
1519 "TextFieldSize" => 50,
1520 "MaxLength" => 100),
1521 MetadataSchema::MDFTYPE_CONTROLLEDNAME => array(
1522 "DefaultValue" => NULL,
1523 "SearchWeight" => 3,
1524 "TextFieldSize" => 50,
1525 "MaxLength" => 100),
1526 MetadataSchema::MDFTYPE_OPTION => array(
1527 "DefaultValue" => NULL,
1528 "SearchWeight" => 3,
1529 "TextFieldSize" => 50,
1530 "MaxLength" => 100),
1531 MetadataSchema::MDFTYPE_USER => array(
1532 "DefaultValue" => NULL,
1533 "SearchWeight" => 1,
1534 "TextFieldSize" => 50,
1535 "MaxLength" => 100),
1536 MetadataSchema::MDFTYPE_IMAGE => array(
1537 "DefaultValue" => NULL,
1538 "SearchWeight" => 1,
1539 "TextFieldSize" => 50,
1540 "MaxLength" => 100),
1541 MetadataSchema::MDFTYPE_FILE => array(
1542 "DefaultValue" => NULL,
1543 "SearchWeight" => 1,
1544 "TextFieldSize" => 50,
1545 "MaxLength" => 100),
1546 MetadataSchema::MDFTYPE_URL => array(
1547 "DefaultValue" => NULL,
1548 "SearchWeight" => 1,
1549 "TextFieldSize" => 50,
1550 "MaxLength" => 255),
1551 MetadataSchema::MDFTYPE_POINT => array(
1552 "DefaultValue" => array("X" => NULL, "Y" => NULL),
1553 "SearchWeight" => 1,
1554 "TextFieldSize" => 10,
1555 "MaxLength" => 100),
1556 MetadataSchema::MDFTYPE_REFERENCE => array(
1557 "DefaultValue" => NULL,
1558 "SearchWeight" => 1,
1559 "TextFieldSize" => 50,
1560 "MaxLength" => 100));
1561
1566 function SetDefaults()
1567 {
1568 # set defaults that are the same for every field
1569 foreach (self::$FixedDefaults as $Key => $Value)
1570 {
1571 $this->$Key($Value);
1572 }
1573
1574 # set defaults that depend on the type of the field
1575 foreach (self::$TypeBasedDefaults[$this->Type()] as $Key => $Value)
1576 {
1577 $this->$Key($Value);
1578 }
1579
1580 # tweak the update method if dealing with the date of record creation
1581 if ($this->Name() == "Date Of Record Creation")
1582 {
1583 $this->UpdateMethod("OnRecordCreate");
1584 }
1585 }
1586
1587 # remove field from database (only for use by MetadataSchema object)
1588 function Drop()
1589 {
1590 # clear other database entries as appropriate for field type
1591 $DB = $this->DB;
1592 $DBFieldName = $this->DBFields["DBFieldName"];
1593 $Schema = new MetadataSchema($this->SchemaId());
1594 switch (MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]])
1595 {
1596 case MetadataSchema::MDFTYPE_TEXT:
1597 case MetadataSchema::MDFTYPE_PARAGRAPH:
1598 case MetadataSchema::MDFTYPE_NUMBER:
1599 case MetadataSchema::MDFTYPE_USER:
1600 case MetadataSchema::MDFTYPE_IMAGE:
1601 case MetadataSchema::MDFTYPE_TIMESTAMP:
1602 case MetadataSchema::MDFTYPE_URL:
1603 case MetadataSchema::MDFTYPE_FLAG:
1604 # remove field from resources table
1605 if ($DB->FieldExists("Resources", $DBFieldName))
1606 {
1607 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."`");
1608 }
1609 break;
1610
1611 case MetadataSchema::MDFTYPE_POINT:
1612 if ($DB->FieldExists("Resources", $DBFieldName."X"))
1613 {
1614 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."X`");
1615 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Y`");
1616 }
1617 break;
1618
1619 case MetadataSchema::MDFTYPE_DATE:
1620 # remove fields from resources table
1621 if ($DB->FieldExists("Resources", $DBFieldName."Begin"))
1622 {
1623 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Begin`");
1624 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."End`");
1625 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Precision`");
1626 }
1627 break;
1628
1629 case MetadataSchema::MDFTYPE_TREE:
1630 $DB->Query("SELECT ClassificationId FROM Classifications "
1631 ."WHERE FieldId = ".$this->Id());
1632 $TempDB = new Database();
1633 while ($ClassificationId = $DB->FetchField("ClassificationId"))
1634 {
1635 # remove any resource / name intersections
1636 $TempDB->Query("DELETE FROM ResourceClassInts WHERE "
1637 ."ClassificationId = ".$ClassificationId);
1638
1639 # remove controlled name
1640 $TempDB->Query("DELETE FROM Classifications WHERE "
1641 ."ClassificationId = ".$ClassificationId);
1642 }
1643 break;
1644
1645 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1646 case MetadataSchema::MDFTYPE_OPTION:
1647 $DB->Query("SELECT ControlledNameId FROM ControlledNames "
1648 ."WHERE FieldId = ".$this->Id());
1649 $TempDB = new Database();
1650 while ($ControlledNameId = $DB->FetchField("ControlledNameId"))
1651 {
1652 # remove any resource / name intersections
1653 $TempDB->Query("DELETE FROM ResourceNameInts WHERE "
1654 ."ControlledNameId = ".$ControlledNameId);
1655
1656 # remove any variant names
1657 $TempDB->Query("DELETE FROM VariantNames WHERE "
1658 ."ControlledNameId = ".$ControlledNameId);
1659
1660 # remove controlled name
1661 $TempDB->Query("DELETE FROM ControlledNames WHERE "
1662 ."ControlledNameId = ".$ControlledNameId);
1663 }
1664 break;
1665
1666 case MetadataSchema::MDFTYPE_FILE:
1667 # for each file associated with this field
1668 $DB->Query("SELECT FileId FROM Files WHERE FieldId = '".$this->Id()."'");
1669 while ($FileId = $DB->FetchRow())
1670 {
1671 # delete file
1672 $File = new File(intval($FileId));
1673 $File->Delete();
1674 }
1675 break;
1676
1677 case MetadataSchema::MDFTYPE_REFERENCE:
1678 # remove any resource references for the field
1679 $DB->Query("
1680 DELETE FROM ReferenceInts
1681 WHERE FieldId = '".addslashes($this->Id())."'");
1682 break;
1683 }
1684
1685 # remove field from database
1686 $DB->Query("DELETE FROM MetadataFields "
1687 ."WHERE FieldId = '".$this->DBFields["FieldId"]."'");
1688
1689 # remove any qualifier associations
1690 $DB->Query("DELETE FROM FieldQualifierInts WHERE MetadataFieldId = '"
1691 .$this->DBFields["FieldId"]."'");
1692
1693 # get the order objects the field is part of
1694 foreach (MetadataFieldOrder::GetOrdersForSchema($Schema) as $Order)
1695 {
1696 # remove it if it's a direct descendant
1697 $Order->RemoveItem($this->Id(), "MetadataField");
1698
1699 # also make sure to remove it if it's part of a group
1700 foreach ($Order->GetItemIds() as $Item)
1701 {
1702 if ($Item["Type"] == "MetadataFieldGroup")
1703 {
1704 $Group = new MetadataFieldGroup($Item["ID"]);
1705 $Group->RemoveItem($this->Id(), "MetadataField");
1706 }
1707 }
1708 }
1709 }
1710
1711 # modify any database fields
1712 private function ModifyField($NewName = NULL, $NewType = NULL)
1713 {
1714 # grab old DB field name
1715 $OldDBFieldName = $this->DBFields["DBFieldName"];
1716 $OldFieldType = NULL;
1717
1718 # if new field name supplied
1719 if ($NewName != NULL)
1720 {
1721 # cache the old name for options and controllednames below
1722 $OldName = $this->DBFields["FieldName"];
1723
1724 # store new name
1725 $this->UpdateValue("FieldName", $NewName);
1726
1727 # determine new DB field name
1728 $NewDBFieldName = $this->NormalizeFieldNameForDB($NewName);
1729
1730 # store new database field name
1731 $this->DBFields["DBFieldName"] = $NewDBFieldName;
1732 }
1733 else
1734 {
1735 # set new field name equal to old field name
1736 $NewDBFieldName = $OldDBFieldName;
1737 }
1738
1739 # if new type supplied
1740 if ($NewType != NULL)
1741 {
1742 # grab old field type
1743 $OldFieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
1744
1745 # store new field type
1746 $this->UpdateValue("FieldType", MetadataField::$FieldTypeDBEnums[$NewType]);
1747 }
1748
1749 # if this is not a temporary field
1750 if ($this->Id() >= 0)
1751 {
1752 # modify field in DB as appropriate for field type
1753 $DB = $this->DB;
1754 $FieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
1755 switch ($FieldType)
1756 {
1757 case MetadataSchema::MDFTYPE_TEXT:
1758 case MetadataSchema::MDFTYPE_PARAGRAPH:
1759 case MetadataSchema::MDFTYPE_URL:
1760 # alter field declaration in Resources table
1761 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1762 .$OldDBFieldName."` `"
1763 .$NewDBFieldName."` TEXT "
1764 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1765 break;
1766
1767 case MetadataSchema::MDFTYPE_NUMBER:
1768 case MetadataSchema::MDFTYPE_USER:
1769 # alter field declaration in Resources table
1770 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1771 .$OldDBFieldName."` `"
1772 .$NewDBFieldName."` INT "
1773 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1774 break;
1775
1776 case MetadataSchema::MDFTYPE_POINT:
1777 $Precision = $this->UpdateValue("PointPrecision",
1778 DB_NOVALUE);
1779 $Digits = $this->UpdateValue("PointDecimalDigits",
1780 DB_NOVALUE);
1781 $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
1782 ."`".$OldDBFieldName."X` "
1783 ."`".$NewDBFieldName."X`".
1784 " DECIMAL(".$Precision.",".$Digits.")");
1785 $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
1786 ."`".$OldDBFieldName."Y` "
1787 ."`".$NewDBFieldName."Y`".
1788 " DECIMAL(".$Precision.",".$Digits.")");
1789 break;
1790
1791 case MetadataSchema::MDFTYPE_FILE:
1792 # if DB field name has changed
1793 if ($NewDBFieldName != $OldDBFieldName)
1794 {
1795 # alter field declaration in Resources table
1796 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1797 .$OldDBFieldName."` `"
1798 .$NewDBFieldName."` TEXT");
1799 }
1800 break;
1801
1802 case MetadataSchema::MDFTYPE_FLAG:
1803 # alter field declaration in Resources table
1804 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1805 .$OldDBFieldName."` `"
1806 .$NewDBFieldName."` INT"
1807 ." DEFAULT ".intval($this->DefaultValue()));
1808
1809 # set any unset values to default
1810 $DB->Query("UPDATE Resources SET `".$NewDBFieldName
1811 ."` = ".intval($this->DefaultValue())
1812 ." WHERE `".$NewDBFieldName."` IS NULL");
1813 break;
1814
1815 case MetadataSchema::MDFTYPE_DATE:
1816 # if new type supplied and new type is different from old
1817 if (($NewType != NULL) && ($NewType != $OldFieldType))
1818 {
1819 # if old type was time stamp
1820 if ($OldFieldType == MetadataSchema::MDFTYPE_TIMESTAMP)
1821 {
1822 # change time stamp field in resources table to begin date
1823 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1824 .$OldDBFieldName."` `"
1825 .$NewDBFieldName."Begin` DATE "
1826 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1827
1828 # add end date and precision fields
1829 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."End"
1830 ."` DATE");
1831 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."Precision`"
1832 ." INT ".($Optional ? "" : "NOT NULL"));
1833
1834 # set precision to reflect time stamp content
1835 $DB->Query("UPDATE Resources SET `".$NewDBFieldName."Precision` = "
1836 .(DATEPRE_BEGINYEAR|DATEPRE_BEGINMONTH|DATEPRE_BEGINDAY));
1837 }
1838 else
1839 {
1840 exit("<br>ERROR: Attempt to convert metadata field to date from type other than timestamp<br>\n");
1841 }
1842 }
1843 else
1844 {
1845 # change name of fields
1846 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1847 .$OldDBFieldName."Begin` `"
1848 .$NewDBFieldName."Begin` DATE "
1849 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1850 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1851 .$OldDBFieldName."End` `"
1852 .$NewDBFieldName."End` DATE "
1853 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1854 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1855 .$OldDBFieldName."Precision` `"
1856 .$NewDBFieldName."Precision` INT "
1857 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1858 }
1859 break;
1860
1861 case MetadataSchema::MDFTYPE_TIMESTAMP:
1862 # if new type supplied and new type is different from old
1863 if (($NewType != NULL) && ($NewType != $OldFieldType))
1864 {
1865 # if old type was date
1866 if ($OldFieldType == MetadataSchema::MDFTYPE_DATE)
1867 {
1868 # change begin date field in resource table to time stamp
1869 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1870 .$OldDBFieldName."Begin` `"
1871 .$NewDBFieldName."` DATETIME "
1872 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1873
1874 # drop end date and precision fields
1875 $DB->Query("ALTER TABLE Resources DROP COLUMN `"
1876 .$OldDBFieldName."End`");
1877 $DB->Query("ALTER TABLE Resources DROP COLUMN `"
1878 .$OldDBFieldName."Precision`");
1879 }
1880 else
1881 {
1882 exit("<br>ERROR: Attempt to convert metadata field to time stamp from type other than date<br>\n");
1883 }
1884 }
1885 else
1886 {
1887 # change name of field
1888 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1889 .$OldDBFieldName."` `"
1890 .$NewDBFieldName."` DATETIME "
1891 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1892 }
1893 break;
1894
1895 case MetadataSchema::MDFTYPE_TREE:
1896 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1897 case MetadataSchema::MDFTYPE_OPTION:
1898 case MetadataSchema::MDFTYPE_REFERENCE:
1899 case MetadataSchema::MDFTYPE_IMAGE:
1900 break;
1901 }
1902
1903 # if qualifier DB field exists
1904 if ($DB->FieldExists("Resources", $OldDBFieldName."Qualifier"))
1905 {
1906 # rename qualifier DB field
1907 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1908 .$OldDBFieldName."Qualifier` `"
1909 .$NewDBFieldName."Qualifier` INT ");
1910 }
1911 }
1912 }
1913
1914 # convenience functions to supply parameters to Database->UpdateValue()
1915 private function UpdateValue($FieldName, $NewValue)
1916 {
1917 return $this->DB->UpdateValue("MetadataFields", $FieldName, $NewValue,
1918 "FieldId = ".intval($this->DBFields["FieldId"]),
1919 $this->DBFields);
1920 }
1921 private function UpdateIntValue($FieldName, $NewValue)
1922 {
1923 return $this->DB->UpdateIntValue("MetadataFields", $FieldName, $NewValue,
1924 "FieldId = ".intval($this->DBFields["FieldId"]),
1925 $this->DBFields);
1926 }
1927 private function UpdateFloatValue($FieldName, $NewValue)
1928 {
1929 return $this->DB->UpdateFloatValue("MetadataFields", $FieldName, $NewValue,
1930 "FieldId = ".intval($this->DBFields["FieldId"]),
1931 $this->DBFields);
1932 }
1933 private function UpdateBoolValue($FieldName, $NewValue)
1934 {
1935 $NewValue = $this->TranslateStringToConstants($NewValue);
1936 return $this->DB->UpdateIntValue("MetadataFields", $FieldName, $NewValue,
1937 "FieldId = ".intval($this->DBFields["FieldId"]),
1938 $this->DBFields);
1939 }
1940 private function UpdateConstValue($FieldName, $NewValue, $ClassName=NULL)
1941 {
1942 $NewValue = $this->TranslateStringToConstants($NewValue, $ClassName);
1943 return $this->DB->UpdateIntValue("MetadataFields", $FieldName, $NewValue,
1944 "FieldId = ".intval($this->DBFields["FieldId"]),
1945 $this->DBFields);
1946 }
1947
1948 # normalize field name for use as database field name
1949 private function NormalizeFieldNameForDB($Name)
1950 {
1951 return preg_replace("/[^a-z0-9]/i", "", $Name)
1952 .(($this->SchemaId() != MetadataSchema::SCHEMAID_DEFAULT)
1953 ? $this->SchemaId() : "");
1954 }
1955
1956 # add any needed database fields and/or entries
1957 private function AddDatabaseFields()
1958 {
1959 # grab values for common use
1960 $DB = $this->DB;
1961 $FieldName = $this->Name();
1962 $DBFieldName = $this->DBFieldName();
1963 $Optional = $this->Optional();
1964 $DefaultValue = $this->DefaultValue();
1965
1966 # set up field(s) based on field type
1967 switch ($this->Type())
1968 {
1969 case MetadataSchema::MDFTYPE_TEXT:
1970 case MetadataSchema::MDFTYPE_PARAGRAPH:
1971 case MetadataSchema::MDFTYPE_URL:
1972 # add field to resources table (if not already present)
1973 if (!$DB->FieldExists("Resources", $DBFieldName))
1974 {
1975 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
1976 ."` TEXT ".($Optional ? "" : "NOT NULL"));
1977 }
1978
1979 # if default value supplied
1980 if ($DefaultValue != NULL)
1981 {
1982 # set all existing records to default value
1983 $DB->Query("UPDATE Resources SET `"
1984 .$DBFieldName."` = '".addslashes($DefaultValue)."'");
1985 }
1986 break;
1987
1988 case MetadataSchema::MDFTYPE_NUMBER:
1989 # add field to resources table (if not already present)
1990 if (!$DB->FieldExists("Resources", $DBFieldName))
1991 {
1992 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
1993 ."` INT ".($Optional ? "" : "NOT NULL"));
1994 }
1995
1996 # if default value supplied
1997 if ($DefaultValue != NULL)
1998 {
1999 # set all existing records to default value
2000 $DB->Query("UPDATE Resources SET `"
2001 .$DBFieldName."` = '".addslashes($DefaultValue)."'");
2002 }
2003 break;
2004
2005 case MetadataSchema::MDFTYPE_POINT:
2006 if (!$DB->FieldExists("Resources", $DBFieldName."X"))
2007 {
2008 $Precision = $this->UpdateValue("PointPrecision",
2009 DB_NOVALUE);
2010 $Digits = $this->UpdateValue("PointDecimalDigits",
2011 DB_NOVALUE);
2012
2013 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2014 .$DBFieldName."X`".
2015 " DECIMAL(".$Precision.",".$Digits.")");
2016 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2017 .$DBFieldName."Y`".
2018 " DECIMAL(".$Precision.",".$Digits.")");
2019 }
2020
2021 break;
2022 case MetadataSchema::MDFTYPE_FLAG:
2023 # if field is not already present in database
2024 if (!$DB->FieldExists("Resources", $DBFieldName))
2025 {
2026 # add field to resources table
2027 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
2028 ."` INT DEFAULT ".intval($DefaultValue));
2029
2030 # set all existing records to default value
2031 $DB->Query("UPDATE Resources SET `"
2032 .$DBFieldName."` = ".intval($DefaultValue));
2033 }
2034 break;
2035
2036 case MetadataSchema::MDFTYPE_USER:
2037 # add field to resources table (if not already present)
2038 if (!$DB->FieldExists("Resources", $DBFieldName))
2039 {
2040 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
2041 ."` INT ".($Optional ? "" : "NOT NULL"));
2042 }
2043 break;
2044
2045 case MetadataSchema::MDFTYPE_FILE:
2046 # add fields to resources table (if not already present)
2047 if (!$DB->FieldExists("Resources", $DBFieldName))
2048 {
2049 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2050 .$DBFieldName."` TEXT");
2051 }
2052 break;
2053
2054 case MetadataSchema::MDFTYPE_IMAGE:
2055 # add fields to resources table (if not already present)
2056 if (!$DB->FieldExists("Resources", $DBFieldName))
2057 {
2058 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2059 .$DBFieldName."` INT");
2060 }
2061 break;
2062
2063 case MetadataSchema::MDFTYPE_DATE:
2064 # add fields to resources table (if not already present)
2065 if (!$DB->FieldExists("Resources", $DBFieldName."Begin"))
2066 {
2067 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Begin`"
2068 ." DATE ".($Optional ? "" : "NOT NULL"));
2069 }
2070 if (!$DB->FieldExists("Resources", $DBFieldName."End"))
2071 {
2072 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."End`"
2073 ." DATE");
2074 }
2075 if (!$DB->FieldExists("Resources", $DBFieldName."Precision"))
2076 {
2077 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Precision`"
2078 ." INT ".($Optional ? "" : "NOT NULL"));
2079 }
2080 break;
2081
2082 case MetadataSchema::MDFTYPE_TIMESTAMP:
2083 # add fields to resources table (if not already present)
2084 if (!$DB->FieldExists("Resources", $DBFieldName))
2085 {
2086 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
2087 ."` DATETIME ".($Optional ? "" : "NOT NULL"));
2088 }
2089 break;
2090
2091 case MetadataSchema::MDFTYPE_TREE:
2092 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
2093 case MetadataSchema::MDFTYPE_OPTION:
2094 case MetadataSchema::MDFTYPE_REFERENCE:
2095 break;
2096
2097 default:
2098 exit("<br>ERROR: Attempt to add database fields for illegal metadata field type<br>\n");
2099 break;
2100 }
2101 }
2102
2110 private function TranslateStringToConstants($CString, $ClassName = NULL)
2111 {
2112 # if not a string return value unchanged to caller
2113 if (!is_string($CString) || ($CString === DB_NOVALUE))
2114 {
2115 $ReturnValue = $CString;
2116 }
2117 # handle booleans as a special case
2118 elseif (strtoupper(trim($CString)) == "TRUE")
2119 {
2120 $ReturnValue = TRUE;
2121 }
2122 elseif (strtoupper(trim($CString)) == "FALSE")
2123 {
2124 $ReturnValue = FALSE;
2125 }
2126 else
2127 {
2128 # assume no values will be found
2129 $ReturnValue = NULL;
2130
2131 # split apart any ORed-together values
2132 $Values = explode("|", $CString);
2133
2134 # for each value found
2135 foreach ($Values as $Value)
2136 {
2137 # trim off any extraneous whitespace
2138 $Value = trim($Value);
2139
2140 # add class name prefix to constant name if requested
2141 if ($ClassName) { $Value = $ClassName."::".$Value; }
2142
2143 # if value corresponds to a constant
2144 if (defined($Value))
2145 {
2146 # add constant to return value
2147 $ReturnValue = ($ReturnValue === NULL)
2148 ? constant($Value)
2149 : ($ReturnValue | constant($Value));
2150 }
2151 }
2152
2153 # if no corresponding constants were found
2154 if ($ReturnValue === NULL)
2155 {
2156 # return original value to caller
2157 $ReturnValue = $CString;
2158 }
2159 }
2160
2161 # return result to caller
2162 return $ReturnValue;
2163 }
2164
2165 }
MetadataSchema\MDFSTAT_ILLEGALLABEL
const MDFSTAT_ILLEGALLABEL
Definition: MetadataSchema.php:49
MetadataSchema\MDFTYPE_IMAGE
const MDFTYPE_IMAGE
Definition: MetadataSchema.php:30
MetadataField\DefaultQualifier
DefaultQualifier($NewValue=DB_NOVALUE)
Definition: MetadataField.php:554
MetadataField\$FieldTypeDBEnums
static $FieldTypeDBEnums
Definition: MetadataField.php:1233
MetadataField\ViewingPrivileges
ViewingPrivileges(PrivilegeSet $NewValue=NULL)
Get/set privileges that allowing viewing values for this field.
Definition: MetadataField.php:364
MetadataField\$FixedDefaults
static $FixedDefaults
The metadata field defaults that are the same for all field types.
Definition: MetadataField.php:1437
DATEPRE_BEGINDAY
const DATEPRE_BEGINDAY
Definition: Axis--Date.php:747
MetadataField\$FieldTypeDBAllowedEnums
static $FieldTypeDBAllowedEnums
Definition: MetadataField.php:1250
MetadataField\SetDefaults
SetDefaults()
Set defaults values for the field.
Definition: MetadataField.php:1566
MetadataSchema\MDFTYPE_TIMESTAMP
const MDFTYPE_TIMESTAMP
Definition: MetadataSchema.php:24
MetadataField\Owner
Owner($NewValue=DB_NOVALUE)
Get/set field owner.
Definition: MetadataField.php:431
MetadataField\MaxPreviewHeight
MaxPreviewHeight($NewValue=DB_NOVALUE)
Definition: MetadataField.php:540
MetadataSchema\MDFTYPE_FILE
const MDFTYPE_FILE
Definition: MetadataSchema.php:31
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
MetadataField\$FieldTypePHPEnums
static $FieldTypePHPEnums
Definition: MetadataField.php:1267
MetadataField\Status
Status()
Get current error status of object.
Definition: MetadataField.php:34
MetadataField\IncludeInFacetedSearch
IncludeInFacetedSearch($NewValue=DB_NOVALUE)
Get/set whether to include field in faceted search.
Definition: MetadataField.php:494
MetadataField\UPDATEMETHOD_ONRECORDCHANGE
const UPDATEMETHOD_ONRECORDCHANGE
Definition: MetadataField.php:22
MetadataField\GetValueForId
GetValueForId($Id)
Definition: MetadataField.php:916
MetadataField\__construct
__construct($FieldId)
Object contstructor, used to load an existing metadata field.
Definition: MetadataField.php:1387
MetadataField\Instructions
Instructions($NewValue=DB_NOVALUE)
Get/set field instructions.
Definition: MetadataField.php:423
MetadataField\Create
static Create($SchemaId, $FieldType, $FieldName, $Optional=NULL, $DefaultValue=NULL)
Create a new metadata field.
Definition: MetadataField.php:1306
MetadataField\ValueUseCount
ValueUseCount($Value)
Check how many times a specific value is currently used for this field.
Definition: MetadataField.php:956
MetadataField\UseForOaiSets
UseForOaiSets($NewValue=DB_NOVALUE)
Definition: MetadataField.php:560
MetadataField\AllowHTML
AllowHTML($NewValue=DB_NOVALUE)
Definition: MetadataField.php:556
MetadataField\UnassociatedQualifierList
UnassociatedQualifierList()
Definition: MetadataField.php:1078
MetadataField\USERISVALUE_UNSET
const USERISVALUE_UNSET
Definition: MetadataField.php:26
MetadataField\IncludeInRecommender
IncludeInRecommender($NewValue=DB_NOVALUE)
Get/set whether to include field in recommender system comparisons.
Definition: MetadataField.php:512
Database
SQL database abstraction object with smart query caching.
Definition: Axis--Database.php:22
MetadataSchema\MDFTYPE_REFERENCE
const MDFTYPE_REFERENCE
Definition: MetadataSchema.php:34
DB_NOVALUE
const DB_NOVALUE
Definition: Axis--Database.php:1180
MetadataField\GetFactory
GetFactory()
Definition: MetadataField.php:1149
MetadataField\AuthoringPrivileges
AuthoringPrivileges(PrivilegeSet $NewValue=NULL)
Get/set privileges that allowing authoring values for this field.
Definition: MetadataField.php:326
MetadataField\ShowQualifiers
ShowQualifiers($NewValue=DB_NOVALUE)
Definition: MetadataField.php:552
MetadataField\FlagOffLabel
FlagOffLabel($NewValue=DB_NOVALUE)
Definition: MetadataField.php:528
MetadataField\PointPrecision
PointPrecision($NewValue=DB_NOVALUE)
Definition: MetadataField.php:644
MetadataField\OptionListThreshold
OptionListThreshold($NewValue=DB_NOVALUE)
Definition: MetadataField.php:562
MetadataSchema\MDFSTAT_OK
const MDFSTAT_OK
Definition: MetadataSchema.php:42
MetadataSchema\MDFTYPE_NUMBER
const MDFTYPE_NUMBER
Definition: MetadataSchema.php:22
MetadataField\ViewingUserValue
ViewingUserValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:610
MetadataField\USERISVALUE_OR
const USERISVALUE_OR
Definition: MetadataField.php:25
MetadataField\USERISVALUE_AND
const USERISVALUE_AND
Definition: MetadataField.php:27
MetadataField\AllowMultiple
AllowMultiple($NewValue=DB_NOVALUE)
Get/set whether to allow multiple values for field.
Definition: MetadataField.php:467
MetadataField\$TypeBasedDefaults
static $TypeBasedDefaults
The metadata field defaults that vary depending on the field type.
Definition: MetadataField.php:1485
MetadataField\Description
Description($NewValue=DB_NOVALUE)
Get/set field description.
Definition: MetadataField.php:415
MetadataField\EditingUserValue
EditingUserValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:614
MetadataField\MaxThumbnailHeight
MaxThumbnailHeight($NewValue=DB_NOVALUE)
Definition: MetadataField.php:544
MetadataField\SearchWeight
SearchWeight($NewValue=DB_NOVALUE)
Definition: MetadataField.php:532
PrivilegeSet
Set of privileges used to access resource information or other parts of the system.
Definition: PrivilegeSet.php:16
MetadataField\HasItemLevelQualifiers
HasItemLevelQualifiers($NewValue=DB_NOVALUE)
Definition: MetadataField.php:1019
MetadataField\AssociateWithQualifier
AssociateWithQualifier($Qualifier)
DEPRECATED METHOD.
Definition: MetadataField.php:1129
MetadataField\AuthoringPrivilege
AuthoringPrivilege($NewValue=DB_NOVALUE)
Definition: MetadataField.php:576
MetadataField\DefaultAltText
DefaultAltText($NewValue=DB_NOVALUE)
Definition: MetadataField.php:548
MetadataFieldGroup
Class that builds on the foldering functionality to provide groups of metadata fields.
Definition: MetadataFieldGroup.php:14
Qualifier
Definition: Qualifier.php:10
MetadataSchema\MDFTYPE_TREE
const MDFTYPE_TREE
Definition: MetadataSchema.php:26
MetadataField\AjaxThreshold
AjaxThreshold($NewValue=DB_NOVALUE)
Definition: MetadataField.php:564
MetadataSchema\MDFTYPE_OPTION
const MDFTYPE_OPTION
Definition: MetadataSchema.php:28
QualifierFactory
Factory class for Qualifier.
Definition: QualifierFactory.php:13
MetadataSchema\MDFTYPE_FLAG
const MDFTYPE_FLAG
Definition: MetadataSchema.php:25
MetadataSchema\MDFTYPE_CONTROLLEDNAME
const MDFTYPE_CONTROLLEDNAME
Definition: MetadataSchema.php:27
MetadataField\$UpdateTypes
static $UpdateTypes
Definition: MetadataField.php:1285
MetadataField\Viewable
Viewable()
This function has been deprecated and should not be used.
Definition: MetadataField.php:1174
CWUserFactory
CWIS-specific user factory class.
Definition: CWUserFactory.php:13
MetadataField\$FieldTypeHumanEnums
static $FieldTypeHumanEnums
A map of metadata field types to human-readable strings.
Definition: MetadataField.php:1215
MetadataField\MaxPreviewWidth
MaxPreviewWidth($NewValue=DB_NOVALUE)
Definition: MetadataField.php:542
MetadataField\UPDATEMETHOD_ONRECORDEDIT
const UPDATEMETHOD_ONRECORDEDIT
Definition: MetadataField.php:21
PHP
PHP
Definition: OAIClient.php:39
MetadataField\UserPrivilegeRestrictions
UserPrivilegeRestrictions($NewValue=DB_NOVALUE)
Definition: MetadataField.php:619
MetadataField\AddQualifier
AddQualifier($Qualifier)
Associate qualifier with field.
Definition: MetadataField.php:1095
MetadataField\SchemaId
SchemaId()
Get ID of schema for field.
Definition: MetadataField.php:70
MetadataField\IncludeInSortOptions
IncludeInSortOptions($NewValue=DB_NOVALUE)
Get/set whether to include field in search result sort options.
Definition: MetadataField.php:503
MetadataField\DefaultValue
DefaultValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:693
MetadataSchema\SCHEMAID_DEFAULT
const SCHEMAID_DEFAULT
Definition: MetadataSchema.php:52
MetadataField\TypeAsName
TypeAsName()
Get type of field as string.
Definition: MetadataField.php:61
DATEPRE_BEGINMONTH
const DATEPRE_BEGINMONTH
Definition: Axis--Date.php:746
MetadataField\MaxLength
MaxLength($NewValue=DB_NOVALUE)
Definition: MetadataField.php:516
MetadataField\MaxValue
MaxValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:524
MetadataField\PointDecimalDigits
PointDecimalDigits($NewValue=DB_NOVALUE)
Definition: MetadataField.php:668
MetadataField\GetAllowedConversionTypes
GetAllowedConversionTypes()
Get metadata field types that this field can be converted to.
Definition: MetadataField.php:179
MetadataField\Optional
Optional($NewValue=DB_NOVALUE)
Get/set whether a value is required for this field.
Definition: MetadataField.php:449
MetadataField\ViewingPrivilege
ViewingPrivilege($NewValue=DB_NOVALUE)
Definition: MetadataField.php:568
MetadataField\ParagraphRows
ParagraphRows($NewValue=DB_NOVALUE)
Definition: MetadataField.php:518
MetadataField\EditingPrivilege
EditingPrivilege($NewValue=DB_NOVALUE)
Definition: MetadataField.php:584
MetadataField\RequiredBySPT
RequiredBySPT($NewValue=DB_NOVALUE)
Definition: MetadataField.php:616
MetadataField\AuthoringUserIsValue
AuthoringUserIsValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:606
MetadataField\UsesQualifiers
UsesQualifiers($NewValue=DB_NOVALUE)
Definition: MetadataField.php:550
MetadataField\MaxThumbnailWidth
MaxThumbnailWidth($NewValue=DB_NOVALUE)
Definition: MetadataField.php:546
MetadataField\Enabled
Enabled($NewValue=DB_NOVALUE)
Get/set whether field is enabled.
Definition: MetadataField.php:440
MetadataField\AssociatedQualifierList
AssociatedQualifierList()
Definition: MetadataField.php:1056
ControlledNameFactory
Factory for manipulating ControlledName objects.
Definition: ControlledNameFactory.php:13
MetadataField\UPDATEMETHOD_NOAUTOUPDATE
const UPDATEMETHOD_NOAUTOUPDATE
Definition: MetadataField.php:18
MetadataField
Object representing a locally-defined type of metadata field.
Definition: MetadataField.php:13
MetadataField\NumAjaxResults
NumAjaxResults($NewValue=DB_NOVALUE)
Definition: MetadataField.php:566
MetadataField\Drop
Drop()
Definition: MetadataField.php:1588
MetadataSchema\MDFTYPE_PARAGRAPH
const MDFTYPE_PARAGRAPH
Definition: MetadataSchema.php:21
MetadataField\TextFieldSize
TextFieldSize($NewValue=DB_NOVALUE)
Definition: MetadataField.php:514
MetadataSchema\MDFTYPE_DATE
const MDFTYPE_DATE
Definition: MetadataSchema.php:23
MetadataField\ViewingUserIsValue
ViewingUserIsValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:604
MetadataSchema\MDFTYPE_URL
const MDFTYPE_URL
Definition: MetadataSchema.php:32
MetadataField\EnableOnOwnerReturn
EnableOnOwnerReturn($NewValue=DB_NOVALUE)
Definition: MetadataField.php:602
MetadataField\FlagOnLabel
FlagOnLabel($NewValue=DB_NOVALUE)
Definition: MetadataField.php:526
MetadataField\TreeBrowsingPrivilege
TreeBrowsingPrivilege($NewValue=DB_NOVALUE)
Definition: MetadataField.php:594
MetadataSchema\MDFTYPE_TEXT
const MDFTYPE_TEXT
Definition: MetadataSchema.php:20
MetadataField\UPDATEMETHOD_BUTTON
const UPDATEMETHOD_BUTTON
Definition: MetadataField.php:20
MetadataField\UseWysiwygEditor
UseWysiwygEditor($NewValue=DB_NOVALUE)
Definition: MetadataField.php:558
MetadataField\EditingUserIsValue
EditingUserIsValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:608
MetadataField\GetPossibleValues
GetPossibleValues($MaxNumberOfValues=NULL, $Offset=0)
Definition: MetadataField.php:771
MetadataSchema\MDFSTAT_ILLEGALNAME
const MDFSTAT_ILLEGALNAME
Definition: MetadataSchema.php:47
MetadataField\EditingPrivileges
EditingPrivileges(PrivilegeSet $NewValue=NULL)
Get/set privileges that allowing editing values for this field.
Definition: MetadataField.php:345
MetadataFieldOrder\GetOrdersForSchema
static GetOrdersForSchema(MetadataSchema $Schema)
Get all of the orders associated with a schema.
Definition: MetadataFieldOrder.php:640
MetadataField\GetIdForValue
GetIdForValue($Value)
Definition: MetadataField.php:884
MetadataField\RecommenderWeight
RecommenderWeight($NewValue=DB_NOVALUE)
Definition: MetadataField.php:534
MetadataSchema\MDFTYPE_USER
const MDFTYPE_USER
Definition: MetadataSchema.php:29
MetadataField\Label
Label($NewLabel=DB_NOVALUE)
Get/set label for field.
Definition: MetadataField.php:148
MetadataField\Type
Type($NewValue=DB_NOVALUE)
Get/set type of metadata field (enumerated value).
Definition: MetadataField.php:42
MetadataField\Id
Id()
Get metadata field ID.
Definition: MetadataField.php:401
MetadataField\DBFieldName
DBFieldName()
Get base name of database column used to store metadata field value.
Definition: MetadataField.php:408
MetadataField\ImagePreviewPrivilege
ImagePreviewPrivilege($NewValue=DB_NOVALUE)
Definition: MetadataField.php:592
MetadataField\DateFormat
DateFormat($NewValue=DB_NOVALUE)
Definition: MetadataField.php:530
MetadataField\MaxWidth
MaxWidth($NewValue=DB_NOVALUE)
Definition: MetadataField.php:538
MetadataField\MaxHeight
MaxHeight($NewValue=DB_NOVALUE)
Definition: MetadataField.php:536
MetadataField\UPDATEMETHOD_ONRECORDCREATE
const UPDATEMETHOD_ONRECORDCREATE
Definition: MetadataField.php:19
MetadataSchema\MDFSTAT_DUPLICATENAME
const MDFSTAT_DUPLICATENAME
Definition: MetadataSchema.php:44
ClassificationFactory
Factory for producing and manipulating Classification objects.
Definition: ClassificationFactory.php:14
MetadataField\PreviewingPrivileges
PreviewingPrivileges(PrivilegeSet $NewValue=NULL)
Get/set privileges that allowing previewing values for this field.
Definition: MetadataField.php:383
MetadataField\ParagraphCols
ParagraphCols($NewValue=DB_NOVALUE)
Definition: MetadataField.php:520
MetadataField\UnassociateWithQualifier
UnassociateWithQualifier($QualifierIdOrObject)
Definition: MetadataField.php:1133
MetadataSchema\MDFTYPE_POINT
const MDFTYPE_POINT
Definition: MetadataSchema.php:33
File
Class representing a stored (usually uploaded) file.
Definition: File.php:13
MetadataField\IncludeInKeywordSearch
IncludeInKeywordSearch($NewValue=DB_NOVALUE)
Get/set whether to include field in keyword search.
Definition: MetadataField.php:476
MetadataField\Editable
Editable($NewValue=DB_NOVALUE)
Get/set whether this field is editable.
Definition: MetadataField.php:458
MetadataField\IsTempItem
IsTempItem($NewSetting=NULL)
Get/set whether field is temporary instance.
Definition: MetadataField.php:241
MetadataField\Name
Name($NewName=DB_NOVALUE)
Get/set name of field.
Definition: MetadataField.php:91
MetadataField\IncludeInAdvancedSearch
IncludeInAdvancedSearch($NewValue=DB_NOVALUE)
Get/set whether to include field in advanced search.
Definition: MetadataField.php:485
MetadataField\GetCountOfPossibleValues
GetCountOfPossibleValues()
Definition: MetadataField.php:846
MetadataField\GetDisplayName
GetDisplayName()
Get display name for field.
Definition: MetadataField.php:80
MetadataField\MinValue
MinValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:522
MetadataField\AuthoringUserValue
AuthoringUserValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:612
MetadataField\UpdateMethod
UpdateMethod($NewValue=DB_NOVALUE)
Get/set method by which field is updated.
Definition: MetadataField.php:763