Add basket
This commit is contained in:
@@ -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);
|
||||
104
source/lib/MediaListing/Basket.class.php
Normal file
104
source/lib/MediaListing/Basket.class.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
class MediaListing_Basket {
|
||||
|
||||
const CONF_BasketContents = 'basket.contents';
|
||||
|
||||
protected $items;
|
||||
|
||||
protected $cached_total_size = null;
|
||||
protected $source_prefixes = null;
|
||||
|
||||
public function __construct() {
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
81
source/webui/pages/basket.php
Normal file
81
source/webui/pages/basket.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
$main = MediaListing_Main::instance();
|
||||
$request = $main->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);
|
||||
|
||||
?>
|
||||
@@ -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);
|
||||
|
||||
?>
|
||||
@@ -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());
|
||||
|
||||
?>
|
||||
55
source/webui/templates/basket.tpl
Normal file
55
source/webui/templates/basket.tpl
Normal file
@@ -0,0 +1,55 @@
|
||||
<h2>Basket</h2>
|
||||
|
||||
{if $basket->count()}
|
||||
<form method="post" action="{$base_uri}basket/do/edit/">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
<input id="list_select_all" class="select_all" type="checkbox" />
|
||||
Remove
|
||||
</th>
|
||||
<th>Item</th>
|
||||
<th>Size</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{foreach from=$basket->getItems() item=item}
|
||||
<tr>
|
||||
<td><input type="checkbox" class="list_select_all" name="items[]" value="{$item|escape:html}" /></td>
|
||||
<td>{$item|escape:html}</td>
|
||||
<td>{$basket->size($item)|formatFilesize|escape:html}</td>
|
||||
</tr>
|
||||
{/foreach}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<input type="submit" class="btn danger" name="remove-items" value="Remove from Basket" />
|
||||
<input type="submit" class="btn danger" name="clear" value="Clear Basket" />
|
||||
<button id="basket_list_copy" class="btn secondary pull-right">Copy List</button>
|
||||
</td>
|
||||
<td>
|
||||
{$basket->totalSize()|formatFilesize|escape:html}
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
{else}
|
||||
<em>There are no items in your basket at the moment.</em>
|
||||
{/if}
|
||||
<div id="basket_list" class="modal hide fade">
|
||||
<div class="modal-header">
|
||||
<a href="#" class="close">×</a>
|
||||
<h3>Basket Text List</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<textarea id="basket_list_text" class="xxlarge" rows="15"></textarea>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button id="basket_list_close" class="btn primary">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
ml.basket.init();
|
||||
</script>
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
var base_uri = "{$base_uri|escape:'quote'}";
|
||||
var base_url = "{$base_url|escape:'quote'}";
|
||||
</script>
|
||||
|
||||
<!-- JQuery //-->
|
||||
|
||||
@@ -1,31 +1,54 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Filename</th>
|
||||
<th>Size</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{if $parent}
|
||||
<tr>
|
||||
<td><a href="{$base_uri}list/source/{$source->name()}/{$parent_path}" title="Parent Directory">../ <em>Parent Directory</em></a></td>
|
||||
<td>-</td>
|
||||
</tr>
|
||||
{/if}
|
||||
{foreach from=$files item=file}
|
||||
<tr>
|
||||
{if $file->isFile()}
|
||||
<td>{$file->basename()}</td>
|
||||
{else}
|
||||
<td><a href="{$base_uri}list/source/{$source->name()}/{$path}{$file->basename()}" title="{$file->basename()}">{$file->basename()}</a></td>
|
||||
{/if}
|
||||
<td>{if $file->isFile()}
|
||||
{$file->size()|formatFilesize}
|
||||
{else}
|
||||
-
|
||||
{/if}
|
||||
</td>
|
||||
</tr>
|
||||
{/foreach}
|
||||
</tbody>
|
||||
</table>
|
||||
<form method="post" action="{$base_uri}basket/do/add-items/">
|
||||
<input type="hidden" name="redirect" value="list/source/{$source->name()|escape:html}/{$path|escape:html}" />
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
<input id="list_select_all" class="select_all" type="checkbox" />
|
||||
Add to Basket
|
||||
</th>
|
||||
<th>Filename</th>
|
||||
<th>Size</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{if $parent}
|
||||
<tr>
|
||||
<td> </td>
|
||||
<td><a href="{$base_uri}list/source/{$source->name()|escape:html}/{$parent_path|escape:html}" title="Parent Directory">../ <em>Parent Directory</em></a></td>
|
||||
<td>-</td>
|
||||
</tr>
|
||||
{/if}
|
||||
{foreach from=$files item=file}
|
||||
{assign var=item value="{$source->name()}:{$path}{$file->basename()}"}
|
||||
<tr>
|
||||
<td>
|
||||
{if $basket->contains($item)}
|
||||
<input type="checkbox" checked="checked" disabled="disabled" />
|
||||
{else}
|
||||
<input type="checkbox" class="list_select_all" name="items[]" value="{$item|escape:html}" />
|
||||
{/if}
|
||||
</td>
|
||||
<td>
|
||||
{if $file->isFile()}
|
||||
{$file->basename()|escape:html}
|
||||
{else}
|
||||
<a href="{$base_uri}list/source/{$source->name()|escape:html}/{$path|escape:html}{$file->basename()|escape:html}" title="{$file->basename()|escape:html}">{$file->basename()|escape:html}</a>
|
||||
{/if}
|
||||
</td>
|
||||
<td>{*if $file->isFile()*}
|
||||
{$file->size()|formatFilesize|escape:html}
|
||||
{*else}
|
||||
-
|
||||
{/if*}
|
||||
</td>
|
||||
</tr>
|
||||
{/foreach}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="3"><input type="submit" class="btn primary" name="submit" value="Add to Basket" /></td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</form>
|
||||
@@ -13,7 +13,10 @@
|
||||
{/foreach}
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
<li class="{if $source_name == $source->name()}active{/if}">
|
||||
<a href="{$base_uri}basket/" title="Basket">
|
||||
Basket ({$basket_items} {MediaListing_Formatting::pluralise($basket_items, 'item', 'items')})
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
Reference in New Issue
Block a user