CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

FolderFactory.php
Go to the documentation of this file.
1 <?PHP
2
3 #
4 # FILE: FolderFactory.php
5 #
6 # Part of the Collection Workflow Information System (CWIS)
7 # Copyright 2012 Edward Almasy and Internet Scout
8 # http://scout.wisc.edu
9 #
10
15 class FolderFactory extends ItemFactory {
16
17 # ---- PUBLIC INTERFACE --------------------------------------------------
18
25 function FolderFactory($OwnerId = NULL)
26 {
27 # set up item factory base class
28 $this->ItemFactory("Folder", "Folders", "FolderId", "FolderName", NULL, TRUE);
29
30 # set up
31 if ($OwnerId !== NULL)
32 {
33 $this->OwnerId = intval($OwnerId);
34 $this->SetOrderOpsCondition("OwnerId = ".$this->OwnerId);
35 }
36 }
37
45 function CreateFolder($ItemType, $FolderName = NULL, $OwnerId = NULL)
46 {
47 # retrieve numerical item type
48 $ItemTypeId = ($ItemType === Folder::MIXEDCONTENT)
49 ? $ItemType : Folder::GetItemTypeId($ItemType);
50
51 # use default owner if available and none specified
52 if (($OwnerId === NULL) & ($this->OwnerId !== NULL))
53 { $OwnerId = $this->OwnerId; }
54
55 # add new folder to database
56 $this->DB->Query("INSERT INTO Folders SET"
57 ." ContentType = ".$ItemTypeId
58 .($FolderName ? ", FolderName = '".addslashes($FolderName)."'" : "")
59 .(($OwnerId !== NULL) ? ", OwnerId = ".intval($OwnerId) : ""));
60
61 # retrieve ID of new folder
62 $Id = $this->DB->LastInsertId("Folders");
63
64 # create new folder object and return it to caller
65 return new Folder($Id);
66 }
67
74 function CreateMixedFolder($FolderName = NULL, $OwnerId = NULL)
75 {
76 # create new mixed-content folder and return it to caller
77 return $this->CreateFolder(Folder::MIXEDCONTENT, $FolderName, $OwnerId);
78 }
79
84 function GetFolderCount()
85 {
86 return $this->GetItemCount(isset($this->OwnerId)
87 ? "OwnerId = ".intval($this->OwnerId) : NULL);
88 }
89
90 /*
91 * Retrieve folder with specified normalized name (as generated by
92 * Folder::NormalizeFolderName() method).
93 * @param Normalized folder name.
94 * @param OwnerId ID of folder owner. (OPTIONAL)
95 * @return Folder object or NULL if no folder found. If multiple folders
96 * with the specified name are found, the one with the lowest folder
97 * ID is returned.
98 */
99 function GetFolderByNormalizedName($NormalizedName, $OwnerId = NULL)
100 {
101 # use default owner if available and none specified
102 if (($OwnerId === NULL) & ($this->OwnerId !== NULL))
103 { $OwnerId = $this->OwnerId; }
104
105 # query database for folder ID
106 $FolderId = $this->DB->Query("SELECT FolderId FROM Folders"
107 ." WHERE NormalizedName = '".addslashes($NormalizedName)."'"
108 .(($OwnerId !== NULL) ? " AND OwnerId = ".$this->OwnerId : "")
109 ." ORDER BY FolderId ASC",
110 "FolderId");
111
112 # if folder found with specified name and owner
113 if ($FolderId !== FALSE)
114 {
115 # create folder object and return it to caller
116 return new Folder($FolderId);
117 }
118 else
119 {
120 # return NULL to caller to indicate folder not found
121 return NULL;
122 }
123 }
124
133 function GetFoldersContainingItem($Item, $ItemType, $OwnerId = NULL, $SharedFoldersOnly = FALSE)
134 {
135 # assume we won't find any folders
136 $Folders = array();
137
138 # retrieve item ID
139 $ItemId = is_object($Item) ? $Item->Id() : $Item;
140
141 # retrieve numerical item type
142 $ItemTypeId = ($ItemType === Folder::MIXEDCONTENT)
143 ? $ItemType : Folder::GetItemTypeId($ItemType);
144
145 # use default owner if available and none specified
146 if (($OwnerId === NULL) & ($this->OwnerId !== NULL))
147 { $OwnerId = $this->OwnerId; }
148
149 # query database for IDs of all folders that contain item
150 $this->DB->Query("
151 SELECT DISTINCT FolderItemInts.FolderId
152 FROM FolderItemInts
153 LEFT JOIN Folders
154 ON FolderItemInts.FolderId = Folders.FolderId
155 WHERE FolderItemInts.ItemId = '".intval($ItemId)."'
156 AND (FolderItemInts.ItemTypeId = '".intval($ItemTypeId)."'
157 OR Folders.ContentType = '".intval($ItemTypeId)."')
158 ".(($OwnerId !== NULL) ? " AND Folders.OwnerId = ".intval($OwnerId) : "")."
159 ".(($SharedFoldersOnly) ? " AND Folders.IsShared = 1" : ""));
160 $FolderIds = $this->DB->FetchColumn("FolderId");
161
162 # create array of folders from folder IDs
163 foreach ($FolderIds as $Id)
164 {
165 $Folders[$Id] = new Folder($Id);
166 }
167
168 # return folders (if any) to caller
169 return $Folders;
170 }
171
187 function GetFolders($ItemType = NULL, $OwnerId = NULL, $Name = NULL,
188 $Offset = 0, $Count = NULL)
189 {
190 # retrieve numerical item type
191 $ItemTypeId = ($ItemType === Folder::MIXEDCONTENT)
192 ? $ItemType : Folder::GetItemTypeId($ItemType);
193
194 # retrieve IDs of all folders that match specified parameters
195 $Condition = ($ItemTypeId !== NULL) ? "ContentType = ".intval($ItemTypeId) : NULL;
196 if (($OwnerId !== NULL) || ($this->OwnerId !== NULL))
197 {
198 $Condition .= ($Condition ? " AND " : "")."OwnerId = "
199 .intval(($OwnerId !== NULL) ? $OwnerId : $this->OwnerId);
200 }
201 if ($Name !== NULL)
202 {
203 $Condition .= ($Condition ? " AND " : "")."FolderName = '"
204 .addslashes($Name)."'";
205 }
206 $FolderIds = $this->GetItemIds($Condition);
207
208 # pare down list to requested range
209 if ($Offset || $Count)
210 {
211 $FolderIds = $Count ? array_slice($FolderIds, $Offset, $Count)
212 : array_slice($FolderIds, $Offset);
213 }
214
215 # create array of folders based on IDs
216 $Folders = array();
217 foreach ($FolderIds as $FolderId)
218 {
219 $Folders[$FolderId] = new Folder($FolderId);
220 }
221
222 # return folders (if any) to caller
223 return $Folders;
224 }
225
226
227 # ---- PRIVATE INTERFACE -------------------------------------------------
228
229 private $OwnerId;
230 }
231
232
233 ?>