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 $Resource->SetByField($Field, $DefaultValue);
118 # update timestamps as required
120 foreach ($TimestampFields as $Field)
122 if ($Field->UpdateMethod() ==
125 $Resource->SetByField($Field,
"now");
129 # signal resource creation
130 $GLOBALS[
"AF"]->SignalEvent(
"EVENT_RESOURCE_CREATE", array(
131 "Resource" => $Resource,
134 # return new Resource object to caller
146 # signal that resource deletion is about to occur
148 $AF->SignalEvent(
"EVENT_RESOURCE_DELETE", array(
152 # grab list of classifications
155 # delete resource/classification intersections
157 $DB->Query(
"DELETE FROM ResourceClassInts WHERE ResourceId = ".$this->
Id());
159 # for each classification type
160 foreach ($Classifications as $ClassType => $ClassesOfType)
162 # for each classification of that type
163 foreach ($ClassesOfType as $ClassId => $ClassName)
165 # recalculate resource count for classification
167 $Class->RecalcResourceCount();
171 # delete resource references
173 DELETE FROM ReferenceInts
174 WHERE SrcResourceId = '".addslashes($this->
Id()).
"'
175 OR DstResourceId = '".addslashes($this->
Id()).
"'");
177 # delete resource/name intersections
178 $DB->Query(
"DELETE FROM ResourceNameInts WHERE ResourceId = ".$this->
Id());
180 # delete any associated images not in use by other resources
181 $DB->Query(
"SELECT ImageId FROM ResourceImageInts"
182 .
" WHERE ResourceId = ".intval($this->
Id()));
183 $ImageIds = $DB->FetchColumn(
"ImageId");
184 foreach ($ImageIds as $ImageId)
186 $DB->Query(
"SELECT ResourceId FROM ResourceImageInts"
187 .
" WHERE ImageId = ".intval($ImageId)
188 .
" AND ResourceId != ".intval($this->
Id()));
189 if ($DB->NumRowsSelected() == 0)
196 # delete any associated files
198 $Files = $Factory->GetFilesForResource($this->
Id());
199 foreach ($Files as $File)
204 # delete resource record from database
205 $DB->Query(
"DELETE FROM Resources WHERE ResourceId = ".$this->
Id());
207 # drop item from search engine and recommender system
208 if ($SysConfig->SearchDBEnabled())
211 $SearchEngine->DropItem($this->
Id());
213 if ($SysConfig->RecommenderDBEnabled())
216 $Recommender->DropItem($this->
Id());
219 # get the folders containing the resource
221 $Folders = $FolderFactory->GetFoldersContainingItem(
225 # drop the resource from each folder it belongs to
226 foreach ($Folders as $Folder)
228 # mixed item type folder
229 if ($Folder->ContainsItem($this->Id,
"Resource"))
231 $Folder->RemoveItem($this->
Id,
"Resource");
234 # single item type folder
237 $Folder->RemoveItem($this->
Id);
241 # delete any resource comments
242 $DB->Query(
"DELETE FROM Messages WHERE ParentId = ".$this->
Id);
249 function Status() {
return $this->LastStatus; }
255 function Id() {
return $this->Id; }
261 function SchemaId() {
return $this->DBFields[
"SchemaId"]; }
271 # if new temp resource setting supplied
272 if (!is_null($NewSetting))
274 # if caller requested to switch
276 if ((($this->
Id() < 0) && ($NewSetting == FALSE))
277 || (($this->
Id() >= 0) && ($NewSetting == TRUE)))
279 # lock DB tables to prevent next ID from being grabbed
280 $DB->Query(
"LOCK TABLES Resources write");
282 # get next resource ID as appropriate
283 $OldResourceId = $this->Id;
285 if ($NewSetting == TRUE)
287 $this->
Id = $Factory->GetNextTempItemId();
291 $this->
Id = $Factory->GetNextItemId();
295 $DB->Query(
"UPDATE Resources SET ResourceId = ".
296 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
299 $DB->Query(
"UNLOCK TABLES");
301 # change associations
302 unset($this->ClassificationCache);
303 $DB->Query(
"UPDATE ResourceClassInts SET ResourceId = ".
304 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
305 unset($this->ControlledNameCache);
306 unset($this->ControlledNameVariantCache);
307 $DB->Query(
"UPDATE ResourceNameInts SET ResourceId = ".
308 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
309 $DB->Query(
"UPDATE Files SET ResourceId = ".
310 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
311 $DB->Query(
"UPDATE ReferenceInts SET SrcResourceId = ".
312 $this->
Id.
" WHERE SrcResourceId = ".$OldResourceId);
313 $DB->Query(
"UPDATE ResourceImageInts SET ResourceId = ".
314 $this->
Id.
" WHERE ResourceId = ".$OldResourceId);
316 # signal event as appropriate
317 if ($NewSetting === FALSE)
319 $GLOBALS[
"AF"]->SignalEvent(
"EVENT_RESOURCE_ADD", array(
326 # report to caller whether we are a temp resource
327 return ($this->
Id() < 0) ? TRUE : FALSE;
331 # --- Generic Attribute Retrieval Methods -------------------------------
347 function Get($FieldNameOrObject, $ReturnObject = FALSE, $IncludeVariants = FALSE)
349 # load field object if needed
350 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
351 : $this->Schema->GetFieldByName($FieldNameOrObject);
353 # return no value found if we don't have a valid field
356 # grab database field name
357 $DBFieldName = $Field->DBFieldName();
359 # format return value based on field type
360 switch ($Field->Type())
365 $ReturnValue = isset($this->DBFields[$DBFieldName])
366 ? (string)$this->DBFields[$DBFieldName] : NULL;
370 $ReturnValue = isset($this->DBFields[$DBFieldName])
371 ? (int)$this->DBFields[$DBFieldName] : NULL;
375 $ReturnValue = isset($this->DBFields[$DBFieldName])
376 ? (bool)$this->DBFields[$DBFieldName] : NULL;
380 $ReturnValue = array(
"X" => (
float)$this->DBFields[$DBFieldName.
"X"],
381 "Y" => (
float)$this->DBFields[$DBFieldName.
"Y"]);
385 $Date =
new Date($this->DBFields[$DBFieldName.
"Begin"],
386 $this->DBFields[$DBFieldName.
"End"],
387 $this->DBFields[$DBFieldName.
"Precision"]);
390 $ReturnValue = $Date;
394 $ReturnValue = $Date->Formatted();
399 $ReturnValue = $this->DBFields[$DBFieldName];
403 # start with empty array
404 $ReturnValue = array();
406 # if classification cache has not been loaded
407 if (!isset($this->ClassificationCache))
409 # load all classifications associated with this resource into cache
410 $this->ClassificationCache = array();
412 "SELECT Classifications.ClassificationId,"
413 .
" Classifications.FieldId,ClassificationName"
414 .
" FROM ResourceClassInts, Classifications"
415 .
" WHERE ResourceClassInts.ResourceId = ".$this->
Id
416 .
" AND ResourceClassInts.ClassificationId"
417 .
" = Classifications.ClassificationId");
418 while ($Record = $this->DB->FetchRow())
420 $this->ClassificationCache[$Record[
"ClassificationId"]][
"Name"] =
421 $Record[
"ClassificationName"];
422 $this->ClassificationCache[$Record[
"ClassificationId"]][
"FieldId"] =
427 # for each entry in classification cache
428 foreach ($this->ClassificationCache as $ClassificationId => $ClassificationInfo)
430 # if classification ID matches field we are looking for
431 if ($ClassificationInfo[
"FieldId"] == $Field->Id())
433 # add field to result
436 $ReturnValue[$ClassificationId] =
new Classification($ClassificationId);
440 $ReturnValue[$ClassificationId] = $ClassificationInfo[
"Name"];
448 # start with empty array
449 $ReturnValue = array();
451 # if controlled name cache has not been loaded
452 if (!isset($this->ControlledNameCache))
454 # load all controlled names associated with this resource into cache
455 $this->ControlledNameCache = array();
457 "SELECT ControlledNames.ControlledNameId,"
458 .
" ControlledNames.FieldId,ControlledName"
459 .
" FROM ResourceNameInts, ControlledNames"
460 .
" WHERE ResourceNameInts.ResourceId = ".$this->
Id
461 .
" AND ResourceNameInts.ControlledNameId"
462 .
" = ControlledNames.ControlledNameId"
463 .
" ORDER BY ControlledNames.ControlledName ASC");
464 while ($Record = $this->DB->FetchRow())
466 $this->ControlledNameCache[$Record[
"ControlledNameId"]][
"Name"] = $Record[
"ControlledName"];
467 $this->ControlledNameCache[$Record[
"ControlledNameId"]][
"FieldId"] = $Record[
"FieldId"];
471 # if variant names requested and variant name cache has not been loaded
472 if ($IncludeVariants && !isset($this->ControlledNameVariantCache))
474 # load all controlled names associated with this resource into cache
475 $this->ControlledNameVariantCache = array();
476 $this->DB->Query(
"SELECT ControlledNames.ControlledNameId,"
477 .
" ControlledNames.FieldId,"
478 .
" ControlledName, VariantName"
479 .
" FROM ResourceNameInts, ControlledNames, VariantNames"
480 .
" WHERE ResourceNameInts.ResourceId = ".$this->
Id
481 .
" AND ResourceNameInts.ControlledNameId"
482 .
" = ControlledNames.ControlledNameId"
483 .
" AND VariantNames.ControlledNameId"
484 .
" = ControlledNames.ControlledNameId");
485 while ($Record = $this->DB->FetchRow())
487 $this->ControlledNameVariantCache[$Record[
"ControlledNameId"]][]
488 = $Record[
"VariantName"];
492 # for each entry in controlled name cache
493 foreach ($this->ControlledNameCache as $ControlledNameId => $ControlledNameInfo)
495 # if controlled name type matches field we are looking for
496 if ($ControlledNameInfo[
"FieldId"] == $Field->Id())
498 # if objects requested
501 $ReturnValue[$ControlledNameId] =
506 # if variant names requested
507 if ($IncludeVariants)
509 # add field to result
510 $ReturnValue[] = $ControlledNameInfo[
"Name"];
512 # add any variant names to result
513 if (isset($this->ControlledNameVariantCache[$ControlledNameId]))
515 $ReturnValue = array_merge(
517 $this->ControlledNameVariantCache[$ControlledNameId]);
522 # add field with index to result
523 $ReturnValue[$ControlledNameId] =
524 $ControlledNameInfo[
"Name"];
532 $User =
new CWUser(intval($this->DBFields[$DBFieldName]));
535 $ReturnValue = $User;
539 $ReturnValue = $User->Get(
"UserName");
544 # start out assuming no images will be found
545 $ReturnValue = array();
547 # find all images associated with this resource
548 $this->DB->Query(
"SELECT ImageId FROM ResourceImageInts"
549 .
" WHERE ResourceId = ".intval($this->
Id())
550 .
" AND FieldId = ".intval($Field->Id()));
552 # if images were found
553 if ($this->DB->NumRowsSelected())
555 # if we are to return an object
556 $ImageIds = $this->DB->FetchColumn(
"ImageId");
559 # load array of Image objects for return value
560 foreach ($ImageIds as $ImageId)
562 $ReturnValue[$ImageId] =
new SPTImage($ImageId);
567 # load array of Image ids for return value
568 $ReturnValue = $ImageIds;
574 # retrieve files using factory
577 $this->
Id, $ReturnObject);
581 # query for resource references
583 SELECT * FROM ReferenceInts
584 WHERE FieldId = '".addslashes($Field->Id()).
"'
585 AND SrcResourceId = '".addslashes($this->
Id()).
"'");
587 $ReturnValue = array();
589 # return each reference as a Resource object
592 $FoundErrors = FALSE;
594 while (FALSE !== ($Record = $this->DB->FetchRow()))
596 $ReferenceId = $Record[
"DstResourceId"];
597 $Reference =
new Resource($ReferenceId);
599 # the reference is bad, so flag that there were errors
600 if ($Reference->Status() != 1)
607 $ReturnValue[$ReferenceId] = $Reference;
611 # try to fix the errors by removing any references to
612 # resources that were bad
615 $this->
Set($Field, $ReturnValue);
619 # return each reference as a resource ID
622 while (FALSE !== ($Record = $this->DB->FetchRow()))
624 $ReferenceId = $Record[
"DstResourceId"];
625 $ReturnValue[$ReferenceId] = $ReferenceId;
632 exit(
"<br>SPT - ERROR: attempt to retrieve unknown resource field type (".$Field->Type().
")<br>\n");
636 # return formatted value to caller
644 $ReturnObject = FALSE, $IncludeVariants = FALSE)
645 {
return $this->
Get($FieldNameOrObject, $ReturnObject, $IncludeVariants); }
660 function GetByFieldId($FieldId, $ReturnObject = FALSE, $IncludeVariants = FALSE)
662 $Field = $this->Schema->GetField($FieldId);
663 return ($Field) ? $this->
Get($Field, $ReturnObject, $IncludeVariants) : NULL;
678 function GetAsArray($IncludeDisabledFields = FALSE, $ReturnObjects = TRUE)
680 # retrieve field info
681 $Fields = $this->Schema->GetFields();
684 foreach ($Fields as $Field)
686 # if field is enabled or caller requested disabled fields
687 if ($Field->Enabled() || $IncludeDisabledFields)
689 # retrieve info and add it to the array
690 $FieldStrings[$Field->Name()] = $this->
Get($Field, $ReturnObjects);
692 # if field uses qualifiers
693 if ($Field->UsesQualifiers())
695 # get qualifier attributes and add to the array
696 $FieldStrings[$Field->Name().
" Qualifier"] =
702 # add in internal values
703 $FieldStrings[
"ResourceId"] = $this->
Id();
706 # return array to caller
707 return $FieldStrings;
724 function GetMapped($MappedName, $ReturnObject = FALSE, $IncludeVariants = FALSE)
726 return $this->Schema->StdNameToFieldMapping($MappedName)
727 ? $this->
GetByFieldId($this->Schema->StdNameToFieldMapping($MappedName),
728 $ReturnObject, $IncludeVariants)
742 $Field = $this->Schema->GetFieldByName($FieldName);
756 $Field = $this->Schema->GetField($FieldId);
770 # return NULL if field is invalid
773 # assume no qualifiers if not otherwise determined
776 # if field uses qualifiers
777 if ($Field->UsesQualifiers())
779 # retrieve qualifiers based on field type
780 switch ($Field->Type())
785 # retrieve list of items
788 # if field uses item-level qualifiers
789 if ($Field->HasItemLevelQualifiers())
791 # determine general item name in DB
793 ?
"Classification" :
"ControlledName";
796 foreach (
$Items as $ItemId => $ItemName)
798 # look up qualifier for item
799 $QualId = $this->DB->Query(
800 "SELECT * FROM ".$TableName.
"s"
801 .
" WHERE ".$TableName.
"Id = ".$ItemId
807 # if object was requested by caller
810 # load qualifier and add to return value array
811 $ReturnValue[$ItemId] =
new Qualifier($QualId);
815 # add qualifier ID to return value array
816 $ReturnValue[$ItemId] = $QualId;
821 # add NULL to return value array for this item
822 $ReturnValue[$ItemId] = NULL;
829 foreach (
$Items as $ItemId => $ItemName)
831 # if object was requested by caller
834 # load default qualifier and add to return value array
835 $ReturnValue[$ItemId] =
new Qualifier($Field->DefaultQualifier());
839 # add default qualifier ID to return value array
840 $ReturnValue[$ItemId] = $Field->DefaultQualifier();
847 # if field uses item-level qualifiers
848 if ($Field->HasItemLevelQualifiers())
850 # if qualifier available
851 if ($this->DBFields[$Field->DBFieldName().
"Qualifier"] > 0)
853 # if object was requested by caller
856 # return qualifier for field
857 $ReturnValue =
new Qualifier($this->DBFields[$Field->DBFieldName().
"Qualifier"]);
861 # return qualifier ID for field
862 $ReturnValue = $this->DBFields[$Field->DBFieldName().
"Qualifier"];
868 # if default qualifier available
869 if ($Field->DefaultQualifier() > 0)
871 # if object was requested by caller
874 # return default qualifier
875 $ReturnValue =
new Qualifier($Field->DefaultQualifier());
879 # return default qualifier ID
880 $ReturnValue = $Field->DefaultQualifier();
888 # return qualifier object or ID (or array of same) to caller
899 function FieldIsSet($FieldNameOrObject, $IgnorePadding=FALSE)
901 # load field object if needed
902 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
903 : $this->Schema->GetFieldByName($FieldNameOrObject);
905 # return no value found if we don't have a valid field
909 $Value = $this->
Get($Field);
911 # checks depend on the field type
912 switch ($Field->Type())
920 && (!$IgnorePadding || ($IgnorePadding && strlen(trim($Value))));
927 return isset($Value[
"X"])
928 && isset($Value[
"Y"])
929 && strlen(trim($Value[
"X"]))
930 && strlen(trim($Value[
"Y"]));
934 && strlen(trim($Value))
935 && $Value !=
"0000-00-00";
939 && strlen(trim($Value))
940 && $Value !=
"0000-00-00 00:00:00";
948 return count($Value) > 0;
954 && $Factory->UserNameExists($Value);
961 # --- Generic Attribute Setting Methods ---------------------------------
968 function Set($FieldNameOrObject, $NewValue)
970 # load field object if needed
971 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject
972 : $this->Schema->GetFieldByName($FieldNameOrObject);
974 # return if we don't have a valid field
977 # grab commonly-used values for local use
979 $ResourceId = $this->Id;
981 # grab database field name
982 $DBFieldName = $Field->DBFieldName();
984 # Flag to deterimine if we've actually changed anything.
985 $UpdateModTime = FALSE;
987 # store value in DB based on field type
988 switch ($Field->Type())
993 if ($this->DBFields[$DBFieldName] != $NewValue)
995 # save value directly to DB
996 $DB->Query(
"UPDATE Resources SET `"
997 .$DBFieldName.
"` = '".addslashes($NewValue).
"' "
998 .
"WHERE ResourceId = ".$ResourceId);
1000 # save value locally
1001 $this->DBFields[$DBFieldName] = $NewValue;
1002 $UpdateModTime=TRUE;
1007 if ( $this->DBFields[$DBFieldName] != $NewValue )
1009 # save value directly to DB
1010 if (is_null($NewValue))
1012 $DB->Query(
"UPDATE Resources SET `"
1013 .$DBFieldName.
"` = NULL"
1014 .
" WHERE ResourceId = ".$ResourceId);
1018 $DB->Query(
"UPDATE Resources SET `"
1019 .$DBFieldName.
"` = ".intval($NewValue)
1020 .
" WHERE ResourceId = ".$ResourceId);
1023 # save value locally
1024 $this->DBFields[$DBFieldName] = $NewValue;
1025 $UpdateModTime = TRUE;
1031 if ($this->DBFields[$DBFieldName.
"X"] != $NewValue[
"X"] ||
1032 $this->DBFields[$DBFieldName.
"Y"] != $NewValue[
"Y"] )
1034 if (is_null($NewValue))
1036 $DB->Query(
"UPDATE Resources SET "
1037 .
"`".$DBFieldName.
"X` = NULL, "
1038 .
"`".$DBFieldName.
"Y` = NULL "
1039 .
"WHERE ResourceId = ".$ResourceId);
1040 $this->DBFields[$DBFieldName.
"X"] = NULL;
1041 $this->DBFields[$DBFieldName.
"Y"] = NULL;
1045 $DB->Query(
"UPDATE Resources SET "
1046 .
"`".$DBFieldName.
"X` = ".(strlen($NewValue[
"X"])
1047 ?
"'".$NewValue[
"X"].
"'" :
"NULL").
", "
1048 .
"`".$DBFieldName.
"Y` = ".(strlen($NewValue[
"Y"])
1049 ?
"'".$NewValue[
"Y"].
"'" :
"NULL")
1050 .
" WHERE ResourceId = ".$ResourceId);
1052 $Digits = $Field->PointDecimalDigits();
1054 $this->DBFields[$DBFieldName.
"X"] =
1055 strlen($NewValue[
"X"]) ? round($NewValue[
"X"], $Digits) : NULL;
1056 $this->DBFields[$DBFieldName.
"Y"] =
1057 strlen($NewValue[
"Y"]) ? round($NewValue[
"Y"], $Digits) : NULL;
1059 $UpdateModTime = TRUE;
1064 if ($this->DBFields[$DBFieldName] != $NewValue)
1066 # save value directly to DB
1067 if (is_null($NewValue))
1069 $DB->Query(
"UPDATE Resources SET `"
1070 .$DBFieldName.
"` = NULL"
1071 .
" WHERE ResourceId = ".$ResourceId);
1075 $NewValue = $NewValue ?
"1" :
"0";
1076 $DB->Query(
"UPDATE Resources SET `"
1077 .$DBFieldName.
"` = ".$NewValue
1078 .
" WHERE ResourceId = ".$ResourceId);
1081 $this->DBFields[$DBFieldName] = $NewValue;
1083 # recalculate counts for any associated classifications if necessary
1084 if ($DBFieldName ==
"ReleaseFlag")
1086 $DB->Query(
"SELECT ClassificationId FROM ResourceClassInts"
1087 .
" WHERE ResourceId = ".$ResourceId);
1088 while ($ClassId = $DB->FetchField(
"ClassificationId"))
1091 $Class->RecalcResourceCount();
1094 $UpdateModTime = TRUE;
1099 # if value passed in was object
1100 if (is_object($NewValue))
1102 # retrieve user ID from object
1103 $UserId = $NewValue->Get(
"UserId");
1105 # else if value passed in was user name
1106 elseif (is_string($NewValue) && strlen($NewValue))
1108 # create user object and retrieve user ID from there
1109 $User =
new CWUser($NewValue);
1110 $UserId = $User->Get(
"UserId");
1114 # assume value is user ID and use value directly
1115 $UserId = $NewValue;
1118 if ($this->DBFields[$DBFieldName] != $UserId)
1120 # save value directly to DB
1121 $DB->Query(
"UPDATE Resources SET `"
1122 .$DBFieldName.
"` = '".$UserId.
"' "
1123 .
"WHERE ResourceId = ".$ResourceId);
1125 # save value locally
1126 $this->DBFields[$DBFieldName] = $UserId;
1127 $UpdateModTime = TRUE;
1132 # if we were given a date object
1133 if (is_object($NewValue))
1135 # use supplied date object
1140 # create date object
1141 $Date =
new Date($NewValue);
1144 $OldDate =
new Date(
1145 $this->DBFields[$DBFieldName.
"Begin"],
1146 $this->DBFields[$DBFieldName.
"End"]);
1148 if ($OldDate->BeginDate() != $Date->BeginDate() ||
1149 $OldDate->EndDate() != $Date->EndDate() ||
1150 $OldDate->Precision() != $Date->Precision() )
1152 # extract values from date object and store in DB
1153 $BeginDate =
"'".$Date->BeginDate().
"'";
1154 if (strlen($BeginDate) < 3) { $BeginDate =
"NULL"; }
1155 $EndDate =
"'".$Date->EndDate().
"'";
1156 if (strlen($EndDate) < 3) { $EndDate =
"NULL"; }
1158 $DB->Query(
"UPDATE Resources SET "
1159 .$DBFieldName.
"Begin = ".$BeginDate.
", "
1160 .$DBFieldName.
"End = ".$EndDate.
", "
1161 .$DBFieldName.
"Precision = '".$Date->Precision().
"' "
1162 .
"WHERE ResourceId = ".$ResourceId);
1164 # save values locally
1165 $this->DBFields[$DBFieldName.
"Begin"] = $Date->BeginDate();
1166 $this->DBFields[$DBFieldName.
"End"] = $Date->EndDate();
1167 $this->DBFields[$DBFieldName.
"Precision"] = $Date->Precision();
1168 $UpdateModTime=TRUE;
1173 if (is_null($NewValue) || !strlen(trim($NewValue)))
1175 $DateValue = $NewValue;
1177 if (!is_null($this->DBFields[$DBFieldName]))
1179 # save value directly to DB
1180 $DB->Query(
"UPDATE Resources SET "
1181 .
"`".$DBFieldName.
"` = NULL "
1182 .
"WHERE ResourceId = ".$ResourceId);
1183 $UpdateModTime = TRUE;
1188 # assume value is date and use directly
1189 $TimestampValue = strtotime($NewValue);
1191 # use the new value if the date is valid
1192 if ($TimestampValue !== FALSE && $TimestampValue >= 0)
1194 $DateValue = date(
"Y-m-d H:i:s", $TimestampValue);
1196 if ($this->DBFields[$DBFieldName] != $DateValue)
1198 # save value directly to DB
1199 $DB->Query(
"UPDATE Resources SET "
1200 .
"`".$DBFieldName.
"` = '".addslashes($DateValue).
"' "
1201 .
"WHERE ResourceId = ".$ResourceId);
1202 $UpdateModTime=TRUE;
1206 # continue using the old value if invalid
1209 $DateValue = $this->
Get($Field);
1213 # save value locally
1214 $this->DBFields[$DBFieldName] = $DateValue;
1218 $OldValue = $this->
Get($Field);
1220 # if incoming value is array
1221 if (is_array($NewValue))
1223 if ($OldValue != $NewValue)
1225 # for each element of array
1226 foreach ($NewValue as
1227 $ClassificationId => $ClassificationName)
1232 # associate with resource if not already associated
1233 $this->AddAssociation(
"ResourceClassInts",
1236 $Class->RecalcResourceCount();
1239 $UpdateModTime=TRUE;
1244 # associate with resource if not already associated
1245 if (is_object($NewValue))
1248 $NewValue = $Class->Id();
1255 if (!array_key_exists($Class->Id(), $OldValue))
1258 $this->AddAssociation(
"ResourceClassInts",
1261 $Class->RecalcResourceCount();
1262 $UpdateModTime=TRUE;
1266 # clear our classification cache
1269 unset($this->ClassificationCache);
1275 $OldValue = $this->
Get($Field);
1277 # Clear other values if this field expects unique options
1278 if ($Field->AllowMultiple() === FALSE)
1280 # If we're fed an array for a unique option,
1281 # just use the last element of the array
1282 if (is_array($NewValue)){
1283 $NewValue = array_pop($NewValue);
1286 if (!array_key_exists($NewValue, $OldValue))
1289 $this->RemoveAllAssociations(
"ResourceNameInts",
1292 $UpdateModTime=TRUE;
1296 # if incoming value is array
1297 if (is_array($NewValue) && ($Field->AllowMultiple() !== FALSE) )
1299 if ($OldValue != $NewValue)
1301 # for each element of array
1302 foreach ($NewValue as $ControlledNameId => $ControlledName)
1304 # associate with resource if not already associated
1305 $this->AddAssociation(
"ResourceNameInts",
1309 $UpdateModTime=TRUE;
1314 # associate with resource if not already associated
1315 if (is_object($NewValue)) { $NewValue = $NewValue->Id(); }
1316 if (!array_key_exists($NewValue, $OldValue))
1318 $this->AddAssociation(
"ResourceNameInts",
1321 $UpdateModTime=TRUE;
1327 # clear our controlled name cache
1328 unset($this->ControlledNameCache);
1329 unset($this->ControlledNameVariantCache);
1335 # associate value(s) with resource
1336 $this->AddAssociation(
1337 "ResourceImageInts",
"ImageId", $NewValue, $Field);
1341 # convert incoming value to array if necessary
1342 if (!is_array($NewValue)) { $NewValue = array($NewValue); }
1344 # for each incoming file
1346 foreach ($NewValue as $File)
1349 $NewFile = $Factory->
Copy($File);
1351 # associate copy with this resource and field
1352 $NewFile->ResourceId($this->
Id);
1353 $NewFile->FieldId($Field->Id());
1355 # Since we make a fresh copy of the File whenever Set is called,
1356 # we'll always update the modification time for this field.
1357 $UpdateModTime = TRUE;
1361 # convert incoming value to array to simplify the workflow
1362 if (is_scalar($NewValue) || $NewValue instanceof
Resource)
1364 $NewValue = array($NewValue);
1367 # delete existing resource references
1370 # add each reference
1371 foreach ($NewValue as $ReferenceOrId)
1373 # initially issume it's a reference ID and not an object...
1374 $ReferenceId = $ReferenceOrId;
1376 # ... but get the reference ID if it's an object
1377 if ($ReferenceOrId instanceof
Resource)
1379 $ReferenceId = $ReferenceOrId->Id();
1382 # skip blank reference IDs
1383 if (strlen(trim($ReferenceId)) < 1)
1388 # skip reference IDs that don't look right
1389 if (!is_numeric($ReferenceId))
1394 # skip references to the current resource
1395 if ($ReferenceId == $this->
Id())
1400 # add the reference to the references table
1402 INSERT INTO ReferenceInts (
1407 ".addslashes($Field->Id()).
",
1408 ".addslashes($this->
Id()).
",
1409 ".addslashes($ReferenceId).
")");
1415 exit(
"<br>SPT - ERROR: attempt to set unknown resource field type<br>\n");
1421 # update modification timestamps
1423 $UserId = $G_User->IsLoggedIn() ? $G_User->Get(
"UserId") : -1;
1424 $DB->Query(
"DELETE FROM ResourceFieldTimestamps "
1425 .
"WHERE ResourceId=".$this->
Id.
" AND "
1426 .
"FieldId=".$Field->Id() );
1427 $DB->Query(
"INSERT INTO ResourceFieldTimestamps "
1428 .
"(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1429 .$this->
Id.
",".$Field->Id().
","
1430 .$UserId.
",NOW())");
1439 # set value by field ID
1442 $Field = $this->Schema->GetField($FieldId);
1443 $this->
Set($Field, $NewValue);
1446 # set qualifier by field name
1449 $Field = $this->Schema->GetFieldByName($FieldName);
1453 # set qualifier by field ID
1456 $Field = $this->Schema->GetField($FieldId);
1460 # set qualifier using field object
1463 # if field uses qualifiers and uses item-level qualifiers
1464 if ($Field->UsesQualifiers() && $Field->HasItemLevelQualifiers())
1466 # if qualifier object passed in
1467 if (is_object($NewValue))
1469 # grab qualifier ID from object
1470 $QualifierId = $NewValue->Id();
1474 # assume value passed in is qualifier ID
1475 $QualifierId = $NewValue;
1478 # update qualifier value in database
1479 $DBFieldName = $Field->DBFieldName();
1480 $this->DB->Query(
"UPDATE Resources SET "
1481 .$DBFieldName.
"Qualifier = '".$QualifierId.
"' "
1482 .
"WHERE ResourceId = ".$this->Id);
1484 # update local qualifier value
1485 $this->DBFields[$DBFieldName.
"Qualifier"] = $QualifierId;
1489 # clear value by field ID
1492 $Field = $this->Schema->GetField($FieldId);
1496 # clear value using field object
1497 function Clear($Field, $ValueToClear = NULL)
1499 # convert field name to object if necessary
1500 if (!is_object($Field))
1502 $Field = $this->Schema->GetFieldByName($Field);
1505 # grab commonly-used values for local use
1507 $ResourceId = $this->Id;
1509 # grab database field name
1510 $DBFieldName = $Field->DBFieldName();
1512 $UpdateModTime=FALSE;
1514 # store value in DB based on field type
1515 switch ($Field->Type())
1524 if (strlen($this->DBFields[$DBFieldName])>0)
1527 $DB->Query(
"UPDATE Resources SET `"
1528 .$DBFieldName.
"` = NULL "
1529 .
"WHERE ResourceId = ".$ResourceId);
1531 # clear value locally
1532 $this->DBFields[$DBFieldName] = NULL;
1533 $UpdateModTime=TRUE;
1538 if (!is_null($this->DBFields[$DBFieldName.
"X"]) ||
1539 !is_null($this->DBFields[$DBFieldName.
"Y"]) )
1542 $DB->Query(
"UPDATE Resources SET "
1543 .
"`".$DBFieldName.
"X` = NULL ,"
1544 .
"`".$DBFieldName.
"Y` = NULL "
1545 .
"WHERE ResourceId = ".$ResourceId);
1547 # Clear local values
1548 $this->DBFields[$DBFieldName.
"X"] = NULL;
1549 $this->DBFields[$DBFieldName.
"Y"] = NULL;
1550 $UpdateModTime=TRUE;
1555 if (!is_null($this->DBFields[$DBFieldName.
"Begin"]) ||
1556 !is_null($this->DBFields[$DBFieldName.
"End"]) ||
1557 !is_null($this->DBFields[$DBFieldName.
"Precision"]))
1559 # clear date object values in DB
1560 $DB->Query(
"UPDATE Resources SET "
1561 .$DBFieldName.
"Begin = '', "
1562 .$DBFieldName.
"End = '', "
1563 .$DBFieldName.
"Precision = '' "
1564 .
"WHERE ResourceId = ".$ResourceId);
1566 # clear value locally
1567 $this->DBFields[$DBFieldName.
"Begin"] = NULL;
1568 $this->DBFields[$DBFieldName.
"End"] = NULL;
1569 $this->DBFields[$DBFieldName.
"Precision"] = NULL;
1570 $UpdateModTime=TRUE;
1575 $OldValue = $this->
Get($Field);
1577 # if value to clear supplied
1578 if ($ValueToClear !== NULL)
1580 # if supplied value is array
1581 if (is_array($ValueToClear))
1583 # for each element of array
1584 foreach ($ValueToClear as $ClassificationId => $Dummy)
1586 if (array_key_exists($ClassificationId, $OldValue))
1588 # remove association with resource (if any)
1589 $this->RemoveAssociation(
"ResourceClassInts",
1593 $Class->RecalcResourceCount();
1594 $UpdateModTime=TRUE;
1600 if (array_key_exists($ValueToClear, $OldValue))
1602 # remove association with resource (if any)
1603 $this->RemoveAssociation(
"ResourceClassInts",
1607 $Class->RecalcResourceCount();
1608 $UpdateModTime=TRUE;
1614 if (count($OldValue)>0)
1616 # remove all associations for resource and field
1617 $this->RemoveAllAssociations(
"ResourceClassInts",
"ClassificationId", $Field);
1619 # recompute resource count
1620 $Values = $this->
Get($Field);
1621 foreach ($Values as $ClassificationId => $Dummy)
1624 $Class->RecalcResourceCount();
1626 $UpdateModTime=TRUE;
1630 # clear our classification cache
1633 unset($this->ClassificationCache);
1639 $OldValue = $this->
Get($Field);
1640 # if value to clear supplied
1641 if ($ValueToClear !== NULL)
1643 # if incoming value is array
1644 if (is_array($ValueToClear))
1646 # for each element of array
1647 foreach ($ValueToClear as $ControlledNameId =>
1650 if (array_key_exists($ControlledNameId, $OldValue))
1652 # remove association with resource (if any)
1653 $this->RemoveAssociation(
"ResourceNameInts",
1656 $UpdateModTime=TRUE;
1662 if (array_key_exists($ValueToClear, $OldValue))
1664 # remove association with resource (if any)
1665 $this->RemoveAssociation(
"ResourceNameInts",
1668 $UpdateModTime=TRUE;
1674 if (count($OldValue)>0)
1676 # remove all associations for resource and field
1677 $this->RemoveAllAssociations(
"ResourceNameInts",
"ControlledNameId", $Field);
1678 $UpdateModTime=TRUE;
1684 # clear our controlled name cache
1685 unset($this->ControlledNameCache);
1686 unset($this->ControlledNameVariantCache);
1691 # if value to clear supplied
1692 if ($ValueToClear !== NULL)
1694 # convert value to array if necessary
1695 $Files = $ValueToClear;
1696 if (!is_array($Files)) { $Files = array($Files); }
1698 # convert values to objects if necessary
1699 foreach ($Files as $Index => $File)
1701 if (!is_object($File))
1703 $Files[$Index] =
new File($File);
1709 # use all files associated with resource
1710 $Files = $this->
Get($Field, TRUE);
1714 foreach ($Files as $File) { $File->Delete(); }
1718 # if value to clear supplied
1719 if ($ValueToClear !== NULL)
1721 # convert value to array if necessary
1722 $Images = $ValueToClear;
1723 if (!is_array($Images)) { $Images = array($Images); }
1725 # convert values to objects if necessary
1726 foreach ($Images as $Index => $Image)
1728 if (!is_object($Image))
1730 $Images[$Index] =
new SPTImage($Image);
1736 # use all images associated with resource
1737 $Images = $this->
Get($Field, TRUE);
1740 # delete images if we are the last resource referencing
1741 # a particular image.
1742 foreach ($Images as $Image) {
1743 $Cnt = $this->DB->Query(
1744 "SELECT COUNT(*) AS Cnt FROM ResourceImageInts WHERE ".
1745 "ImageId=".$Image->Id(),
"Cnt");
1750 # remove connections to images
1751 $UpdateModTime = $this->RemoveAssociation(
1752 "ResourceImageInts",
"ImageId", $Images, $Field);
1756 # remove references from the references table
1758 DELETE FROM ReferenceInts
1759 WHERE FieldId = '".addslashes($Field->Id()).
"'
1760 AND SrcResourceId = '".addslashes($this->
Id()).
"'");
1765 exit(
"<br>SPT - ERROR: attempt to clear unknown resource field type<br>\n");
1771 # update modification timestamps
1773 $UserId = $G_User->IsLoggedIn() ? $G_User->Get(
"UserId") : -1;
1774 $DB->Query(
"DELETE FROM ResourceFieldTimestamps "
1775 .
"WHERE ResourceId=".$this->
Id.
" AND "
1776 .
"FieldId=".$Field->Id() );
1777 $DB->Query(
"INSERT INTO ResourceFieldTimestamps "
1778 .
"(ResourceId,FieldId,ModifiedBy,Timestamp) VALUES ("
1779 .$this->
Id.
",".$Field->Id().
","
1780 .$UserId.
",NOW())");
1784 { $this->
Clear($Field, $ValueToClear); }
1787 # --- Field-Specific or Type-Specific Attribute Retrieval Methods -------
1789 # return 2D array of classifications associated with resource
1790 # (first index is classification (field) name, second index is classification ID)
1795 # start with empty array
1798 # for each controlled name
1799 $DB->Query(
"SELECT ClassificationName, MetadataFields.FieldName, "
1800 .
"ResourceClassInts.ClassificationId FROM ResourceClassInts, "
1801 .
"Classifications, MetadataFields "
1802 .
"WHERE ResourceClassInts.ResourceId = ".$this->
Id.
" "
1803 .
"AND ResourceClassInts.ClassificationId = Classifications.ClassificationId "
1804 .
"AND Classifications.FieldId = MetadataFields.FieldId ");
1805 while ($Record = $DB->FetchRow())
1808 $Names[$Record[
"FieldName"]][$Record[
"ClassificationId"]] =
1809 $Record[
"ClassificationName"];
1812 # return array to caller
1817 # --- Ratings Methods ---------------------------------------------------
1819 # return cumulative rating (range is usually 0-100)
1822 # return cumulative rating scaled to 1/10th (range is usually 0-10)
1835 # return current number of ratings for resource
1838 # if number of ratings not already set
1841 # obtain number of ratings
1843 $this->DB->Query(
"SELECT Count(*) AS NumberOfRatings "
1844 .
"FROM ResourceRatings "
1845 .
"WHERE ResourceId = ".$this->
Id,
1849 # recalculate cumulative rating if it looks erroneous
1852 $this->UpdateCumulativeRating();
1856 # return number of ratings to caller
1857 return $this->NumberOfRatings;
1860 # update individual rating for resource
1861 function Rating($NewRating = NULL, $UserId = NULL)
1865 # if user ID not supplied
1866 if ($UserId == NULL)
1868 # if user is logged in
1870 if ($User->IsLoggedIn())
1872 # use ID of current user
1873 $UserId = $User->Get(
"UserId");
1877 # return NULL to caller
1882 # sanitize $NewRating
1883 if (!is_null($NewRating))
1885 $NewRating = intval($NewRating);
1888 # if there is a rating for resource and user
1889 $DB->Query(
"SELECT Rating FROM ResourceRatings "
1890 .
"WHERE UserId = ${UserId} AND ResourceId = ".$this->
Id);
1891 if ($Record = $DB->FetchRow())
1893 # if new rating was supplied
1894 if ($NewRating != NULL)
1896 # update existing rating
1897 $DB->Query(
"UPDATE ResourceRatings "
1898 .
"SET Rating = ${NewRating}, DateRated = NOW() "
1899 .
"WHERE UserId = ${UserId} AND ResourceId = ".$this->
Id);
1901 # update cumulative rating value
1902 $this->UpdateCumulativeRating();
1904 # return value is new rating
1905 $Rating = $NewRating;
1909 # get rating value to return to caller
1910 $Rating = $Record[
"Rating"];
1915 # if new rating was supplied
1916 if ($NewRating != NULL)
1919 $DB->Query(
"INSERT INTO ResourceRatings "
1920 .
"(ResourceId, UserId, DateRated, Rating) "
1927 # update cumulative rating value
1928 $this->UpdateCumulativeRating();
1930 # return value is new rating
1931 $Rating = $NewRating;
1935 # return value is NULL
1940 # return rating value to caller
1945 # --- Resource Comment Methods ------------------------------------------
1947 # return comments as array of Message objects
1950 # read in comments if not already loaded
1953 $this->DB->Query(
"SELECT MessageId FROM Messages "
1954 .
"WHERE ParentId = ".$this->
Id
1955 .
" AND ParentType = 2 "
1956 .
"ORDER BY DatePosted DESC");
1957 while ($MessageId = $this->DB->FetchField(
"MessageId"))
1963 # return array of comments to caller
1964 return $this->Comments;
1967 # return current number of comments
1970 # obtain number of comments if not already set
1974 $this->DB->Query(
"SELECT Count(*) AS NumberOfComments "
1976 .
"WHERE ParentId = ".$this->
Id
1977 .
" AND ParentType = 2",
1982 # return number of comments to caller
1983 return $this->NumberOfComments;
1987 # --- Permission Methods -------------------------------------------------
1998 # cannot view a resource that is not valid
1999 if ($this->
Status() !== 1) {
return FALSE; }
2001 # get viewing privilege set for resource
2003 $ResourcePrivs = $Schema->ViewingPrivileges();
2005 # get privilege set for user
2006 $UserPrivs = $User->Privileges();
2008 # user can view if privileges are greater than resource set
2009 $CanView = $UserPrivs->IsGreaterThan($ResourcePrivs, $this);
2011 # allow plugins to modify result of the permission check
2012 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2013 "EVENT_RESOURCE_VIEW_PERMISSION_CHECK",
2014 array(
"Resource" => $this,
"User" => $User,
"CanView" => $CanView));
2015 $CanView = $SignalResult[
"CanView"];
2028 # cannot edit a resource that you cannot view
2031 # get editing privilege set for resource
2033 $ResourcePrivs = $Schema->EditingPrivileges();
2035 # get privilege set for user
2036 $UserPrivs = $User->Privileges();
2038 # user can edit if privileges are greater than resource set
2039 $CanEdit = $UserPrivs->IsGreaterThan($ResourcePrivs, $this);
2041 # allow plugins to modify result of permission check
2042 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2043 "EVENT_RESOURCE_EDIT_PERMISSION_CHECK", array(
2044 "Resource" => $this,
2046 "CanEdit" => $CanEdit));
2047 $CanEdit = $SignalResult[
"CanEdit"];
2049 # report back to caller whether user can edit field
2061 # get field object (if not supplied)
2062 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2063 : $this->Schema->GetFieldByName($FieldOrFieldName);
2065 # field absolutely cannot be viewed if it is not valid
2068 # field should not be viewed if it is disabled
2069 if (!$Field->Enabled())
2075 # if the user can edit the field they can also view it
2082 # get appropriate privilege set for field
2083 $FieldPrivs = $Field->ViewingPrivileges();
2085 # get privilege set for user
2086 $UserPrivs = $User->Privileges();
2088 # user can view if privileges are greater than field set
2089 $CanView = $UserPrivs->IsGreaterThan($FieldPrivs, $this);
2093 # allow plugins to modify result of permission check
2094 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2095 "EVENT_FIELD_VIEW_PERMISSION_CHECK", array(
2097 "Resource" => $this,
2099 "CanView" => $CanView));
2100 $CanView = $SignalResult[
"CanView"];
2102 # report back to caller whether user can view field
2114 # fields hard-coded to not be authorable
2115 $UnauthorableFields = array(
2116 "Cumulative Rating",
2117 "Date Of Record Creation",
2118 "Date Of Record Release",
2119 "Date Last Modified",
2120 "Last Modified By Id");
2122 # get field object (if not supplied)
2123 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2124 : $this->Schema->GetFieldByName($FieldOrFieldName);
2126 # field absolutely cannot be authored if it is not valid
2129 # field should not be authored if it is disabled or is on "no author" list
2130 # or user is not the resource creator
2131 if (!$Field->Enabled()
2132 || in_array($Field->Name(), $UnauthorableFields)
2133 || ($User->Name() != $this->
Get(
"Added By Id")))
2139 # get appropriate privilege set for field
2140 $FieldPrivs = $Field->AuthoringPrivileges();
2142 # get privilege set for user
2143 $UserPrivs = $User->Privileges();
2145 # user can author if privileges are greater than field set
2146 $CanAuthor = $UserPrivs->IsGreaterThan($FieldPrivs, $this);
2149 # allow plugins to modify result of permission check
2150 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2151 "EVENT_FIELD_AUTHOR_PERMISSION_CHECK", array(
2153 "Resource" => $this,
2155 "CanAuthor" => $CanAuthor));
2156 $CanAuthor = $SignalResult[
"CanAuthor"];
2158 # report back to caller whether user can author field
2170 # fields hard-coded to not be editable
2171 $UneditableFields = array(
2172 "Cumulative Rating",
2173 "Date Of Record Creation",
2174 "Date Of Record Release",
2175 "Date Last Modified",
2176 "Last Modified By Id");
2178 # get field object (if not supplied)
2179 $Field = is_object($FieldOrFieldName) ? $FieldOrFieldName
2180 : $this->Schema->GetFieldByName($FieldOrFieldName);
2182 # field absolutely cannot be edited if it is not valid
2185 # field should not be edited if it is disabled or is on "no edit" list
2186 if (!$Field->Enabled() || in_array($Field->Name(), $UneditableFields))
2192 # get appropriate privilege set for field
2193 $FieldPrivs = $Field->EditingPrivileges();
2195 # get privilege set for user
2196 $UserPrivs = $User->Privileges();
2198 # user can edit if privileges are greater than field set
2199 $CanEdit = $UserPrivs->IsGreaterThan($FieldPrivs, $this);
2202 # allow plugins to modify result of permission check
2203 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
2204 "EVENT_FIELD_EDIT_PERMISSION_CHECK", array(
2206 "Resource" => $this,
2208 "CanEdit" => $CanEdit));
2209 $CanEdit = $SignalResult[
"CanEdit"];
2211 # report back to caller whether user can edit field
2215 # ---- PRIVATE INTERFACE -------------------------------------------------
2221 private $NumberOfRatings;
2222 private $CumulativeRating;
2223 private $NumberOfComments;
2225 private $LastStatus;
2226 private $ControlledNameCache;
2227 private $ControlledNameVariantCache;
2228 private $ClassificationCache;
2230 # recalculate and save cumulative rating value for resource
2231 private function UpdateCumulativeRating()
2233 # grab totals from DB
2234 $this->DB->Query(
"SELECT COUNT(Rating) AS Count, "
2235 .
"SUM(Rating) AS Total FROM ResourceRatings "
2236 .
"WHERE ResourceId = ".$this->
Id);
2237 $Record = $this->DB->FetchRow();
2239 # calculate new cumulative rating
2242 # save new cumulative rating in DB
2243 $this->DB->Query(
"UPDATE Resources "
2245 .
"WHERE ResourceId = ".$this->Id);
2258 private function AddAssociation($TableName, $FieldName, $Value, $Field = NULL)
2260 # start out assuming no association will be added
2261 $AssociationAdded = FALSE;
2263 # convert new value to array if necessary
2264 $Values = is_array($Value) ? $Value : array($Value);
2266 # for each new value
2267 foreach ($Values as $Value)
2269 # retrieve ID from value if necessary
2270 if (is_object($Value)) { $Value = $Value->Id(); }
2272 # if value is not already associated with resource
2273 if ($this->DB->Query(
"SELECT COUNT(*) AS RecordCount FROM ".$TableName
2274 .
" WHERE ResourceId = ".$this->Id
2275 .
" AND ".$FieldName.
" = ".intval($Value)
2276 .($Field ?
" AND FieldId = ".intval($Field->Id()) :
""),
2277 "RecordCount") == 0)
2279 # associate value with resource
2280 $this->DB->Query(
"INSERT INTO ".$TableName.
" SET"
2281 .
" ResourceId = ".intval($this->
Id)
2282 .
", ".$FieldName.
" = ".intval($Value)
2283 .($Field ?
", FieldId = ".intval($Field->Id()) :
""));
2284 $AssociationAdded = TRUE;
2288 # report to caller whether association was added
2289 return $AssociationAdded;
2302 private function RemoveAssociation($TableName, $FieldName, $Value, $Field = NULL)
2304 # start out assuming no association will be removed
2305 $AssociationRemoved = FALSE;
2307 # convert value to array if necessary
2308 $Values = is_array($Value) ? $Value : array($Value);
2311 foreach ($Values as $Value)
2313 # retrieve ID from value if necessary
2314 if (is_object($Value)) { $Value = $Value->Id(); }
2316 # remove any intersections with target ID from DB
2317 $this->DB->Query(
"DELETE FROM ".$TableName
2318 .
" WHERE ResourceId = ".intval($this->
Id)
2319 .($Field ?
" AND FieldId = ".intval($Field->Id()) :
"")
2320 .
" AND ".$FieldName.
" = ".intval($Value));
2323 # report to caller whether association was added
2324 return $AssociationRemoved;
2327 # remove all intersections for resource and field (if any)
2328 private function RemoveAllAssociations($TableName, $TargetFieldName, $Field)
2330 # retrieve list of entries for this field and resource
2331 $Entries = $this->
Get($Field);
2334 foreach ($Entries as $EntryId => $EntryName)
2336 # remove intersection
2337 $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...