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 { return $this->UpdateConstValue("ViewingPrivilege", $NewValue); }
570 function AuthoringPrivilege($NewValue = DB_NOVALUE)
571 { return $this->UpdateConstValue("AuthoringPrivilege", $NewValue); }
572 function EditingPrivilege($NewValue = DB_NOVALUE)
573 { return $this->UpdateConstValue("EditingPrivilege", $NewValue); }
574 function ImagePreviewPrivilege($NewValue = DB_NOVALUE)
575 { return $this->UpdateConstValue("ImagePreviewPrivilege", $NewValue); }
576 function TreeBrowsingPrivilege($NewValue = DB_NOVALUE)
577 { return $this->UpdateConstValue("TreeBrowsingPrivilege", $NewValue); }
578 function EnableOnOwnerReturn($NewValue = DB_NOVALUE)
579 { return $this->UpdateBoolValue("EnableOnOwnerReturn", $NewValue); }
580 function ViewingUserIsValue($NewValue = DB_NOVALUE)
581 { return $this->UpdateConstValue("ViewingUserIsValue", $NewValue, "MetadataField"); }
582 function AuthoringUserIsValue($NewValue = DB_NOVALUE)
583 { return $this->UpdateConstValue("AuthoringUserIsValue", $NewValue, "MetadataField"); }
584 function EditingUserIsValue($NewValue = DB_NOVALUE)
585 { return $this->UpdateConstValue("EditingUserIsValue", $NewValue, "MetadataField"); }
586 function ViewingUserValue($NewValue = DB_NOVALUE)
587 { return $this->UpdateIntValue("ViewingUserValue", $NewValue, "MetadataField"); }
588 function AuthoringUserValue($NewValue = DB_NOVALUE)
589 { return $this->UpdateIntValue("AuthoringUserValue", $NewValue, "MetadataField"); }
590 function EditingUserValue($NewValue = DB_NOVALUE)
591 { return $this->UpdateIntValue("EditingUserValue", $NewValue, "MetadataField"); }
592 function RequiredBySPT($NewValue = DB_NOVALUE)
593 { return $this->UpdateBoolValue("RequiredBySPT", $NewValue); }
594
595 function UserPrivilegeRestrictions($NewValue = DB_NOVALUE)
596 {
597 # new value
598 if ($NewValue != DB_NOVALUE)
599 {
600 $NewValue = serialize((array) $NewValue);
601 }
602
603 $Value = $this->UpdateValue("UserPrivilegeRestrictions", $NewValue);
604
605 # value set
606 if (strlen($Value))
607 {
608 $Value = (array) unserialize($Value);
609 }
610
611 # no value set, set it to an empty array
612 else
613 {
614 $Value = $this->UserPrivilegeRestrictions(array());
615 }
616
617 return $Value;
618 }
619
620 function PointPrecision($NewValue = DB_NOVALUE)
621 {
622 if ($NewValue !== DB_NOVALUE && $this->Id() >= 0
623 && $this->Type() == MetadataSchema::MDFTYPE_POINT)
624 {
625 $OldValue = $this->UpdateValue("PointPrecision", DB_NOVALUE);
626
627 if ($NewValue != $OldValue)
628 {
629 $Decimals = $this->UpdateValue("PointDecimalDigits", DB_NOVALUE);
630 $TotalDigits = $NewValue + $Decimals;
631
632 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
633 ."`".$this->DBFields["DBFieldName"]."X` "
634 ."DECIMAL(".$TotalDigits.",".$Decimals.")");
635 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
636 ."`".$this->DBFields["DBFieldName"]."Y` "
637 ."DECIMAL(".$TotalDigits.",".$Decimals.")");
638 }
639 }
640
641 return $this->UpdateValue("PointPrecision", $NewValue);
642 }
643
644 function PointDecimalDigits($NewValue = DB_NOVALUE)
645 {
646 if ($NewValue !== DB_NOVALUE && $this->Id() >= 0
647 && $this->Type() == MetadataSchema::MDFTYPE_POINT)
648 {
649 $OldValue = $this->UpdateValue("PointDecimalDigits", DB_NOVALUE);
650
651 if ($NewValue != $OldValue)
652 {
653 $Precision = $this->UpdateValue("PointPrecision", DB_NOVALUE);
654
655 $TotalDigits = $NewValue + $Precision;
656
657 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
658 ."`".$this->DBFields["DBFieldName"]."X` "
659 ."DECIMAL(".$TotalDigits.",".$NewValue.")");
660 $this->DB->Query("ALTER TABLE Resources MODIFY COLUMN "
661 ."`".$this->DBFields["DBFieldName"]."Y` "
662 ."DECIMAL(".$TotalDigits.",".$NewValue.")");
663 }
664 }
665
666 return $this->UpdateValue("PointDecimalDigits", $NewValue);
667 }
668
669 function DefaultValue($NewValue = DB_NOVALUE)
670 {
671 if ($this->Type() == MetadataSchema::MDFTYPE_POINT)
672 {
673 # valid value given
674 if ($NewValue !== DB_NOVALUE &&
675 isset($NewValue["X"]) && isset($NewValue["Y"]))
676 {
677 $NewValue = $NewValue["X"].",".$NewValue["Y"];
678 }
679
680 # invalid value given
681 else
682 {
683 $NewValue = DB_NOVALUE;
684 }
685
686 $Value = $this->UpdateValue("DefaultValue", $NewValue);
687
688 if (is_array($Value))
689 {
690 $tmp = explode(",", $Value);
691
692 if (count($tmp)==2)
693 {
694 return array("X" => $tmp[0], "Y" => $tmp[1]);
695 }
696 }
697
698 return array("X" => NULL, "Y" => NULL);
699 }
700
701 else if ($this->Type() == MetadataSchema::MDFTYPE_OPTION)
702 {
703 # multiple default values to set
704 if (is_array($NewValue))
705 {
706 # empty array
707 if (count($NewValue) == 0)
708 {
709 $NewValue = NULL;
710 }
711
712 # multiple defaults are allowed
713 else if ($this->AllowMultiple())
714 {
715 $NewValue = serialize($NewValue);
716 }
717
718 # only one default is allowed so get the first one
719 else
720 {
721 $NewValue = array_shift($NewValue);
722 }
723 }
724
725 $Result = $this->UpdateValue("DefaultValue", $NewValue);
726
727 return empty($Result) || is_numeric($Result) ?
728 $Result : unserialize($Result);
729 }
730
731 return $this->UpdateValue("DefaultValue", $NewValue);
732 }
733
739 function UpdateMethod($NewValue = DB_NOVALUE)
740 {
741 return $this->UpdateValue("UpdateMethod", $NewValue);
742 }
743
744 # get possible values (only meaningful for Trees, Controlled Names, Options,
745 # Flags, and Users)
746 # (index for returned array is IDs for values)
747 function GetPossibleValues($MaxNumberOfValues = NULL, $Offset=0)
748 {
749 # retrieve values based on field type
750 switch ($this->Type())
751 {
752 case MetadataSchema::MDFTYPE_TREE:
753 $QueryString = "SELECT ClassificationId, ClassificationName"
754 ." FROM Classifications WHERE FieldId = ".$this->Id()
755 ." ORDER BY ClassificationName";
756 if ($MaxNumberOfValues)
757 {
758 $QueryString .= " LIMIT ".intval($MaxNumberOfValues)." OFFSET "
759 .intval($Offset);
760 }
761 $this->DB->Query($QueryString);
762 $PossibleValues = $this->DB->FetchColumn(
763 "ClassificationName", "ClassificationId");
764 break;
765
766 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
767 case MetadataSchema::MDFTYPE_OPTION:
768 $QueryString = "SELECT ControlledNameId, ControlledName"
769 ." FROM ControlledNames WHERE FieldId = ".$this->Id()
770 ." ORDER BY ControlledName";
771 if ($MaxNumberOfValues)
772 {
773 $QueryString .= " LIMIT ".intval($MaxNumberOfValues)." OFFSET "
774 .intval($Offset);
775 }
776 $this->DB->Query($QueryString);
777 $PossibleValues = $this->DB->FetchColumn(
778 "ControlledName", "ControlledNameId");
779 break;
780
781 case MetadataSchema::MDFTYPE_FLAG:
782 $PossibleValues[0] = $this->FlagOffLabel();
783 $PossibleValues[1] = $this->FlagOnLabel();
784 break;
785
786 case MetadataSchema::MDFTYPE_USER:
787 $UserFactory = new CWUserFactory();
788 $Restrictions = $this->UserPrivilegeRestrictions();
789 $PossibleValues = array();
790
791 if (count($Restrictions))
792 {
793 $PossibleValues = call_user_func_array(
794 array($UserFactory, "GetUsersWithPrivileges"),
795 $Restrictions);
796 }
797
798 else
799 {
800 $Users = $UserFactory->GetMatchingUsers(".*.");
801
802 foreach ($Users as $Id => $Data)
803 {
804 $PossibleValues[$Id] = $Data["UserName"];
805 }
806 }
807
808 break;
809
810 default:
811 # for everything else return an empty array
812 $PossibleValues = array();
813 break;
814 }
815
816 # return array of possible values to caller
817 return $PossibleValues;
818 }
819
820 # get count of possible values (only meaningful for Trees, Controlled Names,
821 # Options, and Users)
822 function GetCountOfPossibleValues()
823 {
824 # retrieve values based on field type
825 switch ($this->Type())
826 {
827 case MetadataSchema::MDFTYPE_TREE:
828 $Count = $this->DB->Query("SELECT count(*) AS ValueCount"
829 ." FROM Classifications WHERE FieldId = ".$this->Id(),
830 "ValueCount");
831 break;
832
833 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
834 case MetadataSchema::MDFTYPE_OPTION:
835 $Count = $this->DB->Query("SELECT count(*) AS ValueCount"
836 ." FROM ControlledNames WHERE FieldId = ".$this->Id(),
837 "ValueCount");
838 break;
839
840 case MetadataSchema::MDFTYPE_FLAG:
841 $Count = 2;
842 break;
843
844 case MetadataSchema::MDFTYPE_USER:
845 $Count = count($this->GetPossibleValues());
846 break;
847
848 default:
849 # for everything else return an empty array
850 $Count = 0;
851 break;
852 }
853
854 # return count of possible values to caller
855 return $Count;
856 }
857
858 # get ID for specified value (only meaningful for Trees / Controlled Names / Options)
859 # (returns NULL if value not found)
860 function GetIdForValue($Value)
861 {
862 # retrieve ID based on field type
863 switch ($this->Type())
864 {
865 case MetadataSchema::MDFTYPE_TREE:
866 $Id = $this->DB->Query("SELECT ClassificationId FROM Classifications"
867 ." WHERE ClassificationName = '".addslashes($Value)."'"
868 ." AND FieldId = ".$this->Id(),
869 "ClassificationId");
870 break;
871
872 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
873 case MetadataSchema::MDFTYPE_OPTION:
874 $Id = $this->DB->Query("SELECT ControlledNameId FROM ControlledNames"
875 ." WHERE ControlledName = '".addslashes($Value)."'"
876 ." AND FieldId = ".$this->Id(),
877 "ControlledNameId");
878 break;
879
880 default:
881 # for everything else return NULL
882 $Id = NULL;
883 break;
884 }
885
886 # return ID for value to caller
887 return $Id;
888 }
889
890 # get value for specified ID (only meaningful for Trees / Controlled Names / Options)
891 # (returns NULL if ID not found)
892 function GetValueForId($Id)
893 {
894 # retrieve ID based on field type
895 switch ($this->Type())
896 {
897 case MetadataSchema::MDFTYPE_TREE:
898 $Value = $this->DB->Query("SELECT ClassificationName FROM Classifications"
899 ." WHERE ClassificationId = '".intval($Id)."'"
900 ." AND FieldId = ".$this->Id(),
901 "ClassificationName");
902 break;
903
904 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
905 case MetadataSchema::MDFTYPE_OPTION:
906 $Value = $this->DB->Query("SELECT ControlledName FROM ControlledNames"
907 ." WHERE ControlledNameId = '".intval($Id)."'"
908 ." AND FieldId = ".$this->Id(),
909 "ControlledName");
910 break;
911
912 default:
913 # for everything else return NULL
914 $Value = NULL;
915 break;
916 }
917
918 # return ID for value to caller
919 return $Value;
920 }
921
932 function ValueUseCount($Value)
933 {
934 # retrieve ID if object passed in
935 if (is_object($Value) && method_exists($Value, "Id"))
936 {
937 $Value = $Value->Id();
938 }
939
940 # check value based on field type
941 $DBFieldName = $this->DBFields["DBFieldName"];
942 switch ($this->Type())
943 {
944 case MetadataSchema::MDFTYPE_TEXT:
945 case MetadataSchema::MDFTYPE_PARAGRAPH:
946 case MetadataSchema::MDFTYPE_NUMBER:
947 case MetadataSchema::MDFTYPE_USER:
948 case MetadataSchema::MDFTYPE_IMAGE:
949 case MetadataSchema::MDFTYPE_TIMESTAMP:
950 case MetadataSchema::MDFTYPE_URL:
951 case MetadataSchema::MDFTYPE_FLAG:
952 case MetadataSchema::MDFTYPE_FILE:
953 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
954 ." FROM Resources"
955 ." WHERE `".$DBFieldName."` = '".addslashes($Value)."'"
956 ." AND SchemaId = ".intval($this->DBFields["SchemaId"]),
957 "UseCount");
958 break;
959
960 case MetadataSchema::MDFTYPE_TREE:
961 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
962 ." FROM ResourceClassInts"
963 ." WHERE ClassificationId = ".intval($Value),
964 "UseCount");
965 break;
966
967 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
968 case MetadataSchema::MDFTYPE_OPTION:
969 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
970 ." FROM ResourceNameInts"
971 ." WHERE ControlledNameId = ".intval($Value),
972 "UseCount");
973 break;
974
975 case MetadataSchema::MDFTYPE_POINT:
976 $UseCount = $this->DB->Query("SELECT COUNT(*) AS UseCount"
977 ." FROM Resources"
978 ." WHERE `".$DBFieldName."X` = '".$Value["X"]."'"
979 ." AND `".$DBFieldName."Y` = '".$Value["Y"]."'"
980 ." AND SchemaId = ".intval($this->DBFields["SchemaId"]),
981 "UseCount");
982 break;
983
984 default:
985 throw new Exception(__CLASS__."::".__METHOD__."() called for"
986 ." unsupported field type (".$this->Type().").");
987 break;
988 }
989
990 # report use count to caller
991 return $UseCount;
992 }
993
994 # get/set whether field uses item-level qualifiers
995 function HasItemLevelQualifiers($NewValue = DB_NOVALUE)
996 {
997 # if value provided different from present value
998 if (($NewValue != DB_NOVALUE)
999 && ($NewValue != $this->DBFields["HasItemLevelQualifiers"]))
1000 {
1001 # check if qualifier column currently exists
1002 $QualColName = $this->DBFieldName()."Qualifier";
1003 $QualColExists = $this->DB->FieldExists("Resources", $QualColName);
1004
1005 # if new value indicates qualifiers should now be used
1006 if ($NewValue == TRUE)
1007 {
1008 # if qualifier column does not exist in DB for this field
1009 if ($QualColExists == FALSE)
1010 {
1011 # add qualifier column in DB for this field
1012 $this->DB->Query("ALTER TABLE Resources ADD COLUMN `"
1013 .$QualColName."` INT");
1014 }
1015 }
1016 else
1017 {
1018 # if qualifier column exists in DB for this field
1019 if ($QualColExists == TRUE)
1020 {
1021 # remove qualifier column from DB for this field
1022 $this->DB->Query("ALTER TABLE Resources DROP COLUMN `"
1023 .$QualColName."`");
1024 }
1025 }
1026 }
1027
1028 return $this->UpdateValue("HasItemLevelQualifiers", $NewValue);
1029 }
1030
1031 # get list of qualifiers associated with field
1032 function AssociatedQualifierList()
1033 {
1034 # start with empty list
1035 $List = array();
1036
1037 # for each associated qualifier
1038 $this->DB->Query("SELECT QualifierId FROM FieldQualifierInts"
1039 ." WHERE MetadataFieldId = ".$this->DBFields["FieldId"]);
1040 while ($Record = $this->DB->FetchRow())
1041 {
1042 # load qualifier object
1043 $Qual = new Qualifier($Record["QualifierId"]);
1044
1045 # add qualifier ID and name to list
1046 $List[$Qual->Id()] = $Qual->Name();
1047 }
1048
1049 # return list to caller
1050 return $List;
1051 }
1052
1053 # get list of qualifiers not associated with field
1054 function UnassociatedQualifierList()
1055 {
1056 # grab list of associated qualifiers
1057 $AssociatedQualifiers = $this->AssociatedQualifierList();
1058
1059 # get list of all qualifiers
1060 $QFactory = new QualifierFactory();
1061 $AllQualifiers = $QFactory->GetItemNames();
1062
1063 # return list of unassociated qualifiers
1064 return array_diff($AllQualifiers, $AssociatedQualifiers);
1065 }
1066
1071 function AddQualifier($Qualifier)
1072 {
1073 # if qualifier object passed in
1074 if (is_object($Qualifier))
1075 {
1076 # grab qualifier ID from object
1077 $Qualifier = $Qualifier->Id();
1078 }
1079 # else if string passed in does not look like ID
1080 elseif (!preg_match('/^[0-9]+$/', $Qualifier))
1081 {
1082 # assume string passed in is name and use it to retrieve ID
1083 $QFact = new QualifierFactory();
1084 $Qualifier = $QFact->GetItemIdByName($Qualifier);
1085 }
1086
1087 # if not already associated
1088 $RecordCount = $this->DB->Query(
1089 "SELECT COUNT(*) AS RecordCount FROM FieldQualifierInts"
1090 ." WHERE QualifierId = ".$Qualifier
1091 ." AND MetadataFieldId = ".$this->Id(), "RecordCount");
1092 if ($RecordCount < 1)
1093 {
1094 # associate field with qualifier
1095 $this->DB->Query("INSERT INTO FieldQualifierInts SET"
1096 ." QualifierId = ".$Qualifier.","
1097 ." MetadataFieldId = ".$this->Id());
1098 }
1099 }
1105 function AssociateWithQualifier($Qualifier)
1106 { $this->AddQualifier($Qualifier); }
1107
1108 # delete qualifier association
1109 function UnassociateWithQualifier($QualifierIdOrObject)
1110 {
1111 # if qualifier object passed in
1112 if (is_object($QualifierIdOrObject))
1113 {
1114 # grab qualifier ID from object
1115 $QualifierIdOrObject = $QualifierIdOrObject->Id();
1116 }
1117
1118 # delete intersection record from database
1119 $this->DB->Query("DELETE FROM FieldQualifierInts WHERE QualifierId = "
1120 .$QualifierIdOrObject." AND MetadataFieldId = ".
1121 $this->Id());
1122 }
1123
1124 # retrieve item factory object for this field
1125 function GetFactory()
1126 {
1127 switch ($this->Type())
1128 {
1129 case MetadataSchema::MDFTYPE_TREE:
1130 $Factory = new ClassificationFactory($this->Id());
1131 break;
1132
1133 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1134 case MetadataSchema::MDFTYPE_OPTION:
1135 $Factory = new ControlledNameFactory($this->Id());
1136 break;
1137
1138 default:
1139 $Factory = NULL;
1140 break;
1141 }
1142
1143 return $Factory;
1144 }
1145
1150 function Viewable()
1151 {
1152 # the field should not be viewed if it is disabled
1153 if (!$this->Enabled())
1154 {
1155 return FALSE;
1156 }
1157
1158 $UserPrivs = $GLOBALS["G_User"]->Privileges();
1159
1160 # the user can view the field if they can edit it
1161 if ($UserPrivs->IsGreaterThan($this->EditingPrivileges()))
1162 {
1163 return TRUE;
1164 }
1165
1166 # if the user can view the field
1167 if ($UserPrivs->IsGreaterThan($this->ViewingPrivileges()))
1168 {
1169 return TRUE;
1170 }
1171
1172 # the user can't view the field
1173 return FALSE;
1174 }
1175
1176 # ---- PRIVATE INTERFACE -------------------------------------------------
1177
1178 private $DB;
1179 private $Id;
1180 private $DBFields;
1181 private $ErrorStatus;
1182 private $AuthoringPrivileges;
1183 private $EditingPrivileges;
1184 private $ViewingPrivileges;
1185 private $PreviewingPrivileges;
1186
1191 public static $FieldTypeHumanEnums = array(
1192 MetadataSchema::MDFTYPE_TEXT => "Text",
1193 MetadataSchema::MDFTYPE_PARAGRAPH => "Paragraph",
1194 MetadataSchema::MDFTYPE_NUMBER => "Number",
1195 MetadataSchema::MDFTYPE_DATE => "Date",
1196 MetadataSchema::MDFTYPE_TIMESTAMP => "Timestamp",
1197 MetadataSchema::MDFTYPE_FLAG => "Flag",
1198 MetadataSchema::MDFTYPE_TREE => "Tree",
1199 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "Controlled Name",
1200 MetadataSchema::MDFTYPE_OPTION => "Option",
1201 MetadataSchema::MDFTYPE_USER => "User",
1202 MetadataSchema::MDFTYPE_IMAGE => "Image",
1203 MetadataSchema::MDFTYPE_FILE => "File",
1204 MetadataSchema::MDFTYPE_URL => "URL",
1205 MetadataSchema::MDFTYPE_POINT => "Point",
1206 MetadataSchema::MDFTYPE_REFERENCE => "Reference");
1207
1208 # field type DB/PHP enum translations
1209 public static $FieldTypeDBEnums = array(
1210 MetadataSchema::MDFTYPE_TEXT => "Text",
1211 MetadataSchema::MDFTYPE_PARAGRAPH => "Paragraph",
1212 MetadataSchema::MDFTYPE_NUMBER => "Number",
1213 MetadataSchema::MDFTYPE_DATE => "Date",
1214 MetadataSchema::MDFTYPE_TIMESTAMP => "TimeStamp",
1215 MetadataSchema::MDFTYPE_FLAG => "Flag",
1216 MetadataSchema::MDFTYPE_TREE => "Tree",
1217 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "ControlledName",
1218 MetadataSchema::MDFTYPE_OPTION => "Option",
1219 MetadataSchema::MDFTYPE_USER => "User",
1220 MetadataSchema::MDFTYPE_IMAGE => "Still Image",
1221 MetadataSchema::MDFTYPE_FILE => "File",
1222 MetadataSchema::MDFTYPE_URL => "Url",
1223 MetadataSchema::MDFTYPE_POINT => "Point",
1224 MetadataSchema::MDFTYPE_REFERENCE => "Reference"
1225 );
1226 public static $FieldTypeDBAllowedEnums = array(
1227 MetadataSchema::MDFTYPE_TEXT => "Text",
1228 MetadataSchema::MDFTYPE_PARAGRAPH => "Paragraph",
1229 MetadataSchema::MDFTYPE_NUMBER => "Number",
1230 MetadataSchema::MDFTYPE_DATE => "Date",
1231 MetadataSchema::MDFTYPE_TIMESTAMP => "TimeStamp",
1232 MetadataSchema::MDFTYPE_FLAG => "Flag",
1233 MetadataSchema::MDFTYPE_TREE => "Tree",
1234 MetadataSchema::MDFTYPE_CONTROLLEDNAME => "ControlledName",
1235 MetadataSchema::MDFTYPE_OPTION => "Option",
1236 MetadataSchema::MDFTYPE_USER => "User",
1237 MetadataSchema::MDFTYPE_IMAGE => "Still Image",
1238 MetadataSchema::MDFTYPE_FILE => "File",
1239 MetadataSchema::MDFTYPE_URL => "Url",
1240 MetadataSchema::MDFTYPE_POINT => "Point",
1241 MetadataSchema::MDFTYPE_REFERENCE => "Reference"
1242 );
1243 public static $FieldTypePHPEnums = array(
1244 "Text" => MetadataSchema::MDFTYPE_TEXT,
1245 "Paragraph" => MetadataSchema::MDFTYPE_PARAGRAPH,
1246 "Number" => MetadataSchema::MDFTYPE_NUMBER,
1247 "Date" => MetadataSchema::MDFTYPE_DATE,
1248 "TimeStamp" => MetadataSchema::MDFTYPE_TIMESTAMP,
1249 "Flag" => MetadataSchema::MDFTYPE_FLAG,
1250 "Tree" => MetadataSchema::MDFTYPE_TREE,
1251 "ControlledName" => MetadataSchema::MDFTYPE_CONTROLLEDNAME,
1252 "Option" => MetadataSchema::MDFTYPE_OPTION,
1253 "User" => MetadataSchema::MDFTYPE_USER,
1254 "Still Image" => MetadataSchema::MDFTYPE_IMAGE,
1255 "File" => MetadataSchema::MDFTYPE_FILE,
1256 "Url" => MetadataSchema::MDFTYPE_URL,
1257 "Point" => MetadataSchema::MDFTYPE_POINT,
1258 "Reference" => MetadataSchema::MDFTYPE_REFERENCE
1259 );
1260
1261 public static $UpdateTypes = array(
1262 MetadataField::UPDATEMETHOD_NOAUTOUPDATE => "Do not update automatically",
1263 MetadataField::UPDATEMETHOD_ONRECORDCREATE => "Update on record creation",
1264 MetadataField::UPDATEMETHOD_BUTTON => "Provide an update button",
1265 MetadataField::UPDATEMETHOD_ONRECORDEDIT => "Update when record is edited",
1266 MetadataField::UPDATEMETHOD_ONRECORDCHANGE => "Update when record is changed"
1267 );
1268
1282 static function Create($SchemaId, $FieldType, $FieldName,
1283 $Optional = NULL, $DefaultValue = NULL)
1284 {
1285 # error out if field type is bad
1286 if (empty(MetadataField::$FieldTypeDBEnums[$FieldType]))
1287 {
1288 throw new InvalidArgumentException("Bad field type (".$FieldType.").");
1289 }
1290
1291 # error out if field name is duplicate
1292 $DB = new Database();
1293 $FieldName = trim($FieldName);
1294 $DuplicateCount = $DB->Query(
1295 "SELECT COUNT(*) AS RecordCount FROM MetadataFields"
1296 ." WHERE FieldName = '".addslashes($FieldName)."'"
1297 ." AND SchemaId = ".intval($SchemaId),
1298 "RecordCount");
1299 if ($DuplicateCount > 0)
1300 {
1301 throw new InvalidArgumentException("Duplicate field name (".$FieldName.").");
1302 }
1303
1304 # grab current user ID
1305 $UserId = $GLOBALS["G_User"]->Get("UserId");
1306
1307 # normalize schema ID
1308 $Schema = new MetadataSchema($SchemaId);
1309 $SchemaId = $Schema->Id();
1310
1311 # use schema privileges as starting privilege values
1312 $AuthorPrivs = $Schema->AuthoringPrivileges();
1313 $EditPrivs = $Schema->EditingPrivileges();
1314 $ViewPrivs = $Schema->ViewingPrivileges();
1315 $PreviewPrivs = $Schema->ViewingPrivileges();
1316
1317 # lock DB tables and get next temporary field ID
1318 $DB->Query("LOCK TABLES MetadataFields WRITE");
1319 $FieldId = $Schema->GetNextTempItemId();
1320
1321 # add field to MDF table in database
1322 $DB->Query("INSERT INTO MetadataFields"
1323 ." (FieldId, SchemaId, FieldName, FieldType, LastModifiedById,"
1324 ." Optional, AuthoringPrivileges, EditingPrivileges,"
1325 ." ViewingPrivileges, PreviewingPrivileges)"
1326 ." VALUES ("
1327 .intval($FieldId).", "
1328 .intval($SchemaId).","
1329 ." '".addslashes($FieldName)."',"
1330 ." '".MetadataField::$FieldTypeDBEnums[$FieldType]."', "
1331 .intval($UserId).", "
1332 .($Optional ? "1" : "0").","
1333 ."'".mysql_escape_string($AuthorPrivs->Data())."',"
1334 ."'".mysql_escape_string($EditPrivs->Data())."',"
1335 ."'".mysql_escape_string($ViewPrivs->Data())."',"
1336 ."'".mysql_escape_string($PreviewPrivs->Data())."')");
1337
1338 # release DB tables
1339 $DB->Query("UNLOCK TABLES");
1340
1341 # load field object
1342 $Field = new MetadataField($FieldId);
1343
1344 # set field defaults
1345 $Field->SetDefaults();
1346
1347 # set the default value if specified
1348 if ($DefaultValue !== NULL)
1349 {
1350 $Field->DefaultValue($DefaultValue);
1351 }
1352
1353 # return newly-constructed field to caller
1354 return $Field;
1355 }
1356
1363 function __construct($FieldId)
1364 {
1365 # assume everything will be okay
1366 $this->ErrorStatus = MetadataSchema::MDFSTAT_OK;
1367
1368 # retrieve field info from database
1369 $this->DB = new Database();
1370 $Result = $this->DB->Query("SELECT * FROM MetadataFields"
1371 ." WHERE FieldId = ".intval($FieldId));
1372 if ($this->DB->NumRowsSelected() == 0)
1373 {
1374 throw new InvalidArgumentException("Invalid metadata field ID ("
1375 .$FieldId.")");
1376 }
1377 $Row = $this->DB->FetchRow();
1378 $this->DBFields = $Row;
1379 $this->Id = $FieldId;
1380
1381 # if privileges have not yet been initialized
1382 if (!strlen($this->DBFields["AuthoringPrivileges"]))
1383 {
1384 # set default values for privileges from metadata schema
1385 $Schema = new MetadataSchema($Row["SchemaId"]);
1386 $this->AuthoringPrivileges($Schema->AuthoringPrivileges());
1387 $this->EditingPrivileges($Schema->EditingPrivileges());
1388 $this->ViewingPrivileges($Schema->ViewingPrivileges());
1389 $this->PreviewingPrivileges($Schema->ViewingPrivileges());
1390 }
1391 else
1392 {
1393 # set privileges from stored values
1394 $this->AuthoringPrivileges = new PrivilegeSet(
1395 $Row["AuthoringPrivileges"]);
1396 $this->EditingPrivileges = new PrivilegeSet(
1397 $Row["EditingPrivileges"]);
1398 $this->ViewingPrivileges = new PrivilegeSet(
1399 $Row["ViewingPrivileges"]);
1400 $this->PreviewingPrivileges = new PrivilegeSet(
1401 $Row["PreviewingPrivileges"]);
1402 }
1403
1404 # set database column name
1405 $this->DBFields["DBFieldName"] =
1406 $this->NormalizeFieldNameForDB($this->DBFields["FieldName"]);
1407 }
1408
1413 public static $FixedDefaults = array(
1414 "Label" => NULL,
1415 "Description" => NULL,
1416 "Instructions" => NULL,
1417 "Enabled" => TRUE,
1418 "Optional" => TRUE,
1419 "Editable" => TRUE,
1420 "AllowMultiple" => FALSE,
1421 "IncludeInKeywordSearch" => FALSE,
1422 "IncludeInAdvancedSearch" => FALSE,
1423 "IncludeInFacetedSearch" => FALSE,
1424 "IncludeInSortOptions" => TRUE,
1425 "IncludeInRecommender" => FALSE,
1426 "ParagraphRows" => 4,
1427 "ParagraphCols" => 50,
1428 "MinValue" => 1,
1429 "FlagOnLabel" => "On",
1430 "FlagOffLabel" => "Off",
1431 "DateFormat" => NULL,
1432 "RecommenderWeight" => 1,
1433 "MaxHeight" => 500,
1434 "MaxWidth" => 500,
1435 "MaxPreviewHeight" => 100,
1436 "MaxPreviewWidth" => 100,
1437 "MaxThumbnailHeight" => 50,
1438 "MaxThumbnailWidth" => 50,
1439 "DefaultAltText" => NULL,
1440 "UsesQualifiers" => FALSE,
1441 "HasItemLevelQualifiers" => FALSE,
1442 "ShowQualifiers" => FALSE,
1443 "DefaultQualifier" => NULL,
1444 "AllowHTML" => FALSE,
1445 "UseWysiwygEditor" => FALSE,
1446 "UseForOaiSets" => FALSE,
1447 "OptionListThreshold" => 25,
1448 "AjaxThreshold" => 50,
1449 "NumAjaxResults" => 50,
1450 "ViewingPrivilege" => NULL,
1451 "AuthoringPrivilege" => PRIV_MYRESOURCEADMIN,
1452 "EditingPrivilege" => PRIV_RESOURCEADMIN,
1453 "ImagePreviewPrivilege" => NULL,
1454 "TreeBrowsingPrivilege" => NULL,
1455 "ViewingUserIsValue" => self::USERISVALUE_UNSET,
1456 "AuthoringUserIsValue" => self::USERISVALUE_UNSET,
1457 "EditingUserIsValue" => self::USERISVALUE_UNSET,
1458 "ViewingUserValue" => NULL,
1459 "AuthoringUserValue" => NULL,
1460 "EditingUserValue" => NULL,
1461 "PointPrecision" => 8,
1462 "PointDecimalDigits" => 5,
1463 "UserPrivilegeRestrictions" => array(),
1464 "UpdateMethod" => "NoAutoUpdate",
1465 # 9999 is the default max value because default number field length is 4
1466 "MaxValue" => 9999);
1467
1472 public static $TypeBasedDefaults = array(
1473 MetadataSchema::MDFTYPE_TEXT => array(
1474 "DefaultValue" => NULL,
1475 "SearchWeight" => 1,
1476 "TextFieldSize" => 50,
1477 "MaxLength" => 100),
1478 MetadataSchema::MDFTYPE_PARAGRAPH => array(
1479 "DefaultValue" => NULL,
1480 "SearchWeight" => 1,
1481 "TextFieldSize" => 50,
1482 "MaxLength" => 100),
1483 MetadataSchema::MDFTYPE_NUMBER => array(
1484 "DefaultValue" => NULL,
1485 "SearchWeight" => 1,
1486 "TextFieldSize" => 4,
1487 "MaxLength" => 100),
1488 MetadataSchema::MDFTYPE_DATE => array(
1489 "DefaultValue" => NULL,
1490 "SearchWeight" => 1,
1491 "TextFieldSize" => 10,
1492 "MaxLength" => 100),
1493 MetadataSchema::MDFTYPE_TIMESTAMP => array(
1494 "DefaultValue" => NULL,
1495 "SearchWeight" => 1,
1496 "TextFieldSize" => 50,
1497 "MaxLength" => 100),
1498 MetadataSchema::MDFTYPE_FLAG => array(
1499 "DefaultValue" => NULL,
1500 "SearchWeight" => 1,
1501 "TextFieldSize" => 50,
1502 "MaxLength" => 100),
1503 MetadataSchema::MDFTYPE_TREE => array(
1504 "DefaultValue" => NULL,
1505 "SearchWeight" => 1,
1506 "TextFieldSize" => 50,
1507 "MaxLength" => 100),
1508 MetadataSchema::MDFTYPE_CONTROLLEDNAME => array(
1509 "DefaultValue" => NULL,
1510 "SearchWeight" => 3,
1511 "TextFieldSize" => 50,
1512 "MaxLength" => 100),
1513 MetadataSchema::MDFTYPE_OPTION => array(
1514 "DefaultValue" => NULL,
1515 "SearchWeight" => 3,
1516 "TextFieldSize" => 50,
1517 "MaxLength" => 100),
1518 MetadataSchema::MDFTYPE_USER => array(
1519 "DefaultValue" => NULL,
1520 "SearchWeight" => 1,
1521 "TextFieldSize" => 50,
1522 "MaxLength" => 100),
1523 MetadataSchema::MDFTYPE_IMAGE => array(
1524 "DefaultValue" => NULL,
1525 "SearchWeight" => 1,
1526 "TextFieldSize" => 50,
1527 "MaxLength" => 100),
1528 MetadataSchema::MDFTYPE_FILE => array(
1529 "DefaultValue" => NULL,
1530 "SearchWeight" => 1,
1531 "TextFieldSize" => 50,
1532 "MaxLength" => 100),
1533 MetadataSchema::MDFTYPE_URL => array(
1534 "DefaultValue" => NULL,
1535 "SearchWeight" => 1,
1536 "TextFieldSize" => 50,
1537 "MaxLength" => 255),
1538 MetadataSchema::MDFTYPE_POINT => array(
1539 "DefaultValue" => array("X" => NULL, "Y" => NULL),
1540 "SearchWeight" => 1,
1541 "TextFieldSize" => 10,
1542 "MaxLength" => 100),
1543 MetadataSchema::MDFTYPE_REFERENCE => array(
1544 "DefaultValue" => NULL,
1545 "SearchWeight" => 1,
1546 "TextFieldSize" => 50,
1547 "MaxLength" => 100));
1548
1553 public function SetDefaults()
1554 {
1555 # set defaults that are the same for every field
1556 foreach (self::$FixedDefaults as $Key => $Value)
1557 {
1558 $this->$Key($Value);
1559 }
1560
1561 # set defaults that depend on the type of the field
1562 foreach (self::$TypeBasedDefaults[$this->Type()] as $Key => $Value)
1563 {
1564 $this->$Key($Value);
1565 }
1566
1567 # tweak the update method if dealing with the date of record creation
1568 if ($this->Name() == "Date Of Record Creation")
1569 {
1570 $this->UpdateMethod("OnRecordCreate");
1571 }
1572 }
1573
1574 # remove field from database (only for use by MetadataSchema object)
1575 function Drop()
1576 {
1577 # clear other database entries as appropriate for field type
1578 $DB = $this->DB;
1579 $DBFieldName = $this->DBFields["DBFieldName"];
1580 $Schema = new MetadataSchema($this->SchemaId());
1581 switch (MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]])
1582 {
1583 case MetadataSchema::MDFTYPE_TEXT:
1584 case MetadataSchema::MDFTYPE_PARAGRAPH:
1585 case MetadataSchema::MDFTYPE_NUMBER:
1586 case MetadataSchema::MDFTYPE_USER:
1587 case MetadataSchema::MDFTYPE_IMAGE:
1588 case MetadataSchema::MDFTYPE_TIMESTAMP:
1589 case MetadataSchema::MDFTYPE_URL:
1590 case MetadataSchema::MDFTYPE_FLAG:
1591 # remove field from resources table
1592 if ($DB->FieldExists("Resources", $DBFieldName))
1593 {
1594 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."`");
1595 }
1596 break;
1597
1598 case MetadataSchema::MDFTYPE_POINT:
1599 if ($DB->FieldExists("Resources", $DBFieldName."X"))
1600 {
1601 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."X`");
1602 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Y`");
1603 }
1604 break;
1605
1606 case MetadataSchema::MDFTYPE_DATE:
1607 # remove fields from resources table
1608 if ($DB->FieldExists("Resources", $DBFieldName."Begin"))
1609 {
1610 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Begin`");
1611 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."End`");
1612 $DB->Query("ALTER TABLE Resources DROP COLUMN `".$DBFieldName."Precision`");
1613 }
1614 break;
1615
1616 case MetadataSchema::MDFTYPE_TREE:
1617 $DB->Query("SELECT ClassificationId FROM Classifications "
1618 ."WHERE FieldId = ".$this->Id());
1619 $TempDB = new Database();
1620 while ($ClassificationId = $DB->FetchField("ClassificationId"))
1621 {
1622 # remove any resource / name intersections
1623 $TempDB->Query("DELETE FROM ResourceClassInts WHERE "
1624 ."ClassificationId = ".$ClassificationId);
1625
1626 # remove controlled name
1627 $TempDB->Query("DELETE FROM Classifications WHERE "
1628 ."ClassificationId = ".$ClassificationId);
1629 }
1630 break;
1631
1632 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1633 case MetadataSchema::MDFTYPE_OPTION:
1634 $DB->Query("SELECT ControlledNameId FROM ControlledNames "
1635 ."WHERE FieldId = ".$this->Id());
1636 $TempDB = new Database();
1637 while ($ControlledNameId = $DB->FetchField("ControlledNameId"))
1638 {
1639 # remove any resource / name intersections
1640 $TempDB->Query("DELETE FROM ResourceNameInts WHERE "
1641 ."ControlledNameId = ".$ControlledNameId);
1642
1643 # remove any variant names
1644 $TempDB->Query("DELETE FROM VariantNames WHERE "
1645 ."ControlledNameId = ".$ControlledNameId);
1646
1647 # remove controlled name
1648 $TempDB->Query("DELETE FROM ControlledNames WHERE "
1649 ."ControlledNameId = ".$ControlledNameId);
1650 }
1651 break;
1652
1653 case MetadataSchema::MDFTYPE_FILE:
1654 # for each file associated with this field
1655 $DB->Query("SELECT FileId FROM Files WHERE FieldId = '".$this->Id()."'");
1656 while ($FileId = $DB->FetchRow())
1657 {
1658 # delete file
1659 $File = new File(intval($FileId));
1660 $File->Delete();
1661 }
1662 break;
1663
1664 case MetadataSchema::MDFTYPE_REFERENCE:
1665 # remove any resource references for the field
1666 $DB->Query("
1667 DELETE FROM ReferenceInts
1668 WHERE FieldId = '".addslashes($this->Id())."'");
1669 break;
1670 }
1671
1672 # remove field from database
1673 $DB->Query("DELETE FROM MetadataFields "
1674 ."WHERE FieldId = '".$this->DBFields["FieldId"]."'");
1675
1676 # remove any qualifier associations
1677 $DB->Query("DELETE FROM FieldQualifierInts WHERE MetadataFieldId = '"
1678 .$this->DBFields["FieldId"]."'");
1679
1680 # get the order objects the field is part of
1681 foreach (MetadataFieldOrder::GetOrdersForSchema($Schema) as $Order)
1682 {
1683 # remove it if it's a direct descendant
1684 $Order->RemoveItem($this->Id(), "MetadataField");
1685
1686 # also make sure to remove it if it's part of a group
1687 foreach ($Order->GetItemIds() as $Item)
1688 {
1689 if ($Item["Type"] == "MetadataFieldGroup")
1690 {
1691 $Group = new MetadataFieldGroup($Item["ID"]);
1692 $Group->RemoveItem($this->Id(), "MetadataField");
1693 }
1694 }
1695 }
1696 }
1697
1698 # modify any database fields
1699 private function ModifyField($NewName = NULL, $NewType = NULL)
1700 {
1701 # grab old DB field name
1702 $OldDBFieldName = $this->DBFields["DBFieldName"];
1703 $OldFieldType = NULL;
1704
1705 # if new field name supplied
1706 if ($NewName != NULL)
1707 {
1708 # cache the old name for options and controllednames below
1709 $OldName = $this->DBFields["FieldName"];
1710
1711 # store new name
1712 $this->UpdateValue("FieldName", $NewName);
1713
1714 # determine new DB field name
1715 $NewDBFieldName = $this->NormalizeFieldNameForDB($NewName);
1716
1717 # store new database field name
1718 $this->DBFields["DBFieldName"] = $NewDBFieldName;
1719 }
1720 else
1721 {
1722 # set new field name equal to old field name
1723 $NewDBFieldName = $OldDBFieldName;
1724 }
1725
1726 # if new type supplied
1727 if ($NewType != NULL)
1728 {
1729 # grab old field type
1730 $OldFieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
1731
1732 # store new field type
1733 $this->UpdateValue("FieldType", MetadataField::$FieldTypeDBEnums[$NewType]);
1734 }
1735
1736 # if this is not a temporary field
1737 if ($this->Id() >= 0)
1738 {
1739 # modify field in DB as appropriate for field type
1740 $DB = $this->DB;
1741 $FieldType = MetadataField::$FieldTypePHPEnums[$this->DBFields["FieldType"]];
1742 switch ($FieldType)
1743 {
1744 case MetadataSchema::MDFTYPE_TEXT:
1745 case MetadataSchema::MDFTYPE_PARAGRAPH:
1746 case MetadataSchema::MDFTYPE_URL:
1747 # alter field declaration in Resources table
1748 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1749 .$OldDBFieldName."` `"
1750 .$NewDBFieldName."` TEXT "
1751 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1752 break;
1753
1754 case MetadataSchema::MDFTYPE_NUMBER:
1755 case MetadataSchema::MDFTYPE_USER:
1756 # alter field declaration in Resources table
1757 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1758 .$OldDBFieldName."` `"
1759 .$NewDBFieldName."` INT "
1760 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1761 break;
1762
1763 case MetadataSchema::MDFTYPE_POINT:
1764 $Precision = $this->UpdateValue("PointPrecision",
1765 DB_NOVALUE);
1766 $Digits = $this->UpdateValue("PointDecimalDigits",
1767 DB_NOVALUE);
1768 $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
1769 ."`".$OldDBFieldName."X` "
1770 ."`".$NewDBFieldName."X`".
1771 " DECIMAL(".$Precision.",".$Digits.")");
1772 $DB->Query("ALTER TABLE Resources CHANGE COLUMN "
1773 ."`".$OldDBFieldName."Y` "
1774 ."`".$NewDBFieldName."Y`".
1775 " DECIMAL(".$Precision.",".$Digits.")");
1776 break;
1777
1778 case MetadataSchema::MDFTYPE_FILE:
1779 # if DB field name has changed
1780 if ($NewDBFieldName != $OldDBFieldName)
1781 {
1782 # alter field declaration in Resources table
1783 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1784 .$OldDBFieldName."` `"
1785 .$NewDBFieldName."` TEXT");
1786 }
1787 break;
1788
1789 case MetadataSchema::MDFTYPE_FLAG:
1790 # alter field declaration in Resources table
1791 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1792 .$OldDBFieldName."` `"
1793 .$NewDBFieldName."` INT"
1794 ." DEFAULT ".intval($this->DefaultValue()));
1795
1796 # set any unset values to default
1797 $DB->Query("UPDATE Resources SET `".$NewDBFieldName
1798 ."` = ".intval($this->DefaultValue())
1799 ." WHERE `".$NewDBFieldName."` IS NULL");
1800 break;
1801
1802 case MetadataSchema::MDFTYPE_DATE:
1803 # if new type supplied and new type is different from old
1804 if (($NewType != NULL) && ($NewType != $OldFieldType))
1805 {
1806 # if old type was time stamp
1807 if ($OldFieldType == MetadataSchema::MDFTYPE_TIMESTAMP)
1808 {
1809 # change time stamp field in resources table to begin date
1810 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1811 .$OldDBFieldName."` `"
1812 .$NewDBFieldName."Begin` DATE "
1813 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1814
1815 # add end date and precision fields
1816 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."End"
1817 ."` DATE");
1818 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$NewDBFieldName."Precision`"
1819 ." INT ".($Optional ? "" : "NOT NULL"));
1820
1821 # set precision to reflect time stamp content
1822 $DB->Query("UPDATE Resources SET `".$NewDBFieldName."Precision` = "
1823 .(DATEPRE_BEGINYEAR|DATEPRE_BEGINMONTH|DATEPRE_BEGINDAY));
1824 }
1825 else
1826 {
1827 exit("<br>ERROR: Attempt to convert metadata field to date from type other than timestamp<br>\n");
1828 }
1829 }
1830 else
1831 {
1832 # change name of fields
1833 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1834 .$OldDBFieldName."Begin` `"
1835 .$NewDBFieldName."Begin` DATE "
1836 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1837 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1838 .$OldDBFieldName."End` `"
1839 .$NewDBFieldName."End` DATE "
1840 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1841 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1842 .$OldDBFieldName."Precision` `"
1843 .$NewDBFieldName."Precision` INT "
1844 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1845 }
1846 break;
1847
1848 case MetadataSchema::MDFTYPE_TIMESTAMP:
1849 # if new type supplied and new type is different from old
1850 if (($NewType != NULL) && ($NewType != $OldFieldType))
1851 {
1852 # if old type was date
1853 if ($OldFieldType == MetadataSchema::MDFTYPE_DATE)
1854 {
1855 # change begin date field in resource table to time stamp
1856 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1857 .$OldDBFieldName."Begin` `"
1858 .$NewDBFieldName."` DATETIME "
1859 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1860
1861 # drop end date and precision fields
1862 $DB->Query("ALTER TABLE Resources DROP COLUMN `"
1863 .$OldDBFieldName."End`");
1864 $DB->Query("ALTER TABLE Resources DROP COLUMN `"
1865 .$OldDBFieldName."Precision`");
1866 }
1867 else
1868 {
1869 exit("<br>ERROR: Attempt to convert metadata field to time stamp from type other than date<br>\n");
1870 }
1871 }
1872 else
1873 {
1874 # change name of field
1875 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1876 .$OldDBFieldName."` `"
1877 .$NewDBFieldName."` DATETIME "
1878 .($this->DBFields["Optional"] ? "" : "NOT NULL"));
1879 }
1880 break;
1881
1882 case MetadataSchema::MDFTYPE_TREE:
1883 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1884 case MetadataSchema::MDFTYPE_OPTION:
1885 case MetadataSchema::MDFTYPE_REFERENCE:
1886 case MetadataSchema::MDFTYPE_IMAGE:
1887 break;
1888 }
1889
1890 # if qualifier DB field exists
1891 if ($DB->FieldExists("Resources", $OldDBFieldName."Qualifier"))
1892 {
1893 # rename qualifier DB field
1894 $DB->Query("ALTER TABLE Resources CHANGE COLUMN `"
1895 .$OldDBFieldName."Qualifier` `"
1896 .$NewDBFieldName."Qualifier` INT ");
1897 }
1898 }
1899 }
1900
1901 # convenience functions to supply parameters to Database->UpdateValue()
1902 private function UpdateValue($FieldName, $NewValue)
1903 {
1904 return $this->DB->UpdateValue("MetadataFields", $FieldName, $NewValue,
1905 "FieldId = ".intval($this->DBFields["FieldId"]),
1906 $this->DBFields);
1907 }
1908 private function UpdateIntValue($FieldName, $NewValue)
1909 {
1910 return $this->DB->UpdateIntValue("MetadataFields", $FieldName, $NewValue,
1911 "FieldId = ".intval($this->DBFields["FieldId"]),
1912 $this->DBFields);
1913 }
1914 private function UpdateFloatValue($FieldName, $NewValue)
1915 {
1916 return $this->DB->UpdateFloatValue("MetadataFields", $FieldName, $NewValue,
1917 "FieldId = ".intval($this->DBFields["FieldId"]),
1918 $this->DBFields);
1919 }
1920 private function UpdateBoolValue($FieldName, $NewValue)
1921 {
1922 $NewValue = $this->TranslateStringToConstants($NewValue);
1923 return $this->DB->UpdateIntValue("MetadataFields", $FieldName, $NewValue,
1924 "FieldId = ".intval($this->DBFields["FieldId"]),
1925 $this->DBFields);
1926 }
1927 private function UpdateConstValue($FieldName, $NewValue, $ClassName=NULL)
1928 {
1929 $NewValue = $this->TranslateStringToConstants($NewValue, $ClassName);
1930 return $this->DB->UpdateIntValue("MetadataFields", $FieldName, $NewValue,
1931 "FieldId = ".intval($this->DBFields["FieldId"]),
1932 $this->DBFields);
1933 }
1934
1935 # normalize field name for use as database field name
1936 private function NormalizeFieldNameForDB($Name)
1937 {
1938 return preg_replace("/[^a-z0-9]/i", "", $Name)
1939 .(($this->SchemaId() != MetadataSchema::SCHEMAID_DEFAULT)
1940 ? $this->SchemaId() : "");
1941 }
1942
1943 # add any needed database fields and/or entries
1944 private function AddDatabaseFields()
1945 {
1946 # grab values for common use
1947 $DB = $this->DB;
1948 $FieldName = $this->Name();
1949 $DBFieldName = $this->DBFieldName();
1950 $Optional = $this->Optional();
1951 $DefaultValue = $this->DefaultValue();
1952
1953 # set up field(s) based on field type
1954 switch ($this->Type())
1955 {
1956 case MetadataSchema::MDFTYPE_TEXT:
1957 case MetadataSchema::MDFTYPE_PARAGRAPH:
1958 case MetadataSchema::MDFTYPE_URL:
1959 # add field to resources table (if not already present)
1960 if (!$DB->FieldExists("Resources", $DBFieldName))
1961 {
1962 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
1963 ."` TEXT ".($Optional ? "" : "NOT NULL"));
1964 }
1965
1966 # if default value supplied
1967 if ($DefaultValue != NULL)
1968 {
1969 # set all existing records to default value
1970 $DB->Query("UPDATE Resources SET `"
1971 .$DBFieldName."` = '".addslashes($DefaultValue)."'");
1972 }
1973 break;
1974
1975 case MetadataSchema::MDFTYPE_NUMBER:
1976 # add field to resources table (if not already present)
1977 if (!$DB->FieldExists("Resources", $DBFieldName))
1978 {
1979 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
1980 ."` INT ".($Optional ? "" : "NOT NULL"));
1981 }
1982
1983 # if default value supplied
1984 if ($DefaultValue != NULL)
1985 {
1986 # set all existing records to default value
1987 $DB->Query("UPDATE Resources SET `"
1988 .$DBFieldName."` = '".addslashes($DefaultValue)."'");
1989 }
1990 break;
1991
1992 case MetadataSchema::MDFTYPE_POINT:
1993 if (!$DB->FieldExists("Resources", $DBFieldName."X"))
1994 {
1995 $Precision = $this->UpdateValue("PointPrecision",
1996 DB_NOVALUE);
1997 $Digits = $this->UpdateValue("PointDecimalDigits",
1998 DB_NOVALUE);
1999
2000 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2001 .$DBFieldName."X`".
2002 " DECIMAL(".$Precision.",".$Digits.")");
2003 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2004 .$DBFieldName."Y`".
2005 " DECIMAL(".$Precision.",".$Digits.")");
2006 }
2007
2008 break;
2009 case MetadataSchema::MDFTYPE_FLAG:
2010 # if field is not already present in database
2011 if (!$DB->FieldExists("Resources", $DBFieldName))
2012 {
2013 # add field to resources table
2014 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
2015 ."` INT DEFAULT ".intval($DefaultValue));
2016
2017 # set all existing records to default value
2018 $DB->Query("UPDATE Resources SET `"
2019 .$DBFieldName."` = ".intval($DefaultValue));
2020 }
2021 break;
2022
2023 case MetadataSchema::MDFTYPE_USER:
2024 # add field to resources table (if not already present)
2025 if (!$DB->FieldExists("Resources", $DBFieldName))
2026 {
2027 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
2028 ."` INT ".($Optional ? "" : "NOT NULL"));
2029 }
2030 break;
2031
2032 case MetadataSchema::MDFTYPE_FILE:
2033 # add fields to resources table (if not already present)
2034 if (!$DB->FieldExists("Resources", $DBFieldName))
2035 {
2036 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2037 .$DBFieldName."` TEXT");
2038 }
2039 break;
2040
2041 case MetadataSchema::MDFTYPE_IMAGE:
2042 # add fields to resources table (if not already present)
2043 if (!$DB->FieldExists("Resources", $DBFieldName))
2044 {
2045 $DB->Query("ALTER TABLE Resources ADD COLUMN `"
2046 .$DBFieldName."` INT");
2047 }
2048 break;
2049
2050 case MetadataSchema::MDFTYPE_DATE:
2051 # add fields to resources table (if not already present)
2052 if (!$DB->FieldExists("Resources", $DBFieldName."Begin"))
2053 {
2054 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Begin`"
2055 ." DATE ".($Optional ? "" : "NOT NULL"));
2056 }
2057 if (!$DB->FieldExists("Resources", $DBFieldName."End"))
2058 {
2059 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."End`"
2060 ." DATE");
2061 }
2062 if (!$DB->FieldExists("Resources", $DBFieldName."Precision"))
2063 {
2064 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName."Precision`"
2065 ." INT ".($Optional ? "" : "NOT NULL"));
2066 }
2067 break;
2068
2069 case MetadataSchema::MDFTYPE_TIMESTAMP:
2070 # add fields to resources table (if not already present)
2071 if (!$DB->FieldExists("Resources", $DBFieldName))
2072 {
2073 $DB->Query("ALTER TABLE Resources ADD COLUMN `".$DBFieldName
2074 ."` DATETIME ".($Optional ? "" : "NOT NULL"));
2075 }
2076 break;
2077
2078 case MetadataSchema::MDFTYPE_TREE:
2079 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
2080 case MetadataSchema::MDFTYPE_OPTION:
2081 case MetadataSchema::MDFTYPE_REFERENCE:
2082 break;
2083
2084 default:
2085 exit("<br>ERROR: Attempt to add database fields for illegal metadata field type<br>\n");
2086 break;
2087 }
2088 }
2089
2097 private function TranslateStringToConstants($CString, $ClassName = NULL)
2098 {
2099 # if not a string return value unchanged to caller
2100 if (!is_string($CString) || ($CString === DB_NOVALUE))
2101 {
2102 $ReturnValue = $CString;
2103 }
2104 # handle booleans as a special case
2105 elseif (strtoupper(trim($CString)) == "TRUE")
2106 {
2107 $ReturnValue = TRUE;
2108 }
2109 elseif (strtoupper(trim($CString)) == "FALSE")
2110 {
2111 $ReturnValue = FALSE;
2112 }
2113 else
2114 {
2115 # assume no values will be found
2116 $ReturnValue = NULL;
2117
2118 # split apart any ORed-together values
2119 $Values = explode("|", $CString);
2120
2121 # for each value found
2122 foreach ($Values as $Value)
2123 {
2124 # trim off any extraneous whitespace
2125 $Value = trim($Value);
2126
2127 # add class name prefix to constant name if requested
2128 if ($ClassName) { $Value = $ClassName."::".$Value; }
2129
2130 # if value corresponds to a constant
2131 if (defined($Value))
2132 {
2133 # add constant to return value
2134 $ReturnValue = ($ReturnValue === NULL)
2135 ? constant($Value)
2136 : ($ReturnValue | constant($Value));
2137 }
2138 }
2139
2140 # if no corresponding constants were found
2141 if ($ReturnValue === NULL)
2142 {
2143 # return original value to caller
2144 $ReturnValue = $CString;
2145 }
2146 }
2147
2148 # return result to caller
2149 return $ReturnValue;
2150 }
2151
2152 }
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:1209
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:1413
DATEPRE_BEGINDAY
const DATEPRE_BEGINDAY
Definition: Axis--Date.php:747
MetadataField\$FieldTypeDBAllowedEnums
static $FieldTypeDBAllowedEnums
Definition: MetadataField.php:1226
MetadataField\SetDefaults
SetDefaults()
Set defaults values for the field.
Definition: MetadataField.php:1553
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:1243
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:892
MetadataField\__construct
__construct($FieldId)
Object contstructor, used to load an existing metadata field.
Definition: MetadataField.php:1363
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:1282
MetadataField\ValueUseCount
ValueUseCount($Value)
Check how many times a specific value is currently used for this field.
Definition: MetadataField.php:932
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:1054
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:1091
MetadataField\GetFactory
GetFactory()
Definition: MetadataField.php:1125
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:620
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:586
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:1472
MetadataField\Description
Description($NewValue=DB_NOVALUE)
Get/set field description.
Definition: MetadataField.php:415
MetadataField\EditingUserValue
EditingUserValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:590
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:995
MetadataField\AssociateWithQualifier
AssociateWithQualifier($Qualifier)
DEPRECATED METHOD.
Definition: MetadataField.php:1105
MetadataField\AuthoringPrivilege
AuthoringPrivilege($NewValue=DB_NOVALUE)
Definition: MetadataField.php:570
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:1261
MetadataField\Viewable
Viewable()
This function has been deprecated and should not be used.
Definition: MetadataField.php:1150
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:1191
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:595
MetadataField\AddQualifier
AddQualifier($Qualifier)
Associate qualifier with field.
Definition: MetadataField.php:1071
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:669
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:644
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:572
MetadataField\RequiredBySPT
RequiredBySPT($NewValue=DB_NOVALUE)
Definition: MetadataField.php:592
MetadataField\AuthoringUserIsValue
AuthoringUserIsValue($NewValue=DB_NOVALUE)
Definition: MetadataField.php:582
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:1032
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:1575
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:580
MetadataSchema\MDFTYPE_URL
const MDFTYPE_URL
Definition: MetadataSchema.php:32
MetadataField\EnableOnOwnerReturn
EnableOnOwnerReturn($NewValue=DB_NOVALUE)
Definition: MetadataField.php:578
MetadataField\FlagOnLabel
FlagOnLabel($NewValue=DB_NOVALUE)
Definition: MetadataField.php:526
MetadataField\TreeBrowsingPrivilege
TreeBrowsingPrivilege($NewValue=DB_NOVALUE)
Definition: MetadataField.php:576
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:584
MetadataField\GetPossibleValues
GetPossibleValues($MaxNumberOfValues=NULL, $Offset=0)
Definition: MetadataField.php:747
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:860
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:574
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:1109
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:822
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:588
MetadataField\UpdateMethod
UpdateMethod($NewValue=DB_NOVALUE)
Get/set method by which field is updated.
Definition: MetadataField.php:739