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 
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
22  public function __construct($FieldId = NULL)
23  {
24  # save field ID for our later use
25  $this->FieldId = $FieldId;
26 
27  # set up item factory base class
28  parent::__construct("File", "Files", "FileId", "FileName", FALSE,
29  ($FieldId ? "FieldId = ".intval($FieldId) : NULL));
30  }
31 
39  public function GetFilesForResource($ResourceOrResourceId, $ReturnObjects = TRUE)
40  {
41  # start out assuming that no files will be found
42  $ReturnValue = array();
43 
44  # sanitize resource ID or grab it from object
45  $ResourceOrResourceId = is_object($ResourceOrResourceId)
46  ? $ResourceOrResourceId->Id() : intval($ResourceOrResourceId);
47 
48  # retrieve names and IDs of files associated with resource
49  $this->DB->Query(
50  "SELECT FileId, FileName FROM Files"
51  ." WHERE ResourceId = ".$ResourceOrResourceId
52  ." AND FieldId"
53  .($this->FieldId ? "=".$this->FieldId : ">0"));
54  $FileNames = $this->DB->FetchColumn("FileName", "FileId");
55 
56  # if files were found
57  if (count($FileNames))
58  {
59  # if caller asked us to return objects
60  if ($ReturnObjects)
61  {
62  # for each file
63  foreach ($FileNames as $FileId => $FileName)
64  {
65  # create file object and add it to array
66  $ReturnValue[$FileId] = new File($FileId);
67  }
68  }
69  else
70  {
71  # return array of file names with IDs as index
72  $ReturnValue = $FileNames;
73  }
74  }
75 
76  # return resulting array of files or file names to caller
77  return $ReturnValue;
78  }
79 
85  public function Copy($FileToCopy)
86  {
87  return $FileToCopy->CreateCopy();
88  }
89 
90 
91  # ---- PRIVATE INTERFACE -------------------------------------------------
92 
93  private $FieldId;
94 }
GetFilesForResource($ResourceOrResourceId, $ReturnObjects=TRUE)
Retrieve all files (names or objects) for specified resource.
Definition: FileFactory.php:39
__construct($FieldId=NULL)
Object constructor.
Definition: FileFactory.php:22
Copy($FileToCopy)
Create copy of File and return to caller.
Definition: FileFactory.php:85
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