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
75 SET `ResourceId` = '".intval($Id).
"',
76 `SchemaId` = '".intval($SchemaId).
"'");
79 $DB->Query(
"UNLOCK TABLES");
81 # create new Resource object
84 # set some additional fields for default resources
87 $Resource->Set(
"Added By Id", $GLOBALS[
"G_User"]->
Id());
88 $Resource->Set(
"Last Modified By Id", $GLOBALS[
"G_User"]->
Id());
89 $Resource->Set(
"Date Of Record Creation", date(
"Y-m-d H:i:s"));
90 $Resource->Set(
"Date Last Modified", date(
"Y-m-d H:i:s"));
93 # set any default values
100 foreach ($Fields as $Field)
102 $DefaultValue = $Field->DefaultValue();
104 # flip option default values to get into the form that
105 # Resource::Set() expects
107 && is_array($DefaultValue))
109 $DefaultValue = array_flip($DefaultValue);
112 $Resource->SetByField($Field, $DefaultValue);
115 # update timestamps as required
117 foreach ($TimestampFields as $Field)
119 if ($Field->UpdateMethod() ==
122 $Resource->SetByField($Field,
"now");
126 # signal resource creation
127 $GLOBALS[
"AF"]->SignalEvent(
"EVENT_RESOURCE_CREATE", array(
128 "Resource" => $Resource,
131 # return new Resource object to caller
143 # signal that resource deletion is about to occur
145 $AF->SignalEvent(
"EVENT_RESOURCE_DELETE", array(
149 # grab list of classifications
152 # delete resource/classification intersections
154 $DB->Query(
"DELETE FROM ResourceClassInts WHERE ResourceId = ".$this->
Id());
156 # for each classification type
157 foreach ($Classifications as $ClassType => $ClassesOfType)
159 # for each classification of that type
160 foreach ($ClassesOfType as $ClassId => $ClassName)
162 # recalculate resource count for classification
164 $Class->RecalcResourceCount();
168 # delete resource references
170 DELETE FROM ReferenceInts
171 WHERE SrcResourceId = '".addslashes($this->
Id()).
"'
172 OR DstResourceId = '".addslashes($this->
Id()).
"'");
174 # delete resource/name intersections
175 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ResourceId = ".$this->
Id());
177 # delete any associated images not in use by other resources
178 $DB->Query(
"SELECT ImageId FROM ResourceImageInts"
179 .
" WHERE ResourceId = ".intval($this->
Id()));
180 $ImageIds = $DB->FetchColumn(
"ImageId");
181 foreach ($ImageIds as $ImageId)
183 $DB->Query(
"SELECT ResourceId FROM ResourceImageInts"
184 .
" WHERE ImageId = ".intval($ImageId)
185 .
" AND ResourceId != ".intval($this->
Id()));
186 if ($DB->NumRowsSelected() == 0)
193 # delete any associated files
195 $Files = $Factory->GetFilesForResource($this->
Id());
196 foreach ($Files as $File)
201 # delete resource record from database
202 $DB->Query(
"DELETE FROM Resources WHERE ResourceId = ".$this->
Id());
204 # drop item from search engine and recommender system
205 if ($SysConfig->SearchDBEnabled())
208 $SearchEngine->DropItem($this->
Id());
210 if ($SysConfig->RecommenderDBEnabled())
213 $Recommender->DropItem($this->
Id());
216 # get the folders containing the resource
218 $Folders = $FolderFactory->GetFoldersContainingItem(
222 # drop the resource from each folder it belongs to
223 foreach ($Folders as $Folder)
225 # mixed item type folder
226 if ($Folder->ContainsItem($this->Id,
"Resource"))
228 $Folder->RemoveItem($this->
Id,
"Resource");
231 # single item type folder
234 $Folder->RemoveItem($this->
Id);
238 # delete any resource comments
239 $DB->Query(
"DELETE FROM Messages WHERE ParentId = ".$this->
Id);
246 function Status() {
return $this->LastStatus; }
252 function Id() {
return $this->Id; }
258 function SchemaId() {
return $this->DBFields[
"SchemaId"]; }
268 # if new temp resource setting supplied
269 if (!is_null($NewSetting))
271 # if caller requested to switch
273 if ((($this->
Id() < 0) && ($NewSetting == FALSE))
274 || (($this->
Id() >= 0) && ($NewSetting == TRUE)))
276 # lock DB tables to prevent next ID from being grabbed
277 $DB->Query(
"LOCK TABLES Resources write");
279 # get next resource ID as appropriate
280 $OldResourceId = $this->Id;
282 if ($NewSetting == TRUE)
284 $this->
Id = $Factory->GetNextTempItemId();
288 $this->
Id = $Factory->GetNextItemId();
292 $DB->Query(
"UPDATE Resources SET ResourceId = ".
293 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
296 $DB->Query(
"UNLOCK TABLES");
298 # change associations
299 unset($this->ClassificationCache);
300 $DB->Query(
"UPDATE ResourceClassInts SET ResourceId = ".
301 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
302 unset($this->ControlledNameCache);
303 unset($this->ControlledNameVariantCache);
304 $DB->Query(
"UPDATE ResourceNameInts SET ResourceId = ".
305 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
306 $DB->Query(
"UPDATE Files SET ResourceId = ".
307 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
308 $DB->Query(
"UPDATE ReferenceInts SET SrcResourceId = ".
309 $this->
Id.
" WHERE SrcResourceId = ".$OldResourceId);
310 $DB->Query(
"UPDATE ResourceImageInts SET ResourceId = ".
311 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
313 # signal event as appropriate
314 if ($NewSetting === FALSE)
316 $GLOBALS[
"AF"]->SignalEvent(
"EVENT_RESOURCE_ADD", array(
323 # report to caller whether we are a temp resource
324 return ($this->
Id() < 0) ? TRUE : FALSE;
328 # --- Generic Attribute Retrieval Methods -------------------------------
344 function Get($FieldNameOrObject, $ReturnObject = FALSE, $IncludeVariants = FALSE)
346 # load field object if needed
347 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
348 : $this->Schema->GetFieldByName($FieldNameOrObject);
350 # return no value found if we don't have a valid field
353 # grab database field name
354 $DBFieldName = $Field->DBFieldName();
356 # format return value based on field type
357 switch ($Field->Type())
362 $ReturnValue = isset($this->DBFields[$DBFieldName])
363 ? (string)$this->DBFields[$DBFieldName] : NULL;
367 $ReturnValue = isset($this->DBFields[$DBFieldName])
368 ? (int)$this->DBFields[$DBFieldName] : NULL;
372 $ReturnValue = isset($this->DBFields[$DBFieldName])
373 ? (bool)$this->DBFields[$DBFieldName] : NULL;
377 $ReturnValue = array(
"X" => (
float)$this->DBFields[$DBFieldName.
"X"],
378 "Y" => (
float)$this->DBFields[$DBFieldName.
"Y"]);
382 $Date =
new Date($this->DBFields[$DBFieldName.
"Begin"],
383 $this->DBFields[$DBFieldName.
"End"],
384 $this->DBFields[$DBFieldName.
"Precision"]);
387 $ReturnValue = $Date;
391 $ReturnValue = $Date->Formatted();
396 $ReturnValue = $this->DBFields[$DBFieldName];
400 # start with empty array
401 $ReturnValue = array();
403 # if classification cache has not been loaded
404 if (!isset($this->ClassificationCache))
406 # load all classifications associated with this resource into cache
407 $this->ClassificationCache = array();
409 "SELECT Classifications.ClassificationId,"
410 .
" Classifications.FieldId,ClassificationName"
411 .
" FROM ResourceClassInts, Classifications"
412 .
" WHERE ResourceClassInts.ResourceId = ".$this->
Id
413 .
" AND ResourceClassInts.ClassificationId"
414 .
" = Classifications.ClassificationId");
415 while ($Record = $this->DB->FetchRow())
417 $this->ClassificationCache[$Record[
"ClassificationId"]][
"Name"] =
418 $Record[
"ClassificationName"];
419 $this->ClassificationCache[$Record[
"ClassificationId"]][
"FieldId"] =
424 # for each entry in classification cache
425 foreach ($this->ClassificationCache as $ClassificationId => $ClassificationInfo)
427 # if classification ID matches field we are looking for
428 if ($ClassificationInfo[
"FieldId"] == $Field->Id())
430 # add field to result
433 $ReturnValue[$ClassificationId] =
new Classification($ClassificationId);
437 $ReturnValue[$ClassificationId] = $ClassificationInfo[
"Name"];
445 # start with empty array
446 $ReturnValue = array();
448 # if controlled name cache has not been loaded
449 if (!isset($this->ControlledNameCache))
451 # load all controlled names associated with this resource into cache
452 $this->ControlledNameCache = array();
454 "SELECT ControlledNames.ControlledNameId,"
455 .
" ControlledNames.FieldId,ControlledName"
456 .
" FROM ResourceNameInts, ControlledNames"
457 .
" WHERE ResourceNameInts.ResourceId = ".$this->
Id
458 .
" AND ResourceNameInts.ControlledNameId"
459 .
" = ControlledNames.ControlledNameId"
460 .
" ORDER BY ControlledNames.ControlledName ASC");
461 while ($Record = $this->DB->FetchRow())
463 $this->ControlledNameCache[$Record[
"ControlledNameId"]][
"Name"] = $Record[
"ControlledName"];
464 $this->ControlledNameCache[$Record[
"ControlledNameId"]][
"FieldId"] = $Record[
"FieldId"];
468 # if variant names requested and variant name cache has not been loaded
469 if ($IncludeVariants && !isset($this->ControlledNameVariantCache))
471 # load all controlled names associated with this resource into cache
472 $this->ControlledNameVariantCache = array();
473 $this->DB->Query(
"SELECT ControlledNames.ControlledNameId,"
474 .
" ControlledNames.FieldId,"
475 .
" ControlledName, VariantName"
476 .
" FROM ResourceNameInts, ControlledNames, VariantNames"
477 .
" WHERE ResourceNameInts.ResourceId = ".$this->
Id
478 .
" AND ResourceNameInts.ControlledNameId"
479 .
" = ControlledNames.ControlledNameId"
480 .
" AND VariantNames.ControlledNameId"
481 .
" = ControlledNames.ControlledNameId");
482 while ($Record = $this->DB->FetchRow())
484 $this->ControlledNameVariantCache[$Record[
"ControlledNameId"]][]
485 = $Record[
"VariantName"];
489 # for each entry in controlled name cache
490 foreach ($this->ControlledNameCache as $ControlledNameId => $ControlledNameInfo)
492 # if controlled name type matches field we are looking for
493 if ($ControlledNameInfo[
"FieldId"] == $Field->Id())
495 # if objects requested
498 $ReturnValue[$ControlledNameId] =
503 # if variant names requested
504 if ($IncludeVariants)
506 # add field to result
507 $ReturnValue[] = $ControlledNameInfo[
"Name"];
509 # add any variant names to result
510 if (isset($this->ControlledNameVariantCache[$ControlledNameId]))
512 $ReturnValue = array_merge(
514 $this->ControlledNameVariantCache[$ControlledNameId]);
519 # add field with index to result
520 $ReturnValue[$ControlledNameId] =
521 $ControlledNameInfo[
"Name"];
529 $User =
new CWUser(intval($this->DBFields[$DBFieldName]));
532 $ReturnValue = $User;
536 $ReturnValue = $User->Get(
"UserName");
541 # start out assuming no images will be found
542 $ReturnValue = array();
544 # find all images associated with this resource
545 $this->DB->Query(
"SELECT ImageId FROM ResourceImageInts"
546 .
" WHERE ResourceId = ".intval($this->
Id())
547 .
" AND FieldId = ".intval($Field->Id()));
549 # if images were found
550 if ($this->DB->NumRowsSelected())
552 # if we are to return an object
553 $ImageIds = $this->DB->FetchColumn(
"ImageId");
556 # load array of Image objects for return value
557 foreach ($ImageIds as $ImageId)
559 $ReturnValue[$ImageId] =
new SPTImage($ImageId);
564 # load array of Image ids for return value
565 $ReturnValue = $ImageIds;
571 # retrieve files using factory
574 $this->
Id, $ReturnObject);
578 # query for resource references
580 SELECT * FROM ReferenceInts
581 WHERE FieldId = '".addslashes($Field->Id()).
"'
582 AND SrcResourceId = '".addslashes($this->
Id()).
"'");
584 $ReturnValue = array();
586 # return each reference as a Resource object
589 $FoundErrors = FALSE;
591 while (FALSE !== ($Record = $this->DB->FetchRow()))
593 $ReferenceId = $Record[
"DstResourceId"];
594 $Reference =
new Resource($ReferenceId);
596 # the reference is bad, so flag that there were errors
597 if ($Reference->Status() != 1)
604 $ReturnValue[$ReferenceId] = $Reference;
608 # try to fix the errors by removing any references to
609 # resources that were bad
612 $this->
Set($Field, $ReturnValue);
616 # return each reference as a resource ID
619 while (FALSE !== ($Record = $this->DB->FetchRow()))
621 $ReferenceId = $Record[
"DstResourceId"];
622 $ReturnValue[$ReferenceId] = $ReferenceId;
629 exit(
"<br>SPT - ERROR: attempt to retrieve unknown resource field type (".$Field->Type().
")<br>\n");
633 # return formatted value to caller
641 $ReturnObject = FALSE, $IncludeVariants = FALSE)
642 {
return $this->
Get($FieldNameOrObject, $ReturnObject, $IncludeVariants); }
657 function GetByFieldId($FieldId, $ReturnObject = FALSE, $IncludeVariants = FALSE)
659 $Field = $this->Schema->GetField($FieldId);
660 return ($Field) ? $this->
Get($Field, $ReturnObject, $IncludeVariants) : NULL;
675 function GetAsArray($IncludeDisabledFields = FALSE, $ReturnObjects = TRUE)
677 # retrieve field info
678 $Fields = $this->Schema->GetFields();
681 foreach ($Fields as $Field)
683 # if field is enabled or caller requested disabled fields
684 if ($Field->Enabled() || $IncludeDisabledFields)
686 # retrieve info and add it to the array
687 $FieldStrings[$Field->Name()] = $this->
Get($Field, $ReturnObjects);
689 # if field uses qualifiers
690 if ($Field->UsesQualifiers())
692 # get qualifier attributes and add to the array
693 $FieldStrings[$Field->Name().
" Qualifier"] =
699 # add in internal values
700 $FieldStrings[
"ResourceId"] = $this->
Id();
703 # return array to caller
704 return $FieldStrings;
721 function GetMapped($MappedName, $ReturnObject = FALSE, $IncludeVariants = FALSE)
723 return $this->Schema->StdNameToFieldMapping($MappedName)
724 ? $this->
GetByFieldId($this->Schema->StdNameToFieldMapping($MappedName),
725 $ReturnObject, $IncludeVariants)
739 $Field = $this->Schema->GetFieldByName($FieldName);
753 $Field = $this->Schema->GetField($FieldId);
767 # return NULL if field is invalid
770 # assume no qualifiers if not otherwise determined
773 # if field uses qualifiers
774 if ($Field->UsesQualifiers())
776 # retrieve qualifiers based on field type
777 switch ($Field->Type())
782 # retrieve list of items
785 # if field uses item-level qualifiers
786 if ($Field->HasItemLevelQualifiers())
788 # determine general item name in DB
790 ?
"Classification" :
"ControlledName";
793 foreach (
$Items as $ItemId => $ItemName)
795 # look up qualifier for item
796 $QualId = $this->DB->Query(
797 "SELECT * FROM ".$TableName.
"s"
798 .
" WHERE ".$TableName.
"Id = ".$ItemId
804 # if object was requested by caller
807 # load qualifier and add to return value array
808 $ReturnValue[$ItemId] =
new Qualifier($QualId);
812 # add qualifier ID to return value array
813 $ReturnValue[$ItemId] = $QualId;
818 # add NULL to return value array for this item
819 $ReturnValue[$ItemId] = NULL;
826 foreach (
$Items as $ItemId => $ItemName)
828 # if object was requested by caller
831 # load default qualifier and add to return value array
832 $ReturnValue[$ItemId] =
new Qualifier($Field->DefaultQualifier());
836 # add default qualifier ID to return value array
837 $ReturnValue[$ItemId] = $Field->DefaultQualifier();
844 # if field uses item-level qualifiers
845 if ($Field->HasItemLevelQualifiers())
847 # if qualifier available
848 if ($this->DBFields[$Field->DBFieldName().
"Qualifier"] > 0)
850 # if object was requested by caller
853 # return qualifier for field
854 $ReturnValue =
new Qualifier($this->DBFields[$Field->DBFieldName().
"Qualifier"]);
858 # return qualifier ID for field
859 $ReturnValue = $this->DBFields[$Field->DBFieldName().
"Qualifier"];
865 # if default qualifier available
866 if ($Field->DefaultQualifier() > 0)
868 # if object was requested by caller
871 # return default qualifier
872 $ReturnValue =
new Qualifier($Field->DefaultQualifier());
876 # return default qualifier ID
877 $ReturnValue = $Field->DefaultQualifier();
885 # return qualifier object or ID (or array of same) to caller
896 function FieldIsSet($FieldNameOrObject, $IgnorePadding=FALSE)
898 # load field object if needed
899 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
900 : $this->Schema->GetFieldByName($FieldNameOrObject);
902 # return no value found if we don't have a valid field
906 $Value = $this->
Get($Field);
908 # checks depend on the field type
909 switch ($Field->Type())
917 && (!$IgnorePadding || ($IgnorePadding && strlen(trim($Value))));
924 return isset($Value[
"X"])
925 && isset($Value[
"Y"])
926 && strlen(trim($Value[
"X"]))
927 && strlen(trim($Value[
"Y"]));
931 && strlen(trim($Value))
932 && $Value !=
"0000-00-00";
936 && strlen(trim($Value))
937 && $Value !=
"0000-00-00 00:00:00";
945 return count($Value) > 0;
951 && $Factory->UserNameExists($Value);
958 # --- Generic Attribute Setting Methods ---------------------------------
965 function Set($FieldNameOrObject, $NewValue)
967 # load field object if needed
968 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
969 : $this->Schema->GetFieldByName($FieldNameOrObject);
971 # return if we don't have a valid field
974 # grab commonly-used values for local use
976 $ResourceId = $this->Id;
978 # grab database field name
979 $DBFieldName = $Field->DBFieldName();
981 # Flag to deterimine if we've actually changed anything.
982 $UpdateModTime = FALSE;
984 # store value in DB based on field type
985 switch ($Field->Type())
990 if ($this->DBFields[$DBFieldName] != $NewValue)
992 # save value directly to DB
993 $DB->Query(
"UPDATE Resources SET `"
994 .$DBFieldName.
"` = '".addslashes($NewValue).
"' "
995 .
"WHERE ResourceId = ".$ResourceId);
998 $this->DBFields[$DBFieldName] = $NewValue;
1004 if ( $this->DBFields[$DBFieldName] != $NewValue )
1006 # save value directly to DB
1007 if (is_null($NewValue))
1009 $DB->Query(
"UPDATE Resources SET `"
1010 .$DBFieldName.
"` = NULL"
1011 .
" WHERE ResourceId = ".$ResourceId);
1015 $DB->Query(
"UPDATE Resources SET `"
1016 .$DBFieldName.
"` = ".intval($NewValue)
1017 .
" WHERE ResourceId = ".$ResourceId);
1020 # save value locally
1021 $this->DBFields[$DBFieldName] = $NewValue;
1022 $UpdateModTime = TRUE;
1028 if ($this->DBFields[$DBFieldName.
"X"] != $NewValue[
"X"] ||
1029 $this->DBFields[$DBFieldName.
"Y"] != $NewValue[
"Y"] )
1031 if (is_null($NewValue))
1033 $DB->Query(
"UPDATE Resources SET "
1034 .
"`".$DBFieldName.
"X` = NULL, "
1035 .
"`".$DBFieldName.
"Y` = NULL "
1036 .
"WHERE ResourceId = ".$ResourceId);
1037 $this->DBFields[$DBFieldName.
"X"] = NULL;
1038 $this->DBFields[$DBFieldName.
"Y"] = NULL;
1042 $DB->Query(
"UPDATE Resources SET "
1043 .
"`".$DBFieldName.
"X` = ".(strlen($NewValue[
"X"])
1044 ?
"'".$NewValue[
"X"].
"'" :
"NULL").
", "
1045 .
"`".$DBFieldName.
"Y` = ".(strlen($NewValue[
"Y"])
1046 ?
"'".$NewValue[
"Y"].
"'" :
"NULL")
1047 .
" WHERE ResourceId = ".$ResourceId);
1049 $Digits = $Field->PointDecimalDigits();
1051 $this->DBFields[$DBFieldName.
"X"] =
1052 strlen($NewValue[
"X"]) ? round($NewValue[
"X"], $Digits) : NULL;
1053 $this->DBFields[$DBFieldName.
"Y"] =
1054 strlen($NewValue[
"Y"]) ? round($NewValue[
"Y"], $Digits) : NULL;
1056 $UpdateModTime = TRUE;
1061 if ($this->DBFields[$DBFieldName] != $NewValue)
1063 # save value directly to DB
1064 if (is_null($NewValue))
1066 $DB->Query(
"UPDATE Resources SET `"
1067 .$DBFieldName.
"` = NULL"
1068 .
" WHERE ResourceId = ".$ResourceId);
1072 $NewValue = $NewValue ?
"1" :
"0";
1073 $DB->Query(
"UPDATE Resources SET `"
1074 .$DBFieldName.
"` = ".$NewValue
1075 .
" WHERE ResourceId = ".$ResourceId);
1078 $this->DBFields[$DBFieldName] = $NewValue;
1080 # recalculate counts for any associated classifications if necessary
1081 if ($DBFieldName ==
"ReleaseFlag")
1083 $DB->Query(
"SELECT ClassificationId FROM ResourceClassInts"
1084 .
" WHERE ResourceId = ".$ResourceId);
1085 while ($ClassId = $DB->FetchField(
"ClassificationId"))
1088 $Class->RecalcResourceCount();
1091 $UpdateModTime = TRUE;
1096 # if value passed in was object
1097 if (is_object($NewValue))
1099 # retrieve user ID from object
1100 $UserId = $NewValue->Get(
"UserId");
1102 # else if value passed in was user name
1103 elseif (is_string($NewValue) && strlen($NewValue))
1105 # create user object and retrieve user ID from there
1106 $User =
new CWUser($NewValue);
1107 $UserId = $User->Get(
"UserId");
1111 # assume value is user ID and use value directly
1112 $UserId = $NewValue;
1115 if ($this->DBFields[$DBFieldName] != $UserId)
1117 # save value directly to DB
1118 $DB->Query(
"UPDATE Resources SET `"
1119 .$DBFieldName.
"` = '".$UserId.
"' "
1120 .
"WHERE ResourceId = ".$ResourceId);
1122 # save value locally
1123 $this->DBFields[$DBFieldName] = $UserId;
1124 $UpdateModTime = TRUE;
1129 # if we were given a date object
1130 if (is_object($NewValue))
1132 # use supplied date object
1137 # create date object
1138 $Date =
new Date($NewValue);
1141 $OldDate =
new Date(
1142 $this->DBFields[$DBFieldName.
"Begin"],
1143 $this->DBFields[$DBFieldName.
"End"]);
1145 if ($OldDate->BeginDate() != $Date->BeginDate() ||
1146 $OldDate->EndDate() != $Date->EndDate() ||
1147 $OldDate->Precision() != $Date->Precision() )
1149 # extract values from date object and store in DB
1150 $BeginDate =
"'".$Date->BeginDate().
"'";
1151 if (strlen($BeginDate) < 3) { $BeginDate =
"NULL"; }
1152 $EndDate =
"'".$Date->EndDate().
"'";
1153 if (strlen($EndDate) < 3) { $EndDate =
"NULL"; }
1155 $DB->Query(
"UPDATE Resources SET "
1156 .$DBFieldName.
"Begin = ".$BeginDate.
", "
1157 .$DBFieldName.
"End = ".$EndDate.
", "
1158 .$DBFieldName.
"Precision = '".$Date->Precision().
"' "
1159 .
"WHERE ResourceId = ".$ResourceId);
1161 # save values locally
1162 $this->DBFields[$DBFieldName.
"Begin"] = $Date->BeginDate();
1163 $this->DBFields[$DBFieldName.
"End"] = $Date->EndDate();
1164 $this->DBFields[$DBFieldName.
"Precision"] = $Date->Precision();
1165 $UpdateModTime=TRUE;
1170 if (is_null($NewValue) || !strlen(trim($NewValue)))
1172 $DateValue = $NewValue;
1174 if (!is_null($this->DBFields[$DBFieldName]))
1176 # save value directly to DB
1177 $DB->Query(
"UPDATE Resources SET "
1178 .
"`".$DBFieldName.
"` = NULL "
1179 .
"WHERE ResourceId = ".$ResourceId);
1180 $UpdateModTime = TRUE;
1185 # assume value is date and use directly
1186 $TimestampValue = strtotime($NewValue);
1188 # use the new value if the date is valid
1189 if ($TimestampValue !== FALSE && $TimestampValue >= 0)
1191 $DateValue = date(
"Y-m-d H:i:s", $TimestampValue);
1193 if ($this->DBFields[$DBFieldName] != $DateValue)
1195 # save value directly to DB
1196 $DB->Query(
"UPDATE Resources SET "
1197 .
"`".$DBFieldName.
"` = '".addslashes($DateValue).
"' "
1198 .
"WHERE ResourceId = ".$ResourceId);
1199 $UpdateModTime=TRUE;
1203 # continue using the old value if invalid
1206 $DateValue = $this->
Get($Field);
1210 # save value locally
1211 $this->DBFields[$DBFieldName] = $DateValue;
1215 $OldValue = $this->
Get($Field);
1217 # if incoming value is array
1218 if (is_array($NewValue))
1220 if ($OldValue != $NewValue)
1222 # for each element of array
1223 foreach ($NewValue as
1224 $ClassificationId => $ClassificationName)
1229 # associate with resource if not already associated
1230 $this->AddAssociation(
"ResourceClassInts",
1233 $Class->RecalcResourceCount();
1236 $UpdateModTime=TRUE;
1241 # associate with resource if not already associated
1242 if (is_object($NewValue))
1245 $NewValue = $Class->Id();
1252 if (!array_key_exists($Class->Id(), $OldValue))
1255 $this->AddAssociation(
"ResourceClassInts",
1258 $Class->RecalcResourceCount();
1259 $UpdateModTime=TRUE;
1263 # clear our classification cache
1266 unset($this->ClassificationCache);
1272 $OldValue = $this->
Get($Field);
1274 # Clear other values if this field expects unique options
1275 if ($Field->AllowMultiple() === FALSE)
1277 # If we're fed an array for a unique option,
1278 # just use the last element of the array
1279 if (is_array($NewValue)){
1280 $NewValue = array_pop($NewValue);
1283 if (!array_key_exists($NewValue, $OldValue))
1286 $this->RemoveAllAssociations(
"ResourceNameInts",
1289 $UpdateModTime=TRUE;
1293 # if incoming value is array
1294 if (is_array($NewValue) && ($Field->AllowMultiple() !== FALSE) )
1296 if ($OldValue != $NewValue)
1298 # for each element of array
1299 foreach ($NewValue as $ControlledNameId => $ControlledName)
1301 # associate with resource if not already associated
1302 $this->AddAssociation(
"ResourceNameInts",
1306 $UpdateModTime=TRUE;
1311 # associate with resource if not already associated
1312 if (is_object($NewValue)) { $NewValue = $NewValue->Id(); }
1313 if (!array_key_exists($NewValue, $OldValue))
1315 $this->AddAssociation(
"ResourceNameInts",
1318 $UpdateModTime=TRUE;
1324 # clear our controlled name cache
1325 unset($this->ControlledNameCache);
1326 unset($this->ControlledNameVariantCache);
1332 # associate value(s) with resource
1333 $this->AddAssociation(
1334 "ResourceImageInts",
"ImageId", $NewValue, $Field);
1338 # convert incoming value to array if necessary
1339 if (!is_array($NewValue)) { $NewValue = array($NewValue); }
1341 # for each incoming file
1343 foreach ($NewValue as $File)
1346 $NewFile = $Factory->
Copy($File);
1348 # associate copy with this resource and field
1349 $NewFile->ResourceId($this->
Id);
1350 $NewFile->FieldId($Field->Id());
1352 # Since we make a fresh copy of the File whenever Set is called,
1353 # we'll always update the modification time for this field.
1354 $UpdateModTime = TRUE;
1358 # convert incoming value to array to simplify the workflow
1359 if (is_scalar($NewValue) || $NewValue instanceof
Resource)
1361 $NewValue = array($NewValue);
1364 # delete existing resource references
1367 # add each reference
1368 foreach ($NewValue as $ReferenceOrId)
1370 # initially issume it's a reference ID and not an object...
1371 $ReferenceId = $ReferenceOrId;
1373 # ... but get the reference ID if it's an object
1374 if ($ReferenceOrId instanceof
Resource)
1376 $ReferenceId = $ReferenceOrId->Id();
1379 # skip blank reference IDs
1380 if (strlen(trim($ReferenceId)) < 1)
1385 # skip reference IDs that don't look right
1386 if (!is_numeric($ReferenceId))
1391 # skip references to the current resource
1392 if ($ReferenceId == $this->
Id())
1397 # add the reference to the references table
1399 INSERT INTO ReferenceInts (
1404 ".addslashes($Field->Id()).
",
1405 ".addslashes($this->
Id()).
",
1406 ".addslashes($ReferenceId).
")");
1412 exit(
"<br>SPT - ERROR: attempt to set unknown resource field type<br>\n");
1418 # update modification timestamps
1420 $UserId = $G_User->IsLoggedIn() ? $G_User->Get(
"UserId") : -1;
1421 $DB->Query(
"DELETE FROM ResourceFieldTimestamps "
1422 .
"WHERE ResourceId=".$this->
Id.
" AND "
1423 .
"FieldId=".$Field->Id() );
1424 $DB->Query(
"INSERT INTO ResourceFieldTimestamps "
1425 .
"(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1426 .$this->
Id.
",".$Field->Id().
","
1427 .$UserId.
",NOW())");
1436 # set value by field ID
1439 $Field = $this->Schema->GetField($FieldId);
1440 $this->
Set($Field, $NewValue);
1443 # set qualifier by field name
1446 $Field = $this->Schema->GetFieldByName($FieldName);
1450 # set qualifier by field ID
1453 $Field = $this->Schema->GetField($FieldId);
1457 # set qualifier using field object
1460 # if field uses qualifiers and uses item-level qualifiers
1461 if ($Field->UsesQualifiers() && $Field->HasItemLevelQualifiers())
1463 # if qualifier object passed in
1464 if (is_object($NewValue))
1466 # grab qualifier ID from object
1467 $QualifierId = $NewValue->Id();
1471 # assume value passed in is qualifier ID
1472 $QualifierId = $NewValue;
1475 # update qualifier value in database
1476 $DBFieldName = $Field->DBFieldName();
1477 $this->DB->Query(
"UPDATE Resources SET "
1478 .$DBFieldName.
"Qualifier = '".$QualifierId.
"' "
1479 .
"WHERE ResourceId = ".$this->Id);
1481 # update local qualifier value
1482 $this->DBFields[$DBFieldName.
"Qualifier"] = $QualifierId;
1486 # clear value by field ID
1489 $Field = $this->Schema->GetField($FieldId);
1493 # clear value using field object
1494 function Clear($Field, $ValueToClear = NULL)
1496 # convert field name to object if necessary
1497 if (!is_object($Field))
1499 $Field = $this->Schema->GetFieldByName($Field);
1502 # grab commonly-used values for local use
1504 $ResourceId = $this->Id;
1506 # grab database field name
1507 $DBFieldName = $Field->DBFieldName();
1509 $UpdateModTime=FALSE;
1511 # store value in DB based on field type
1512 switch ($Field->Type())
1521 if (strlen($this->DBFields[$DBFieldName])>0)
1524 $DB->Query(
"UPDATE Resources SET `"
1525 .$DBFieldName.
"` = NULL "
1526 .
"WHERE ResourceId = ".$ResourceId);
1528 # clear value locally
1529 $this->DBFields[$DBFieldName] = NULL;
1530 $UpdateModTime=TRUE;
1535 if (!is_null($this->DBFields[$DBFieldName.
"X"]) ||
1536 !is_null($this->DBFields[$DBFieldName.
"Y"]) )
1539 $DB->Query(
"UPDATE Resources SET "
1540 .
"`".$DBFieldName.
"X` = NULL ,"
1541 .
"`".$DBFieldName.
"Y` = NULL "
1542 .
"WHERE ResourceId = ".$ResourceId);
1544 # Clear local values
1545 $this->DBFields[$DBFieldName.
"X"] = NULL;
1546 $this->DBFields[$DBFieldName.
"Y"] = NULL;
1547 $UpdateModTime=TRUE;
1552 if (!is_null($this->DBFields[$DBFieldName.
"Begin"]) ||
1553 !is_null($this->DBFields[$DBFieldName.
"End"]) ||
1554 !is_null($this->DBFields[$DBFieldName.
"Precision"]))
1556 # clear date object values in DB
1557 $DB->Query(
"UPDATE Resources SET "
1558 .$DBFieldName.
"Begin = '', "
1559 .$DBFieldName.
"End = '', "
1560 .$DBFieldName.
"Precision = '' "
1561 .
"WHERE ResourceId = ".$ResourceId);
1563 # clear value locally
1564 $this->DBFields[$DBFieldName.
"Begin"] = NULL;
1565 $this->DBFields[$DBFieldName.
"End"] = NULL;
1566 $this->DBFields[$DBFieldName.
"Precision"] = NULL;
1567 $UpdateModTime=TRUE;
1572 $OldValue = $this->
Get($Field);
1574 # if value to clear supplied
1575 if ($ValueToClear !== NULL)
1577 # if supplied value is array
1578 if (is_array($ValueToClear))
1580 # for each element of array
1581 foreach ($ValueToClear as $ClassificationId => $Dummy)
1583 if (array_key_exists($ClassificationId, $OldValue))
1585 # remove association with resource (if any)
1586 $this->RemoveAssociation(
"ResourceClassInts",
1590 $Class->RecalcResourceCount();
1591 $UpdateModTime=TRUE;
1597 if (array_key_exists($ValueToClear, $OldValue))
1599 # remove association with resource (if any)
1600 $this->RemoveAssociation(
"ResourceClassInts",
1604 $Class->RecalcResourceCount();
1605 $UpdateModTime=TRUE;
1611 if (count($OldValue)>0)
1613 # remove all associations for resource and field
1614 $this->RemoveAllAssociations(
"ResourceClassInts",
"ClassificationId", $Field);
1616 # recompute resource count
1617 $Values = $this->
Get($Field);
1618 foreach ($Values as $ClassificationId => $Dummy)
1621 $Class->RecalcResourceCount();
1623 $UpdateModTime=TRUE;
1627 # clear our classification cache
1630 unset($this->ClassificationCache);
1636 $OldValue = $this->
Get($Field);
1637 # if value to clear supplied
1638 if ($ValueToClear !== NULL)
1640 # if incoming value is array
1641 if (is_array($ValueToClear))
1643 # for each element of array
1644 foreach ($ValueToClear as $ControlledNameId =>
1647 if (array_key_exists($ControlledNameId, $OldValue))
1649 # remove association with resource (if any)
1650 $this->RemoveAssociation(
"ResourceNameInts",
1653 $UpdateModTime=TRUE;
1659 if (array_key_exists($ValueToClear, $OldValue))
1661 # remove association with resource (if any)
1662 $this->RemoveAssociation(
"ResourceNameInts",
1665 $UpdateModTime=TRUE;
1671 if (count($OldValue)>0)
1673 # remove all associations for resource and field
1674 $this->RemoveAllAssociations(
"ResourceNameInts",
"ControlledNameId", $Field);
1675 $UpdateModTime=TRUE;
1681 # clear our controlled name cache
1682 unset($this->ControlledNameCache);
1683 unset($this->ControlledNameVariantCache);
1688 # if value to clear supplied
1689 if ($ValueToClear !== NULL)
1691 # convert value to array if necessary
1692 $Files = $ValueToClear;
1693 if (!is_array($Files)) { $Files = array($Files); }
1695 # convert values to objects if necessary
1696 foreach ($Files as $Index => $File)
1698 if (!is_object($File))
1700 $Files[$Index] =
new File($File);
1706 # use all files associated with resource
1707 $Files = $this->
Get($Field, TRUE);
1711 foreach ($Files as $File) { $File->Delete(); }
1715 # if value to clear supplied
1716 if ($ValueToClear !== NULL)
1718 # convert value to array if necessary
1719 $Images = $ValueToClear;
1720 if (!is_array($Images)) { $Images = array($Images); }
1722 # convert values to objects if necessary
1723 foreach ($Images as $Index => $Image)
1725 if (!is_object($Image))
1727 $Images[$Index] =
new SPTImage($Image);
1733 # use all images associated with resource
1734 $Images = $this->
Get($Field, TRUE);
1737 # delete images if we are the last resource referencing
1738 # a particular image.
1739 foreach ($Images as $Image) {
1740 $Cnt = $this->DB->Query(
1741 "SELECT COUNT(*) AS Cnt FROM ResourceImageInts WHERE ".
1742 "ImageId=".$Image->Id(),
"Cnt");
1747 # remove connections to images
1748 $UpdateModTime = $this->RemoveAssociation(
1749 "ResourceImageInts",
"ImageId", $Images, $Field);
1753 # remove references from the references table
1755 DELETE FROM ReferenceInts
1756 WHERE FieldId = '".addslashes($Field->Id()).
"'
1757 AND SrcResourceId = '".addslashes($this->
Id()).
"'");
1762 exit(
"<br>SPT - ERROR: attempt to clear unknown resource field type<br>\n");
1768 # update modification timestamps
1770 $UserId = $G_User->IsLoggedIn() ? $G_User->Get(
"UserId") : -1;
1771 $DB->Query(
"DELETE FROM ResourceFieldTimestamps "
1772 .
"WHERE ResourceId=".$this->
Id.
" AND "
1773 .
"FieldId=".$Field->Id() );
1774 $DB->Query(
"INSERT INTO ResourceFieldTimestamps "
1775 .
"(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1776 .$this->
Id.
",".$Field->Id().
","
1777 .$UserId.
",NOW())");
1781 { $this->
Clear($Field, $ValueToClear); }
1784 # --- Field-Specific or Type-Specific Attribute Retrieval Methods -------
1786 # return 2D array of classifications associated with resource
1787 # (first index is classification (field) name, second index is classification ID)
1792 # start with empty array
1795 # for each controlled name
1796 $DB->Query(
"SELECT ClassificationName, MetadataFields.FieldName, "
1797 .
"ResourceClassInts.ClassificationId FROM ResourceClassInts, "
1798 .
"Classifications, MetadataFields "
1799 .
"WHERE ResourceClassInts.ResourceId = ".$this->
Id.
" "
1800 .
"AND ResourceClassInts.ClassificationId = Classifications.ClassificationId "
1801 .
"AND Classifications.FieldId = MetadataFields.FieldId ");
1802 while ($Record = $DB->FetchRow())
1805 $Names[$Record[
"FieldName"]][$Record[
"ClassificationId"]] =
1806 $Record[
"ClassificationName"];
1809 # return array to caller
1814 # --- Ratings Methods ---------------------------------------------------
1816 # return cumulative rating (range is usually 0-100)
1819 # return cumulative rating scaled to 1/10th (range is usually 0-10)
1832 # return current number of ratings for resource
1835 # if number of ratings not already set
1838 # obtain number of ratings
1840 $this->DB->Query(
"SELECT Count(*) AS NumberOfRatings "
1841 .
"FROM ResourceRatings "
1842 .
"WHERE ResourceId = ".$this->
Id,
1846 # recalculate cumulative rating if it looks erroneous
1849 $this->UpdateCumulativeRating();
1853 # return number of ratings to caller
1854 return $this->NumberOfRatings;
1857 # update individual rating for resource
1858 function Rating($NewRating = NULL, $UserId = NULL)
1862 # if user ID not supplied
1863 if ($UserId == NULL)
1865 # if user is logged in
1867 if ($User->IsLoggedIn())
1869 # use ID of current user
1870 $UserId = $User->Get(
"UserId");
1874 # return NULL to caller
1879 # sanitize $NewRating
1880 if (!is_null($NewRating))
1882 $NewRating = intval($NewRating);
1885 # if there is a rating for resource and user
1886 $DB->Query(
"SELECT Rating FROM ResourceRatings "
1887 .
"WHERE UserId = ${UserId} AND ResourceId = ".$this->
Id);
1888 if ($Record = $DB->FetchRow())
1890 # if new rating was supplied
1891 if ($NewRating != NULL)
1893 # update existing rating
1894 $DB->Query(
"UPDATE ResourceRatings "
1895 .
"SET Rating = ${NewRating}, DateRated = NOW() "
1896 .
"WHERE UserId = ${UserId} AND ResourceId = ".$this->
Id);
1898 # update cumulative rating value
1899 $this->UpdateCumulativeRating();
1901 # return value is new rating
1902 $Rating = $NewRating;
1906 # get rating value to return to caller
1907 $Rating = $Record[
"Rating"];
1912 # if new rating was supplied
1913 if ($NewRating != NULL)
1916 $DB->Query(
"INSERT INTO ResourceRatings "
1917 .
"(ResourceId, UserId, DateRated, Rating) "
1924 # update cumulative rating value
1925 $this->UpdateCumulativeRating();
1927 # return value is new rating
1928 $Rating = $NewRating;
1932 # return value is NULL
1937 # return rating value to caller
1942 # --- Resource Comment Methods ------------------------------------------
1944 # return comments as array of Message objects
1947 # read in comments if not already loaded
1950 $this->DB->Query(
"SELECT MessageId FROM Messages "
1951 .
"WHERE ParentId = ".$this->
Id
1952 .
" AND ParentType = 2 "
1953 .
"ORDER BY DatePosted DESC");
1954 while ($MessageId = $this->DB->FetchField(
"MessageId"))
1960 # return array of comments to caller
1961 return $this->Comments;
1964 # return current number of comments
1967 # obtain number of comments if not already set
1971 $this->DB->Query(
"SELECT Count(*) AS NumberOfComments "
1973 .
"WHERE ParentId = ".$this->
Id
1974 .
" AND ParentType = 2",
1979 # return number of comments to caller
1980 return $this->NumberOfComments;
1984 # --- Permission Methods -------------------------------------------------
1995 # cannot view a resource that is not valid
1996 if ($this->
Status() !== 1) {
return FALSE; }
1998 # get viewing privilege set for resource
2000 $ResourcePrivs = $Schema->ViewingPrivileges();
2002 # get privilege set for user
2003 $UserPrivs = $User->Privileges();
2005 # user can view if privileges are greater than resource set
2006 $CanView = $UserPrivs->IsGreaterThan($ResourcePrivs, $this);
2008 # allow plugins to modify result of the permission check
2009 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2010 "EVENT_RESOURCE_VIEW_PERMISSION_CHECK",
2011 array(
"Resource" => $this,
"User" => $User,
"CanView" => $CanView));
2012 $CanView = $SignalResult[
"CanView"];
2025 # cannot edit a resource that you cannot view
2028 # get editing privilege set for resource
2030 $ResourcePrivs = $Schema->EditingPrivileges();
2032 # get privilege set for user
2033 $UserPrivs = $User->Privileges();
2035 # user can edit if privileges are greater than resource set
2036 $CanEdit = $UserPrivs->IsGreaterThan($ResourcePrivs, $this);
2038 # allow plugins to modify result of permission check
2039 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2040 "EVENT_RESOURCE_EDIT_PERMISSION_CHECK", array(
2041 "Resource" => $this,
2043 "CanEdit" => $CanEdit));
2044 $CanEdit = $SignalResult[
"CanEdit"];
2046 # report back to caller whether user can edit field
2058 # get field object (if not supplied)
2059 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2060 : $this->Schema->GetFieldByName($FieldOrFieldName);
2062 # field absolutely cannot be viewed if it is not valid
2065 # field should not be viewed if it is disabled
2066 if (!$Field->Enabled())
2072 # if the user can edit the field they can also view it
2079 # get appropriate privilege set for field
2080 $FieldPrivs = $Field->ViewingPrivileges();
2082 # get privilege set for user
2083 $UserPrivs = $User->Privileges();
2085 # user can view if privileges are greater than field set
2086 $CanView = $UserPrivs->IsGreaterThan($FieldPrivs, $this);
2090 # allow plugins to modify result of permission check
2091 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2092 "EVENT_FIELD_VIEW_PERMISSION_CHECK", array(
2094 "Resource" => $this,
2096 "CanView" => $CanView));
2097 $CanView = $SignalResult[
"CanView"];
2099 # report back to caller whether user can view field
2111 # fields hard-coded to not be authorable
2112 $UnauthorableFields = array(
2113 "Cumulative Rating",
2114 "Date Of Record Creation",
2115 "Date Of Record Release",
2116 "Date Last Modified",
2117 "Last Modified By Id");
2119 # get field object (if not supplied)
2120 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2121 : $this->Schema->GetFieldByName($FieldOrFieldName);
2123 # field absolutely cannot be authored if it is not valid
2126 # field should not be authored if it is disabled or is on "no author" list
2127 # or user is not the resource creator
2128 if (!$Field->Enabled()
2129 || in_array($Field->Name(), $UnauthorableFields)
2130 || ($User->Name() != $this->
Get(
"Added By Id")))
2136 # get appropriate privilege set for field
2137 $FieldPrivs = $Field->AuthoringPrivileges();
2139 # get privilege set for user
2140 $UserPrivs = $User->Privileges();
2142 # user can author if privileges are greater than field set
2143 $CanAuthor = $UserPrivs->IsGreaterThan($FieldPrivs, $this);
2146 # allow plugins to modify result of permission check
2147 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2148 "EVENT_FIELD_AUTHOR_PERMISSION_CHECK", array(
2150 "Resource" => $this,
2152 "CanAuthor" => $CanAuthor));
2153 $CanAuthor = $SignalResult[
"CanAuthor"];
2155 # report back to caller whether user can author field
2167 # fields hard-coded to not be editable
2168 $UneditableFields = array(
2169 "Cumulative Rating",
2170 "Date Of Record Creation",
2171 "Date Of Record Release",
2172 "Date Last Modified",
2173 "Last Modified By Id");
2175 # get field object (if not supplied)
2176 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2177 : $this->Schema->GetFieldByName($FieldOrFieldName);
2179 # field absolutely cannot be edited if it is not valid
2182 # field should not be edited if it is disabled or is on "no edit" list
2183 if (!$Field->Enabled() || in_array($Field->Name(), $UneditableFields))
2189 # get appropriate privilege set for field
2190 $FieldPrivs = $Field->EditingPrivileges();
2192 # get privilege set for user
2193 $UserPrivs = $User->Privileges();
2195 # user can edit if privileges are greater than field set
2196 $CanEdit = $UserPrivs->IsGreaterThan($FieldPrivs, $this);
2199 # allow plugins to modify result of permission check
2200 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2201 "EVENT_FIELD_EDIT_PERMISSION_CHECK", array(
2203 "Resource" => $this,
2205 "CanEdit" => $CanEdit));
2206 $CanEdit = $SignalResult[
"CanEdit"];
2208 # report back to caller whether user can edit field
2212 # ---- PRIVATE INTERFACE -------------------------------------------------
2218 private $NumberOfRatings;
2219 private $CumulativeRating;
2220 private $NumberOfComments;
2222 private $LastStatus;
2223 private $ControlledNameCache;
2224 private $ControlledNameVariantCache;
2225 private $ClassificationCache;
2227 # recalculate and save cumulative rating value for resource
2228 private function UpdateCumulativeRating()
2230 # grab totals from DB
2231 $this->DB->Query(
"SELECT COUNT(Rating) AS Count, "
2232 .
"SUM(Rating) AS Total FROM ResourceRatings "
2233 .
"WHERE ResourceId = ".$this->
Id);
2234 $Record = $this->DB->FetchRow();
2236 # calculate new cumulative rating
2239 # save new cumulative rating in DB
2240 $this->DB->Query(
"UPDATE Resources "
2242 .
"WHERE ResourceId = ".$this->Id);
2255 private function AddAssociation($TableName, $FieldName, $Value, $Field = NULL)
2257 # start out assuming no association will be added
2258 $AssociationAdded = FALSE;
2260 # convert new value to array if necessary
2261 $Values = is_array($Value) ? $Value : array($Value);
2263 # for each new value
2264 foreach ($Values as $Value)
2266 # retrieve ID from value if necessary
2267 if (is_object($Value)) { $Value = $Value->Id(); }
2269 # if value is not already associated with resource
2270 if ($this->DB->Query(
"SELECT COUNT(*) AS RecordCount FROM ".$TableName
2271 .
" WHERE ResourceId = ".$this->Id
2272 .
" AND ".$FieldName.
" = ".intval($Value)
2273 .($Field ?
" AND FieldId = ".intval($Field->Id()) :
""),
2274 "RecordCount") == 0)
2276 # associate value with resource
2277 $this->DB->Query(
"INSERT INTO ".$TableName.
" SET"
2278 .
" ResourceId = ".intval($this->
Id)
2279 .
", ".$FieldName.
" = ".intval($Value)
2280 .($Field ?
", FieldId = ".intval($Field->Id()) :
""));
2281 $AssociationAdded = TRUE;
2285 # report to caller whether association was added
2286 return $AssociationAdded;
2299 private function RemoveAssociation($TableName, $FieldName, $Value, $Field = NULL)
2301 # start out assuming no association will be removed
2302 $AssociationRemoved = FALSE;
2304 # convert value to array if necessary
2305 $Values = is_array($Value) ? $Value : array($Value);
2308 foreach ($Values as $Value)
2310 # retrieve ID from value if necessary
2311 if (is_object($Value)) { $Value = $Value->Id(); }
2313 # remove any intersections with target ID from DB
2314 $this->DB->Query(
"DELETE FROM ".$TableName
2315 .
" WHERE ResourceId = ".intval($this->
Id)
2316 .($Field ?
" AND FieldId = ".intval($Field->Id()) :
"")
2317 .
" AND ".$FieldName.
" = ".intval($Value));
2320 # report to caller whether association was added
2321 return $AssociationRemoved;
2324 # remove all intersections for resource and field (if any)
2325 private function RemoveAllAssociations($TableName, $TargetFieldName, $Field)
2327 # retrieve list of entries for this field and resource
2328 $Entries = $this->
Get($Field);
2331 foreach ($Entries as $EntryId => $EntryName)
2333 # remove intersection
2334 $this->RemoveAssociation($TableName, $TargetFieldName, $EntryId);
GetByField($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Old method for retrieving values, deprecated in favor of Get().
GetFilesForResource($ResourceOrResourceId, $ReturnObjects=TRUE)
Retrieve all files (names or objects) for specified resource.
Set($FieldNameOrObject, $NewValue)
Set value using field name or field object.
SetQualifier($FieldName, $NewValue)
Resource($ResourceId)
Object constructor for loading an existing resource.
Abstraction for forum messages and resource comments.
SQL database abstraction object with smart query caching.
GetAsArray($IncludeDisabledFields=FALSE, $ReturnObjects=TRUE)
Retrieve all resource values as an array.
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.
UserCanView(User $User)
Determine if the given user can view the resource, e.g., on the full record page. ...
Clear($Field, $ValueToClear=NULL)
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.
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...