From b6a089914154ae5448d40e8d63b329eff5e9222e Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Thu, 5 Jan 2012 00:44:23 +0000 Subject: [PATCH] Add basket --- public/scripts/main.js | 44 ++++++++++ source/lib/MediaListing/Basket.class.php | 104 +++++++++++++++++++++++ source/lib/MediaListing/Main.class.php | 20 ++++- source/webui/pages/basket.php | 81 ++++++++++++++++++ source/webui/pages/list.php | 2 + source/webui/pages/navigation.php | 4 + source/webui/templates/basket.tpl | 55 ++++++++++++ source/webui/templates/index.tpl | 1 - source/webui/templates/list.tpl | 85 +++++++++++------- source/webui/templates/navigation.tpl | 9 +- 10 files changed, 367 insertions(+), 38 deletions(-) create mode 100644 source/lib/MediaListing/Basket.class.php create mode 100644 source/webui/pages/basket.php create mode 100644 source/webui/templates/basket.tpl diff --git a/public/scripts/main.js b/public/scripts/main.js index e69de29..27efcd2 100644 --- a/public/scripts/main.js +++ b/public/scripts/main.js @@ -0,0 +1,44 @@ +var ml = { + + init: function() { + ml.ui.init(); + }, + + ui: { + + init: function() { + $('input[type=checkbox].select_all').click(function() { + $('input[type=checkbox].'+$(this).attr('id')).attr('checked', $(this).attr('checked') == 'checked'); + }); + } + + }, + + basket: { + + init: function() { + $('#basket_list_copy').click(ml.basket.showModal); + $('#basket_list_close').click(ml.basket.hideModal); + }, + + showModal: function() { + var list = ''; + $('input[type=checkbox][name="items[]"]').each(function() { + list += $(this).val() + "\n"; + }); + + $('#basket_list_text').val(list); + $('#basket_list').modal('show'); + + return false; + }, + + hideModal: function() { + $('#basket_list').modal('hide'); + } + + } + +} + +$('document').ready(ml.init); \ No newline at end of file diff --git a/source/lib/MediaListing/Basket.class.php b/source/lib/MediaListing/Basket.class.php new file mode 100644 index 0000000..58a22ed --- /dev/null +++ b/source/lib/MediaListing/Basket.class.php @@ -0,0 +1,104 @@ +items = array(); + + $this->restoreSession(); + } + + public function restoreSession() { + $session = MediaListing_Main::instance()->session(); + if ($session->exists(static::CONF_BasketContents)) { + $this->items = $session->get(static::CONF_BasketContents); + } + } + + public function saveSession() { + $session = MediaListing_Main::instance()->session(); + $session->set(static::CONF_BasketContents, $this->items); + } + + public function addItems($items) { + if ( ! is_array($items)) { + $items = array($items); + } + + $this->items = array_merge($this->items, $items); + $this->saveSession(); + } + + public function getItems() { + return $this->items; + } + + public function clear() { + $this->items = array(); + $this->saveSession(); + } + + public function removeItems($items) { + if ( ! is_array($items)) { + $items = array($items); + } + + $this->items = array_values(array_diff($this->items, $items)); + $this->saveSession(); + } + + public function count() { + return count($this->items); + } + + public function contains($item) { + return in_array($item, $this->items); + } + + protected function loadSourcePrefixes() { + if ($this->source_prefixes !== null) { + return; + } + + $config = MediaListing_Main::instance()->config(); + $sources = $config->get('sources'); + $this->source_prefixes = array(); + foreach ($sources as $source_path) { + list($name, $path) = explode(':', $source_path); + $this->source_prefixes[$name] = $path; + } + + } + + public function size($item) { + $this->loadSourcePrefixes(); + + list($source,$path) = explode(':', $item); + return MediaListing_Main::recursiveFilesize($this->source_prefixes[$source] . '/' . $path); + } + + public function totalSize() { + if ($this->cached_total_size === null) { + $this->cached_total_size = 0; + + $this->loadSourcePrefixes(); + foreach ($this->items as $item_path) { + list($source,$path) = explode(':', $item_path); + + $this->cached_total_size += MediaListing_Main::recursiveFilesize($this->source_prefixes[$source] . '/' . $path); + } + } + + return $this->cached_total_size; + } + +} + +?> \ No newline at end of file diff --git a/source/lib/MediaListing/Main.class.php b/source/lib/MediaListing/Main.class.php index e7d9775..d176fa6 100644 --- a/source/lib/MediaListing/Main.class.php +++ b/source/lib/MediaListing/Main.class.php @@ -11,24 +11,30 @@ class MediaListing_Main extends SihnonFramework_Main { protected $smarty; protected $request; + protected $basket; protected function __construct() { parent::__construct(); + } + + public function init() { + parent::init(); $request_string = isset($_GET['l']) ? $_GET['l'] : ''; - $this->request = new MediaListing_RequestParser($request_string, $this->template_dir, $this->template_code_dir); + $this->basket = new MediaListing_Basket(); + if (ML_File == 'index') { $this->smarty = new Smarty(); $this->smarty->template_dir = $this->template_dir; $this->smarty->compile_dir = '../private/tmp/templates'; $this->smarty->cache_dir = '../private/tmp/cache'; $this->smarty->config_dir = '../private/tmp/config'; - + $this->smarty->registerPlugin('modifier', 'formatDuration', array('MediaListing_Main', 'formatDuration')); $this->smarty->registerPlugin('modifier', 'formatFilesize', array('MediaListing_Main', 'formatFilesize')); - + $this->smarty->assign('title', 'Media Listing'); $this->smarty->assign('version', '0.1'); $this->smarty->assign('messages', array()); @@ -62,6 +68,14 @@ class MediaListing_Main extends SihnonFramework_Main { return $this->request; } + /** + * + * @return MediaListing_Basket + */ + public function basket() { + return $this->basket; + } + } diff --git a/source/webui/pages/basket.php b/source/webui/pages/basket.php new file mode 100644 index 0000000..f8b163e --- /dev/null +++ b/source/webui/pages/basket.php @@ -0,0 +1,81 @@ +request(); +$session = $main->session(); +$basket = $main->basket(); +$messages = array(); + +if ($request->exists('do')) { + $activity = $request->get('do'); + + $redirect = MediaListing_Main::issetelse($_POST['redirect'], 'basket'); + + switch ($activity) { + case 'add-items': { + try { + $items = MediaListing_Main::issetelse($_POST['items'], 'Sihnon_Exception_InvalidParameters'); + + $basket->addItems($items); + + $messages[] = array( + 'severity' => 'success', + 'content' => 'The selected items were successfully added to the basket.', + ); + } catch (SihnonFramework_Exception_InvalidParameters $e) { + $messages[] = array( + 'severity' => 'error', + 'content' => 'The selected items were not added to the basket due to invalid parameters being passed.', + ); + } + } break; + + case 'edit': { + try { + $remove_items = MediaListing_Main::issetelse($_POST['remove-items'], false); + $clear_basket = MediaListing_Main::issetelse($_POST['clear'], false); + + if ($remove_items) { + $items = MediaListing_Main::issetelse($_POST['items'], 'Sihnon_Exception_InvalidParameters'); + + $basket->removeItems($items); + + $messages[] = array( + 'severity' => 'success', + 'content' => 'The selected items were successfully removed from the basket.', + ); + } else if ($clear_basket) { + $basket->clear(); + $messages[] = array( + 'severity' => 'success', + 'content' => 'The basket was successfully cleared.', + ); + } else { + $messages[] = array( + 'severity' => 'error', + 'content' => 'The requested activity was not recognised.', + ); + } + } catch (SihnonFramework_Exception_InvalidParameters $e) { + $messages[] = array( + 'severity' => 'error', + 'content' => 'The selected items were not removed from the basket due to invalid parameters being passed.', + ); + } + } break; + + default: { + $messages[] = array( + 'severity' => 'error', + 'content' => 'The requested activity was not recognised.', + ); + } break; + } + + $session->set('messages', $messages); + MediaListing_Page::redirect($redirect); +} + +$this->smarty->assign('basket', $basket); + +?> \ No newline at end of file diff --git a/source/webui/pages/list.php b/source/webui/pages/list.php index 3b69ec0..320f347 100644 --- a/source/webui/pages/list.php +++ b/source/webui/pages/list.php @@ -4,6 +4,7 @@ $main = MediaListing_Main::instance(); $config = $main->config(); $request = $main->request(); $smarty = $main->smarty(); +$basket = $main->basket(); $source_paths = $config->get('sources'); $sources = array(); @@ -40,5 +41,6 @@ $smarty->assign('source', $source); $smarty->assign('path', $path); $smarty->assign('root', $directory); $smarty->assign('files', $directory->files()); +$smarty->assign('basket', $basket); ?> \ No newline at end of file diff --git a/source/webui/pages/navigation.php b/source/webui/pages/navigation.php index 0d5199f..865a67a 100644 --- a/source/webui/pages/navigation.php +++ b/source/webui/pages/navigation.php @@ -4,6 +4,7 @@ $main = MediaListing_Main::instance(); $config = $main->config(); $smarty = $main->smarty(); $request = $main->request(); +$basket = $main->basket(); $source_paths = $config->get('sources'); $sources = array(); @@ -17,4 +18,7 @@ foreach ($source_paths as $source_path) { $smarty->assign('sources', $sources); $smarty->assign('source_name', $request->get('source')); +$smarty->assign('basket', $basket); +$smarty->assign('basket_items', $basket->count()); + ?> \ No newline at end of file diff --git a/source/webui/templates/basket.tpl b/source/webui/templates/basket.tpl new file mode 100644 index 0000000..89103d1 --- /dev/null +++ b/source/webui/templates/basket.tpl @@ -0,0 +1,55 @@ +

Basket

+ +{if $basket->count()} +
+ + + + + + + + + + {foreach from=$basket->getItems() item=item} + + + + + + {/foreach} + + + + + + + +
+ + Remove + ItemSize
{$item|escape:html}{$basket->size($item)|formatFilesize|escape:html}
+ + + + + {$basket->totalSize()|formatFilesize|escape:html} +
+{else} + There are no items in your basket at the moment. +{/if} + + \ No newline at end of file diff --git a/source/webui/templates/index.tpl b/source/webui/templates/index.tpl index 6b4ecd4..c0fc255 100644 --- a/source/webui/templates/index.tpl +++ b/source/webui/templates/index.tpl @@ -5,7 +5,6 @@ diff --git a/source/webui/templates/list.tpl b/source/webui/templates/list.tpl index 00de221..2603314 100644 --- a/source/webui/templates/list.tpl +++ b/source/webui/templates/list.tpl @@ -1,31 +1,54 @@ - - - - - - - - - {if $parent} - - - - - {/if} - {foreach from=$files item=file} - - {if $file->isFile()} - - {else} - - {/if} - - - {/foreach} - -
FilenameSize
../ Parent Directory-
{$file->basename()}{$file->basename()}{if $file->isFile()} - {$file->size()|formatFilesize} - {else} - - - {/if} -
\ No newline at end of file + + + + + + + + + + + + {if $parent} + + + + + + {/if} + {foreach from=$files item=file} + {assign var=item value="{$source->name()}:{$path}{$file->basename()}"} + + + + + + {/foreach} + + + + + + +
+ + Add to Basket + FilenameSize
 ../ Parent Directory-
+ {if $basket->contains($item)} + + {else} + + {/if} + + {if $file->isFile()} + {$file->basename()|escape:html} + {else} + {$file->basename()|escape:html} + {/if} + {*if $file->isFile()*} + {$file->size()|formatFilesize|escape:html} + {*else} + - + {/if*} +
+
\ No newline at end of file diff --git a/source/webui/templates/navigation.tpl b/source/webui/templates/navigation.tpl index b7d7dae..ebfcacb 100644 --- a/source/webui/templates/navigation.tpl +++ b/source/webui/templates/navigation.tpl @@ -13,7 +13,10 @@ {/foreach} - - \ No newline at end of file +
  • + + Basket ({$basket_items} {MediaListing_Formatting::pluralise($basket_items, 'item', 'items')}) + +
  • +