CWIS Developer Documentation
FileFactory.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: FileFactory.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2007-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
13 class FileFactory extends ItemFactory {
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
21  function __construct($FieldId = NULL)
22  {
23  # save field ID for our later use
24  $this->FieldId = $FieldId;
25 
26  # set up item factory base class
27  $this->ItemFactory("File", "Files", "FileId", "FileName", FALSE,
28  ($FieldId ? "FieldId = ".intval($FieldId) : NULL));
29  }
30 
38  function GetFilesForResource($ResourceOrResourceId, $ReturnObjects = TRUE)
39  {
40  # start out assuming that no files will be found
41  $ReturnValue = array();
42 
43  # sanitize resource ID or grab it from object
44  $ResourceOrResourceId = is_object($ResourceOrResourceId)
45  ? $ResourceOrResourceId->Id() : intval($ResourceOrResourceId);
46 
47  # retrieve names and IDs of files associated with resource
48  $this->DB->Query(
49  "SELECT FileId, FileName FROM Files"
50  ." WHERE ResourceId = ".$ResourceOrResourceId
51  ." AND FieldId"
52  .($this->FieldId ? "=".$this->FieldId : ">0"));
53  $FileNames = $this->DB->FetchColumn("FileName", "FileId");
54 
55  # if files were found
56  if (count($FileNames))
57  {
58  # if caller asked us to return objects
59  if ($ReturnObjects)
60  {
61  # for each file
62  foreach ($FileNames as $FileId => $FileName)
63  {
64  # create file object and add it to array
65  $ReturnValue[$FileId] = new File($FileId);
66  }
67  }
68  else
69  {
70  # return array of file names with IDs as index
71  $ReturnValue = $FileNames;
72  }
73  }
74 
75  # return resulting array of files or file names to caller
76  return $ReturnValue;
77  }
78 
84  function Copy($FileToCopy)
85  {
86  return new File($FileToCopy->GetNameOfStoredFile(),
87  $FileToCopy->ResourceId(),
88  $FileToCopy->FieldId(),
89  $FileToCopy->Name());
90  }
91 
92 
93  # ---- PRIVATE INTERFACE -------------------------------------------------
94 
95  private $FieldId;
96 }
97 
GetFilesForResource($ResourceOrResourceId, $ReturnObjects=TRUE)
Retrieve all files (names or objects) for specified resource.
Definition: FileFactory.php:38
__construct($FieldId=NULL)
Object constructor.
Definition: FileFactory.php:21
Copy($FileToCopy)
Create copy of File and return to caller.
Definition: FileFactory.php:84
Factory for manipulating File objects.
Definition: FileFactory.php:13
Common factory class for item manipulation.
Definition: ItemFactory.php:17
Class representing a stored (usually uploaded) file.
Definition: File.php:13
ItemFactory($ItemClassName, $ItemTableName, $ItemIdFieldName, $ItemNameFieldName=NULL, $OrderOpsAllowed=FALSE, $SqlCondition=NULL)
Class constructor.
Definition: ItemFactory.php:36