3 # FILE: ResourceFactory.php
8 # DuplicateResource($ResourceId)
9 # - create duplicate resource and return to caller
10 # (SEE ALSO: ItemFactory.php)
12 # AUTHOR: Edward Almasy
14 # Part of the Collection Workflow Integration System (CWIS)
15 # Copyright 2011 Internet Scout Project
16 # http://scout.wisc.edu/
21 # ---- PUBLIC INTERFACE --------------------------------------------------
26 # set up item factory base class
27 $this->
ItemFactory(
"Resource",
"Resources",
"ResourceId");
30 # create duplicate resource and return to caller
33 # create new target resource
36 # load up resource to duplicate
37 $SrcResource =
new Resource($ResourceId);
39 # if resource to duplicate was found
40 if ($SrcResource->Status() > 0)
42 # for each metadata field
44 $Fields = $Schema->GetFields();
45 foreach ($Fields as $Field)
47 # skip the cumulative rating field
48 if ($Field->Name() !=
"Cumulative Rating")
50 $NewValue = $SrcResource->GetByField($Field, TRUE);
52 # clear default value from destination resource that is
53 # set when creating a new resource
54 $DstResource->ClearByField($Field);
56 # copy value from source resource to destination resource
57 $DstResource->SetByField($Field, $NewValue);
62 # return new resource to caller
66 # clear or change specific qualifier for all resources
69 # sanitize qualifier ID or retrieve from object
70 $QualifierId = is_object($ObjectOrId)
71 ? $ObjectOrId->Id() : intval($ObjectOrId);
73 # if new qualifier passed in
74 if ($NewObjectOrId !== NULL)
76 # sanitize qualifier ID to change to or retrieve it from object
77 $NewQualifierIdVal = is_object($NewObjectOrId)
78 ? $NewObjectOrId->Id() : intval($NewObjectOrId);
82 # qualifier should be cleared
83 $NewQualifierIdVal =
"NULL";
86 # for each metadata field
88 $Fields = $Schema->GetFields();
89 foreach ($Fields as $Field)
91 # if field uses qualifiers and uses item-level qualifiers
92 $QualColName = $Field->DBFieldName().
"Qualifier";
93 if ($Field->UsesQualifiers()
94 && $Field->HasItemLevelQualifiers()
95 && $this->DB->FieldExists(
"Resources", $QualColName))
97 # set all occurrences to new qualifier value
98 $this->DB->Query(
"UPDATE Resources"
99 .
" SET ".$QualColName.
" = ".$NewQualifierIdVal.
""
100 .
" WHERE ".$QualColName.
" = '".$QualifierId.
"'");
104 # clear or change qualifier association with controlled names
105 # (NOTE: this should probably be done in a controlled name factory object)
106 $this->DB->Query(
"UPDATE ControlledNames"
107 .
" SET QualifierId = ".$NewQualifierIdVal
108 .
" WHERE QualifierId = '".$QualifierId.
"'");
110 # clear or change qualifier association with classifications
111 # (NOTE: this should probably be done in a classification factory object)
112 $this->DB->Query(
"UPDATE Classifications"
113 .
" SET QualifierId = ".$NewQualifierIdVal
114 .
" WHERE QualifierId = '".$QualifierId.
"'");
117 # return count of rated resources
120 $RatedResourceCount = $this->DB->Query(
121 "SELECT COUNT(DISTINCT ResourceId) AS ResourceCount "
122 .
"FROM ResourceRatings",
"ResourceCount");
123 return $RatedResourceCount;
126 # return count of users who have rated resources
129 $RatedResourceCount = $this->DB->Query(
130 "SELECT COUNT(DISTINCT UserId) AS UserCount "
131 .
"FROM ResourceRatings",
"UserCount");
132 return $RatedResourceCount;
135 # return recently released resources
138 # assume that no resources will be found
139 $Resources = array();
141 # calculate cutoff date for resources
142 $CutoffDate = date(
"Y-m-d H:i:s", strtotime($MaxDaysToGoBack.
" days ago"));
144 # query for resource IDs
145 $this->DB->Query(
"SELECT ResourceId FROM Resources WHERE"
146 .
" DateOfRecordRelease > '".$CutoffDate.
"'"
147 .
" AND ReleaseFlag = 1"
148 .
" AND ResourceId >= 0"
149 .
" ORDER BY DateOfRecordRelease DESC, DateOfRecordCreation DESC"
150 .
" LIMIT ".intval($Offset).
", ".intval($Count));
151 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
153 # for each resource ID found
154 foreach ($ResourceIds as $ResourceId)
156 # load resource and add to list of found resources
157 $Resources[$ResourceId] =
new Resource($ResourceId);
160 # return found resources to caller
164 # return resources sorted by specified field
167 # assume no resources will be found
168 $ResourceIds = array();
172 $Field = $Schema->GetFieldByName($FieldName);
177 # construct query based on field type
178 switch ($Field->Type())
183 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
184 .
" FROM Resources WHERE "
185 .$Field->DBFieldName().
" IS NOT NULL"
186 .
" AND LENGTH(LTRIM(RTRIM(".$Field->DBFieldName().
"))) > 0",
190 $Query =
"SELECT ResourceId FROM Resources"
191 .
" ORDER BY ".$Field->DBFieldName()
192 .($Ascending ?
" ASC" :
" DESC");
198 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
199 .
" FROM Resources WHERE "
200 .$Field->DBFieldName().
" IS NOT NULL",
204 $Query =
"SELECT ResourceId FROM Resources"
205 .
" ORDER BY ".$Field->DBFieldName()
206 .($Ascending ?
" ASC" :
" DESC");
211 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
212 .
" FROM Resources WHERE "
213 .$Field->DBFieldName().
"Begin IS NOT NULL",
217 $Query =
"SELECT ResourceId FROM Resources"
218 .
" ORDER BY ".$Field->DBFieldName().
"Begin"
219 .($Ascending ?
" ASC" :
" DESC");
224 # if appropriate query was found
227 # if limited number of results were requested
231 $Query .=
" LIMIT ".intval($Limit);
234 # perform query and retrieve resource IDs
235 $this->DB->Query($Query);
236 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
240 # return resource IDs to caller
244 # get date/time of last resource modification (returned as Unix timestamp)
247 $LastChangeDate = $this->DB->Query(
248 "SELECT MAX(DateLastModified) AS LastChangeDate"
249 .
" FROM Resources".($OnlyReleasedResources ?
" WHERE ReleaseFlag = 1" :
""),
251 return ($LastChangeDate ? strtotime($LastChangeDate) : NULL);
254 # get list of possible field names for resources
257 # retrieve field names from schema
258 $FieldNames = array();
260 $Fields = $Schema->GetFields();
261 foreach ($Fields as $Field)
263 $FieldNames[$Field->Id()] = $Field->Name();
266 # return field names to caller
270 # find resources with values that match those specified
271 # (index of $ValuesToMatch is field IDs)
274 # start out assuming we won't find any resources
275 $Resources = array();
279 $Fields = $Schema->GetFields(
288 foreach ($ValuesToMatch as
$FieldId => $Value)
290 # if field can be used for comparison
293 # add comparison to condition
294 $Condition .= $LinkingTerm.$Fields[
$FieldId]->DBFieldName()
295 .
" = '".addslashes($Value).
"'";
296 $LinkingTerm =
" AND ";
300 # if there were valid conditions
301 if (strlen($Condition))
303 # build query statment
304 $Query =
"SELECT ResourceId FROM Resources WHERE ".$Condition;
306 # execute query to retrieve matching resource IDs
307 $this->DB->Query($Query);
308 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
310 # retrieve resource objects
311 foreach ($ResourceIds as $Id)
313 $Resources[$Id] =
new Resource($Id);
317 # return any resources found to caller
321 # Functions for keeping per-field resource counts updated:
324 if ($this->ResourceCount === NULL)
327 "SELECT FieldId, ClassName, CountType, Count from ResourceCounts");
329 while ($Row = $this->DB->FetchRow())
332 $ClassName = $Row[
"ClassName"];
333 $CountType = $Row[
"CountType"];
334 $Count = $Row[
"Count"];
336 $this->ResourceCount[
$FieldId][$ClassName][$CountType] = $Count;
341 $Field = $Schema->GetField(
$FieldId);
347 return isset($this->ResourceCount[
$FieldId][$Value][$CountType]) ?
348 $this->ResourceCount[
$FieldId][$Value][$CountType] :
363 return $this->DB->Query(
"
364 SELECT COUNT(*) AS ResourceTotal
366 WHERE ResourceId > 0 AND ReleaseFlag = 1",
"ResourceTotal");
376 return $this->DB->Query(
"
377 SELECT COUNT(*) AS ResourceTotal
379 WHERE ResourceId > 0",
"ResourceTotal");
386 # be sure that we're not a gigantic object when the task is queued
387 $TmpResourceCount = $this->ResourceCount;
388 $this->ResourceCount = NULL;
390 $AF->QueueUniqueTask(
391 array($this,
"UpdateResourceCountCallback"), array());
392 $this->ResourceCount = $TmpResourceCount;
399 "CREATE TABLE ResourceCountsNew (FieldId INT, ClassName TEXT, CountType TEXT, Count INT);");
401 $Start = microtime(TRUE);
403 foreach ($this->ResourceCountConditions as $CountType => $CountCondition)
406 "INSERT INTO ResourceCountsNew "
407 .
"SELECT FieldId, ControlledName AS ClassName,"
408 .
"'".$CountType.
"' AS CountType, Count(ResourceId) AS Count "
409 .
"FROM (SELECT * FROM ResourceNameInts WHERE ResourceId IN "
410 .
"(SELECT ResourceId FROM Resources "
411 . (($CountCondition!==NULL)?
"WHERE ".$CountCondition:
"").
")) AS T0 "
412 .
"JOIN ControlledNames USING(ControlledNameId) GROUP BY ControlledNameId;" );
415 $Stop = microtime(TRUE);
418 "INSERT INTO ResourceCountsNew VALUES (-1, '__LAST_UPDATED__', '', UNIX_TIMESTAMP()); ");
420 "INSERT INTO ResourceCountsNew VALUES (-2, '__UPDATE_RUNTIME__','',".($Stop-$Start).
");");
422 "RENAME TABLE ResourceCounts TO ResourceCountsOld, ResourceCountsNew TO ResourceCounts; ");
424 "DROP TABLE ResourceCountsOld; ");
427 # ---- PRIVATE INTERFACE -------------------------------------------------
429 private $ResourceCount = NULL;
430 private $ResourceCountConditions = array(
"All" => NULL,
"Released" =>
"ReleaseFlag=1");