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 GetItemCount()
294 {
295 return $this->OrderList->GetCount();
296 }
297
304 function RemoveItem($ItemId, $ItemType = NULL)
305 {
306 # if resource is in folder
307 if ($this->ContainsItem($ItemId, $ItemType))
308 {
309 # remove item from item order
310 $ItemTypeId = self::GetItemTypeId($ItemType);
311 $this->OrderList->Remove($ItemId, $ItemTypeId);
312
313 # remove resource from folder locally
314 unset($this->ItemNoteCache[self::GetCacheIndex($ItemId, $ItemTypeId)]);
315
316 # remove resource from folder in DB
317 $this->DB->Query("DELETE FROM FolderItemInts"
318 ." WHERE FolderId = ".intval($this->Id)
319 ." AND ItemId = ".intval($ItemId)
320 ." AND ItemTypeId = ".intval($ItemTypeId));
321 }
322 }
323
331 function NoteForItem($ItemId, $NewValue = DB_NOVALUE, $ItemType = NULL)
332 {
333 $ItemTypeId = self::GetItemTypeId($ItemType);
334 $Index = self::GetCacheIndex($ItemId, $ItemTypeId);
335 $DummyCache = array("ItemNote" => $this->ItemNoteCache[$Index]);
336
337 $Value = $this->DB->UpdateValue("FolderItemInts", "ItemNote", $NewValue,
338 "FolderId = ".intval($this->Id)
339 ." AND ItemId = ".intval($ItemId)
340 ." AND ItemTypeId = ".intval($ItemTypeId),
341 $DummyCache);
342
343 $this->ItemNoteCache[self::GetCacheIndex($ItemId, $ItemTypeId)] = $Value;
344
345 return $Value;
346 }
347
354 function ContainsItem($ItemId, $ItemType = NULL)
355 {
356 $ItemTypeId = self::GetItemTypeId($ItemType);
357 return array_key_exists(self::GetCacheIndex($ItemId, $ItemTypeId),
358 $this->ItemNoteCache) ? TRUE : FALSE;
359 }
360
361 /*@)*/ /* Item Operations */
362
363 # ---- PRIVATE INTERFACE -------------------------------------------------
364
365 private $DB;
366 private $Id;
367
368 # folder attributes - these much match field names in Folders DB table
369 private $OwnerId;
370 private $FolderName;
371 private $NormalizedName;
372 private $FolderNote;
373 private $IsShared;
374 private $ContentType;
375
376 private $ItemNoteCache;
377 private $OrderList;
378 private $UpdateValueCache;
379
380 # item type IDs (indexed by normalized type name)
381 static private $ItemTypeIds;
382 # item type names (indexed by type ID)
383 static private $ItemTypeNames;
384
385 # content type that indicates folder contains mixed content types
386 const MIXEDCONTENT = -1;
387
395 static function GetItemTypeId($TypeName)
396 {
397 # return "no type" ID if null passed in
398 if ($TypeName === NULL) { return -1; }
399
400 # make sure item type map is loaded
401 self::LoadItemTypeMap();
402
403 # normalize item type name
404 $NormalizedTypeName = strtoupper(
405 preg_replace("/[^a-zA-Z0-9]/", "", $TypeName));
406
407 # if name not already mapped
408 if (!array_key_exists($NormalizedTypeName, self::$ItemTypeIds))
409 {
410 # add name to database
411 if (!isset($DB)) { $DB = new Database(); }
412 $DB->Query("INSERT INTO FolderContentTypes SET"
413 ." TypeName = '".addslashes($TypeName)."',"
414 ." NormalizedTypeName = '".addslashes($NormalizedTypeName)."'");
415
416 # add name to cached mappings
417 $NewTypeId = $DB->LastInsertId();
418 self::$ItemTypeIds[$NormalizedTypeName] = $NewTypeId;
419 self::$ItemTypeNames[$NewTypeId] = $TypeName;
420 }
421
422 # return item type ID to caller
423 return self::$ItemTypeIds[$NormalizedTypeName];
424 }
432 private static function GetItemTypeName($TypeId)
433 {
434 # make sure item type map is loaded
435 self::LoadItemTypeMap();
436
437 # if ID not present in mappings
438 if (!array_key_exists($TypeId, self::$ItemTypeNames))
439 {
440 # return null value
441 return NULL;
442 }
443 else
444 {
445 # return item type name to caller
446 return self::$ItemTypeNames[$TypeId];
447 }
448 }
449
454 private static function LoadItemTypeMap()
455 {
456 # if name-to-number item type map not already loaded
457 if (!isset(self::$ItemTypeIds))
458 {
459 # load item type map from database
460 $DB = new Database();
461 $DB->Query("SELECT * FROM FolderContentTypes");
462 self::$ItemTypeIds = array();
463 self::$ItemTypeNames = array();
464 while ($Row = $DB->FetchRow())
465 {
466 self::$ItemTypeIds[$Row["NormalizedTypeName"]] = $Row["TypeId"];
467 self::$ItemTypeNames[$Row["TypeId"]] = $Row["TypeName"];
468 }
469 }
470 }
471
477 private function AddItem($ItemOrItemId, $ItemType)
478 {
479 # convert item to ID if necessary
480 $ItemId = is_object($ItemOrItemId)
481 ? $ItemOrItemId->Id() : $ItemOrItemId;
482
483 # convert item type to item type ID
484 $ItemTypeId = self::GetItemTypeId($ItemType);
485
486 # convert null item type to "no type" value used in database
487 if ($ItemTypeId === NULL) { $ItemTypeId = -1; }
488
489 # if resource is not already in folder
490 if (!$this->ContainsItem($ItemId, $ItemType))
491 {
492 # add resource to folder locally
493 $this->ItemNoteCache[self::GetCacheIndex($ItemId, $ItemTypeId)] = NULL;
494
495 # add resource to folder in DB
496 $this->DB->Query("INSERT INTO FolderItemInts SET"
497 ." FolderId = ".intval($this->Id)
498 .", ItemId = ".intval($ItemId)
499 .", ItemTypeId = ".intval($ItemTypeId));
500 }
501 }
502
509 private static function GetCacheIndex($ItemId, $ItemTypeId)
510 {
511 $ItemTypeId = ($ItemTypeId === NULL) ? -1 : $ItemTypeId;
512 return intval($ItemTypeId).":".intval($ItemId);
513 }
514
522 private function UpdateValue($FieldName, $NewValue)
523 {
524 $this->$FieldName = $this->DB->UpdateValue("Folders", $FieldName, $NewValue,
525 "FolderId = ".$this->Id, $this->UpdateValueCache);
526 return $this->$FieldName;
527 }
528 }
529
530
Folder\NormalizedName
NormalizedName($NewValue=DB_NOVALUE)
Get/set normalized version of folder name.
Definition: Folder.php:131
Folder\MIXEDCONTENT
const MIXEDCONTENT
Definition: Folder.php:386
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:331
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:1180
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:304
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\GetItemCount
GetItemCount()
Get number of items in folder.
Definition: Folder.php:293
Folder\ContainsItem
ContainsItem($ItemId, $ItemType=NULL)
Check whether specified item is contained in folder.
Definition: Folder.php:354
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