CWIS Developer Documentation
FileFactory.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: FileFactory.php
5 #
6 # Part of the Collection Workflow Integration System
7 # Copyright 2007-2009 Edward Almasy and Internet Scout
8 # http://scout.wisc.edu
9 #
10 
14 class FileFactory extends ItemFactory {
15 
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
18  # object constructor
19  function FileFactory($FieldId = NULL)
20  {
21  # set up item factory base class
22  $this->ItemFactory("File", "Files", "FileId", "FileName", $FieldId);
23  }
24 
25  # retrieve all files (names or objects) for specified resource
26  # (array index is file IDs)
27  function GetFilesForResource($ResourceOrResourceId, $ReturnObjects = TRUE)
28  {
29  # start out assuming that no files will be found
30  $ReturnValue = array();
31 
32  # sanitize resource ID or grab it from object
33  $ResourceOrResourceId = is_object($ResourceOrResourceId)
34  ? $ResourceOrResourceId->Id() : intval($ResourceOrResourceId);
35 
36  # retrieve names and IDs of files associated with resource
37  $this->DB->Query(
38  "SELECT FileId, FileName FROM Files"
39  ." WHERE ResourceId = ".$ResourceOrResourceId
40  ." AND FieldId"
41  .($this->FieldId ? "=".$this->FieldId : ">0"));
42  $FileNames = $this->DB->FetchColumn("FileName", "FileId");
43 
44  # if files were found
45  if (count($FileNames))
46  {
47  # if caller asked us to return objects
48  if ($ReturnObjects)
49  {
50  # for each file
51  foreach ($FileNames as $FileId => $FileName)
52  {
53  # create file object and add it to array
54  $ReturnValue[$FileId] = new File($FileId);
55  }
56  }
57  else
58  {
59  # return array of file names with IDs as index
60  $ReturnValue = $FileNames;
61  }
62  }
63 
64  # return resulting array of files or file names to caller
65  return $ReturnValue;
66  }
67 
68  # create copy of File and return to caller
69  function Copy($FileToCopy)
70  {
71  return new File($FileToCopy->GetNameOfStoredFile(),
72  $FileToCopy->ResourceId(),
73  $FileToCopy->FieldId(),
74  $FileToCopy->Name());
75  }
76 
77 
78  # ---- PRIVATE INTERFACE -------------------------------------------------
79 
80 }
81 
82 
83 ?>