From c155513040809c3959edbfcf507dc8555feefbca Mon Sep 17 00:00:00 2001 From: paul Date: Tue, 21 Nov 2006 16:34:38 +0000 Subject: [PATCH] Added documentation. Added public/private function status. Renamed reLoadM() to reloadMetadata(). Put in some checks to ensure that the number of scratchpad items returned isnt more than the number set in the user preferences. --- .../htmlUI/var/ui_scratchpad.class.php | 127 ++++++++++++++---- 1 file changed, 100 insertions(+), 27 deletions(-) diff --git a/campcaster/src/modules/htmlUI/var/ui_scratchpad.class.php b/campcaster/src/modules/htmlUI/var/ui_scratchpad.class.php index 2e0794cdb..1dbcda812 100644 --- a/campcaster/src/modules/htmlUI/var/ui_scratchpad.class.php +++ b/campcaster/src/modules/htmlUI/var/ui_scratchpad.class.php @@ -1,14 +1,36 @@ + * @author Paul Baranowski * @package Campcaster * @subpackage htmlUI * @version $Revision$ + * @copyright 2006 MDLF, Inc. + * @license http://www.gnu.org/licenses/gpl.txt + * @link http://www.campware.org */ class uiScratchPad { + /** + * @var uiBase + */ private $Base; + + /** + * The contents of the scratchpad. + * + * @var array + */ private $items; + + /** + * @var array + */ private $order; + + /** + * @var string + */ private $reloadUrl; public function __construct(&$uiBase) @@ -20,34 +42,48 @@ class uiScratchPad } - function setReload() + public function setReload() { $this->Base->redirUrl = $this->reloadUrl; } - function &get() + /** + * Get the scratchpad, automatically load from the DB if necessary. + * + * @return array + */ + public function &get() { if (!is_array($this->items)) { $this->_load(); } - //print_r($this->items); return $this->items; } - function _load() + /** + * Load the scratchpad from the database and save it in the session. + * @return void + */ + private function _load() { $this->items = array(); + // The scratchpad is kept as a user preference. + // The value of the preference is a list of of media file IDs + // separate by space characters. $spData = $this->Base->gb->loadPref($this->Base->sessid, UI_SCRATCHPAD_KEY); if (!PEAR::isError($spData)) { - // ScratchPad found in DB + // The ScratchPad was found in the DB, + // get the scratchpad list $arr = explode(' ', $spData); - + $maxLength = $this->Base->STATIONPREFS[UI_SCRATCHPAD_MAXLENGTH_KEY]; + $arr = array_slice($arr, 0, $maxLength); foreach ($arr as $gunid) { if (preg_match('/[0-9]{1,20}/', $gunid)) { - if ($this->Base->gb->_idFromGunid($this->Base->toHex($gunid)) != FALSE) { - if ($i = $this->Base->getMetaInfo($this->Base->gb->_idFromGunid($this->Base->toHex($gunid)))) { + $id = $this->Base->gb->idFromGunid($this->Base->toHex($gunid)); + if ($id != FALSE) { + if ($i = $this->Base->getMetaInfo($id)) { $this->items[] = $i; } } @@ -57,17 +93,28 @@ class uiScratchPad } // fn _load - function save() + /** + * Save the scratchpad to the database. + * @return void + */ + public function save() { foreach ($this->items as $val) { - //$str .= $val['gunid'].':'.$val['added'].' '; ## new format ### - $str .= $this->Base->toInt8($val['gunid']).' '; ## Akos� old format ### + //$str .= $val['gunid'].':'.$val['added'].' '; // new format + $str .= $this->Base->toInt8($val['gunid']).' '; // Akos old format } $this->Base->gb->savePref($this->Base->sessid, UI_SCRATCHPAD_KEY, $str); } // fn save - function addItem($ids) + /** + * Add an item to the scratchpad + * + * @param string|array $ids + * One or more media IDs. + * @return boolean + */ + public function addItem($ids) { if (!$this->Base->STATIONPREFS[UI_SCRATCHPAD_MAXLENGTH_KEY]) { if (UI_WARNING) { @@ -85,13 +132,13 @@ class uiScratchPad $ids = array($ids); } - $sp = $this->get(); + $scratchpad = $this->get(); foreach ($ids as $id) { $item = $this->Base->getMetaInfo($id); - foreach ($sp as $key=>$val) { + foreach ($scratchpad as $key => $val) { if ($val['id'] == $item['id']) { - unset($sp[$key]); + unset($scratchpad[$key]); if (UI_VERBOSE) { $this->Base->_retMsg('Entry $1 is already on the scratchpad. It has been moved to the top of the list.', $item['title'], $val['added']); } @@ -99,19 +146,30 @@ class uiScratchPad #$this->Base->incAccessCounter($id); } } - $sp = array_merge(array($item), is_array($sp) ? $sp : NULL); + $scratchpad = array_merge(array($item), is_array($scratchpad) ? $scratchpad : NULL); } - for ($n=0; $n < $this->Base->STATIONPREFS[UI_SCRATCHPAD_MAXLENGTH_KEY]; $n++) { - if (is_array($sp[$n])) { - $this->items[$n] = $sp[$n]; + $maxScratchpadLength = $this->Base->STATIONPREFS[UI_SCRATCHPAD_MAXLENGTH_KEY]; + for ($n = 0; $n < $maxScratchpadLength; $n++) { + if (!isset($scratchpad[$n])) { + break; + } + if (is_array($scratchpad[$n])) { + $this->items[$n] = $scratchpad[$n]; } } ksort($this->items); + return true; } // fn addItem - function removeItems($ids) + /** + * Remove one or more items from the scratchpad. + * + * @param string|array $ids + * @return boolean + */ + public function removeItems($ids) { if (!$ids) { if (UI_WARNING) { @@ -137,16 +195,22 @@ class uiScratchPad } // fn removeItems - function reOrder($by) + /** + * Enter description here... + * + * @param unknown_type $by + * @return void + */ + public function reorder($by) { if (count($this->items) == 0) { - return FALSE; + return; } foreach ($this->items as $key=>$val) { $s[$key] = $val[$by]; } - $curr = $this->order[$by]; + $curr = $this->order[$by]; $this->order = array(); if (is_null($curr) || $curr=='DESC') { $this->order[$by] = 'ASC'; @@ -154,8 +218,12 @@ class uiScratchPad $this->order[$by] = 'DESC'; } switch ($this->order[$by]) { - case "ASC": asort($s); break; - case "DESC": arsort($s); break; + case "ASC": + asort($s); + break; + case "DESC": + arsort($s); + break; } foreach ($s as $key=>$val) { $res[] = $this->items[$key]; @@ -164,9 +232,14 @@ class uiScratchPad } - function reLoadM() + /** + * Reload the metadata for the items in the scratchpad. + * + * @return void + */ + public function reloadMetadata() { - foreach($this->items as $key=>$val) { + foreach ($this->items as $key => $val) { $this->items[$key] = $this->Base->getMetaInfo($val['id']); } }