CWIS Developer Documentation

- Home
- Main Page
- Classes
- Files

- File List
- File Members

Folder.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Folder.php
4 #
5 # Part of the Collection Workflow Information System (CWIS)
6 # Copyright 2012-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
17 class Folder {
18
19 # ---- PUBLIC INTERFACE --------------------------------------------------
20 /*@(*/
22
28 function Folder($FolderId)
29 {
30 # create our own DB handle
31 $this->DB = new Database();
32
33 # store folder ID
34 $this->Id = intval($FolderId);
35
36 # attempt to load in folder info
37 $this->DB->Query("SELECT * FROM Folders WHERE FolderId = ".$this->Id);
38 $Record = $this->DB->FetchRow();
39
40 # if folder was not found
41 if ($Record === FALSE)
42 {
43 # bail out with exception
44 throw new Exception("Unknown Folder ID (".$FolderId.").");
45 }
46
47 # save folder info
48 $this->OwnerId = $Record["OwnerId"];
49 $this->FolderName = $Record["FolderName"];
50 $this->NormalizedName = $Record["NormalizedName"];
51 $this->FolderNote = $Record["FolderNote"];
52 $this->IsShared = $Record["IsShared"];
53 $this->ContentType = $Record["ContentType"];
54 $this->UpdateValueCache = $Record;
55
56 # load list of resources in folder from database
57 $this->DB->Query("SELECT ItemId, ItemTypeId, ItemNote FROM FolderItemInts"
58 ." WHERE FolderId = ".$this->Id);
59
60 # create internal cache for item notes
61 $this->ItemNoteCache = array();
62 while ($Record = $this->DB->FetchRow())
63 {
64 $Index = self::GetCacheIndex($Record["ItemId"], $Record["ItemTypeId"]);
65 $this->ItemNoteCache[$Index] = $Record["ItemNote"];
66 }
67
68 # load item ordering
69 if ($this->ContentType == self::MIXEDCONTENT)
70 {
71 $this->OrderList = new PersistentDoublyLinkedList(
72 "FolderItemInts", "ItemId", "FolderId = ".$this->Id, "ItemTypeId");
73 }
74 else
75 {
76 $this->OrderList = new PersistentDoublyLinkedList(
77 "FolderItemInts", "ItemId", "FolderId = ".$this->Id);
78 }
79 }
80
84 function Delete()
85 {
86 # take folder out of global folder order
87 $Factory = new FolderFactory();
88 $Factory->RemoveItemFromOrder($this->Id);
89
90 # remove resource listings from DB
91 $this->DB->Query("DELETE FROM FolderItemInts WHERE FolderId = ".$this->Id);
92
93 # remove folder listing from DB
94 $this->DB->Query("DELETE FROM Folders WHERE FolderId = ".$this->Id);
95 }
96
97 /*@)*/ /* Setup/Initialization */
98 # ------------------------------------------------------------------------
99 /*@(*/
100
105 function Id()
106 {
107 return $this->Id;
108 }
109
115 function Name($NewValue = DB_NOVALUE)
116 {
117 if ($NewValue != DB_NOVALUE)
118 {
119 $this->NormalizedName(self::NormalizeFolderName($NewValue));
120 }
121 return $this->UpdateValue("FolderName", $NewValue);
122 }
123
131 function NormalizedName($NewValue = DB_NOVALUE)
132 {
133 $Name = $this->UpdateValue("NormalizedName", $NewValue);
134 # attempt to generate and set new normalized name if none found
135 if (!strlen($Name))
136 {
137 $Name = $this->UpdateValue("NormalizedName",
138 self::NormalizeFolderName($this->Name()));
139 }
140 return $Name;
141 }
142
148 static function NormalizeFolderName($Name)
149 {
150 return preg_replace("/[^a-z0-9]/", "", strtolower($Name));
151 }
152
159 function IsShared($NewValue = DB_NOVALUE)
160 {
161 return $this->UpdateValue("IsShared", $NewValue);
162 }
163
169 function OwnerId($NewValue = DB_NOVALUE)
170 {
171 if ($NewValue !== DB_NOVALUE) { unset($this->Owner); }
172 return intval($this->UpdateValue("OwnerId", $NewValue));
173 }
174
180 function Note($NewValue = DB_NOVALUE)
181 {
182 return $this->UpdateValue("FolderNote", $NewValue);
183 }
184
185 /*@)*/ /* Attribute Setting/Retrieval */
186 # ------------------------------------------------------------------------
187 /*@(*/
188
201 function InsertItemBefore($TargetItemOrItemId, $NewItemOrItemId,
202 $TargetItemType = NULL, $NewItemType = NULL)
203 {
204 $this->AddItem($NewItemOrItemId, $NewItemType);
205 $this->OrderList->InsertBefore($TargetItemOrItemId, $NewItemOrItemId,
206 self::GetItemTypeId($TargetItemType),
207 self::GetItemTypeId($NewItemType));
208 }
209
221 function InsertItemAfter($TargetItemOrItemId, $NewItemOrItemId,
222 $TargetItemType = NULL, $NewItemType = NULL)
223 {
224 $this->AddItem($NewItemOrItemId, $NewItemType);
225 $this->OrderList->InsertAfter($TargetItemOrItemId, $NewItemOrItemId,
226 self::GetItemTypeId($TargetItemType),
227 self::GetItemTypeId($NewItemType));
228 }
229
237 function PrependItem($ItemOrItemId, $ItemType = NULL)
238 {
239 $this->AddItem($ItemOrItemId, $ItemType);
240 $this->OrderList->Prepend($ItemOrItemId, self::GetItemTypeId($ItemType));
241 }
242
250 function AppendItem($ItemOrItemId, $ItemType = NULL)
251 {
252 $this->AddItem($ItemOrItemId, $ItemType);
253 $this->OrderList->Append($ItemOrItemId, self::GetItemTypeId($ItemType));
254 }
255
263 function GetItemIds()
264 {
265 # retrieve item ordered list of type IDs
266 $ItemIds = $this->OrderList->GetIds();
267
268 # if this is a mixed-item-type folder
269 if ($this->ContentType == self::MIXEDCONTENT)
270 {
271 # convert item type IDs to corresponding type names
272 $NewItemIds = array();
273 foreach ($ItemIds as $ItemInfo)
274 {
275 $NewItemIds[] = array(
276 "ID" => $ItemInfo["ID"],
277 "Type" => self::GetItemTypeName($ItemInfo["Type"]),
278 );
279 }
280 $ItemIds = $NewItemIds;
281 }
282
283 # return list of item type IDs (and possibly types) to caller
284 return $ItemIds;
285 }
286
293 function RemoveItem($ItemId, $ItemType = NULL)
294 {
295 # if resource is in folder
296 if ($this->ContainsItem($ItemId, $ItemType))
297 {
298 # remove item from item order
299 $ItemTypeId = self::GetItemTypeId($ItemType);
300 $this->OrderList->Remove($ItemId, $ItemTypeId);
301
302 # remove resource from folder locally
303 unset($this->ItemNoteCache[self::GetCacheIndex($ItemId, $ItemTypeId)]);
304
305 # remove resource from folder in DB
306 $this->DB->Query("DELETE FROM FolderItemInts"
307 ." WHERE FolderId = ".intval($this->Id)
308 ." AND ItemId = ".intval($ItemId)
309 ." AND ItemTypeId = ".intval($ItemTypeId));
310 }
311 }
312
320 function NoteForItem($ItemId, $NewValue = DB_NOVALUE, $ItemType = NULL)
321 {
322 $ItemTypeId = self::GetItemTypeId($ItemType);
323 $Index = self::GetCacheIndex($ItemId, $ItemTypeId);
324 $DummyCache = array("ItemNote" => $this->ItemNoteCache[$Index]);
325
326 $Value = $this->DB->UpdateValue("FolderItemInts", "ItemNote", $NewValue,
327 "FolderId = ".intval($this->Id)
328 ." AND ItemId = ".intval($ItemId)
329 ." AND ItemTypeId = ".intval($ItemTypeId),
330 $DummyCache);
331
332 $this->ItemNoteCache[self::GetCacheIndex($ItemId, $ItemTypeId)] = $Value;
333
334 return $Value;
335 }
336
343 function ContainsItem($ItemId, $ItemType = NULL)
344 {
345 $ItemTypeId = self::GetItemTypeId($ItemType);
346 return array_key_exists(self::GetCacheIndex($ItemId, $ItemTypeId),
347 $this->ItemNoteCache) ? TRUE : FALSE;
348 }
349
350 /*@)*/ /* Item Operations */
351
352 # ---- PRIVATE INTERFACE -------------------------------------------------
353
354 private $DB;
355 private $Id;
356
357 # folder attributes - these much match field names in Folders DB table
358 private $OwnerId;
359 private $FolderName;
360 private $NormalizedName;
361 private $FolderNote;
362 private $IsShared;
363 private $ContentType;
364
365 private $ItemNoteCache;
366 private $OrderList;
367 private $UpdateValueCache;
368
369 # item type IDs (indexed by normalized type name)
370 static private $ItemTypeIds;
371 # item type names (indexed by type ID)
372 static private $ItemTypeNames;
373
374 # content type that indicates folder contains mixed content types
375 const MIXEDCONTENT = -1;
376
384 static function GetItemTypeId($TypeName)
385 {
386 # return "no type" ID if null passed in
387 if ($TypeName === NULL) { return -1; }
388
389 # make sure item type map is loaded
390 self::LoadItemTypeMap();
391
392 # normalize item type name
393 $NormalizedTypeName = strtoupper(
394 preg_replace("/[^a-zA-Z0-9]/", "", $TypeName));
395
396 # if name not already mapped
397 if (!array_key_exists($NormalizedTypeName, self::$ItemTypeIds))
398 {
399 # add name to database
400 if (!isset($DB)) { $DB = new Database(); }
401 $DB->Query("INSERT INTO FolderContentTypes SET"
402 ." TypeName = '".addslashes($TypeName)."',"
403 ." NormalizedTypeName = '".addslashes($NormalizedTypeName)."'");
404
405 # add name to cached mappings
406 $NewTypeId = $DB->LastInsertId("FolderContentTypes");
407 self::$ItemTypeIds[$NormalizedTypeName] = $NewTypeId;
408 self::$ItemTypeNames[$NewTypeId] = $TypeName;
409 }
410
411 # return item type ID to caller
412 return self::$ItemTypeIds[$NormalizedTypeName];
413 }
421 private static function GetItemTypeName($TypeId)
422 {
423 # make sure item type map is loaded
424 self::LoadItemTypeMap();
425
426 # if ID not present in mappings
427 if (!array_key_exists($TypeId, self::$ItemTypeNames))
428 {
429 # return null value
430 return NULL;
431 }
432 else
433 {
434 # return item type name to caller
435 return self::$ItemTypeNames[$TypeId];
436 }
437 }
438
443 private static function LoadItemTypeMap()
444 {
445 # if name-to-number item type map not already loaded
446 if (!isset(self::$ItemTypeIds))
447 {
448 # load item type map from database
449 $DB = new Database();
450 $DB->Query("SELECT * FROM FolderContentTypes");
451 self::$ItemTypeIds = array();
452 self::$ItemTypeNames = array();
453 while ($Row = $DB->FetchRow())
454 {
455 self::$ItemTypeIds[$Row["NormalizedTypeName"]] = $Row["TypeId"];
456 self::$ItemTypeNames[$Row["TypeId"]] = $Row["TypeName"];
457 }
458 }
459 }
460
466 private function AddItem($ItemOrItemId, $ItemType)
467 {
468 # convert item to ID if necessary
469 $ItemId = is_object($ItemOrItemId)
470 ? $ItemOrItemId->Id() : $ItemOrItemId;
471
472 # convert item type to item type ID
473 $ItemTypeId = self::GetItemTypeId($ItemType);
474
475 # convert null item type to "no type" value used in database
476 if ($ItemTypeId === NULL) { $ItemTypeId = -1; }
477
478 # if resource is not already in folder
479 if (!$this->ContainsItem($ItemId, $ItemType))
480 {
481 # add resource to folder locally
482 $this->ItemNoteCache[self::GetCacheIndex($ItemId, $ItemTypeId)] = NULL;
483
484 # add resource to folder in DB
485 $this->DB->Query("INSERT INTO FolderItemInts SET"
486 ." FolderId = ".intval($this->Id)
487 .", ItemId = ".intval($ItemId)
488 .", ItemTypeId = ".intval($ItemTypeId));
489 }
490 }
491
498 private static function GetCacheIndex($ItemId, $ItemTypeId)
499 {
500 $ItemTypeId = ($ItemTypeId === NULL) ? -1 : $ItemTypeId;
501 return intval($ItemTypeId).":".intval($ItemId);
502 }
503
511 private function UpdateValue($FieldName, $NewValue)
512 {
513 $this->$FieldName = $this->DB->UpdateValue("Folders", $FieldName, $NewValue,
514 "FolderId = ".$this->Id, $this->UpdateValueCache);
515 return $this->$FieldName;
516 }
517 }
518
519
Folder\NormalizedName
NormalizedName($NewValue=DB_NOVALUE)
Get/set normalized version of folder name.
Definition: Folder.php:131
Folder\MIXEDCONTENT
const MIXEDCONTENT
Definition: Folder.php:375
Folder\AppendItem
AppendItem($ItemOrItemId, $ItemType=NULL)
Add item to folder as the last item.
Definition: Folder.php:250
Folder\NoteForItem
NoteForItem($ItemId, $NewValue=DB_NOVALUE, $ItemType=NULL)
Get/set note text for specific item within folder.
Definition: Folder.php:320
Folder\Id
Id()
Get folder ID.
Definition: Folder.php:105
Database
SQL database abstraction object with smart query caching.
Definition: Axis--Database.php:22
Folder\IsShared
IsShared($NewValue=DB_NOVALUE)
Get/set whether folder is publically-viewable.
Definition: Folder.php:159
DB_NOVALUE
const DB_NOVALUE
Definition: Axis--Database.php:1091
Folder\GetItemIds
GetItemIds()
Retrieve array of IDs of items in folder, in the order that they appear in the folder.
Definition: Folder.php:263
FolderFactory
Factory object for Folder class, used to retrieve and manage Folders and groups of Folders...
Definition: FolderFactory.php:14
Folder
Folder object used to create and manage groups of items.
Definition: Folder.php:17
Folder\RemoveItem
RemoveItem($ItemId, $ItemType=NULL)
Remove item from folder, if present.
Definition: Folder.php:293
Folder\NormalizeFolderName
static NormalizeFolderName($Name)
Convert folder name to normalized form (lower-case alphanumeric only).
Definition: Folder.php:148
PHP
PHP
Definition: OAIClient.php:39
Folder\InsertItemAfter
InsertItemAfter($TargetItemOrItemId, $NewItemOrItemId, $TargetItemType=NULL, $NewItemType=NULL)
Insert item into folder after specified item.
Definition: Folder.php:221
Folder\Note
Note($NewValue=DB_NOVALUE)
Get/set note text for folder.
Definition: Folder.php:180
Folder\ContainsItem
ContainsItem($ItemId, $ItemType=NULL)
Check whether specified item is contained in folder.
Definition: Folder.php:343
Folder\Folder
Folder($FolderId)
Object constructor – load an existing folder.
Definition: Folder.php:28
Folder\InsertItemBefore
InsertItemBefore($TargetItemOrItemId, $NewItemOrItemId, $TargetItemType=NULL, $NewItemType=NULL)
Insert item into folder before specified item.
Definition: Folder.php:201
PersistentDoublyLinkedList
Persistent doubly-linked-list data structure, with its data stored in a specified database table...
Definition: PersistentDoublyLinkedList.php:21
Folder\OwnerId
OwnerId($NewValue=DB_NOVALUE)
Get/set user ID of folder owner.
Definition: Folder.php:169
Folder\Delete
Delete()
Delete folder.
Definition: Folder.php:84
Folder\PrependItem
PrependItem($ItemOrItemId, $ItemType=NULL)
Add item to folder as the first item.
Definition: Folder.php:237
Folder\Name
Name($NewValue=DB_NOVALUE)
Get/set folder name.
Definition: Folder.php:115