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/
15 # ---- PUBLIC INTERFACE --------------------------------------------------
28 $this->
Id = intval($ResourceId);
30 # locate resource in database
31 $this->DB->Query(
"SELECT * FROM Resources WHERE ResourceId = ".$this->
Id);
33 # if unable to locate resource
34 $Record = $this->DB->FetchRow();
37 # set status to -1 to indicate that creation failed
38 $this->LastStatus = -1;
42 # load in attributes from database
43 $this->DBFields = $Record;
46 # load our local metadata schema
49 # set status to 1 to indicate that creation succeeded
50 $this->LastStatus = 1;
61 # clean out any temp resource records more than three days old
63 $RFactory->CleanOutStaleTempItems(60 * 24 * 3);
65 # lock DB tables to prevent next ID from being grabbed
67 $DB->Query(
"LOCK TABLES Resources WRITE");
69 # find next temp resource ID
70 $Id = $RFactory->GetNextTempItemId();
72 # write out new resource record with temp resource ID
73 # Set DateLastModified = NOW() to avoid being pruned as a
74 # stale temp resource.
76 "INSERT INTO Resources
77 SET `ResourceId` = '".intval($Id).
"',
78 `SchemaId` = '".intval($SchemaId).
"',
79 `DateLastModified` = NOW() " );
82 $DB->Query(
"UNLOCK TABLES");
84 # create new Resource object
87 # set some additional fields for default resources
90 $Resource->Set(
"Added By Id", $GLOBALS[
"G_User"]->
Id());
91 $Resource->Set(
"Last Modified By Id", $GLOBALS[
"G_User"]->
Id());
92 $Resource->Set(
"Date Of Record Creation", date(
"Y-m-d H:i:s"));
93 $Resource->Set(
"Date Last Modified", date(
"Y-m-d H:i:s"));
96 # set any default values
103 foreach ($Fields as $Field)
105 $DefaultValue = $Field->DefaultValue();
107 # flip option default values to get into the form that
108 # Resource::Set() expects
110 && is_array($DefaultValue))
112 $DefaultValue = array_flip($DefaultValue);
115 # there is no default value when DefaultValue is null,
116 # or when it is an empty array
117 if ( !($DefaultValue === NULL ||
118 (is_array($DefaultValue) && empty($DefaultValue)) ))
120 $Resource->SetByField($Field, $DefaultValue);
124 # update timestamps as required
126 foreach ($TimestampFields as $Field)
128 if ($Field->UpdateMethod() ==
131 $Resource->SetByField($Field,
"now");
135 # signal resource creation
136 $GLOBALS[
"AF"]->SignalEvent(
"EVENT_RESOURCE_CREATE", array(
137 "Resource" => $Resource,
140 # return new Resource object to caller
152 # signal that resource deletion is about to occur
154 $AF->SignalEvent(
"EVENT_RESOURCE_DELETE", array(
158 # grab list of classifications
161 # delete resource/classification intersections
163 $DB->Query(
"DELETE FROM ResourceClassInts WHERE ResourceId = ".$this->
Id());
165 # for each classification type
166 foreach ($Classifications as $ClassType => $ClassesOfType)
168 # for each classification of that type
169 foreach ($ClassesOfType as $ClassId => $ClassName)
171 # recalculate resource count for classification
173 $Class->RecalcResourceCount();
177 # delete resource references
179 DELETE FROM ReferenceInts
180 WHERE SrcResourceId = '".addslashes($this->
Id()).
"'
181 OR DstResourceId = '".addslashes($this->
Id()).
"'");
183 # delete resource/name intersections
184 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ResourceId = ".$this->
Id());
186 # delete any associated images not in use by other resources
187 $DB->Query(
"SELECT ImageId FROM ResourceImageInts"
188 .
" WHERE ResourceId = ".intval($this->
Id()));
189 $ImageIds = $DB->FetchColumn(
"ImageId");
190 foreach ($ImageIds as $ImageId)
192 $DB->Query(
"SELECT ResourceId FROM ResourceImageInts"
193 .
" WHERE ImageId = ".intval($ImageId)
194 .
" AND ResourceId != ".intval($this->
Id()));
195 if ($DB->NumRowsSelected() == 0)
202 # delete any associated files
204 $Files = $Factory->GetFilesForResource($this->
Id());
205 foreach ($Files as $File)
210 # delete resource record from database
211 $DB->Query(
"DELETE FROM Resources WHERE ResourceId = ".$this->
Id());
213 # drop item from search engine and recommender system
214 if ($SysConfig->SearchDBEnabled())
217 $SearchEngine->DropItem($this->
Id());
219 if ($SysConfig->RecommenderDBEnabled())
222 $Recommender->DropItem($this->
Id());
225 # get the folders containing the resource
227 $Folders = $FolderFactory->GetFoldersContainingItem(
231 # drop the resource from each folder it belongs to
232 foreach ($Folders as $Folder)
234 # mixed item type folder
235 if ($Folder->ContainsItem($this->Id,
"Resource"))
237 $Folder->RemoveItem($this->
Id,
"Resource");
240 # single item type folder
243 $Folder->RemoveItem($this->
Id);
247 # delete any resource comments
248 $DB->Query(
"DELETE FROM Messages WHERE ParentId = ".$this->
Id);
255 function Status() {
return $this->LastStatus; }
261 function Id() {
return $this->Id; }
267 function SchemaId() {
return $this->DBFields[
"SchemaId"]; }
277 # if new temp resource setting supplied
278 if (!is_null($NewSetting))
280 # if caller requested to switch
282 if ((($this->
Id() < 0) && ($NewSetting == FALSE))
283 || (($this->
Id() >= 0) && ($NewSetting == TRUE)))
285 # lock DB tables to prevent next ID from being grabbed
286 $DB->Query(
"LOCK TABLES Resources write");
288 # get next resource ID as appropriate
289 $OldResourceId = $this->Id;
291 if ($NewSetting == TRUE)
293 $this->
Id = $Factory->GetNextTempItemId();
297 $this->
Id = $Factory->GetNextItemId();
301 $DB->Query(
"UPDATE Resources SET ResourceId = ".
302 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
305 $DB->Query(
"UNLOCK TABLES");
307 # change associations
308 unset($this->ClassificationCache);
309 $DB->Query(
"UPDATE ResourceClassInts SET ResourceId = ".
310 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
311 unset($this->ControlledNameCache);
312 unset($this->ControlledNameVariantCache);
313 $DB->Query(
"UPDATE ResourceNameInts SET ResourceId = ".
314 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
315 $DB->Query(
"UPDATE Files SET ResourceId = ".
316 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
317 $DB->Query(
"UPDATE ReferenceInts SET SrcResourceId = ".
318 $this->
Id.
" WHERE SrcResourceId = ".$OldResourceId);
319 $DB->Query(
"UPDATE ResourceImageInts SET ResourceId = ".
320 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
322 # signal event as appropriate
323 if ($NewSetting === FALSE)
325 $GLOBALS[
"AF"]->SignalEvent(
"EVENT_RESOURCE_ADD", array(
332 # report to caller whether we are a temp resource
333 return ($this->
Id() < 0) ? TRUE : FALSE;
337 # --- Generic Attribute Retrieval Methods -------------------------------
353 function Get($FieldNameOrObject, $ReturnObject = FALSE, $IncludeVariants = FALSE)
355 # load field object if needed
356 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
357 : $this->Schema->GetFieldByName($FieldNameOrObject);
359 # return no value found if we don't have a valid field
362 # grab database field name
363 $DBFieldName = $Field->DBFieldName();
365 # format return value based on field type
366 switch ($Field->Type())
371 $ReturnValue = isset($this->DBFields[$DBFieldName])
372 ? (string)$this->DBFields[$DBFieldName] : NULL;
376 $ReturnValue = isset($this->DBFields[$DBFieldName])
377 ? (int)$this->DBFields[$DBFieldName] : NULL;
381 $ReturnValue = isset($this->DBFields[$DBFieldName])
382 ? (bool)$this->DBFields[$DBFieldName] : NULL;
386 $ReturnValue = array(
"X" => (
float)$this->DBFields[$DBFieldName.
"X"],
387 "Y" => (
float)$this->DBFields[$DBFieldName.
"Y"]);
391 $Date =
new Date($this->DBFields[$DBFieldName.
"Begin"],
392 $this->DBFields[$DBFieldName.
"End"],
393 $this->DBFields[$DBFieldName.
"Precision"]);
396 $ReturnValue = $Date;
400 $ReturnValue = $Date->Formatted();
405 $ReturnValue = $this->DBFields[$DBFieldName];
409 # start with empty array
410 $ReturnValue = array();
412 # if classification cache has not been loaded
413 if (!isset($this->ClassificationCache))
415 # load all classifications associated with this resource into cache
416 $this->ClassificationCache = array();
418 "SELECT Classifications.ClassificationId,"
419 .
" Classifications.FieldId,ClassificationName"
420 .
" FROM ResourceClassInts, Classifications"
421 .
" WHERE ResourceClassInts.ResourceId = ".$this->
Id
422 .
" AND ResourceClassInts.ClassificationId"
423 .
" = Classifications.ClassificationId");
424 while ($Record = $this->DB->FetchRow())
426 $this->ClassificationCache[$Record[
"ClassificationId"]][
"Name"] =
427 $Record[
"ClassificationName"];
428 $this->ClassificationCache[$Record[
"ClassificationId"]][
"FieldId"] =
433 # for each entry in classification cache
434 foreach ($this->ClassificationCache as $ClassificationId => $ClassificationInfo)
436 # if classification ID matches field we are looking for
437 if ($ClassificationInfo[
"FieldId"] == $Field->Id())
439 # add field to result
442 $ReturnValue[$ClassificationId] =
new Classification($ClassificationId);
446 $ReturnValue[$ClassificationId] = $ClassificationInfo[
"Name"];
454 # start with empty array
455 $ReturnValue = array();
457 # if controlled name cache has not been loaded
458 if (!isset($this->ControlledNameCache))
460 # load all controlled names associated with this resource into cache
461 $this->ControlledNameCache = array();
463 "SELECT ControlledNames.ControlledNameId,"
464 .
" ControlledNames.FieldId,ControlledName"
465 .
" FROM ResourceNameInts, ControlledNames"
466 .
" WHERE ResourceNameInts.ResourceId = ".$this->
Id
467 .
" AND ResourceNameInts.ControlledNameId"
468 .
" = ControlledNames.ControlledNameId"
469 .
" ORDER BY ControlledNames.ControlledName ASC");
470 while ($Record = $this->DB->FetchRow())
472 $this->ControlledNameCache[$Record[
"ControlledNameId"]][
"Name"] = $Record[
"ControlledName"];
473 $this->ControlledNameCache[$Record[
"ControlledNameId"]][
"FieldId"] = $Record[
"FieldId"];
477 # if variant names requested and variant name cache has not been loaded
478 if ($IncludeVariants && !isset($this->ControlledNameVariantCache))
480 # load all controlled names associated with this resource into cache
481 $this->ControlledNameVariantCache = array();
482 $this->DB->Query(
"SELECT ControlledNames.ControlledNameId,"
483 .
" ControlledNames.FieldId,"
484 .
" ControlledName, VariantName"
485 .
" FROM ResourceNameInts, ControlledNames, VariantNames"
486 .
" WHERE ResourceNameInts.ResourceId = ".$this->
Id
487 .
" AND ResourceNameInts.ControlledNameId"
488 .
" = ControlledNames.ControlledNameId"
489 .
" AND VariantNames.ControlledNameId"
490 .
" = ControlledNames.ControlledNameId");
491 while ($Record = $this->DB->FetchRow())
493 $this->ControlledNameVariantCache[$Record[
"ControlledNameId"]][]
494 = $Record[
"VariantName"];
498 # for each entry in controlled name cache
499 foreach ($this->ControlledNameCache as $ControlledNameId => $ControlledNameInfo)
501 # if controlled name type matches field we are looking for
502 if ($ControlledNameInfo[
"FieldId"] == $Field->Id())
504 # if objects requested
507 $ReturnValue[$ControlledNameId] =
512 # if variant names requested
513 if ($IncludeVariants)
515 # add field to result
516 $ReturnValue[] = $ControlledNameInfo[
"Name"];
518 # add any variant names to result
519 if (isset($this->ControlledNameVariantCache[$ControlledNameId]))
521 $ReturnValue = array_merge(
523 $this->ControlledNameVariantCache[$ControlledNameId]);
528 # add field with index to result
529 $ReturnValue[$ControlledNameId] =
530 $ControlledNameInfo[
"Name"];
538 $User =
new CWUser(intval($this->DBFields[$DBFieldName]));
541 $ReturnValue = $User;
545 $ReturnValue = $User->Get(
"UserName");
550 # start out assuming no images will be found
551 $ReturnValue = array();
553 # find all images associated with this resource
554 $this->DB->Query(
"SELECT ImageId FROM ResourceImageInts"
555 .
" WHERE ResourceId = ".intval($this->
Id())
556 .
" AND FieldId = ".intval($Field->Id()));
558 # if images were found
559 if ($this->DB->NumRowsSelected())
561 # if we are to return an object
562 $ImageIds = $this->DB->FetchColumn(
"ImageId");
565 # load array of Image objects for return value
566 foreach ($ImageIds as $ImageId)
568 $ReturnValue[$ImageId] =
new SPTImage($ImageId);
573 # load array of Image ids for return value
574 $ReturnValue = $ImageIds;
580 # retrieve files using factory
583 $this->
Id, $ReturnObject);
587 # query for resource references
589 SELECT * FROM ReferenceInts
590 WHERE FieldId = '".addslashes($Field->Id()).
"'
591 AND SrcResourceId = '".addslashes($this->
Id()).
"'");
593 $ReturnValue = array();
595 # return each reference as a Resource object
598 $FoundErrors = FALSE;
600 while (FALSE !== ($Record = $this->DB->FetchRow()))
602 $ReferenceId = $Record[
"DstResourceId"];
603 $Reference =
new Resource($ReferenceId);
605 # the reference is bad, so flag that there were errors
606 if ($Reference->Status() != 1)
613 $ReturnValue[$ReferenceId] = $Reference;
617 # try to fix the errors by removing any references to
618 # resources that were bad
621 $this->
Set($Field, $ReturnValue);
625 # return each reference as a resource ID
628 while (FALSE !== ($Record = $this->DB->FetchRow()))
630 $ReferenceId = $Record[
"DstResourceId"];
631 $ReturnValue[$ReferenceId] = $ReferenceId;
638 exit(
"<br>SPT - ERROR: attempt to retrieve unknown resource field type (".$Field->Type().
")<br>\n");
642 # return formatted value to caller
650 $ReturnObject = FALSE, $IncludeVariants = FALSE)
651 {
return $this->
Get($FieldNameOrObject, $ReturnObject, $IncludeVariants); }
666 function GetByFieldId($FieldId, $ReturnObject = FALSE, $IncludeVariants = FALSE)
668 $Field = $this->Schema->GetField($FieldId);
669 return ($Field) ? $this->
Get($Field, $ReturnObject, $IncludeVariants) : NULL;
684 function GetAsArray($IncludeDisabledFields = FALSE, $ReturnObjects = TRUE)
686 # retrieve field info
687 $Fields = $this->Schema->GetFields();
690 foreach ($Fields as $Field)
692 # if field is enabled or caller requested disabled fields
693 if ($Field->Enabled() || $IncludeDisabledFields)
695 # retrieve info and add it to the array
696 $FieldStrings[$Field->Name()] = $this->
Get($Field, $ReturnObjects);
698 # if field uses qualifiers
699 if ($Field->UsesQualifiers())
701 # get qualifier attributes and add to the array
702 $FieldStrings[$Field->Name().
" Qualifier"] =
708 # add in internal values
709 $FieldStrings[
"ResourceId"] = $this->
Id();
712 # return array to caller
713 return $FieldStrings;
730 function GetMapped($MappedName, $ReturnObject = FALSE, $IncludeVariants = FALSE)
732 return $this->Schema->StdNameToFieldMapping($MappedName)
733 ? $this->
GetByFieldId($this->Schema->StdNameToFieldMapping($MappedName),
734 $ReturnObject, $IncludeVariants)
748 $Field = $this->Schema->GetFieldByName($FieldName);
762 $Field = $this->Schema->GetField($FieldId);
776 # return NULL if field is invalid
779 # assume no qualifiers if not otherwise determined
782 # if field uses qualifiers
783 if ($Field->UsesQualifiers())
785 # retrieve qualifiers based on field type
786 switch ($Field->Type())
791 # retrieve list of items
794 # if field uses item-level qualifiers
795 if ($Field->HasItemLevelQualifiers())
797 # determine general item name in DB
799 ?
"Classification" :
"ControlledName";
802 foreach (
$Items as $ItemId => $ItemName)
804 # look up qualifier for item
805 $QualId = $this->DB->Query(
806 "SELECT * FROM ".$TableName.
"s"
807 .
" WHERE ".$TableName.
"Id = ".$ItemId,
813 # if object was requested by caller
816 # load qualifier and add to return value array
817 $ReturnValue[$ItemId] =
new Qualifier($QualId);
821 # add qualifier ID to return value array
822 $ReturnValue[$ItemId] = $QualId;
827 # add NULL to return value array for this item
828 $ReturnValue[$ItemId] = NULL;
835 foreach (
$Items as $ItemId => $ItemName)
837 # if object was requested by caller
840 # load default qualifier and add to return value array
841 $ReturnValue[$ItemId] =
new Qualifier($Field->DefaultQualifier());
845 # add default qualifier ID to return value array
846 $ReturnValue[$ItemId] = $Field->DefaultQualifier();
853 # if field uses item-level qualifiers
854 if ($Field->HasItemLevelQualifiers())
856 # if qualifier available
857 if ($this->DBFields[$Field->DBFieldName().
"Qualifier"] > 0)
859 # if object was requested by caller
862 # return qualifier for field
863 $ReturnValue =
new Qualifier($this->DBFields[$Field->DBFieldName().
"Qualifier"]);
867 # return qualifier ID for field
868 $ReturnValue = $this->DBFields[$Field->DBFieldName().
"Qualifier"];
874 # if default qualifier available
875 if ($Field->DefaultQualifier() > 0)
877 # if object was requested by caller
880 # return default qualifier
881 $ReturnValue =
new Qualifier($Field->DefaultQualifier());
885 # return default qualifier ID
886 $ReturnValue = $Field->DefaultQualifier();
894 # return qualifier object or ID (or array of same) to caller
905 function FieldIsSet($FieldNameOrObject, $IgnorePadding=FALSE)
907 # load field object if needed
908 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
909 : $this->Schema->GetFieldByName($FieldNameOrObject);
911 # return no value found if we don't have a valid field
915 $Value = $this->
Get($Field);
917 # checks depend on the field type
918 switch ($Field->Type())
926 && (!$IgnorePadding || ($IgnorePadding && strlen(trim($Value))));
933 return isset($Value[
"X"])
934 && isset($Value[
"Y"])
935 && strlen(trim($Value[
"X"]))
936 && strlen(trim($Value[
"Y"]));
940 && strlen(trim($Value))
941 && $Value !=
"0000-00-00";
945 && strlen(trim($Value))
946 && $Value !=
"0000-00-00 00:00:00";
954 return count($Value) > 0;
960 && $Factory->UserNameExists($Value);
967 # --- Generic Attribute Setting Methods ---------------------------------
977 function Set($FieldNameOrObject, $NewValue, $Reset=FALSE)
979 # load field object if needed
980 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
981 : $this->Schema->GetFieldByName($FieldNameOrObject);
983 # return if we don't have a valid field
986 # grab commonly-used values for local use
988 $ResourceId = $this->Id;
990 # grab database field name
991 $DBFieldName = $Field->DBFieldName();
993 # Flag to deterimine if we've actually changed anything.
994 $UpdateModTime = FALSE;
996 # store value in DB based on field type
997 switch ($Field->Type())
1002 if ($this->DBFields[$DBFieldName] != $NewValue)
1004 # save value directly to DB
1005 $DB->Query(
"UPDATE Resources SET `"
1006 .$DBFieldName.
"` = '".addslashes($NewValue).
"' "
1007 .
"WHERE ResourceId = ".$ResourceId);
1009 # save value locally
1010 $this->DBFields[$DBFieldName] = $NewValue;
1011 $UpdateModTime=TRUE;
1016 if ( $this->DBFields[$DBFieldName] != $NewValue )
1018 # save value directly to DB
1019 if (is_null($NewValue))
1021 $DB->Query(
"UPDATE Resources SET `"
1022 .$DBFieldName.
"` = NULL"
1023 .
" WHERE ResourceId = ".$ResourceId);
1027 $DB->Query(
"UPDATE Resources SET `"
1028 .$DBFieldName.
"` = ".intval($NewValue)
1029 .
" WHERE ResourceId = ".$ResourceId);
1032 # save value locally
1033 $this->DBFields[$DBFieldName] = $NewValue;
1034 $UpdateModTime = TRUE;
1040 if ($this->DBFields[$DBFieldName.
"X"] != $NewValue[
"X"] ||
1041 $this->DBFields[$DBFieldName.
"Y"] != $NewValue[
"Y"] )
1043 if (is_null($NewValue))
1045 $DB->Query(
"UPDATE Resources SET "
1046 .
"`".$DBFieldName.
"X` = NULL, "
1047 .
"`".$DBFieldName.
"Y` = NULL "
1048 .
"WHERE ResourceId = ".$ResourceId);
1049 $this->DBFields[$DBFieldName.
"X"] = NULL;
1050 $this->DBFields[$DBFieldName.
"Y"] = NULL;
1054 $DB->Query(
"UPDATE Resources SET "
1055 .
"`".$DBFieldName.
"X` = ".(strlen($NewValue[
"X"])
1056 ?
"'".$NewValue[
"X"].
"'" :
"NULL").
", "
1057 .
"`".$DBFieldName.
"Y` = ".(strlen($NewValue[
"Y"])
1058 ?
"'".$NewValue[
"Y"].
"'" :
"NULL")
1059 .
" WHERE ResourceId = ".$ResourceId);
1061 $Digits = $Field->PointDecimalDigits();
1063 $this->DBFields[$DBFieldName.
"X"] =
1064 strlen($NewValue[
"X"]) ? round($NewValue[
"X"], $Digits) : NULL;
1065 $this->DBFields[$DBFieldName.
"Y"] =
1066 strlen($NewValue[
"Y"]) ? round($NewValue[
"Y"], $Digits) : NULL;
1068 $UpdateModTime = TRUE;
1073 if ($this->DBFields[$DBFieldName] != $NewValue)
1075 # save value directly to DB
1076 if (is_null($NewValue))
1078 $DB->Query(
"UPDATE Resources SET `"
1079 .$DBFieldName.
"` = NULL"
1080 .
" WHERE ResourceId = ".$ResourceId);
1084 $NewValue = $NewValue ?
"1" :
"0";
1085 $DB->Query(
"UPDATE Resources SET `"
1086 .$DBFieldName.
"` = ".$NewValue
1087 .
" WHERE ResourceId = ".$ResourceId);
1090 $this->DBFields[$DBFieldName] = $NewValue;
1092 # recalculate counts for any associated classifications if necessary
1093 if ($DBFieldName ==
"ReleaseFlag")
1095 $DB->Query(
"SELECT ClassificationId FROM ResourceClassInts"
1096 .
" WHERE ResourceId = ".$ResourceId);
1097 while ($ClassId = $DB->FetchField(
"ClassificationId"))
1100 $Class->RecalcResourceCount();
1103 $UpdateModTime = TRUE;
1108 # if value passed in was object
1109 if (is_object($NewValue))
1111 # retrieve user ID from object
1112 $UserId = $NewValue->Get(
"UserId");
1114 # else if value passed in was user name
1115 elseif (is_string($NewValue) && strlen($NewValue))
1117 # create user object and retrieve user ID from there
1118 $User =
new CWUser($NewValue);
1119 $UserId = $User->Get(
"UserId");
1123 # assume value is user ID and use value directly
1124 $UserId = $NewValue;
1127 if ($this->DBFields[$DBFieldName] != $UserId)
1129 # save value directly to DB
1130 $DB->Query(
"UPDATE Resources SET `"
1131 .$DBFieldName.
"` = '".$UserId.
"' "
1132 .
"WHERE ResourceId = ".$ResourceId);
1134 # save value locally
1135 $this->DBFields[$DBFieldName] = $UserId;
1136 $UpdateModTime = TRUE;
1141 # if we were given a date object
1142 if (is_object($NewValue))
1144 # use supplied date object
1149 # create date object
1150 $Date =
new Date($NewValue);
1153 $OldDate =
new Date(
1154 $this->DBFields[$DBFieldName.
"Begin"],
1155 $this->DBFields[$DBFieldName.
"End"]);
1157 if ($OldDate->BeginDate() != $Date->BeginDate() ||
1158 $OldDate->EndDate() != $Date->EndDate() ||
1159 $OldDate->Precision() != $Date->Precision() )
1161 # extract values from date object and store in DB
1162 $BeginDate =
"'".$Date->BeginDate().
"'";
1163 if (strlen($BeginDate) < 3) { $BeginDate =
"NULL"; }
1164 $EndDate =
"'".$Date->EndDate().
"'";
1165 if (strlen($EndDate) < 3) { $EndDate =
"NULL"; }
1167 $DB->Query(
"UPDATE Resources SET "
1168 .$DBFieldName.
"Begin = ".$BeginDate.
", "
1169 .$DBFieldName.
"End = ".$EndDate.
", "
1170 .$DBFieldName.
"Precision = '".$Date->Precision().
"' "
1171 .
"WHERE ResourceId = ".$ResourceId);
1173 # save values locally
1174 $this->DBFields[$DBFieldName.
"Begin"] = $Date->BeginDate();
1175 $this->DBFields[$DBFieldName.
"End"] = $Date->EndDate();
1176 $this->DBFields[$DBFieldName.
"Precision"] = $Date->Precision();
1177 $UpdateModTime=TRUE;
1182 if (is_null($NewValue) || !strlen(trim($NewValue)))
1184 $DateValue = $NewValue;
1186 if (!is_null($this->DBFields[$DBFieldName]))
1188 # save value directly to DB
1189 $DB->Query(
"UPDATE Resources SET "
1190 .
"`".$DBFieldName.
"` = NULL "
1191 .
"WHERE ResourceId = ".$ResourceId);
1192 $UpdateModTime = TRUE;
1197 # assume value is date and use directly
1198 $TimestampValue = strtotime($NewValue);
1200 # use the new value if the date is valid
1201 if ($TimestampValue !== FALSE && $TimestampValue >= 0)
1203 $DateValue = date(
"Y-m-d H:i:s", $TimestampValue);
1205 if ($this->DBFields[$DBFieldName] != $DateValue)
1207 # save value directly to DB
1208 $DB->Query(
"UPDATE Resources SET "
1209 .
"`".$DBFieldName.
"` = '".addslashes($DateValue).
"' "
1210 .
"WHERE ResourceId = ".$ResourceId);
1211 $UpdateModTime=TRUE;
1215 # continue using the old value if invalid
1218 $DateValue = $this->
Get($Field);
1222 # save value locally
1223 $this->DBFields[$DBFieldName] = $DateValue;
1227 $OldValue = $this->
Get($Field);
1229 # if incoming value is array
1230 if (is_array($NewValue))
1232 if ($OldValue != $NewValue)
1236 # Remove values that were in the old value, but not the new one:
1237 $ToRemove = array_diff(array_keys($OldValue), array_keys($NewValue));
1238 foreach ($ToRemove as $ClassificationId)
1240 $this->RemoveAssociation(
"ResourceClassInts",
1245 $Class->RecalcResourceCount();
1249 # for each element of array
1250 foreach ($NewValue as
1251 $ClassificationId => $ClassificationName)
1256 # associate with resource if not already associated
1257 if ($this->AddAssociation(
"ResourceClassInts",
1259 $ClassificationId) )
1261 $Class->UpdateLastAssigned();
1262 $Class->RecalcResourceCount();
1267 $UpdateModTime=TRUE;
1272 # associate with resource if not already associated
1273 if (is_object($NewValue))
1276 $NewValue = $Class->Id();
1283 if (!array_key_exists($Class->Id(), $OldValue))
1286 $this->AddAssociation(
"ResourceClassInts",
1289 $Class->UpdateLastAssigned();
1290 $Class->RecalcResourceCount();
1291 $UpdateModTime=TRUE;
1295 # clear our classification cache
1298 unset($this->ClassificationCache);
1304 $OldValue = $this->
Get($Field);
1306 # Clear other values if this field expects unique options
1307 if ($Field->AllowMultiple() === FALSE)
1309 # If we're fed an array for a unique option,
1310 # just use the last element of the array
1311 if (is_array($NewValue)){
1312 $NewValue = array_pop($NewValue);
1315 if (!array_key_exists($NewValue, $OldValue))
1318 $this->RemoveAllAssociations(
"ResourceNameInts",
1321 $UpdateModTime=TRUE;
1325 # if incoming value is array
1326 if (is_array($NewValue) && ($Field->AllowMultiple() !== FALSE) )
1328 if ($OldValue != $NewValue)
1332 $ToRemove = array_diff(array_keys($OldValue), array_keys($NewValue));
1333 foreach ($ToRemove as $CNId)
1335 $this->RemoveAssociation(
"ResourceNameInts",
1341 # for each element of array
1342 foreach ($NewValue as $ControlledNameId => $ControlledName)
1344 # associate with resource if not already associated
1345 if ($this->AddAssociation(
"ResourceNameInts",
1350 $CN->UpdateLastAssigned();
1353 $UpdateModTime=TRUE;
1358 # associate with resource if not already associated
1359 if (is_object($NewValue)) { $NewValue = $NewValue->Id(); }
1360 if (!array_key_exists($NewValue, $OldValue))
1362 $this->AddAssociation(
"ResourceNameInts",
1365 $UpdateModTime=TRUE;
1371 # clear our controlled name cache
1372 unset($this->ControlledNameCache);
1373 unset($this->ControlledNameVariantCache);
1379 # associate value(s) with resource
1380 $this->AddAssociation(
1381 "ResourceImageInts",
"ImageId", $NewValue, $Field);
1385 # convert incoming value to array if necessary
1386 if (!is_array($NewValue)) { $NewValue = array($NewValue); }
1388 # for each incoming file
1390 foreach ($NewValue as $File)
1393 $NewFile = $Factory->
Copy($File);
1395 # associate copy with this resource and field
1396 $NewFile->ResourceId($this->
Id);
1397 $NewFile->FieldId($Field->Id());
1399 # Since we make a fresh copy of the File whenever Set is called,
1400 # we'll always update the modification time for this field.
1401 $UpdateModTime = TRUE;
1405 # convert incoming value to array to simplify the workflow
1406 if (is_scalar($NewValue) || $NewValue instanceof
Resource)
1408 $NewValue = array($NewValue);
1411 # delete existing resource references
1414 # add each reference
1415 foreach ($NewValue as $ReferenceOrId)
1417 # initially issume it's a reference ID and not an object...
1418 $ReferenceId = $ReferenceOrId;
1420 # ... but get the reference ID if it's an object
1421 if ($ReferenceOrId instanceof
Resource)
1423 $ReferenceId = $ReferenceOrId->Id();
1426 # skip blank reference IDs
1427 if (strlen(trim($ReferenceId)) < 1)
1432 # skip reference IDs that don't look right
1433 if (!is_numeric($ReferenceId))
1438 # skip references to the current resource
1439 if ($ReferenceId == $this->
Id())
1444 # add the reference to the references table
1446 INSERT INTO ReferenceInts (
1451 ".addslashes($Field->Id()).
",
1452 ".addslashes($this->
Id()).
",
1453 ".addslashes($ReferenceId).
")");
1459 exit(
"<br>SPT - ERROR: attempt to set unknown resource field type<br>\n");
1465 # update modification timestamps
1467 $UserId = $G_User->IsLoggedIn() ? $G_User->Get(
"UserId") : -1;
1468 $DB->Query(
"DELETE FROM ResourceFieldTimestamps "
1469 .
"WHERE ResourceId=".$this->
Id.
" AND "
1470 .
"FieldId=".$Field->Id() );
1471 $DB->Query(
"INSERT INTO ResourceFieldTimestamps "
1472 .
"(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1473 .$this->
Id.
",".$Field->Id().
","
1474 .$UserId.
",NOW())");
1476 # on resource modification, clear the UserPermsCache entry
1477 # so that stale permissions checks are not cached
1478 $DB->Query(
"DELETE FROM UserPermsCache WHERE ResourceId=".$this->
Id);
1487 # set value by field ID
1490 $Field = $this->Schema->GetField($FieldId);
1491 $this->
Set($Field, $NewValue);
1494 # set qualifier by field name
1497 $Field = $this->Schema->GetFieldByName($FieldName);
1501 # set qualifier by field ID
1504 $Field = $this->Schema->GetField($FieldId);
1508 # set qualifier using field object
1511 # if field uses qualifiers and uses item-level qualifiers
1512 if ($Field->UsesQualifiers() && $Field->HasItemLevelQualifiers())
1514 # if qualifier object passed in
1515 if (is_object($NewValue))
1517 # grab qualifier ID from object
1518 $QualifierId = $NewValue->Id();
1522 # assume value passed in is qualifier ID
1523 $QualifierId = $NewValue;
1526 # update qualifier value in database
1527 $DBFieldName = $Field->DBFieldName();
1528 $this->DB->Query(
"UPDATE Resources SET "
1529 .$DBFieldName.
"Qualifier = '".$QualifierId.
"' "
1530 .
"WHERE ResourceId = ".$this->Id);
1532 # update local qualifier value
1533 $this->DBFields[$DBFieldName.
"Qualifier"] = $QualifierId;
1537 # clear value by field ID
1540 $Field = $this->Schema->GetField($FieldId);
1544 # clear value using field object
1545 function Clear($Field, $ValueToClear = NULL)
1547 # convert field name to object if necessary
1548 if (!is_object($Field))
1550 $Field = $this->Schema->GetFieldByName($Field);
1553 # grab commonly-used values for local use
1555 $ResourceId = $this->Id;
1557 # grab database field name
1558 $DBFieldName = $Field->DBFieldName();
1560 $UpdateModTime=FALSE;
1562 # store value in DB based on field type
1563 switch ($Field->Type())
1572 if (strlen($this->DBFields[$DBFieldName])>0)
1575 $DB->Query(
"UPDATE Resources SET `"
1576 .$DBFieldName.
"` = NULL "
1577 .
"WHERE ResourceId = ".$ResourceId);
1579 # clear value locally
1580 $this->DBFields[$DBFieldName] = NULL;
1581 $UpdateModTime=TRUE;
1586 if (!is_null($this->DBFields[$DBFieldName.
"X"]) ||
1587 !is_null($this->DBFields[$DBFieldName.
"Y"]) )
1590 $DB->Query(
"UPDATE Resources SET "
1591 .
"`".$DBFieldName.
"X` = NULL ,"
1592 .
"`".$DBFieldName.
"Y` = NULL "
1593 .
"WHERE ResourceId = ".$ResourceId);
1595 # Clear local values
1596 $this->DBFields[$DBFieldName.
"X"] = NULL;
1597 $this->DBFields[$DBFieldName.
"Y"] = NULL;
1598 $UpdateModTime=TRUE;
1603 if (!is_null($this->DBFields[$DBFieldName.
"Begin"]) ||
1604 !is_null($this->DBFields[$DBFieldName.
"End"]) ||
1605 !is_null($this->DBFields[$DBFieldName.
"Precision"]))
1607 # clear date object values in DB
1608 $DB->Query(
"UPDATE Resources SET "
1609 .$DBFieldName.
"Begin = '', "
1610 .$DBFieldName.
"End = '', "
1611 .$DBFieldName.
"Precision = '' "
1612 .
"WHERE ResourceId = ".$ResourceId);
1614 # clear value locally
1615 $this->DBFields[$DBFieldName.
"Begin"] = NULL;
1616 $this->DBFields[$DBFieldName.
"End"] = NULL;
1617 $this->DBFields[$DBFieldName.
"Precision"] = NULL;
1618 $UpdateModTime=TRUE;
1623 $OldValue = $this->
Get($Field);
1625 # if value to clear supplied
1626 if ($ValueToClear !== NULL)
1628 # if supplied value is array
1629 if (is_array($ValueToClear))
1631 # for each element of array
1632 foreach ($ValueToClear as $ClassificationId => $Dummy)
1634 if (array_key_exists($ClassificationId, $OldValue))
1636 # remove association with resource (if any)
1637 $this->RemoveAssociation(
"ResourceClassInts",
1641 $Class->RecalcResourceCount();
1642 $UpdateModTime=TRUE;
1648 if (array_key_exists($ValueToClear, $OldValue))
1650 # remove association with resource (if any)
1651 $this->RemoveAssociation(
"ResourceClassInts",
1655 $Class->RecalcResourceCount();
1656 $UpdateModTime=TRUE;
1662 if (count($OldValue)>0)
1664 # remove all associations for resource and field
1665 $this->RemoveAllAssociations(
"ResourceClassInts",
"ClassificationId", $Field);
1667 # recompute resource count
1668 $Values = $this->
Get($Field);
1669 foreach ($Values as $ClassificationId => $Dummy)
1672 $Class->RecalcResourceCount();
1674 $UpdateModTime=TRUE;
1678 # clear our classification cache
1681 unset($this->ClassificationCache);
1687 $OldValue = $this->
Get($Field);
1688 # if value to clear supplied
1689 if ($ValueToClear !== NULL)
1691 # if incoming value is array
1692 if (is_array($ValueToClear))
1694 # for each element of array
1695 foreach ($ValueToClear as $ControlledNameId =>
1698 if (array_key_exists($ControlledNameId, $OldValue))
1700 # remove association with resource (if any)
1701 $this->RemoveAssociation(
"ResourceNameInts",
1704 $UpdateModTime=TRUE;
1710 if (array_key_exists($ValueToClear, $OldValue))
1712 # remove association with resource (if any)
1713 $this->RemoveAssociation(
"ResourceNameInts",
1716 $UpdateModTime=TRUE;
1722 if (count($OldValue)>0)
1724 # remove all associations for resource and field
1725 $this->RemoveAllAssociations(
"ResourceNameInts",
"ControlledNameId", $Field);
1726 $UpdateModTime=TRUE;
1732 # clear our controlled name cache
1733 unset($this->ControlledNameCache);
1734 unset($this->ControlledNameVariantCache);
1739 # if value to clear supplied
1740 if ($ValueToClear !== NULL)
1742 # convert value to array if necessary
1743 $Files = $ValueToClear;
1744 if (!is_array($Files)) { $Files = array($Files); }
1746 # convert values to objects if necessary
1747 foreach ($Files as $Index => $File)
1749 if (!is_object($File))
1751 $Files[$Index] =
new File($File);
1757 # use all files associated with resource
1758 $Files = $this->
Get($Field, TRUE);
1762 foreach ($Files as $File) { $File->Delete(); }
1766 # if value to clear supplied
1767 if ($ValueToClear !== NULL)
1769 # convert value to array if necessary
1770 $Images = $ValueToClear;
1771 if (!is_array($Images)) { $Images = array($Images); }
1773 # convert values to objects if necessary
1774 foreach ($Images as $Index => $Image)
1776 if (!is_object($Image))
1778 $Images[$Index] =
new SPTImage($Image);
1784 # use all images associated with resource
1785 $Images = $this->
Get($Field, TRUE);
1788 # delete images if we are the last resource referencing
1789 # a particular image.
1790 foreach ($Images as $Image) {
1791 $Cnt = $this->DB->Query(
1792 "SELECT COUNT(*) AS Cnt FROM ResourceImageInts WHERE ".
1793 "ImageId=".$Image->Id(),
"Cnt");
1798 # remove connections to images
1799 $UpdateModTime = $this->RemoveAssociation(
1800 "ResourceImageInts",
"ImageId", $Images, $Field);
1804 # remove references from the references table
1806 DELETE FROM ReferenceInts
1807 WHERE FieldId = '".addslashes($Field->Id()).
"'
1808 AND SrcResourceId = '".addslashes($this->
Id()).
"'");
1813 exit(
"<br>SPT - ERROR: attempt to clear unknown resource field type<br>\n");
1819 # update modification timestamps
1821 $UserId = $G_User->IsLoggedIn() ? $G_User->Get(
"UserId") : -1;
1822 $DB->Query(
"DELETE FROM ResourceFieldTimestamps "
1823 .
"WHERE ResourceId=".$this->
Id.
" AND "
1824 .
"FieldId=".$Field->Id() );
1825 $DB->Query(
"INSERT INTO ResourceFieldTimestamps "
1826 .
"(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1827 .$this->
Id.
",".$Field->Id().
","
1828 .$UserId.
",NOW())");
1832 { $this->
Clear($Field, $ValueToClear); }
1835 # --- Field-Specific or Type-Specific Attribute Retrieval Methods -------
1837 # return 2D array of classifications associated with resource
1838 # (first index is classification (field) name, second index is classification ID)
1843 # start with empty array
1846 # for each controlled name
1847 $DB->Query(
"SELECT ClassificationName, MetadataFields.FieldName, "
1848 .
"ResourceClassInts.ClassificationId FROM ResourceClassInts, "
1849 .
"Classifications, MetadataFields "
1850 .
"WHERE ResourceClassInts.ResourceId = ".$this->
Id.
" "
1851 .
"AND ResourceClassInts.ClassificationId = Classifications.ClassificationId "
1852 .
"AND Classifications.FieldId = MetadataFields.FieldId ");
1853 while ($Record = $DB->FetchRow())
1856 $Names[$Record[
"FieldName"]][$Record[
"ClassificationId"]] =
1857 $Record[
"ClassificationName"];
1860 # return array to caller
1865 # --- Ratings Methods ---------------------------------------------------
1867 # return cumulative rating (range is usually 0-100)
1870 # return cumulative rating scaled to 1/10th (range is usually 0-10)
1883 # return current number of ratings for resource
1886 # if number of ratings not already set
1889 # obtain number of ratings
1891 $this->DB->Query(
"SELECT Count(*) AS NumberOfRatings "
1892 .
"FROM ResourceRatings "
1893 .
"WHERE ResourceId = ".$this->
Id,
1897 # recalculate cumulative rating if it looks erroneous
1900 $this->UpdateCumulativeRating();
1904 # return number of ratings to caller
1905 return $this->NumberOfRatings;
1908 # update individual rating for resource
1909 function Rating($NewRating = NULL, $UserId = NULL)
1913 # if user ID not supplied
1914 if ($UserId == NULL)
1916 # if user is logged in
1918 if ($User->IsLoggedIn())
1920 # use ID of current user
1921 $UserId = $User->Get(
"UserId");
1925 # return NULL to caller
1930 # sanitize $NewRating
1931 if (!is_null($NewRating))
1933 $NewRating = intval($NewRating);
1936 # if there is a rating for resource and user
1937 $DB->Query(
"SELECT Rating FROM ResourceRatings "
1938 .
"WHERE UserId = ${UserId} AND ResourceId = ".$this->
Id);
1939 if ($Record = $DB->FetchRow())
1941 # if new rating was supplied
1942 if ($NewRating != NULL)
1944 # update existing rating
1945 $DB->Query(
"UPDATE ResourceRatings "
1946 .
"SET Rating = ${NewRating}, DateRated = NOW() "
1947 .
"WHERE UserId = ${UserId} AND ResourceId = ".$this->
Id);
1949 # update cumulative rating value
1950 $this->UpdateCumulativeRating();
1952 # return value is new rating
1953 $Rating = $NewRating;
1957 # get rating value to return to caller
1958 $Rating = $Record[
"Rating"];
1963 # if new rating was supplied
1964 if ($NewRating != NULL)
1967 $DB->Query(
"INSERT INTO ResourceRatings "
1968 .
"(ResourceId, UserId, DateRated, Rating) "
1975 # update cumulative rating value
1976 $this->UpdateCumulativeRating();
1978 # return value is new rating
1979 $Rating = $NewRating;
1983 # return value is NULL
1988 # return rating value to caller
1993 # --- Resource Comment Methods ------------------------------------------
1995 # return comments as array of Message objects
1998 # read in comments if not already loaded
2001 $this->DB->Query(
"SELECT MessageId FROM Messages "
2002 .
"WHERE ParentId = ".$this->
Id
2003 .
" AND ParentType = 2 "
2004 .
"ORDER BY DatePosted DESC");
2005 while ($MessageId = $this->DB->FetchField(
"MessageId"))
2011 # return array of comments to caller
2012 return $this->Comments;
2015 # return current number of comments
2018 # obtain number of comments if not already set
2022 $this->DB->Query(
"SELECT Count(*) AS NumberOfComments "
2024 .
"WHERE ParentId = ".$this->
Id
2025 .
" AND ParentType = 2",
2030 # return number of comments to caller
2031 return $this->NumberOfComments;
2035 # --- Permission Methods -------------------------------------------------
2048 return $this->CheckSchemaPermissions($User,
"View", $AllowHooksToModify);
2059 return $this->CheckSchemaPermissions($User,
"Edit");
2070 return $this->CheckSchemaPermissions($User,
"Author");
2081 return $this->CheckFieldPermissions( $User, $FieldOrFieldName,
"View" );
2092 return $this->CheckFieldPermissions( $User, $FieldOrFieldName,
"Edit" );
2103 return $this->CheckFieldPermissions( $User, $FieldOrFieldName,
"Author" );
2115 $CheckFn =
"UserCan".(($this->Id()<0) ?
"Author" :
"Edit").
"Field";
2117 return $this->$CheckFn($User, $FieldOrFieldName);
2120 # ---- PRIVATE INTERFACE -------------------------------------------------
2126 private $NumberOfRatings;
2127 private $CumulativeRating;
2128 private $NumberOfComments;
2130 private $LastStatus;
2131 private $ControlledNameCache;
2132 private $ControlledNameVariantCache;
2133 private $ClassificationCache;
2134 private $PermissionCache;
2144 private function CheckSchemaPermissions($User, $CheckType, $AllowHooksToModify=TRUE)
2146 # checks against invalid reosurces should always fail
2147 if ($this->
Status() !== 1) {
return FALSE; }
2149 # construct a key to use for our permissions cache
2150 $CacheKey =
"UserCan".$CheckType.$User->Id();
2152 # if we don't have a cached value for this perm, compute one
2153 if (!isset($this->PermissionCache[$CacheKey]))
2155 # get privileges for schema
2156 $PermsFn = $CheckType.
"ingPrivileges";
2157 $SchemaPrivs = $this->Schema->$PermsFn();
2159 # check passes if user privileges are greater than resource set
2160 $CheckResult = $SchemaPrivs->MeetsRequirements($User, $this);
2162 # save the result of this check in our cache
2163 $this->PermissionCache[$CacheKey] = $CheckResult;
2166 $Value = $this->PermissionCache[$CacheKey];
2168 if ($AllowHooksToModify)
2170 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2171 "EVENT_RESOURCE_".strtoupper($CheckType).
"_PERMISSION_CHECK",
2173 "Resource" => $this,
2175 "Can".$CheckType => $Value));
2177 $Value = $SignalResult[
"Can".$CheckType];
2190 private function CheckFieldPermissions($User, $FieldOrFieldName, $CheckType)
2192 # checks against invalid resources should always fail
2193 if ($this->
Status() !== 1) {
return FALSE; }
2195 # get field object (if not supplied)
2196 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2197 : $this->Schema->GetFieldByName($FieldOrFieldName);
2199 # checks against invalid fields should also fail
2202 # construct a key to use for our permissions cache
2203 $CacheKey =
"UserCan".$CheckType.
"Field".$Field->Id().
"-".$User->Id();
2205 # if we don't have a cahced value, compute one
2206 if (!isset($this->PermissionCache[$CacheKey]))
2208 # checks for disabled fields should not pass
2209 if (!$Field->Enabled())
2211 $CheckResult = FALSE;
2215 # be sure schema privs allow View/Edit/Author for this resource
2216 $SchemaCheckFn =
"UserCan".$CheckType;
2217 if ($this->$SchemaCheckFn($User))
2219 # get appropriate privilege set for field
2220 $PermsFn = $CheckType.
"ingPrivileges";
2221 $FieldPrivs = $Field->$PermsFn();
2223 # user can View/Edit/Author if privileges are greater than field set
2224 $CheckResult = $FieldPrivs->MeetsRequirements($User, $this);
2228 $CheckResult = FALSE;
2232 # allow plugins to modify result of permission check
2233 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2234 "EVENT_FIELD_".strtoupper($CheckType).
"_PERMISSION_CHECK", array(
2236 "Resource" => $this,
2238 "Can".$CheckType => $CheckResult));
2239 $CheckResult = $SignalResult[
"Can".$CheckType];
2241 # save the result of this check in our cache
2242 $this->PermissionCache[$CacheKey] = $CheckResult;
2245 # return cached permission value
2246 return $this->PermissionCache[$CacheKey];
2249 # recalculate and save cumulative rating value for resource
2250 private function UpdateCumulativeRating()
2252 # grab totals from DB
2253 $this->DB->Query(
"SELECT COUNT(Rating) AS Count, "
2254 .
"SUM(Rating) AS Total FROM ResourceRatings "
2255 .
"WHERE ResourceId = ".$this->
Id);
2256 $Record = $this->DB->FetchRow();
2258 # calculate new cumulative rating
2261 # save new cumulative rating in DB
2262 $this->DB->Query(
"UPDATE Resources "
2264 .
"WHERE ResourceId = ".$this->Id);
2277 private function AddAssociation($TableName, $FieldName, $Value, $Field = NULL)
2279 # We should ignore duplicate key errors when doing inserts:
2280 $this->DB->SetQueryErrorsToIgnore( array(
2281 "/INSERT INTO ".$TableName.
"/" =>
2282 "/Duplicate entry '[0-9]+-[0-9]+' for key/"));
2284 # start out assuming no association will be added
2285 $AssociationAdded = FALSE;
2287 # convert new value to array if necessary
2288 $Values = is_array($Value) ? $Value : array($Value);
2290 # for each new value
2291 foreach ($Values as $Value)
2293 # retrieve ID from value if necessary
2294 if (is_object($Value)) { $Value = $Value->Id(); }
2296 # Try to insert a new entry for this association.
2297 $this->DB->Query(
"INSERT INTO ".$TableName.
" SET"
2298 .
" ResourceId = ".intval($this->
Id)
2299 .
", ".$FieldName.
" = ".intval($Value)
2300 .($Field ?
", FieldId = ".intval($Field->Id()) :
""));
2302 # If the insert ran without a duplicate key error,
2303 # then we added an assocation:
2304 if ($this->DB->IgnoredError() === FALSE)
2305 $AssociationAdded = TRUE;
2308 # Clear ignored errors:
2309 $this->DB->SetQueryErrorsToIgnore( NULL );
2311 # report to caller whether association was added
2312 return $AssociationAdded;
2325 private function RemoveAssociation($TableName, $FieldName, $Value, $Field = NULL)
2327 # start out assuming no association will be removed
2328 $AssociationRemoved = FALSE;
2330 # convert value to array if necessary
2331 $Values = is_array($Value) ? $Value : array($Value);
2334 foreach ($Values as $Value)
2336 # retrieve ID from value if necessary
2337 if (is_object($Value)) { $Value = $Value->Id(); }
2339 # remove any intersections with target ID from DB
2340 $this->DB->Query(
"DELETE FROM ".$TableName
2341 .
" WHERE ResourceId = ".intval($this->
Id)
2342 .($Field ?
" AND FieldId = ".intval($Field->Id()) :
"")
2343 .
" AND ".$FieldName.
" = ".intval($Value));
2344 if ($this->DB->NumRowsAffected()) { $AssociationRemoved = TRUE; }
2347 # report to caller whether association was added
2348 return $AssociationRemoved;
2351 # remove all intersections for resource and field (if any)
2352 private function RemoveAllAssociations($TableName, $TargetFieldName, $Field)
2354 # retrieve list of entries for this field and resource
2355 $Entries = $this->
Get($Field);
2357 # Divide them into chunks of not more than 50:
2358 foreach (array_chunk($Entries, 100, TRUE) as $Chunk)
2360 # Construct a query that will remove assocations in this chunk:
2361 $this->DB->Query(
"DELETE FROM ".$TableName
2362 .
" WHERE ResourceId = ".intval($this->
Id)
2363 .
" AND ".$TargetFieldName.
" IN "
2364 .
"(".implode(
",", array_keys($Chunk)).
")");
GetByField($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Old method for retrieving values, deprecated in favor of Get().
UserCanView(User $User, $AllowHooksToModify=TRUE)
Determine if the given user can view the resource, e.g., on the full record page. ...
GetFilesForResource($ResourceOrResourceId, $ReturnObjects=TRUE)
Retrieve all files (names or objects) for specified resource.
SetQualifier($FieldName, $NewValue)
Abstraction for forum messages and resource comments.
SQL database abstraction object with smart query caching.
UserCanModifyField($User, $FieldOrFieldName)
Check whether user is allowed to modify (Edit for perm resources, Author for temp) specified metadata...
GetAsArray($IncludeDisabledFields=FALSE, $ReturnObjects=TRUE)
Retrieve all resource values as an array.
Set($FieldNameOrObject, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Id()
Retrieve numerical resource ID.
UserCanEditField($User, $FieldOrFieldName)
Check whether user is allowed to edit specified metadata field.
SetQualifierByField($Field, $NewValue)
Rating($NewRating=NULL, $UserId=NULL)
Status()
Retrieve result of last operation if available.
GetQualifier($FieldName, $ReturnObject=TRUE)
Retrieve qualifier by field name.
Factory object for Folder class, used to retrieve and manage Folders and groups of Folders...
Copy($FileToCopy)
Create copy of File and return to caller.
Metadata type representing non-hierarchical controlled vocabulary values.
UserCanEdit($User)
Determine if the given user can edit the resource.
CWIS-specific user factory class.
Factory for manipulating File objects.
GetQualifierByFieldId($FieldId, $ReturnObject=TRUE)
Retrieve qualifier by field ID.
Encapsulates a full-size, preview, and thumbnail image.
UserCanAuthorField($User, $FieldOrFieldName)
Check whether user is allowed to author specified metadata field.
Clear($Field, $ValueToClear=NULL)
UserCanAuthor($User)
Determine if the given user can edit the resource.
GetByFieldId($FieldId, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field ID.
IsTempResource($NewSetting=NULL)
Get/set whether resource is a temporary record.
Get($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
SetByField($Field, $NewValue)
Method replaced by Resource::Set(), preserved for backward compatibility.
__construct($ResourceId)
Object constructor for loading an existing resource.
GetMapped($MappedName, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using standard (mapped) field name.
Represents a "resource" in CWIS.
GetQualifierByField($Field, $ReturnObject=TRUE)
Retrieve qualifier by Field object.
ClearByFieldId($FieldId, $ValueToClear=NULL)
const CLASSSTAT_OK
Status code indicating operation completed successfully.
SetQualifierByFieldId($FieldId, $NewValue)
SchemaId()
Retrieve ID of schema for resource.
static Create($SchemaId)
Create a new resource.
UserCanViewField($User, $FieldOrFieldName)
Check whether user is allowed to view specified metadata field.
Metadata type representing hierarchical ("Tree") controlled vocabulary values.
SetByFieldId($FieldId, $NewValue)
ClearByField($Field, $ValueToClear=NULL)
Class representing a stored (usually uploaded) file.
Factory for Resource objects.
CWIS-specific user class.
FieldIsSet($FieldNameOrObject, $IgnorePadding=FALSE)
Determine if the value for a field is set.
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...