* @license PHP License * @package WB * @subpackage content */ /** * Load required class */ WBClass::load( 'WBContent' , 'WBCache' , 'WBVFS_Gallery' ); /** * Content component: Gallery * * * * @version 0.2.0 * @package WB * @subpackage content */ class WBContent_Gallery extends WBContent { /** * my parameter list * * - process (display action) * - id (gallery's obscure id) * - select (VFS file obscure id) * - user (user id to browse galleries) * - requiregroup (group of users allowed to create/edit their own galleries) * * @var array */ protected $config = array( 'process' => 'slideshow', 'id' => 0, 'select' => 'random', 'user' => 0, 'requiredgroup' => 'vfsgallery' ); /** * vfs gallery * @var WBVFS_Gallery */ protected $gallery; /** * virtual file * @var WBVFS_File */ protected $file; /** * run * * run component * * @return array parameter list */ public function run() { $options = array(); $this->gallery = WBClass::create('WBVFS_Gallery', $options); switch ($this->config['process']) { // display actual slideshow case 'slideshow'; $this->slideshow(); break; case 'edit': case 'add': // both add and edit $values = array(); if ($this->config['process'] == 'add') { $this->config['id'] = 0; } else { if ($this->gallery->loadByObscureId($this->config['id'], $this->user->getId())) { $values = $this->gallery->get(); } else { $this->config['id'] = 0; } } if (!$this->isUserInGroup($this->config['requiredgroup'])) { $this->loadTemplates('anon'); return $this->config; } $this->tmpl->addGlobalVar('obscureid', $this->config['id']); $this->processForm('gallery', $values); $this->config['id'] = 0; break; case 'rm': if ($this->gallery->loadByObscureId($this->config['id'], $this->user->getId())) { $this->gallery->delete(); } $this->config['id'] = 0; $this->loadTemplates('list'); break; case 'adddir': if (!$this->gallery->loadByObscureId($this->config['id'], $this->user->getId())) { $this->loadTemplates('list'); break; } $param = array( 'user' => $this->user->getId() ); $this->file = WBClass::create('WBVFS_File', $param); $dir = $this->req->get('dir', 0); $files = $this->file->listDir($dir, true); $this->gallery->addFile($files); $this->slideshow(); break; case 'addfile': if (!$this->gallery->loadByObscureId($this->config['id'], $this->user->getId())) { $this->loadTemplates('list'); break; } $files = $this->req->get('file', array()); // add folder if no files wer selected if (empty($files)) { $param = array( 'user' => $this->user->getId() ); $this->file = WBClass::create('WBVFS_File', $param); $dir = $this->req->get('dir', 0); $files = $this->file->listDir($dir, true); } $this->gallery->addFile($files); $this->slideshow(); break; case 'rmfile': if (!$this->gallery->loadByObscureId($this->config['id'], $this->user->getId())) { $this->loadTemplates('list'); break; } $files = $this->req->get('file', array()); if (!is_array($files)) { $files = array($files); } $this->gallery->deleteFile($files); $this->slideshow(); break; // list user's galleries case 'list': default: $this->loadTemplates('list'); break; } if ($this->tmpl->exists('gallery_list_entry')) { $list = $this->gallery->getList(); $this->tmpl->addGlobalVar('gallery_total', count($list)); $this->tmpl->addRows('gallery_list_entry', $list); } return $this->config; } /** * display slideshow * * Auxilliary function to load and display media files in slideshow */ protected function slideshow() { try { $this->gallery->loadByObscureId($this->config['id']); $list = $this->gallery->getFiles(); // select the first $select = null; if (count($list)) { switch ($this->config['select']) { case 'random': $select = $list[rand(0, (count($list) -1))]; break; case 'first': $select = $list[0]; break; default: foreach ($list as $l) { // select id, but beware of obscure id $fid = substr($this->config['select'], 2); if ($fid == $l) { $select = $fid; break; } } break; } } $this->loadTemplates('slideshow'); $this->tmpl->addGlobalVars($this->gallery->get(), 'gallery_'); $this->tmpl->addGlobalVar('pager_user', $this->config['user']); $this->tmpl->addGlobalVar('pager_id', $this->config['id']); $this->tmpl->addGlobalVar('pager_total', count($list)); $this->tmpl->addGlobalVar('pager_selected', $select); $this->tmpl->addVar('list_entry', 'id', $list); } catch (Exception $e) { $this->loadTemplates('invalid'); } } /** * save gallery * * * * @param patForms $form * @param array $values * @return bool always false */ public function onGalleryValid($form, $values) { // add new gallery if (empty($this->config['id']) || $this->config['id'] === '0') { $this->gallery->add($values['title'], $values['description']); return true; } // change title and description $old = $this->gallery->get(); if ($old['title'] != $values['title']) { $this->gallery->setTitle($values['title']); } if ($old['description'] != $values['description']) { $this->gallery->setDescription($values['description']); } return true; } /** * location of form config * * Return sub directory where form element definitions are located * * @return string folder */ protected function getFormConfigDir() { return 'vfs'; } /** * receive output * * fetch output of this content component * * @return string */ public function getString() { return $this->tmpl->getParsedTemplate('snippet'); } } ?>