* @license PHP License * @package WB * @subpackage vfs */ WBClass::load('WBLog'); /** * Virtual File System: Gallery * * Galleries are collection of files * * @version 0.1.1 * @package WB * @subpackage vfs */ class WBVFS_Gallery extends WBStdClass { /** * table access * @var WBDatasource_Table */ protected $table; /** * user to act as * @var WBUser */ protected $user; /** * current gallery * @var array */ protected $gallery = array(); /** * primary key of user * @var string */ protected $uPrimary; /** * constructor * * Configure file utiltiy object * * @param array $parameter */ public function __construct($parameter = array()) { $this->table = WBClass::create('WBDatasource_Table'); $this->uPrimary = $this->table->getIdentifier('user'); // load user if (isset($parameter['user']) && !empty($parameter['user'])) { $this->user = WBClass::create('WBUser'); if ($this->user->load($parameter['user'])) { return; } } WBClass::load('WBUser'); $this->user = WBUser::getCurrent(); } /** * add new gallery * * Create new gallery record in database. Each gallery requires a title * (something human readable) and may have contain a more detailed description * also any number of files (actually, VFS file ids) * * Later on the gallery record may be loaded using it's id or obscure id * * @param string $title * @param string $description * @param array $files * @return string id */ public function add($title, $description = '', $files = array()) { $uid = $this->user->getId(); if (!$uid) { WBClass::load('WBException_Call'); throw new WBException_Call('Adding gallery requires a valid user id', 1, __CLASS__); } WBClass::load('WBDatasource_ObscureCode'); $obscure = WBDatasource_ObscureCode::mkRandObscure(2); $save = array( $this->uPrimary => $uid, 'title' => $title, 'description' => $description, 'obscure' => $obscure ); $id = $this->table->save('vfsgallery', '__new', $save); $this->gallery = $save; $this->gallery['obscureid'] = $obscure . $id; $this->gallery['id'] = $id; if (!empty($files)) { $this->addFile($files); } return $id; } /** * remove gallery * * Delete gallery record * * @param string $id */ public function delete($id = null) { if (empty($id)) { $id = $this->getId(); } $this->table->delete('vfsgallery', $id); $this->gallery = array(); } /** * list galleries * * @todo implement this properly * @return array */ public function getList() { $current = $this->gallery; $list = $this->table->get('vfsgallery'); foreach ($list as &$l) { $this->loadByData($l); $l = $this->gallery; $files = $this->getFiles(); $l['count'] = count($files); if ($l['count']) { $l['first'] = $files[0]; } } $this->loadByData($current); return $list; } /** * add files to gallery * * Add number of files to gallery. Each gallery may contain files * only once - duplicates are avoided automatically * * @param array $files */ public function addFile($files) { $gPrimary = $this->table->getIdentifier('vfsgallery'); $fPrimary = $this->table->getIdentifier('vfsfile'); // make sure the gallery belongs to user if ($this->user->getId() != $this->gallery[$this->uPrimary]) { return; } if (!is_array($files)) { $files = array($files); } $files = array_unique($files); if (empty($files)) { return; } $options = array(); $options['column'] = array($fPrimary); // skip files that are in gallery already $clause = array(); $clause[] = array( 'field' => $gPrimary, 'value' => $this->gallery['id'] ); $clause[] = array( 'field' => $fPrimary, 'relation' => 'in', 'value' => $files ); $in = $this->table->getColumn('vfsgalleryfile', null, $clause, $options); $files = array_diff($files, $in); $files = array_values($files); if (empty($files)) { return; } // make sure that user is owner of files $clause = array(); $clause[] = array( 'field' => $this->uPrimary, 'value' => $this->user->getId() ); $clause[] = array( 'field' => $fPrimary, 'relation' => 'in', 'value' => $files ); $files = $this->table->getColumn('vfsfile', null, $clause, $options); // finally add files to gallery $save = array(); foreach ($files as $f) { $save[] = array( $fPrimary => $f, $gPrimary => $this->gallery['id'] ); } $this->table->save('vfsgalleryfile', '__new', $save);; } /** * remove files from gallery * * * @param array $files */ public function deleteFile($files) { $id = $this->getId(); if (!is_array($files)) { $files = array($files); } $files = array_unique($files); if (empty($files)) { return; } // make sure the gallery belongs to user if ($this->user->getId() != $this->gallery[$this->uPrimary]) { return; } $gPrimary = $this->table->getIdentifier('vfsgallery'); $fPrimary = $this->table->getIdentifier('vfsfile'); // remove files $clause = array(); $clause[] = array( 'field' => $gPrimary, 'value' => $id ); $clause[1] = array( 'field' => $fPrimary, 'relation' => 'in', 'value' => $files ); $this->table->delete('vfsgalleryfile', null, null, $clause); } /** * get list of gallery files * * Fetch list of (VFS) file ids * * @param string $limit * @return array */ public function getFiles($limit = null) { $id = $this->getId(); $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier('vfsgallery'), 'value' => $id ); $key = $this->table->getIdentifier('vfsfile'); $options = array( 'column' => $key, ); $files = $this->table->getColumn('vfsgalleryfile', null, $clause, $options); if (empty($files)) { return array(); } // check for valid file ids (not deleted) $clause = array(); $clause[] = array( 'field' => $key, 'relation' => 'in', 'value' => $files ); if (!is_null($limit)) { $options['limit'] = intval($limit); } return $this->table->getColumn('vfsfile', null, $clause, $options); } /** * load gallery by obscure id * * Extract id, load gallery and verify obscure id * * @param string $obscure * @param string $owner user id of owner * @return bool true on success */ public function loadByObscureId($obscure, $owner = null) { // extract id and try to load file $id = substr($obscure, 2); if (!$this->loadById($id, $owner)) { return false; } // verify obscure string if ($this->gallery['obscureid'] == $obscure) { return true; } // flush loaded data $this->gallery = array(); return false; } /** * load gallery by id * * Use id and fetch record * * @param string $id * @return bool true on success */ public function loadById($id, $owner = null) { if (!empty($this->gallery) && $this->gallery['id'] == $id) { return true; } $this->gallery = array(); $list = $this->table->get('vfsgallery', $id); if (count($list) != 1) { return false; } // verify against owner $data = $list[0]; if ($owner !== null && $data[$this->uPrimary] != $owner) { return false; } $data['id'] = $id; return $this->loadByData($data); } /** * load gallery's data into buffer * * fill current gallery's data * * @param array $data * @return bool true on success */ protected function loadByData($data) { if (empty($data)) { $this->gallery = array(); return false; } $this->gallery = $data; $this->gallery['obscureid'] = $data['obscure'] . $data['id']; return true; } /** * find galleries that have file(s) * * Search for galleries using file id. Use either one file id * or list (array) of file ids. This method kind of dereferences * gallery-file-relation. * * @param array $files * @return array list of galleries */ public function getByFile($files) { if (!is_array($files)) { $files = array($files); } if (empty($files)) { return array(); } $key = $this->table->getIdentifier('vfsgallery'); $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier('vfsfile'), 'relation' => 'in', 'value' => $files ); $options = array( 'column' => array( $key ) ); $ids = $this->table->getColumn('vfsgalleryfile', null, $clause, $options); if (empty($ids)) { return array(); } $clause = array(); $clause[] = array( 'field' => $key, 'relation' => 'in', 'value' => $ids ); return $this->table->get('vfsgallery', null, null, $clause); } /** * get gallery information * * Simple getter that return recored as stored in database * * @return array */ public function get() { $this->getId(); return $this->gallery; } /** * select file using file id * * Auxilliary method to fetch (VFS) file object using file id. * * @param string $id * @return WBVFS_File */ public function selectFileById( $id ) { $this->getId(); $file = WBClass::create('WBVFS_File')->loadById( $id ); return $file; } /** * get gallery's id * * Check loaded gallery and return id. Please, note that a specific * gallery has to be loaded first. * * @throws WBException_Call * @return string */ public function getId() { if (empty($this->gallery)) { WBClass::load('WBException_Call'); throw new WBException_Call('There is no gallery to add files, load or add gallery first', 2, __CLASS__); } return $this->gallery['id']; } /** * get gallery's obscure id * * The obscure ids are are meant to hide the actual id. * * @uses getId() * @return string */ public function getObscureId() { $id = $this->getId(); return $this->gallery['obscure'] . $id; } /** * set gallery's title * * Sort of gallery renaming * * @param string $txt */ public function setTitle($txt) { $data = array( 'title' => $txt ); return $this->set($data); } /** * set gerlliery's description * * Save gallery's descritpion text * * @param string $txt */ public function setDescription($txt) { $data = array( 'description' => $txt ); return $this->set($data); } /** * update gallery record * * Save any fields in gallery record * * @param array $data */ protected function set($data) { $id = $this->getId(); $this->table->save('vfsgallery', $id, $data); $this->gallery = array_merge($this->gallery, $data); } } ?>