CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Resource.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Resource.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2011-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
13 class Resource {
14
15 # ---- PUBLIC INTERFACE --------------------------------------------------
16
23 function __construct($ResourceId)
24 {
25 $this->DB = new Database();
26
27 # save resource ID
28 $this->Id = intval($ResourceId);
29
30 # locate resource in database
31 $this->DB->Query("SELECT * FROM Resources WHERE ResourceId = ".$this->Id);
32
33 # if unable to locate resource
34 $Record = $this->DB->FetchRow();
35 if ($Record == FALSE)
36 {
37 # set status to -1 to indicate that creation failed
38 $this->LastStatus = -1;
39 }
40 else
41 {
42 # load in attributes from database
43 $this->DBFields = $Record;
44 $this->CumulativeRating = $Record["CumulativeRating"];
45
46 # load our local metadata schema
47 $this->Schema = new MetadataSchema($this->DBFields["SchemaId"]);
48
49 # set status to 1 to indicate that creation succeeded
50 $this->LastStatus = 1;
51 }
52 }
53
60 static function Create($SchemaId)
61 {
62 # clean out any temp resource records more than three days old
63 $RFactory = new ResourceFactory();
64 $RFactory->CleanOutStaleTempItems(60 * 24 * 3);
65
66 # lock DB tables to prevent next ID from being grabbed
67 $DB = new Database;
68 $DB->Query("LOCK TABLES Resources WRITE");
69
70 # find next temp resource ID
71 $Id = $RFactory->GetNextTempItemId();
72
73 # write out new resource record with temp resource ID
74 # Set DateLastModified = NOW() to avoid being pruned as a
75 # stale temp resource.
76 $DB->Query(
77 "INSERT INTO Resources
78 SET `ResourceId` = '".intval($Id)."',
79 `SchemaId` = '".intval($SchemaId)."',
80 `DateLastModified` = NOW() " );
81
82 # release DB tables
83 $DB->Query("UNLOCK TABLES");
84
85 # create new Resource object
86 $Resource = new Resource($Id);
87
88 if ($Resource->Status() == -1)
89 {
90 throw new Exception("Resource creation failed");
91 }
92
93 # set some additional fields for default resources
94 if ($SchemaId == MetadataSchema::SCHEMAID_DEFAULT)
95 {
96 $Resource->Set("Added By Id", $GLOBALS["G_User"]->Id());
97 $Resource->Set("Last Modified By Id", $GLOBALS["G_User"]->Id());
98 $Resource->Set("Date Of Record Creation", date("Y-m-d H:i:s"));
99 $Resource->Set("Date Last Modified", date("Y-m-d H:i:s"));
100 }
101
102 # set any default values
103 $Schema = new MetadataSchema($SchemaId);
104 $Fields = $Schema->GetFields(MetadataSchema::MDFTYPE_OPTION
105 |MetadataSchema::MDFTYPE_TEXT
106 |MetadataSchema::MDFTYPE_FLAG
107 |MetadataSchema::MDFTYPE_NUMBER
108 |MetadataSchema::MDFTYPE_POINT);
109 foreach ($Fields as $Field)
110 {
111 $DefaultValue = $Field->DefaultValue();
112
113 # flip option default values to get into the form that
114 # Resource::Set() expects
115 if ($Field->Type() == MetadataSchema::MDFTYPE_OPTION
116 && is_array($DefaultValue))
117 {
118 $DefaultValue = array_flip($DefaultValue);
119 }
120
121 # there is no default value when DefaultValue is null,
122 # or when it is an empty array
123 if ( !($DefaultValue === NULL ||
124 (is_array($DefaultValue) && empty($DefaultValue)) ))
125 {
126 $Resource->SetByField($Field, $DefaultValue);
127 }
128 }
129
130 # update timestamps as required
131 $TimestampFields = $Schema->GetFields(MetadataSchema::MDFTYPE_TIMESTAMP);
132 foreach ($TimestampFields as $Field)
133 {
134 if ($Field->UpdateMethod() ==
135 MetadataField::UPDATEMETHOD_ONRECORDCREATE)
136 {
137 $Resource->SetByField($Field, "now");
138 }
139 }
140
141 # signal resource creation
142 $GLOBALS["AF"]->SignalEvent("EVENT_RESOURCE_CREATE", array(
143 "Resource" => $Resource,
144 ));
145
146 # return new Resource object to caller
147 return $Resource;
148 }
149
154 function Delete()
155 {
156 global $SysConfig;
157
158 # signal that resource deletion is about to occur
159 global $AF;
160 $AF->SignalEvent("EVENT_RESOURCE_DELETE", array(
161 "Resource" => $this,
162 ));
163
164 # grab list of classifications
165 $Classifications = $this->Classifications();
166
167 # delete resource/classification intersections
168 $DB = $this->DB;
169 $DB->Query("DELETE FROM ResourceClassInts WHERE ResourceId = ".$this->Id());
170
171 # for each classification type
172 foreach ($Classifications as $ClassType => $ClassesOfType)
173 {
174 # for each classification of that type
175 foreach ($ClassesOfType as $ClassId => $ClassName)
176 {
177 # recalculate resource count for classification
178 $Class = new Classification($ClassId);
179 $Class->RecalcResourceCount();
180 }
181 }
182
183 # delete resource references
184 $DB->Query("
185 DELETE FROM ReferenceInts
186 WHERE SrcResourceId = '".addslashes($this->Id())."'
187 OR DstResourceId = '".addslashes($this->Id())."'");
188
189 # delete resource/name intersections
190 $DB->Query("DELETE FROM ResourceNameInts WHERE ResourceId = ".$this->Id());
191
192 # get the list of all images associated with this resource
193 $DB->Query("SELECT ImageId FROM ResourceImageInts"
194 ." WHERE ResourceId = ".intval($this->Id()));
195 $ImageIds = $DB->FetchColumn("ImageId");
196
197 # disassociate this resource from all images
198 $DB->Query("DELETE FROM ResourceImageInts"
199 ." WHERE ResourceId = ".intval($this->Id()));
200
201 # delete any images that no longer belong to any resources
202 foreach ($ImageIds as $ImageId)
203 {
204 $DB->Query("SELECT ResourceId FROM ResourceImageInts"
205 ." WHERE ImageId = ".intval($ImageId) );
206 if ($DB->NumRowsSelected() == 0)
207 {
208 $Image = new SPTImage($ImageId);
209 $Image->Delete();
210 }
211 }
212
213 # delete any associated files
214 $Factory = new FileFactory(NULL);
215 $Files = $Factory->GetFilesForResource($this->Id());
216 foreach ($Files as $File)
217 {
218 $File->Delete();
219 }
220
221 # delete resource record from database
222 $DB->Query("DELETE FROM Resources WHERE ResourceId = ".$this->Id());
223
224 # drop item from search engine and recommender system
225 if ($SysConfig->SearchDBEnabled())
226 {
227 $SearchEngine = new SPTSearchEngine();
228 $SearchEngine->DropItem($this->Id());
229 }
230 if ($SysConfig->RecommenderDBEnabled())
231 {
232 $Recommender = new SPTRecommender();
233 $Recommender->DropItem($this->Id());
234 }
235
236 # get the folders containing the resource
237 $FolderFactory = new FolderFactory();
238 $Folders = $FolderFactory->GetFoldersContainingItem(
239 $this->Id,
240 "Resource");
241
242 # drop the resource from each folder it belongs to
243 foreach ($Folders as $Folder)
244 {
245 # mixed item type folder
246 if ($Folder->ContainsItem($this->Id, "Resource"))
247 {
248 $Folder->RemoveItem($this->Id, "Resource");
249 }
250
251 # single item type folder
252 else
253 {
254 $Folder->RemoveItem($this->Id);
255 }
256 }
257
258 # delete any resource comments
259 $DB->Query("DELETE FROM Messages WHERE ParentId = ".$this->Id);
260 }
261
266 function Status()
267 {
268 return $this->LastStatus;
269 }
270
275 function Id()
276 {
277 return $this->Id;
278 }
279
284 function SchemaId()
285 {
286 return $this->DBFields["SchemaId"];
287 }
288
295 function IsTempResource($NewSetting = NULL)
296 {
297 # if new temp resource setting supplied
298 if (!is_null($NewSetting))
299 {
300 # if caller requested to switch
301 $DB = $this->DB;
302 if ((($this->Id() < 0) && ($NewSetting == FALSE))
303 || (($this->Id() >= 0) && ($NewSetting == TRUE)))
304 {
305 # lock DB tables to prevent next ID from being grabbed
306 $DB->Query("LOCK TABLES Resources write");
307
308 # get next resource ID as appropriate
309 $OldResourceId = $this->Id;
310 $Factory = new ResourceFactory($this->SchemaId());
311 if ($NewSetting == TRUE)
312 {
313 $this->Id = $Factory->GetNextTempItemId();
314 }
315 else
316 {
317 $this->Id = $Factory->GetNextItemId();
318 }
319
320 # change resource ID
321 $DB->Query("UPDATE Resources SET ResourceId = ".
322 $this->Id. " WHERE ResourceId = ".$OldResourceId);
323
324 # release DB tables
325 $DB->Query("UNLOCK TABLES");
326
327 # change associations
328 unset($this->ClassificationCache);
329 $DB->Query("UPDATE ResourceClassInts SET ResourceId = ".
330 $this->Id. " WHERE ResourceId = ".$OldResourceId);
331 unset($this->ControlledNameCache);
332 unset($this->ControlledNameVariantCache);
333 $DB->Query("UPDATE ResourceNameInts SET ResourceId = ".
334 $this->Id. " WHERE ResourceId = ".$OldResourceId);
335 $DB->Query("UPDATE Files SET ResourceId = ".
336 $this->Id. " WHERE ResourceId = ".$OldResourceId);
337 $DB->Query("UPDATE ReferenceInts SET SrcResourceId = ".
338 $this->Id. " WHERE SrcResourceId = ".$OldResourceId);
339 $DB->Query("UPDATE ResourceImageInts SET ResourceId = ".
340 $this->Id. " WHERE ResourceId = ".$OldResourceId);
341
342 # signal event as appropriate
343 if ($NewSetting === FALSE)
344 {
345 $GLOBALS["AF"]->SignalEvent("EVENT_RESOURCE_ADD", array(
346 "Resource" => $this,
347 ));
348 }
349 }
350 }
351
352 # report to caller whether we are a temp resource
353 return ($this->Id() < 0) ? TRUE : FALSE;
354 }
355
356
357 # --- Generic Attribute Retrieval Methods -------------------------------
358
373 function Get($FieldNameOrObject, $ReturnObject = FALSE, $IncludeVariants = FALSE)
374 {
375 # load field object if needed
376 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
377 : $this->Schema->GetFieldByName($FieldNameOrObject);
378
379 # return no value found if we don't have a valid field
380 if (!($Field instanceof MetadataField)) { return NULL; }
381
382 # grab database field name
383 $DBFieldName = $Field->DBFieldName();
384
385 # format return value based on field type
386 switch ($Field->Type())
387 {
388 case MetadataSchema::MDFTYPE_TEXT:
389 case MetadataSchema::MDFTYPE_PARAGRAPH:
390 case MetadataSchema::MDFTYPE_URL:
391 $ReturnValue = isset($this->DBFields[$DBFieldName])
392 ? (string)$this->DBFields[$DBFieldName] : NULL;
393 break;
394
395 case MetadataSchema::MDFTYPE_NUMBER:
396 $ReturnValue = isset($this->DBFields[$DBFieldName])
397 ? (int)$this->DBFields[$DBFieldName] : NULL;
398 break;
399
400 case MetadataSchema::MDFTYPE_FLAG:
401 $ReturnValue = isset($this->DBFields[$DBFieldName])
402 ? (bool)$this->DBFields[$DBFieldName] : NULL;
403 break;
404
405 case MetadataSchema::MDFTYPE_POINT:
406 $ReturnValue = array("X" => (float)$this->DBFields[$DBFieldName."X"],
407 "Y" => (float)$this->DBFields[$DBFieldName."Y"]);
408 break;
409
410 case MetadataSchema::MDFTYPE_DATE:
411 $Date = new Date($this->DBFields[$DBFieldName."Begin"],
412 $this->DBFields[$DBFieldName."End"],
413 $this->DBFields[$DBFieldName."Precision"]);
414 if ($ReturnObject)
415 {
416 $ReturnValue = $Date;
417 }
418 else
419 {
420 $ReturnValue = $Date->Formatted();
421 }
422 break;
423
424 case MetadataSchema::MDFTYPE_TIMESTAMP:
425 $ReturnValue = $this->DBFields[$DBFieldName];
426 break;
427
428 case MetadataSchema::MDFTYPE_TREE:
429 # start with empty array
430 $ReturnValue = array();
431
432 # if classification cache has not been loaded
433 if (!isset($this->ClassificationCache))
434 {
435 # load all classifications associated with this resource into cache
436 $this->ClassificationCache = array();
437 $this->DB->Query(
438 "SELECT Classifications.ClassificationId,"
439 ." Classifications.FieldId,ClassificationName"
440 ." FROM ResourceClassInts, Classifications"
441 ." WHERE ResourceClassInts.ResourceId = ".$this->Id
442 ." AND ResourceClassInts.ClassificationId"
443 ." = Classifications.ClassificationId");
444 while ($Record = $this->DB->FetchRow())
445 {
446 $ClassId = $Record["ClassificationId"];
447 $this->ClassificationCache[$ClassId]["Name"]
448 = $Record["ClassificationName"];
449 $this->ClassificationCache[$ClassId]["FieldId"]
450 = $Record["FieldId"];
451 }
452 }
453
454 # for each entry in classification cache
455 foreach ($this->ClassificationCache as
456 $ClassificationId => $ClassificationInfo)
457 {
458 # if classification ID matches field we are looking for
459 if ($ClassificationInfo["FieldId"] == $Field->Id())
460 {
461 # add field to result
462 if ($ReturnObject)
463 {
464 $ReturnValue[$ClassificationId] =
465 new Classification($ClassificationId);
466 }
467 else
468 {
469 $ReturnValue[$ClassificationId] = $ClassificationInfo["Name"];
470 }
471 }
472 }
473 break;
474
475 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
476 case MetadataSchema::MDFTYPE_OPTION:
477 # start with empty array
478 $ReturnValue = array();
479
480 # if controlled name cache has not been loaded
481 if (!isset($this->ControlledNameCache))
482 {
483 # load all controlled names associated with this resource into cache
484 $this->ControlledNameCache = array();
485 $this->DB->Query(
486 "SELECT ControlledNames.ControlledNameId,"
487 ." ControlledNames.FieldId,ControlledName"
488 ." FROM ResourceNameInts, ControlledNames"
489 ." WHERE ResourceNameInts.ResourceId = ".$this->Id
490 ." AND ResourceNameInts.ControlledNameId"
491 ." = ControlledNames.ControlledNameId"
492 ." ORDER BY ControlledNames.ControlledName ASC");
493 while ($Record = $this->DB->FetchRow())
494 {
495 $CNameId = $Record["ControlledNameId"];
496 $this->ControlledNameCache[$CNameId]["Name"]
497 = $Record["ControlledName"];
498 $this->ControlledNameCache[$CNameId]["FieldId"]
499 = $Record["FieldId"];
500 }
501 }
502
503 # if variant names requested and variant name cache has not been loaded
504 if ($IncludeVariants && !isset($this->ControlledNameVariantCache))
505 {
506 # load all controlled names associated with this resource into cache
507 $this->ControlledNameVariantCache = array();
508 $this->DB->Query("SELECT ControlledNames.ControlledNameId,"
509 ." ControlledNames.FieldId,"
510 ." ControlledName, VariantName"
511 ." FROM ResourceNameInts, ControlledNames, VariantNames"
512 ." WHERE ResourceNameInts.ResourceId = ".$this->Id
513 ." AND ResourceNameInts.ControlledNameId"
514 ." = ControlledNames.ControlledNameId"
515 ." AND VariantNames.ControlledNameId"
516 ." = ControlledNames.ControlledNameId");
517 while ($Record = $this->DB->FetchRow())
518 {
519 $this->ControlledNameVariantCache[$Record["ControlledNameId"]][]
520 = $Record["VariantName"];
521 }
522 }
523
524 # for each entry in controlled name cache
525 foreach ($this->ControlledNameCache as
526 $CNameId => $ControlledNameInfo)
527 {
528 # if controlled name type matches field we are looking for
529 if ($ControlledNameInfo["FieldId"] == $Field->Id())
530 {
531 # if objects requested
532 if ($ReturnObject)
533 {
534 $ReturnValue[$CNameId] =
535 new ControlledName($CNameId);
536 }
537 else
538 {
539 # if variant names requested
540 if ($IncludeVariants)
541 {
542 # add field to result
543 $ReturnValue[] = $ControlledNameInfo["Name"];
544
545 # add any variant names to result
546 if (isset($this->ControlledNameVariantCache[$CNameId]))
547 {
548 $ReturnValue = array_merge(
549 $ReturnValue,
550 $this->ControlledNameVariantCache[$CNameId]);
551 }
552 }
553 else
554 {
555 # add field with index to result
556 $ReturnValue[$CNameId] =
557 $ControlledNameInfo["Name"];
558 }
559 }
560 }
561 }
562 break;
563
564 case MetadataSchema::MDFTYPE_USER:
565 $User = new CWUser(intval($this->DBFields[$DBFieldName]));
566 if ($ReturnObject)
567 {
568 $ReturnValue = $User;
569 }
570 else
571 {
572 $ReturnValue = $User->Get("UserName");
573 }
574 break;
575
576 case MetadataSchema::MDFTYPE_IMAGE:
577 # start out assuming no images will be found
578 $ReturnValue = array();
579
580 # find all images associated with this resource
581 $this->DB->Query("SELECT ImageId FROM ResourceImageInts"
582 ." WHERE ResourceId = ".intval($this->Id())
583 ." AND FieldId = ".intval($Field->Id()));
584
585 # if images were found
586 if ($this->DB->NumRowsSelected())
587 {
588 # if we are to return an object
589 $ImageIds = $this->DB->FetchColumn("ImageId");
590 if ($ReturnObject)
591 {
592 # load array of Image objects for return value
593 foreach ($ImageIds as $ImageId)
594 {
595 $ReturnValue[$ImageId] = new SPTImage($ImageId);
596 }
597 }
598 else
599 {
600 # load array of Image ids for return value
601 $ReturnValue = $ImageIds;
602 }
603 }
604 break;
605
606 case MetadataSchema::MDFTYPE_FILE:
607 # retrieve files using factory
608 $Factory = new FileFactory($Field->Id());
609 $ReturnValue = $Factory->GetFilesForResource(
610 $this->Id, $ReturnObject);
611 break;
612
613 case MetadataSchema::MDFTYPE_REFERENCE:
614 # query for resource references
615 $this->DB->Query("
616 SELECT * FROM ReferenceInts
617 WHERE FieldId = '".addslashes($Field->Id())."'
618 AND SrcResourceId = '".addslashes($this->Id())."'");
619
620 $ReturnValue = array();
621
622 # return each reference as a Resource object
623 if ($ReturnObject)
624 {
625 $FoundErrors = FALSE;
626
627 while (FALSE !== ($Record = $this->DB->FetchRow()))
628 {
629 $ReferenceId = $Record["DstResourceId"];
630 $Reference = new Resource($ReferenceId);
631
632 # the reference is bad, so flag that there were errors
633 if ($Reference->Status() != 1)
634 {
635 $FoundErrors = TRUE;
636 }
637
638 else
639 {
640 $ReturnValue[$ReferenceId] = $Reference;
641 }
642 }
643
644 # try to fix the errors by removing any references to
645 # resources that were bad
646 if ($FoundErrors)
647 {
648 $this->Set($Field, $ReturnValue);
649 }
650 }
651
652 # return each reference as a resource ID
653 else
654 {
655 while (FALSE !== ($Record = $this->DB->FetchRow()))
656 {
657 $ReferenceId = $Record["DstResourceId"];
658 $ReturnValue[$ReferenceId] = $ReferenceId;
659 }
660 }
661 break;
662
663 default:
664 # ERROR OUT
665 exit("<br>SPT - ERROR: attempt to retrieve "
666 ."unknown resource field type (".$Field->Type().")<br>\n");
667 break;
668 }
669
670 # return formatted value to caller
671 return $ReturnValue;
672 }
677 function GetByField($FieldNameOrObject,
678 $ReturnObject = FALSE, $IncludeVariants = FALSE)
679 { return $this->Get($FieldNameOrObject, $ReturnObject, $IncludeVariants); }
680
694 function GetByFieldId($FieldId, $ReturnObject = FALSE, $IncludeVariants = FALSE)
695 {
696 $Field = $this->Schema->GetField($FieldId);
697 return ($Field) ? $this->Get($Field, $ReturnObject, $IncludeVariants) : NULL;
698 }
699
712 function GetAsArray($IncludeDisabledFields = FALSE, $ReturnObjects = TRUE)
713 {
714 # retrieve field info
715 $Fields = $this->Schema->GetFields();
716
717 # for each field
718 foreach ($Fields as $Field)
719 {
720 # if field is enabled or caller requested disabled fields
721 if ($Field->Enabled() || $IncludeDisabledFields)
722 {
723 # retrieve info and add it to the array
724 $FieldStrings[$Field->Name()] = $this->Get($Field, $ReturnObjects);
725
726 # if field uses qualifiers
727 if ($Field->UsesQualifiers())
728 {
729 # get qualifier attributes and add to the array
730 $FieldStrings[$Field->Name()." Qualifier"] =
731 $this->GetQualifierByField($Field, $ReturnObjects);
732 }
733 }
734 }
735
736 # add in internal values
737 $FieldStrings["ResourceId"] = $this->Id();
738 $FieldStrings["CumulativeRating"] = $this->CumulativeRating();
739
740 # return array to caller
741 return $FieldStrings;
742 }
743
758 function GetMapped($MappedName, $ReturnObject = FALSE, $IncludeVariants = FALSE)
759 {
760 return $this->Schema->StdNameToFieldMapping($MappedName)
761 ? $this->GetByFieldId($this->Schema->StdNameToFieldMapping($MappedName),
762 $ReturnObject, $IncludeVariants)
763 : NULL;
764 }
765
774 function GetQualifier($FieldName, $ReturnObject = TRUE)
775 {
776 $Field = $this->Schema->GetFieldByName($FieldName);
777 return $this->GetQualifierByField($Field, $ReturnObject);
778 }
779
788 function GetQualifierByFieldId($FieldId, $ReturnObject = TRUE)
789 {
790 $Field = $this->Schema->GetField($FieldId);
791 return ($Field) ? $this->GetQualifierByField($Field, $ReturnObject) : NULL;
792 }
793
802 function GetQualifierByField($Field, $ReturnObject = TRUE)
803 {
804 # return NULL if field is invalid
805 if (!($Field instanceof MetadataField)) { return NULL; }
806
807 # assume no qualifiers if not otherwise determined
808 $ReturnValue = NULL;
809
810 # if field uses qualifiers
811 if ($Field->UsesQualifiers())
812 {
813 # retrieve qualifiers based on field type
814 switch ($Field->Type())
815 {
816 case MetadataSchema::MDFTYPE_TREE:
817 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
818 case MetadataSchema::MDFTYPE_OPTION:
819 # retrieve list of items
820 $Items = $this->Get($Field);
821
822 # if field uses item-level qualifiers
823 if ($Field->HasItemLevelQualifiers())
824 {
825 # determine general item name in DB
826 $TableName = ($Field->Type() == MetadataSchema::MDFTYPE_TREE)
827 ? "Classification" : "ControlledName";
828
829 # for each item
830 foreach ($Items as $ItemId => $ItemName)
831 {
832 # look up qualifier for item
833 $QualId = $this->DB->Query(
834 "SELECT * FROM ".$TableName."s"
835 ." WHERE ".$TableName."Id = ".$ItemId,
836 "QualifierId");
837
838
839 if ($QualId > 0)
840 {
841 # if object was requested by caller
842 if ($ReturnObject)
843 {
844 # load qualifier and add to return value array
845 $ReturnValue[$ItemId] = new Qualifier($QualId);
846 }
847 else
848 {
849 # add qualifier ID to return value array
850 $ReturnValue[$ItemId] = $QualId;
851 }
852 }
853 else
854 {
855 # add NULL to return value array for this item
856 $ReturnValue[$ItemId] = NULL;
857 }
858 }
859 }
860 else
861 {
862 # for each item
863 foreach ($Items as $ItemId => $ItemName)
864 {
865 # if object was requested by caller
866 if ($ReturnObject)
867 {
868 # load default qualifier and add to return value array
869 $ReturnValue[$ItemId] = new Qualifier(
870 $Field->DefaultQualifier());
871 }
872 else
873 {
874 # add default qualifier ID to return value array
875 $ReturnValue[$ItemId] = $Field->DefaultQualifier();
876 }
877 }
878 }
879 break;
880
881 default:
882 # if field uses item-level qualifiers
883 if ($Field->HasItemLevelQualifiers())
884 {
885 # if qualifier available
886 if ($this->DBFields[$Field->DBFieldName()."Qualifier"] > 0)
887 {
888 # if object was requested by caller
889 $QFieldName = $Field->DBFieldName()."Qualifier";
890 if ($ReturnObject)
891 {
892 # return qualifier for field
893 $ReturnValue = new Qualifier(
894 $this->DBFields[$QFieldName]);
895 }
896 else
897 {
898 # return qualifier ID for field
899 $ReturnValue = $this->DBFields[$QFieldName];
900 }
901 }
902 }
903 else
904 {
905 # if default qualifier available
906 if ($Field->DefaultQualifier() > 0)
907 {
908 # if object was requested by caller
909 if ($ReturnObject)
910 {
911 # return default qualifier
912 $ReturnValue = new Qualifier($Field->DefaultQualifier());
913 }
914 else
915 {
916 # return default qualifier ID
917 $ReturnValue = $Field->DefaultQualifier();
918 }
919 }
920 }
921 break;
922 }
923 }
924
925 # return qualifier object or ID (or array of same) to caller
926 return $ReturnValue;
927 }
928
936 function FieldIsSet($FieldNameOrObject, $IgnorePadding=FALSE)
937 {
938 # load field object if needed
939 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
940 : $this->Schema->GetFieldByName($FieldNameOrObject);
941
942 # return no value found if we don't have a valid field
943 if (!($Field instanceof MetadataField)) { return FALSE; }
944
945 # get the value
946 $Value = $this->Get($Field);
947
948 # checks depend on the field type
949 switch ($Field->Type())
950 {
951 case MetadataSchema::MDFTYPE_TEXT:
952 case MetadataSchema::MDFTYPE_PARAGRAPH:
953 case MetadataSchema::MDFTYPE_NUMBER:
954 case MetadataSchema::MDFTYPE_URL:
955 return isset($Value)
956 && strlen($Value)
957 && (!$IgnorePadding || ($IgnorePadding && strlen(trim($Value))));
958
959 case MetadataSchema::MDFTYPE_FLAG:
960 return isset($Value)
961 && strlen($Value);
962
963 case MetadataSchema::MDFTYPE_POINT:
964 return isset($Value["X"])
965 && isset($Value["Y"])
966 && strlen(trim($Value["X"]))
967 && strlen(trim($Value["Y"]));
968
969 case MetadataSchema::MDFTYPE_DATE:
970 return isset($Value)
971 && strlen(trim($Value))
972 && $Value != "0000-00-00";
973
974 case MetadataSchema::MDFTYPE_TIMESTAMP:
975 return isset($Value)
976 && strlen(trim($Value))
977 && $Value != "0000-00-00 00:00:00";
978
979 case MetadataSchema::MDFTYPE_TREE:
980 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
981 case MetadataSchema::MDFTYPE_OPTION:
982 case MetadataSchema::MDFTYPE_FILE:
983 case MetadataSchema::MDFTYPE_IMAGE:
984 case MetadataSchema::MDFTYPE_REFERENCE:
985 return count($Value) > 0;
986
987 case MetadataSchema::MDFTYPE_USER:
988 $Factory = new CWUserFactory();
989 return isset($Value)
990 && strlen($Value)
991 && $Factory->UserNameExists($Value);
992
993 default:
994 return FALSE;
995 }
996 }
997
998 # --- Generic Attribute Setting Methods ---------------------------------
999
1008 function Set($FieldNameOrObject, $NewValue, $Reset=FALSE)
1009 {
1010 # load field object if needed
1011 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
1012 : $this->Schema->GetFieldByName($FieldNameOrObject);
1013
1014 # return if we don't have a valid field
1015 if (!($Field instanceof MetadataField)) { return; }
1016
1017 # grab commonly-used values for local use
1018 $DB = $this->DB;
1019 $ResourceId = $this->Id;
1020
1021 # grab database field name
1022 $DBFieldName = $Field->DBFieldName();
1023
1024 # Flag to deterimine if we've actually changed anything.
1025 $UpdateModTime = FALSE;
1026
1027 # store value in DB based on field type
1028 switch ($Field->Type())
1029 {
1030 case MetadataSchema::MDFTYPE_TEXT:
1031 case MetadataSchema::MDFTYPE_PARAGRAPH:
1032 case MetadataSchema::MDFTYPE_URL:
1033 if ($this->DBFields[$DBFieldName] != $NewValue)
1034 {
1035 # save value directly to DB
1036 $DB->Query("UPDATE Resources SET `"
1037 .$DBFieldName."` = '".addslashes($NewValue)."' "
1038 ."WHERE ResourceId = ".$ResourceId);
1039
1040 # save value locally
1041 $this->DBFields[$DBFieldName] = $NewValue;
1042 $UpdateModTime=TRUE;
1043 }
1044 break;
1045
1046 case MetadataSchema::MDFTYPE_NUMBER:
1047 if ( $this->DBFields[$DBFieldName] != $NewValue )
1048 {
1049 # save value directly to DB
1050 if (is_null($NewValue))
1051 {
1052 $DB->Query("UPDATE Resources SET `"
1053 .$DBFieldName."` = NULL"
1054 ." WHERE ResourceId = ".$ResourceId);
1055 }
1056 else
1057 {
1058 $DB->Query("UPDATE Resources SET `"
1059 .$DBFieldName."` = ".intval($NewValue)
1060 ." WHERE ResourceId = ".$ResourceId);
1061 }
1062
1063 # save value locally
1064 $this->DBFields[$DBFieldName] = $NewValue;
1065 $UpdateModTime = TRUE;
1066 }
1067 break;
1068
1069
1070 case MetadataSchema::MDFTYPE_POINT:
1071 if ($this->DBFields[$DBFieldName."X"] != $NewValue["X"] ||
1072 $this->DBFields[$DBFieldName."Y"] != $NewValue["Y"] )
1073 {
1074 if (is_null($NewValue))
1075 {
1076 $DB->Query("UPDATE Resources SET "
1077 ."`".$DBFieldName."X` = NULL, "
1078 ."`".$DBFieldName."Y` = NULL "
1079 ."WHERE ResourceId = ".$ResourceId);
1080 $this->DBFields[$DBFieldName."X"] = NULL;
1081 $this->DBFields[$DBFieldName."Y"] = NULL;
1082 }
1083 else
1084 {
1085 $DB->Query("UPDATE Resources SET "
1086 ."`".$DBFieldName."X` = " .(strlen($NewValue["X"])
1087 ? "'".$NewValue["X"]."'" : "NULL").", "
1088 ."`".$DBFieldName."Y` = ".(strlen($NewValue["Y"])
1089 ? "'".$NewValue["Y"]."'" : "NULL")
1090 ." WHERE ResourceId = ".$ResourceId);
1091
1092 $Digits = $Field->PointDecimalDigits();
1093
1094 $this->DBFields[$DBFieldName."X"] =
1095 strlen($NewValue["X"]) ?
1096 round($NewValue["X"], $Digits) : NULL;
1097 $this->DBFields[$DBFieldName."Y"] =
1098 strlen($NewValue["Y"]) ?
1099 round($NewValue["Y"], $Digits) : NULL;
1100 }
1101 $UpdateModTime = TRUE;
1102 }
1103 break;
1104
1105 case MetadataSchema::MDFTYPE_FLAG:
1106 if ($this->DBFields[$DBFieldName] != $NewValue)
1107 {
1108 # save value directly to DB
1109 if (is_null($NewValue))
1110 {
1111 $DB->Query("UPDATE Resources SET `"
1112 .$DBFieldName."` = NULL"
1113 ." WHERE ResourceId = ".$ResourceId);
1114 }
1115 else
1116 {
1117 $NewValue = $NewValue ? "1" : "0";
1118 $DB->Query("UPDATE Resources SET `"
1119 .$DBFieldName."` = ".$NewValue
1120 ." WHERE ResourceId = ".$ResourceId);
1121 }
1122
1123 $this->DBFields[$DBFieldName] = $NewValue;
1124
1125 # recalculate counts for any associated classifications if necessary
1126 if ($DBFieldName == "ReleaseFlag")
1127 {
1128 $DB->Query("SELECT ClassificationId FROM ResourceClassInts"
1129 ." WHERE ResourceId = ".$ResourceId);
1130 while ($ClassId = $DB->FetchField("ClassificationId"))
1131 {
1132 $Class = new Classification($ClassId);
1133 $Class->RecalcResourceCount();
1134 }
1135 }
1136 $UpdateModTime = TRUE;
1137 }
1138 break;
1139
1140 case MetadataSchema::MDFTYPE_USER:
1141 # if value passed in was object
1142 if (is_object($NewValue))
1143 {
1144 # retrieve user ID from object
1145 $UserId = $NewValue->Get("UserId");
1146 }
1147 # else if value passed in was user name
1148 elseif (is_string($NewValue) && strlen($NewValue))
1149 {
1150 # create user object and retrieve user ID from there
1151 $User = new CWUser($NewValue);
1152 $UserId = $User->Get("UserId");
1153 }
1154 else
1155 {
1156 # assume value is user ID and use value directly
1157 $UserId = $NewValue;
1158 }
1159
1160 if ($this->DBFields[$DBFieldName] != $UserId)
1161 {
1162 # save value directly to DB
1163 $DB->Query("UPDATE Resources SET `"
1164 .$DBFieldName."` = '".$UserId."' "
1165 ."WHERE ResourceId = ".$ResourceId);
1166
1167 # save value locally
1168 $this->DBFields[$DBFieldName] = $UserId;
1169 $UpdateModTime = TRUE;
1170 }
1171 break;
1172
1173 case MetadataSchema::MDFTYPE_DATE:
1174 # if we were given a date object
1175 if (is_object($NewValue))
1176 {
1177 # use supplied date object
1178 $Date = $NewValue;
1179 }
1180 else
1181 {
1182 # create date object
1183 $Date = new Date($NewValue);
1184 }
1185
1186 $OldDate = new Date(
1187 $this->DBFields[$DBFieldName."Begin"],
1188 $this->DBFields[$DBFieldName."End"]);
1189
1190 if ($OldDate->BeginDate() != $Date->BeginDate() ||
1191 $OldDate->EndDate() != $Date->EndDate() ||
1192 $OldDate->Precision() != $Date->Precision() )
1193 {
1194 # extract values from date object and store in DB
1195 $BeginDate = "'".$Date->BeginDate()."'";
1196 if (strlen($BeginDate) < 3) { $BeginDate = "NULL"; }
1197 $EndDate = "'".$Date->EndDate()."'";
1198 if (strlen($EndDate) < 3) { $EndDate = "NULL"; }
1199
1200 $DB->Query("UPDATE Resources SET "
1201 .$DBFieldName."Begin = ".$BeginDate.", "
1202 .$DBFieldName."End = ".$EndDate.", "
1203 .$DBFieldName."Precision = '".$Date->Precision()."' "
1204 ."WHERE ResourceId = ".$ResourceId);
1205
1206 # save values locally
1207 $this->DBFields[$DBFieldName."Begin"] = $Date->BeginDate();
1208 $this->DBFields[$DBFieldName."End"] = $Date->EndDate();
1209 $this->DBFields[$DBFieldName."Precision"] = $Date->Precision();
1210 $UpdateModTime=TRUE;
1211 }
1212 break;
1213
1214 case MetadataSchema::MDFTYPE_TIMESTAMP:
1215 if (is_null($NewValue) || !strlen(trim($NewValue)))
1216 {
1217 $DateValue = $NewValue;
1218
1219 if (!is_null($this->DBFields[$DBFieldName]))
1220 {
1221 # save value directly to DB
1222 $DB->Query("UPDATE Resources SET "
1223 ."`".$DBFieldName."` = NULL "
1224 ."WHERE ResourceId = ".$ResourceId);
1225 $UpdateModTime = TRUE;
1226 }
1227 }
1228 else
1229 {
1230 # assume value is date and use directly
1231 $TimestampValue = strtotime($NewValue);
1232
1233 # use the new value if the date is valid
1234 if ($TimestampValue !== FALSE && $TimestampValue >= 0)
1235 {
1236 $DateValue = date("Y-m-d H:i:s", $TimestampValue);
1237
1238 if ($this->DBFields[$DBFieldName] != $DateValue)
1239 {
1240 # save value directly to DB
1241 $DB->Query("UPDATE Resources SET "
1242 ."`".$DBFieldName."` = '".addslashes($DateValue)."' "
1243 ."WHERE ResourceId = ".$ResourceId);
1244 $UpdateModTime=TRUE;
1245 }
1246 }
1247
1248 # continue using the old value if invalid
1249 else
1250 {
1251 $DateValue = $this->Get($Field);
1252 }
1253 }
1254
1255 # save value locally
1256 $this->DBFields[$DBFieldName] = $DateValue;
1257 break;
1258
1259 case MetadataSchema::MDFTYPE_TREE:
1260 $OldValue = $this->Get($Field);
1261
1262 # if incoming value is array
1263 if (is_array($NewValue))
1264 {
1265 if ($OldValue != $NewValue)
1266 {
1267 if ($Reset)
1268 {
1269 # remove values that were in the old value
1270 # but not the new one
1271 $ToRemove = array_diff(array_keys($OldValue),
1272 array_keys($NewValue));
1273 foreach ($ToRemove as $ClassificationId)
1274 {
1275 $this->RemoveAssociation("ResourceClassInts",
1276 "ClassificationId",
1277 $ClassificationId);
1278 $Class = new Classification($ClassificationId);
1279 if ($Class->Status() == Classification::CLASSSTAT_OK)
1280 {
1281 $Class->RecalcResourceCount();
1282 }
1283 }
1284 }
1285
1286 # for each element of array
1287 foreach ($NewValue as
1288 $ClassificationId => $ClassificationName)
1289 {
1290 $Class = new Classification($ClassificationId);
1291 if ($Class->Status() == Classification::CLASSSTAT_OK)
1292 {
1293 # associate with resource if not already associated
1294 if ($this->AddAssociation("ResourceClassInts",
1295 "ClassificationId",
1296 $ClassificationId) )
1297 {
1298 $Class->UpdateLastAssigned();
1299 $Class->RecalcResourceCount();
1300 }
1301 }
1302 }
1303
1304 $UpdateModTime=TRUE;
1305 }
1306 }
1307 else
1308 {
1309 # associate with resource if not already associated
1310 if (is_object($NewValue))
1311 {
1312 $Class = $NewValue;
1313 $NewValue = $Class->Id();
1314 }
1315 else
1316 {
1317 $Class = new Classification($NewValue);
1318 }
1319
1320 if (!array_key_exists($Class->Id(), $OldValue))
1321 {
1322
1323 $this->AddAssociation("ResourceClassInts",
1324 "ClassificationId",
1325 $NewValue);
1326 $Class->UpdateLastAssigned();
1327 $Class->RecalcResourceCount();
1328 $UpdateModTime=TRUE;
1329 }
1330 }
1331
1332 # clear our classification cache
1333 if ($UpdateModTime)
1334 {
1335 unset($this->ClassificationCache);
1336 }
1337 break;
1338
1339 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1340 case MetadataSchema::MDFTYPE_OPTION:
1341 $OldValue = $this->Get($Field);
1342
1343 # input to Set() for these fields is one of
1344 # 1. an int specifying a ControlledNameId
1345 # 2. a ControlledName object
1346 # 3. an array with keys giving Ids and values giving ControlledNames
1347 #
1348 # normalize 1 and 2 into 3 for simplicity of processing
1349 if (is_object($NewValue) || !is_array($NewValue) )
1350 {
1351 if (!is_object($NewValue))
1352 {
1353 $NewValue = new ControlledName($NewValue);
1354 }
1355
1356 $TmpValue = array();
1357 $TmpValue[$NewValue->Id()] = $NewValue->Name();
1358
1359 $NewValue = $TmpValue;
1360 }
1361
1362 # if this is a unique field, only accept the first of the options given
1363 # NB: all ControlledNames implicitly AllowMultiple
1364 if ($Field->Type() == MetadataSchema::MDFTYPE_OPTION &&
1365 $Field->AllowMultiple() == FALSE && count($NewValue) > 1)
1366 {
1367 $NewValue = array_slice($NewValue, 0, 1, TRUE);
1368 }
1369
1370 # if the value has changed
1371 if ($OldValue != $NewValue)
1372 {
1373 if ($Reset || ($Field->Type() == MetadataSchema::MDFTYPE_OPTION
1374 && $Field->AllowMultiple() == FALSE ) )
1375 {
1376 $ToRemove = array_diff(array_keys($OldValue),
1377 array_keys($NewValue));
1378 foreach ($ToRemove as $CNId)
1379 {
1380 $this->RemoveAssociation("ResourceNameInts",
1381 "ControlledNameId",
1382 $CNId);
1383 }
1384 }
1385
1386 # for each element of array
1387 foreach ($NewValue as $ControlledNameId => $ControlledName)
1388 {
1389 # associate with resource if not already associated
1390 if ($this->AddAssociation("ResourceNameInts",
1391 "ControlledNameId",
1392 $ControlledNameId))
1393 {
1394 $CN = new ControlledName( $ControlledNameId );
1395 $CN->UpdateLastAssigned();
1396 }
1397 }
1398 $UpdateModTime=TRUE;
1399 }
1400
1401 if ($UpdateModTime)
1402 {
1403 # clear our controlled name cache
1404 unset($this->ControlledNameCache);
1405 unset($this->ControlledNameVariantCache);
1406 }
1407
1408 break;
1409
1410 case MetadataSchema::MDFTYPE_IMAGE:
1411 # associate value(s) with resource
1412 $this->AddAssociation(
1413 "ResourceImageInts", "ImageId", $NewValue, $Field);
1414 break;
1415
1416 case MetadataSchema::MDFTYPE_FILE:
1417 # convert incoming value to array if necessary
1418 if (!is_array($NewValue)) { $NewValue = array($NewValue); }
1419
1420 # for each incoming file
1421 $Factory = new FileFactory($Field->Id());
1422 foreach ($NewValue as $File)
1423 {
1424 # make copy of file
1425 $NewFile = $Factory->Copy($File);
1426
1427 # associate copy with this resource and field
1428 $NewFile->ResourceId($this->Id);
1429 $NewFile->FieldId($Field->Id());
1430 }
1431 # Since we make a fresh copy of the File whenever Set is called,
1432 # we'll always update the modification time for this field.
1433 $UpdateModTime = TRUE;
1434 break;
1435
1436 case MetadataSchema::MDFTYPE_REFERENCE:
1437 # convert incoming value to array to simplify the workflow
1438 if (is_scalar($NewValue) || $NewValue instanceof Resource)
1439 {
1440 $NewValue = array($NewValue);
1441 }
1442
1443 # delete existing resource references
1444 $this->ClearByField($Field);
1445
1446 # add each reference
1447 foreach ($NewValue as $ReferenceOrId)
1448 {
1449 # initially issume it's a reference ID and not an object...
1450 $ReferenceId = $ReferenceOrId;
1451
1452 # ... but get the reference ID if it's an object
1453 if ($ReferenceOrId instanceof Resource)
1454 {
1455 $ReferenceId = $ReferenceOrId->Id();
1456 }
1457
1458 # skip blank reference IDs
1459 if (strlen(trim($ReferenceId)) < 1)
1460 {
1461 continue;
1462 }
1463
1464 # skip reference IDs that don't look right
1465 if (!is_numeric($ReferenceId))
1466 {
1467 continue;
1468 }
1469
1470 # skip references to the current resource
1471 if ($ReferenceId == $this->Id())
1472 {
1473 continue;
1474 }
1475
1476 # add the reference to the references table
1477 $DB->Query("
1478 INSERT INTO ReferenceInts (
1479 FieldId,
1480 SrcResourceId,
1481 DstResourceId)
1482 VALUES (
1483 ".addslashes($Field->Id()).",
1484 ".addslashes($this->Id()).",
1485 ".addslashes($ReferenceId).")");
1486 }
1487 break;
1488
1489 default:
1490 # ERROR OUT
1491 exit("<br>SPT - ERROR: attempt to set unknown resource field type<br>\n");
1492 break;
1493 }
1494
1495 if ($UpdateModTime && !$this->IsTempResource())
1496 {
1497 # update modification timestamps
1498 global $G_User;
1499 $UserId = $G_User->IsLoggedIn() ? $G_User->Get("UserId") : -1;
1500 $DB->Query("DELETE FROM ResourceFieldTimestamps "
1501 ."WHERE ResourceId=".$this->Id." AND "
1502 ."FieldId=".$Field->Id() );
1503 $DB->Query("INSERT INTO ResourceFieldTimestamps "
1504 ."(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1505 .$this->Id.",".$Field->Id().","
1506 .$UserId.",NOW())");
1507
1508 # on resource modification, clear the UserPermsCache entry
1509 # so that stale permissions checks are not cached
1510 $DB->Query("DELETE FROM UserPermsCache WHERE ResourceId=".$this->Id);
1511 }
1512 }
1517 function SetByField($Field, $NewValue)
1518 {
1519 $this->Set($Field, $NewValue);
1520 }
1521
1522 # set value by field ID
1523 function SetByFieldId($FieldId, $NewValue)
1524 {
1525 $Field = $this->Schema->GetField($FieldId);
1526 $this->Set($Field, $NewValue);
1527 }
1528
1529 # set qualifier by field name
1530 function SetQualifier($FieldName, $NewValue)
1531 {
1532 $Field = $this->Schema->GetFieldByName($FieldName);
1533 $this->SetQualifierByField($Field, $NewValue);
1534 }
1535
1536 # set qualifier by field ID
1537 function SetQualifierByFieldId($FieldId, $NewValue)
1538 {
1539 $Field = $this->Schema->GetField($FieldId);
1540 $this->SetQualifierByField($Field, $NewValue);
1541 }
1542
1543 # set qualifier using field object
1544 function SetQualifierByField($Field, $NewValue)
1545 {
1546 # if field uses qualifiers and uses item-level qualifiers
1547 if ($Field->UsesQualifiers() && $Field->HasItemLevelQualifiers())
1548 {
1549 # if qualifier object passed in
1550 if (is_object($NewValue))
1551 {
1552 # grab qualifier ID from object
1553 $QualifierId = $NewValue->Id();
1554 }
1555 else
1556 {
1557 # assume value passed in is qualifier ID
1558 $QualifierId = $NewValue;
1559 }
1560
1561 # update qualifier value in database
1562 $DBFieldName = $Field->DBFieldName();
1563 $this->DB->Query("UPDATE Resources SET "
1564 .$DBFieldName."Qualifier = '".$QualifierId."' "
1565 ."WHERE ResourceId = ".$this->Id);
1566
1567 # update local qualifier value
1568 $this->DBFields[$DBFieldName."Qualifier"] = $QualifierId;
1569 }
1570 }
1571
1572 # clear value by field ID
1573 function ClearByFieldId($FieldId, $ValueToClear = NULL)
1574 {
1575 $Field = $this->Schema->GetField($FieldId);
1576 $this->ClearByField($Field, $ValueToClear);
1577 }
1578
1579 # clear value using field object
1580 function Clear($Field, $ValueToClear = NULL)
1581 {
1582 # convert field name to object if necessary
1583 if (!is_object($Field))
1584 {
1585 $Field = $this->Schema->GetFieldByName($Field);
1586 }
1587
1588 # grab commonly-used values for local use
1589 $DB = $this->DB;
1590 $ResourceId = $this->Id;
1591
1592 # grab database field name
1593 $DBFieldName = $Field->DBFieldName();
1594
1595 $UpdateModTime=FALSE;
1596
1597 # store value in DB based on field type
1598 switch ($Field->Type())
1599 {
1600 case MetadataSchema::MDFTYPE_TEXT:
1601 case MetadataSchema::MDFTYPE_PARAGRAPH:
1602 case MetadataSchema::MDFTYPE_NUMBER:
1603 case MetadataSchema::MDFTYPE_FLAG:
1604 case MetadataSchema::MDFTYPE_USER:
1605 case MetadataSchema::MDFTYPE_TIMESTAMP:
1606 case MetadataSchema::MDFTYPE_URL:
1607 if (strlen($this->DBFields[$DBFieldName])>0)
1608 {
1609 # clear value in DB
1610 $DB->Query("UPDATE Resources SET `"
1611 .$DBFieldName."` = NULL "
1612 ."WHERE ResourceId = ".$ResourceId);
1613
1614 # clear value locally
1615 $this->DBFields[$DBFieldName] = NULL;
1616 $UpdateModTime=TRUE;
1617 }
1618 break;
1619
1620 case MetadataSchema::MDFTYPE_POINT:
1621 if (!is_null($this->DBFields[$DBFieldName."X"]) ||
1622 !is_null($this->DBFields[$DBFieldName."Y"]) )
1623 {
1624 # Clear DB Values
1625 $DB->Query("UPDATE Resources SET "
1626 ."`".$DBFieldName."X` = NULL ,"
1627 ."`".$DBFieldName."Y` = NULL "
1628 ."WHERE ResourceId = ".$ResourceId);
1629
1630 # Clear local values
1631 $this->DBFields[$DBFieldName."X"] = NULL;
1632 $this->DBFields[$DBFieldName."Y"] = NULL;
1633 $UpdateModTime=TRUE;
1634 }
1635 break;
1636
1637 case MetadataSchema::MDFTYPE_DATE:
1638 if (!is_null($this->DBFields[$DBFieldName."Begin"]) ||
1639 !is_null($this->DBFields[$DBFieldName."End"]) ||
1640 !is_null($this->DBFields[$DBFieldName."Precision"]))
1641 {
1642 # clear date object values in DB
1643 $DB->Query("UPDATE Resources SET "
1644 .$DBFieldName."Begin = '', "
1645 .$DBFieldName."End = '', "
1646 .$DBFieldName."Precision = '' "
1647 ."WHERE ResourceId = ".$ResourceId);
1648
1649 # clear value locally
1650 $this->DBFields[$DBFieldName."Begin"] = NULL;
1651 $this->DBFields[$DBFieldName."End"] = NULL;
1652 $this->DBFields[$DBFieldName."Precision"] = NULL;
1653 $UpdateModTime=TRUE;
1654 }
1655 break;
1656
1657 case MetadataSchema::MDFTYPE_TREE:
1658 $OldValue = $this->Get($Field);
1659
1660 # if value to clear supplied
1661 if ($ValueToClear !== NULL)
1662 {
1663 # if supplied value is array
1664 if (is_array($ValueToClear))
1665 {
1666 # for each element of array
1667 foreach ($ValueToClear as $ClassificationId => $Dummy)
1668 {
1669 if (array_key_exists($ClassificationId, $OldValue))
1670 {
1671 # remove association with resource (if any)
1672 $this->RemoveAssociation("ResourceClassInts",
1673 "ClassificationId",
1674 $ClassificationId);
1675 $Class = new Classification($ClassificationId);
1676 $Class->RecalcResourceCount();
1677 $UpdateModTime=TRUE;
1678 }
1679 }
1680 }
1681 else
1682 {
1683 if (array_key_exists($ValueToClear, $OldValue))
1684 {
1685 # remove association with resource (if any)
1686 $this->RemoveAssociation("ResourceClassInts",
1687 "ClassificationId",
1688 $ValueToClear);
1689 $Class = new Classification($ValueToClear);
1690 $Class->RecalcResourceCount();
1691 $UpdateModTime=TRUE;
1692 }
1693 }
1694 }
1695 else
1696 {
1697 if (count($OldValue)>0)
1698 {
1699 # remove all associations for resource and field
1700 $this->RemoveAllAssociations(
1701 "ResourceClassInts", "ClassificationId", $Field);
1702
1703 # recompute resource count
1704 $Values = $this->Get($Field);
1705 foreach ($Values as $ClassificationId => $Dummy)
1706 {
1707 $Class = new Classification($ClassificationId);
1708 $Class->RecalcResourceCount();
1709 }
1710 $UpdateModTime=TRUE;
1711 }
1712 }
1713
1714 # clear our classification cache
1715 if ($UpdateModTime)
1716 {
1717 unset($this->ClassificationCache);
1718 }
1719 break;
1720
1721 case MetadataSchema::MDFTYPE_CONTROLLEDNAME:
1722 case MetadataSchema::MDFTYPE_OPTION:
1723 $OldValue = $this->Get($Field);
1724 # if value to clear supplied
1725 if ($ValueToClear !== NULL)
1726 {
1727 # if incoming value is array
1728 if (is_array($ValueToClear))
1729 {
1730 # for each element of array
1731 foreach ($ValueToClear as $ControlledNameId =>
1732 $ControlledName)
1733 {
1734 if (array_key_exists($ControlledNameId, $OldValue))
1735 {
1736 # remove association with resource (if any)
1737 $this->RemoveAssociation("ResourceNameInts",
1738 "ControlledNameId",
1739 $ControlledNameId);
1740 $UpdateModTime=TRUE;
1741 }
1742 }
1743 }
1744 else
1745 {
1746 if (array_key_exists($ValueToClear, $OldValue))
1747 {
1748 # remove association with resource (if any)
1749 $this->RemoveAssociation("ResourceNameInts",
1750 "ControlledNameId",
1751 $ValueToClear);
1752 $UpdateModTime=TRUE;
1753 }
1754 }
1755 }
1756 else
1757 {
1758 if (count($OldValue)>0)
1759 {
1760 # remove all associations for resource and field
1761 $this->RemoveAllAssociations(
1762 "ResourceNameInts", "ControlledNameId", $Field);
1763 $UpdateModTime=TRUE;
1764 }
1765 }
1766
1767 if ($UpdateModTime)
1768 {
1769 # clear our controlled name cache
1770 unset($this->ControlledNameCache);
1771 unset($this->ControlledNameVariantCache);
1772 }
1773 break;
1774
1775 case MetadataSchema::MDFTYPE_FILE:
1776 # if value to clear supplied
1777 if ($ValueToClear !== NULL)
1778 {
1779 # convert value to array if necessary
1780 $Files = $ValueToClear;
1781 if (!is_array($Files)) { $Files = array($Files); }
1782
1783 # convert values to objects if necessary
1784 foreach ($Files as $Index => $File)
1785 {
1786 if (!is_object($File))
1787 {
1788 $Files[$Index] = new File($File);
1789 }
1790 }
1791 }
1792 else
1793 {
1794 # use all files associated with resource
1795 $Files = $this->Get($Field, TRUE);
1796 }
1797
1798 # delete files
1799 foreach ($Files as $File) { $File->Delete(); }
1800 break;
1801
1802 case MetadataSchema::MDFTYPE_IMAGE:
1803 # if value to clear supplied
1804 if ($ValueToClear !== NULL)
1805 {
1806 # convert value to array if necessary
1807 $Images = $ValueToClear;
1808 if (!is_array($Images)) { $Images = array($Images); }
1809
1810 # convert values to objects if necessary
1811 foreach ($Images as $Index => $Image)
1812 {
1813 if (!is_object($Image))
1814 {
1815 $Images[$Index] = new SPTImage($Image);
1816 }
1817 }
1818 }
1819 else
1820 {
1821 # use all images associated with resource
1822 $Images = $this->Get($Field, TRUE);
1823 }
1824
1825 # delete images if we are the last resource referencing
1826 # a particular image.
1827 foreach ($Images as $Image) {
1828 $Cnt = $this->DB->Query(
1829 "SELECT COUNT(*) AS Cnt FROM ResourceImageInts WHERE ".
1830 "ImageId=".$Image->Id(), "Cnt");
1831 if ($Cnt==1)
1832 {
1833 $Image->Delete();
1834 }
1835 }
1836
1837 # remove connections to images
1838 $UpdateModTime = $this->RemoveAssociation(
1839 "ResourceImageInts", "ImageId", $Images, $Field);
1840 break;
1841
1842 case MetadataSchema::MDFTYPE_REFERENCE:
1843 # remove references from the references table
1844 $DB->Query("
1845 DELETE FROM ReferenceInts
1846 WHERE FieldId = '".addslashes($Field->Id())."'
1847 AND SrcResourceId = '".addslashes($this->Id())."'");
1848 break;
1849
1850 default:
1851 # ERROR OUT
1852 exit("<br>SPT - ERROR: attempt to clear "
1853 ."unknown resource field type<br>\n");
1854 break;
1855 }
1856
1857 if ($UpdateModTime && !$this->IsTempResource())
1858 {
1859 # update modification timestamps
1860 global $G_User;
1861 $UserId = $G_User->IsLoggedIn() ? $G_User->Get("UserId") : -1;
1862 $DB->Query("DELETE FROM ResourceFieldTimestamps "
1863 ."WHERE ResourceId=".$this->Id." AND "
1864 ."FieldId=".$Field->Id() );
1865 $DB->Query("INSERT INTO ResourceFieldTimestamps "
1866 ."(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1867 .$this->Id.",".$Field->Id().","
1868 .$UserId.",NOW())");
1869 }
1870 }
1871
1872 function ClearByField($Field, $ValueToClear = NULL)
1873 {
1874 $this->Clear($Field, $ValueToClear);
1875 }
1876
1877 # --- Field-Specific or Type-Specific Attribute Retrieval Methods -------
1878
1879 # return 2D array of classifications associated with resource
1880 # (first index is classification (field) name, second index is classification ID)
1881 function Classifications()
1882 {
1883 $DB = $this->DB;
1884
1885 # start with empty array
1886 $Names = array();
1887
1888 # for each controlled name
1889 $DB->Query("SELECT ClassificationName, MetadataFields.FieldName, "
1890 ."ResourceClassInts.ClassificationId FROM ResourceClassInts, "
1891 ."Classifications, MetadataFields "
1892 ."WHERE ResourceClassInts.ResourceId = ".$this->Id." "
1893 ."AND ResourceClassInts.ClassificationId = "
1894 ."Classifications.ClassificationId "
1895 ."AND Classifications.FieldId = MetadataFields.FieldId ");
1896 while ($Record = $DB->FetchRow())
1897 {
1898 # add name to array
1899 $Names[$Record["FieldName"]][$Record["ClassificationId"]] =
1900 $Record["ClassificationName"];
1901 }
1902
1903 # return array to caller
1904 return $Names;
1905 }
1906
1907
1908 # --- Ratings Methods ---------------------------------------------------
1909
1910 # return cumulative rating (range is usually 0-100)
1911 function CumulativeRating()
1912 {
1913 return $this->CumulativeRating;
1914 }
1915
1916 # return cumulative rating scaled to 1/10th (range is usually 0-10)
1917 function ScaledCumulativeRating()
1918 {
1919 if ($this->CumulativeRating == NULL)
1920 {
1921 return NULL;
1922 }
1923 else
1924 {
1925 return intval(($this->CumulativeRating + 5) / 10);
1926 }
1927 }
1928
1929 # return current number of ratings for resource
1930 function NumberOfRatings()
1931 {
1932 # if number of ratings not already set
1933 if (!isset($this->NumberOfRatings))
1934 {
1935 # obtain number of ratings
1936 $this->NumberOfRatings =
1937 $this->DB->Query("SELECT Count(*) AS NumberOfRatings "
1938 ."FROM ResourceRatings "
1939 ."WHERE ResourceId = ".$this->Id,
1940 "NumberOfRatings"
1941 );
1942
1943 # recalculate cumulative rating if it looks erroneous
1944 if (($this->NumberOfRatings > 0) && !$this->CumulativeRating())
1945 {
1946 $this->UpdateCumulativeRating();
1947 }
1948 }
1949
1950 # return number of ratings to caller
1951 return $this->NumberOfRatings;
1952 }
1953
1954 # update individual rating for resource
1955 function Rating($NewRating = NULL, $UserId = NULL)
1956 {
1957 $DB = $this->DB;
1958
1959 # if user ID not supplied
1960 if ($UserId == NULL)
1961 {
1962 # if user is logged in
1963 global $User;
1964 if ($User->IsLoggedIn())
1965 {
1966 # use ID of current user
1967 $UserId = $User->Get("UserId");
1968 }
1969 else
1970 {
1971 # return NULL to caller
1972 return NULL;
1973 }
1974 }
1975
1976 # sanitize $NewRating
1977 if (!is_null($NewRating))
1978 {
1979 $NewRating = intval($NewRating);
1980 }
1981
1982 # if there is a rating for resource and user
1983 $DB->Query("SELECT Rating FROM ResourceRatings "
1984 ."WHERE UserId = ${UserId} AND ResourceId = ".$this->Id);
1985 if ($Record = $DB->FetchRow())
1986 {
1987 # if new rating was supplied
1988 if ($NewRating != NULL)
1989 {
1990 # update existing rating
1991 $DB->Query("UPDATE ResourceRatings "
1992 ."SET Rating = ${NewRating}, DateRated = NOW() "
1993 ."WHERE UserId = ${UserId} AND ResourceId = ".$this->Id);
1994
1995 # update cumulative rating value
1996 $this->UpdateCumulativeRating();
1997
1998 # return value is new rating
1999 $Rating = $NewRating;
2000 }
2001 else
2002 {
2003 # get rating value to return to caller
2004 $Rating = $Record["Rating"];
2005 }
2006 }
2007 else
2008 {
2009 # if new rating was supplied
2010 if ($NewRating != NULL)
2011 {
2012 # add new rating
2013 $DB->Query("INSERT INTO ResourceRatings "
2014 ."(ResourceId, UserId, DateRated, Rating) "
2015 ."VALUES ("
2016 .$this->Id.", "
2017 ."${UserId}, "
2018 ."NOW(), "
2019 ."${NewRating})");
2020
2021 # update cumulative rating value
2022 $this->UpdateCumulativeRating();
2023
2024 # return value is new rating
2025 $Rating = $NewRating;
2026 }
2027 else
2028 {
2029 # return value is NULL
2030 $Rating = NULL;
2031 }
2032 }
2033
2034 # return rating value to caller
2035 return $Rating;
2036 }
2037
2038
2039 # --- Resource Comment Methods ------------------------------------------
2040
2041 # return comments as array of Message objects
2042 function Comments()
2043 {
2044 # read in comments if not already loaded
2045 if (!isset($this->Comments))
2046 {
2047 $this->DB->Query("SELECT MessageId FROM Messages "
2048 ."WHERE ParentId = ".$this->Id
2049 ." AND ParentType = 2 "
2050 ."ORDER BY DatePosted DESC");
2051 while ($MessageId = $this->DB->FetchField("MessageId"))
2052 {
2053 $this->Comments[] = new Message($MessageId);
2054 }
2055 }
2056
2057 # return array of comments to caller
2058 return $this->Comments;
2059 }
2060
2061 # return current number of comments
2062 function NumberOfComments()
2063 {
2064 # obtain number of comments if not already set
2065 if (!isset($this->NumberOfComments))
2066 {
2067 $this->NumberOfComments =
2068 $this->DB->Query("SELECT Count(*) AS NumberOfComments "
2069 ."FROM Messages "
2070 ."WHERE ParentId = ".$this->Id
2071 ." AND ParentType = 2",
2072 "NumberOfComments"
2073 );
2074 }
2075
2076 # return number of comments to caller
2077 return $this->NumberOfComments;
2078 }
2079
2080
2081 # --- Permission Methods -------------------------------------------------
2082
2092 function UserCanView(User $User, $AllowHooksToModify=TRUE)
2093 {
2094 return $this->CheckSchemaPermissions($User, "View", $AllowHooksToModify);
2095 }
2096
2103 function UserCanEdit($User)
2104 {
2105 return $this->CheckSchemaPermissions($User, "Edit");
2106 }
2107
2114 function UserCanAuthor($User)
2115 {
2116 return $this->CheckSchemaPermissions($User, "Author");
2117 }
2118
2125 function UserCanViewField($User, $FieldOrFieldName)
2126 {
2127 return $this->CheckFieldPermissions( $User, $FieldOrFieldName, "View" );
2128 }
2129
2136 function UserCanEditField($User, $FieldOrFieldName)
2137 {
2138 return $this->CheckFieldPermissions( $User, $FieldOrFieldName, "Edit" );
2139 }
2140
2147 function UserCanAuthorField($User, $FieldOrFieldName)
2148 {
2149 return $this->CheckFieldPermissions( $User, $FieldOrFieldName, "Author" );
2150 }
2151
2159 function UserCanModifyField($User, $FieldOrFieldName)
2160 {
2161 $CheckFn = "UserCan".(($this->Id()<0) ? "Author" : "Edit")."Field";
2162
2163 return $this->$CheckFn($User, $FieldOrFieldName);
2164 }
2165
2166 # ---- PRIVATE INTERFACE -------------------------------------------------
2167
2168 protected $DB;
2169
2170 private $Schema;
2171 private $DBFields;
2172 private $NumberOfRatings;
2173 private $CumulativeRating;
2174 private $NumberOfComments;
2175 private $Comments;
2176 private $LastStatus;
2177 private $ControlledNameCache;
2178 private $ControlledNameVariantCache;
2179 private $ClassificationCache;
2180 private $PermissionCache;
2181
2191 private function CheckSchemaPermissions($User, $CheckType, $AllowHooksToModify=TRUE)
2192 {
2193 # checks against invalid reosurces should always fail
2194 if ($this->Status() !== 1) { return FALSE; }
2195
2196 # construct a key to use for our permissions cache
2197 $CacheKey = "UserCan".$CheckType.$User->Id();
2198
2199 # if we don't have a cached value for this perm, compute one
2200 if (!isset($this->PermissionCache[$CacheKey]))
2201 {
2202 # get privileges for schema
2203 $PermsFn = $CheckType."ingPrivileges";
2204 $SchemaPrivs = $this->Schema->$PermsFn();
2205
2206 # check passes if user privileges are greater than resource set
2207 $CheckResult = $SchemaPrivs->MeetsRequirements($User, $this);
2208
2209 # save the result of this check in our cache
2210 $this->PermissionCache[$CacheKey] = $CheckResult;
2211 }
2212
2213 $Value = $this->PermissionCache[$CacheKey];
2214
2215 if ($AllowHooksToModify)
2216 {
2217 $SignalResult = $GLOBALS["AF"]->SignalEvent(
2218 "EVENT_RESOURCE_".strtoupper($CheckType)."_PERMISSION_CHECK",
2219 array(
2220 "Resource" => $this,
2221 "User" => $User,
2222 "Can".$CheckType => $Value));
2223
2224 $Value = $SignalResult["Can".$CheckType];
2225 }
2226
2227 return $Value;
2228 }
2229
2238 private function CheckFieldPermissions($User, $FieldOrFieldName, $CheckType)
2239 {
2240 # checks against invalid resources should always fail
2241 if ($this->Status() !== 1) { return FALSE; }
2242
2243 # get field object (if not supplied)
2244 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2245 : $this->Schema->GetFieldByName($FieldOrFieldName);
2246
2247 # checks against invalid fields should also fail
2248 if (!($Field instanceof MetadataField)) { return FALSE; }
2249
2250 # construct a key to use for our permissions cache
2251 $CacheKey = "UserCan".$CheckType."Field".$Field->Id()."-".$User->Id();
2252
2253 # if we don't have a cahced value, compute one
2254 if (!isset($this->PermissionCache[$CacheKey]))
2255 {
2256 # checks for disabled fields should not pass
2257 if (!$Field->Enabled())
2258 {
2259 $CheckResult = FALSE;
2260 }
2261 else
2262 {
2263 # be sure schema privs allow View/Edit/Author for this resource
2264 $SchemaCheckFn = "UserCan".$CheckType;
2265 if ($this->$SchemaCheckFn($User))
2266 {
2267 # get appropriate privilege set for field
2268 $PermsFn = $CheckType."ingPrivileges";
2269 $FieldPrivs = $Field->$PermsFn();
2270
2271 # user can View/Edit/Author if privileges are greater than field set
2272 $CheckResult = $FieldPrivs->MeetsRequirements($User, $this);
2273 }
2274 else
2275 {
2276 $CheckResult = FALSE;
2277 }
2278 }
2279
2280 # allow plugins to modify result of permission check
2281 $SignalResult = $GLOBALS["AF"]->SignalEvent(
2282 "EVENT_FIELD_".strtoupper($CheckType)."_PERMISSION_CHECK", array(
2283 "Field" => $Field,
2284 "Resource" => $this,
2285 "User" => $User,
2286 "Can".$CheckType => $CheckResult));
2287 $CheckResult = $SignalResult["Can".$CheckType];
2288
2289 # save the result of this check in our cache
2290 $this->PermissionCache[$CacheKey] = $CheckResult;
2291 }
2292
2293 # return cached permission value
2294 return $this->PermissionCache[$CacheKey];
2295 }
2296
2297 # recalculate and save cumulative rating value for resource
2298 private function UpdateCumulativeRating()
2299 {
2300 # grab totals from DB
2301 $this->DB->Query("SELECT COUNT(Rating) AS Count, "
2302 ."SUM(Rating) AS Total FROM ResourceRatings "
2303 ."WHERE ResourceId = ".$this->Id);
2304 $Record = $this->DB->FetchRow();
2305
2306 # calculate new cumulative rating
2307 $this->CumulativeRating = round($Record["Total"] / $Record["Count"]);
2308
2309 # save new cumulative rating in DB
2310 $this->DB->Query("UPDATE Resources "
2311 ."SET CumulativeRating = ".$this->CumulativeRating." "
2312 ."WHERE ResourceId = ".$this->Id);
2313 }
2314
2325 private function AddAssociation($TableName, $FieldName, $Value, $Field = NULL)
2326 {
2327 # We should ignore duplicate key errors when doing inserts:
2328 $this->DB->SetQueryErrorsToIgnore( array(
2329 "/INSERT INTO ".$TableName."/" =>
2330 "/Duplicate entry '[0-9]+-[0-9]+' for key/"));
2331
2332 # start out assuming no association will be added
2333 $AssociationAdded = FALSE;
2334
2335 # convert new value to array if necessary
2336 $Values = is_array($Value) ? $Value : array($Value);
2337
2338 # for each new value
2339 foreach ($Values as $Value)
2340 {
2341 # retrieve ID from value if necessary
2342 if (is_object($Value)) { $Value = $Value->Id(); }
2343
2344 # Try to insert a new entry for this association.
2345 $this->DB->Query("INSERT INTO ".$TableName." SET"
2346 ." ResourceId = ".intval($this->Id)
2347 .", ".$FieldName." = ".intval($Value)
2348 .($Field ? ", FieldId = ".intval($Field->Id()) : ""));
2349
2350 # If the insert ran without a duplicate key error,
2351 # then we added an assocation:
2352 if ($this->DB->IgnoredError() === FALSE)
2353 {
2354 $AssociationAdded = TRUE;
2355 }
2356 }
2357
2358 # Clear ignored errors:
2359 $this->DB->SetQueryErrorsToIgnore( NULL );
2360
2361 # report to caller whether association was added
2362 return $AssociationAdded;
2363 }
2364
2375 private function RemoveAssociation($TableName, $FieldName, $Value, $Field = NULL)
2376 {
2377 # start out assuming no association will be removed
2378 $AssociationRemoved = FALSE;
2379
2380 # convert value to array if necessary
2381 $Values = is_array($Value) ? $Value : array($Value);
2382
2383 # for each value
2384 foreach ($Values as $Value)
2385 {
2386 # retrieve ID from value if necessary
2387 if (is_object($Value)) { $Value = $Value->Id(); }
2388
2389 # remove any intersections with target ID from DB
2390 $this->DB->Query("DELETE FROM ".$TableName
2391 ." WHERE ResourceId = ".intval($this->Id)
2392 .($Field ? " AND FieldId = ".intval($Field->Id()) : "")
2393 ." AND ".$FieldName." = ".intval($Value));
2394 if ($this->DB->NumRowsAffected()) { $AssociationRemoved = TRUE; }
2395 }
2396
2397 # report to caller whether association was added
2398 return $AssociationRemoved;
2399 }
2400
2401 # remove all intersections for resource and field (if any)
2402 private function RemoveAllAssociations($TableName, $TargetFieldName, $Field)
2403 {
2404 # retrieve list of entries for this field and resource
2405 $Entries = $this->Get($Field);
2406
2407 # Divide them into chunks of not more than 50:
2408 foreach (array_chunk($Entries, 100, TRUE) as $Chunk)
2409 {
2410 # Construct a query that will remove assocations in this chunk:
2411 $this->DB->Query("DELETE FROM ".$TableName
2412 ." WHERE ResourceId = ".intval($this->Id)
2413 ." AND ".$TargetFieldName." IN "
2414 ."(".implode(",", array_keys($Chunk)).")");
2415 }
2416 }
2417 }
MetadataSchema\MDFTYPE_IMAGE
const MDFTYPE_IMAGE
Definition: MetadataSchema.php:30
Resource\GetByField
GetByField($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Old method for retrieving values, deprecated in favor of Get().
Definition: Resource.php:677
Resource\UserCanView
UserCanView(User $User, $AllowHooksToModify=TRUE)
Determine if the given user can view the resource, e.g., on the full record page. ...
Definition: Resource.php:2092
FileFactory\GetFilesForResource
GetFilesForResource($ResourceOrResourceId, $ReturnObjects=TRUE)
Retrieve all files (names or objects) for specified resource.
Definition: FileFactory.php:38
Resource\SetQualifier
SetQualifier($FieldName, $NewValue)
Definition: Resource.php:1530
MetadataSchema\MDFTYPE_TIMESTAMP
const MDFTYPE_TIMESTAMP
Definition: MetadataSchema.php:24
SPTSearchEngine
Definition: SPTSearchEngine.php:10
Resource\$DB
$DB
Definition: Resource.php:2168
MetadataSchema\MDFTYPE_FILE
const MDFTYPE_FILE
Definition: MetadataSchema.php:31
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
Message
Abstraction for forum messages and resource comments.
Definition: Message.php:15
Database
SQL database abstraction object with smart query caching.
Definition: Axis--Database.php:22
Resource\UserCanModifyField
UserCanModifyField($User, $FieldOrFieldName)
Check whether user is allowed to modify (Edit for perm resources, Author for temp) specified metadata...
Definition: Resource.php:2159
MetadataSchema\MDFTYPE_REFERENCE
const MDFTYPE_REFERENCE
Definition: MetadataSchema.php:34
Resource\GetAsArray
GetAsArray($IncludeDisabledFields=FALSE, $ReturnObjects=TRUE)
Retrieve all resource values as an array.
Definition: Resource.php:712
Resource\Set
Set($FieldNameOrObject, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:1008
Resource\Id
Id()
Retrieve numerical resource ID.
Definition: Resource.php:275
Resource\UserCanEditField
UserCanEditField($User, $FieldOrFieldName)
Check whether user is allowed to edit specified metadata field.
Definition: Resource.php:2136
Resource\SetQualifierByField
SetQualifierByField($Field, $NewValue)
Definition: Resource.php:1544
MetadataSchema\MDFTYPE_NUMBER
const MDFTYPE_NUMBER
Definition: MetadataSchema.php:22
Resource\Rating
Rating($NewRating=NULL, $UserId=NULL)
Definition: Resource.php:1955
User
Definition: Axis--User.php:43
Resource\Status
Status()
Retrieve result of last operation if available.
Definition: Resource.php:266
Resource\NumberOfComments
NumberOfComments()
Definition: Resource.php:2062
Resource\NumberOfRatings
NumberOfRatings()
Definition: Resource.php:1930
Resource\GetQualifier
GetQualifier($FieldName, $ReturnObject=TRUE)
Retrieve qualifier by field name.
Definition: Resource.php:774
FolderFactory
Factory object for Folder class, used to retrieve and manage Folders and groups of Folders...
Definition: FolderFactory.php:14
Qualifier
Definition: Qualifier.php:10
FileFactory\Copy
Copy($FileToCopy)
Create copy of File and return to caller.
Definition: FileFactory.php:84
MetadataSchema\MDFTYPE_TREE
const MDFTYPE_TREE
Definition: MetadataSchema.php:26
SPTRecommender
Definition: SPTRecommender.php:10
Date
Definition: Axis--Date.php:18
MetadataSchema\MDFTYPE_OPTION
const MDFTYPE_OPTION
Definition: MetadataSchema.php:28
ControlledName
Metadata type representing non-hierarchical controlled vocabulary values.
Definition: ControlledName.php:14
MetadataSchema\MDFTYPE_FLAG
const MDFTYPE_FLAG
Definition: MetadataSchema.php:25
Resource\UserCanEdit
UserCanEdit($User)
Determine if the given user can edit the resource.
Definition: Resource.php:2103
MetadataSchema\MDFTYPE_CONTROLLEDNAME
const MDFTYPE_CONTROLLEDNAME
Definition: MetadataSchema.php:27
Resource\Comments
Comments()
Definition: Resource.php:2042
CWUserFactory
CWIS-specific user factory class.
Definition: CWUserFactory.php:13
PHP
PHP
Definition: RSSClient--Test.php:12
MetadataSchema\SCHEMAID_DEFAULT
const SCHEMAID_DEFAULT
Definition: MetadataSchema.php:52
FileFactory
Factory for manipulating File objects.
Definition: FileFactory.php:13
Resource\GetQualifierByFieldId
GetQualifierByFieldId($FieldId, $ReturnObject=TRUE)
Retrieve qualifier by field ID.
Definition: Resource.php:788
SPTImage
Encapsulates a full-size, preview, and thumbnail image.
Definition: SPTImage.php:13
Resource\UserCanAuthorField
UserCanAuthorField($User, $FieldOrFieldName)
Check whether user is allowed to author specified metadata field.
Definition: Resource.php:2147
Resource\Clear
Clear($Field, $ValueToClear=NULL)
Definition: Resource.php:1580
Resource\UserCanAuthor
UserCanAuthor($User)
Determine if the given user can edit the resource.
Definition: Resource.php:2114
Resource\GetByFieldId
GetByFieldId($FieldId, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field ID.
Definition: Resource.php:694
Resource\IsTempResource
IsTempResource($NewSetting=NULL)
Get/set whether resource is a temporary record.
Definition: Resource.php:295
Resource\Get
Get($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:373
Resource\SetByField
SetByField($Field, $NewValue)
Method replaced by Resource::Set(), preserved for backward compatibility.
Definition: Resource.php:1517
MetadataField
Object representing a locally-defined type of metadata field.
Definition: MetadataField.php:13
Resource\__construct
__construct($ResourceId)
Object constructor for loading an existing resource.
Definition: Resource.php:23
Resource\GetMapped
GetMapped($MappedName, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using standard (mapped) field name.
Definition: Resource.php:758
MetadataSchema\MDFTYPE_PARAGRAPH
const MDFTYPE_PARAGRAPH
Definition: MetadataSchema.php:21
MetadataSchema\MDFTYPE_DATE
const MDFTYPE_DATE
Definition: MetadataSchema.php:23
Resource
Represents a "resource" in CWIS.
Definition: Resource.php:13
MetadataSchema\MDFTYPE_URL
const MDFTYPE_URL
Definition: MetadataSchema.php:32
Resource\GetQualifierByField
GetQualifierByField($Field, $ReturnObject=TRUE)
Retrieve qualifier by Field object.
Definition: Resource.php:802
MetadataSchema\MDFTYPE_TEXT
const MDFTYPE_TEXT
Definition: MetadataSchema.php:20
Resource\ClearByFieldId
ClearByFieldId($FieldId, $ValueToClear=NULL)
Definition: Resource.php:1573
Classification\CLASSSTAT_OK
const CLASSSTAT_OK
Status code indicating operation completed successfully.
Definition: Classification.php:18
Resource\SetQualifierByFieldId
SetQualifierByFieldId($FieldId, $NewValue)
Definition: Resource.php:1537
MetadataSchema\MDFTYPE_USER
const MDFTYPE_USER
Definition: MetadataSchema.php:29
Resource\SchemaId
SchemaId()
Retrieve ID of schema for resource.
Definition: Resource.php:284
Resource\ScaledCumulativeRating
ScaledCumulativeRating()
Definition: Resource.php:1917
MetadataField\UPDATEMETHOD_ONRECORDCREATE
const UPDATEMETHOD_ONRECORDCREATE
Definition: MetadataField.php:19
Resource\Create
static Create($SchemaId)
Create a new resource.
Definition: Resource.php:60
Resource\UserCanViewField
UserCanViewField($User, $FieldOrFieldName)
Check whether user is allowed to view specified metadata field.
Definition: Resource.php:2125
Classification
Metadata type representing hierarchical ("Tree") controlled vocabulary values.
Definition: Classification.php:13
Resource\SetByFieldId
SetByFieldId($FieldId, $NewValue)
Definition: Resource.php:1523
Resource\Classifications
Classifications()
Definition: Resource.php:1881
Resource\ClearByField
ClearByField($Field, $ValueToClear=NULL)
Definition: Resource.php:1872
MetadataSchema\MDFTYPE_POINT
const MDFTYPE_POINT
Definition: MetadataSchema.php:33
File
Class representing a stored (usually uploaded) file.
Definition: File.php:13
ResourceFactory
Factory for Resource objects.
Definition: ResourceFactory.php:13
CWUser
CWIS-specific user class.
Definition: CWUser.php:13
Resource\CumulativeRating
CumulativeRating()
Definition: Resource.php:1911
Resource\FieldIsSet
FieldIsSet($FieldNameOrObject, $IgnorePadding=FALSE)
Determine if the value for a field is set.
Definition: Resource.php:936
Resource\Delete
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:154
$Items
$Items
Definition: RSSClient--Test.php:26