Add basket
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user