* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBAjax' , 'WBException' , 'WBException_Datasource'); /** * AJAX implementation: VFS * * GUI helper for VFS content component * * @version 0.3.0 * @package WB * @subpackage base */ class WBAjax_VFS extends WBAjax { /** * file * @var WBVFS_File */ protected $file; /** * explorer * @var WBVFS_Explorer */ protected $expl; /** * current working directory * @var array */ protected $cwd = array(); /** * second construcotr * * Called by constructer of super class. * * @see include/WB/WBAjax#init() */ protected function init() { $params = array(); if ($this->sess->has('vfsfile_user')) { $params['user'] = $this->sess->get('vfsfile_user'); } else { WBClass::load('WBUser'); $user = WBUser::getCurrent(); $params['user'] = $user->getId(); } if ($this->sess->has('vfsfile_chroot')) { $params['chroot'] = $this->sess->get('vfsfile_chroot'); } $this->file = WBClass::create('WBVFS_File', $params); $this->file->switchTranslation(false); $this->expl = WBClass::create('WBVFS_Explorer', $params); } /** * select filles respective folders * * Display list of user's VFS tree and allow to select files / folder(s) * * @return string */ public function select() { $dir = $this->req->get('dir', -1); $ids = $this->req->get('id', ''); return $this->doSelect($dir, $ids); } /** * Find folder by file name * * Search media files for name, load folder of media file, * select item */ public function find() { $query = $this->req->get('query', ''); $dir = $this->req->get('dir', -1); $ids = $this->req->get('id', ''); $this->tmpl->addGlobalVar('query', $query); $this->file = $this->file->loadByName($query); if ($this->file->isOK()) { $id = $this->file->getObscureId(); $dir = $this->file->getDirId(); } return $this->doSelect($dir, $id); } /** * Select Files or Folders * * Display list of user's VFS tree and allow to select files / folder(s) * * @param string $dir id * @param array list of selected files * @return string */ private function doSelect($dir, $selected = array()) { if (empty($selected)) { $selected = array(); } if (!is_array($selected)) { $selected = array($selected); } $dir = intval($dir); $type = $this->req->get('type', 'any'); // add current working directory if (0 > $dir) { WBClass::load('WBException_Datasource'); try { $this->cwd = array_merge($this->cwd, $this->expl->getDir($dir)); } catch (WBException_Datasource $e) { if ($e->getCode() != 'WBDatasource_Tree.1') { throw $e; } $dir = null; } } $dirs = $this->expl->lsDir($dir); $files = $this->file->listDir($dir); // preselect files foreach ($files as &$f) { if (in_array($f['obscureid'], $selected)) { $f['selected'] = 'yes'; } } // current working directory $this->cwd['type'] = $type; $this->cwd['parent'] = $this->expl->getParentDirId($dir); $this->cwd['path'] = $this->expl->getPath($dir, true); $this->cwd['file_count'] = count($files); $this->cwd['dir_count'] = count($dirs); $this->tmpl->addGlobalVar('PROCESSOR', $this->req->get('processor', 'WB.Ajax.VFS')); $this->tmpl->addGlobalVars($this->cwd, 'CWD_'); $this->tmpl->addGlobalVar('dir', $dir); switch (strtolower($type)) { case 'file': $this->loadTemplates('ajax/select/file'); break; case 'dir': $this->loadTemplates('ajax/select/dir'); break; case 'any': default: $this->loadTemplates('ajax/select/any'); break; } if ($this->tmpl->exists('dir_list_entry')) { $this->tmpl->addRows('dir_list_entry', $dirs); } if ($this->tmpl->exists('file_list_entry')) { // preselect files foreach ($files as &$f) { if (in_array($f['obscureid'], $selected)) { $f['checked'] = 'checked="checked"'; } } $this->tmpl->addRows('file_list_entry', $files); } return $this->tmpl->getParsedTemplate('snippet'); } /** * move files respective folders * * Move to anothger folder in user's VFS tree * * @return string */ public function move() { $dir = $this->req->get('dir', 0); $type = $this->req->get('type', 'file'); $id = $this->req->get('id', 0); switch (strtolower($type)) { case 'dir': WBClass::load('WBException_Datasource'); try { $src = $this->expl->getDir($id); } catch (WBException_Datasource $e) { if ($e->getCode() != 'WBDatasource_Tree.1') { throw $e; } $id = 0; } if ($dir < 0) { $dir = 0; } $this->loadTemplates('toolbox/move/dir'); $this->tmpl->addGlobalVar('id', $id); $this->tmpl->addGlobalVar('src', $id); $this->tmpl->addGlobalVar('name', $src['name']); break; case 'file': $this->file = $this->file->loadByObscureId($id); // verify file if (!$this->file->isOK()) { $this->loadTemplates('toolbox/move/__nofile'); $this->tmpl->addGlobalVar('path', $this->req->get('path', '')); $this->tmpl->addGlobalVar('content_path', $this->req->get('contentpath', '')); return $this->tmpl->getParsedTemplate('snippet'); } // use directory of file if ($dir < 0) { //$dir = $this->file->getDirId(); $dir = 0; } $this->loadTemplates('toolbox/move/file'); $this->tmpl->addGlobalVar('id', $this->file->getId()); $this->tmpl->addGlobalVar('obscureid', $id); $this->tmpl->addGlobalVar('name', $this->file->getName()); $this->tmpl->addGlobalVar('description', $this->file->getDescription()); break; } // verify dir $cwd = array(); if ($dir > 0) { WBClass::load('WBException_Datasource'); try { $cwd = $this->expl->getDir($dir); } catch (WBException_Datasource $e) { if ($e->getCode() != 'WBDatasource_Tree.1') { throw $e; } $dir = 0; } } $this->tmpl->addGlobalVar('dir', $dir); $dirs = $this->expl->lsDir($dir); $this->tmpl->addRows('dir_list_entry', $dirs); $cwd['dir_count'] = count($dirs); $cwd['parent'] = $this->expl->getParentDirId($dir); $cwd['path'] = $this->expl->getPath($dir, true); $this->tmpl->addGlobalVars($cwd, 'CWD_'); $this->tmpl->addGlobalVar('path', $this->req->get('path', '')); $this->tmpl->addGlobalVar('content_path', $this->req->get('contentpath', '')); return $this->tmpl->getParsedTemplate('snippet'); } /** * Initlialize reimport of media file * */ public function reimport() { $obscure = $this->req->get('obscureid', 0); $this->file = $this->file->loadByObscureId($obscure); if ($this->file->isOK()) { $this->file->reImport(); } return 'OK'; } /** * render file toolbox * * Load file by obscure id. Display toolbox according to mime-type * * @param string $oid file's obscure id * @return HTML string of toolbox */ public function edit() { $obscure = $this->req->get('obscureid', 0); $this->file = $this->file->loadByObscureId($obscure); if (!$this->file->isOK()) { $this->loadTemplates('toolbox/edit/__nofile'); $this->tmpl->addGlobalVar('path', $this->req->get('path', '')); $this->tmpl->addGlobalVar('content_path', $this->req->get('contentpath', '')); return $this->tmpl->getParsedTemplate('snippet'); } // select toolbox by mime type $this->loadTemplates('toolbox/edit/' . $this->file->getMime(WBVFS_File::MIME_MAJOR)); if (!$this->tmpl->exists('snippet')) { $this->loadTemplates('toolbox/edit/__default'); } $this->tmpl->addGlobalVar('id', $this->file->getId()); $this->tmpl->addGlobalVar('obscureid', $obscure); $this->tmpl->addGlobalVar('name', $this->file->getName()); $this->tmpl->addGlobalVar('description', $this->file->getDescription()); $this->tmpl->addGlobalVar('path', $this->req->get('path', '')); $this->tmpl->addGlobalVar('content_path', $this->req->get('contentpath', '')); return $this->tmpl->getParsedTemplate('snippet'); } /** * edit element * * Rename file or folder and alter description. See whether element is editable * and id/obscureid is valid and display form. This method gets called from * edit-in-place element. * * @return string */ public function editElement() { $element = $this->req->get('element', ''); $obscure = $this->req->get('obscureid', 0); $data = array(); switch ($element) { case 'dir': $data = $this->expl->getDir($obscure); if (empty($data['name'])) { return 'error: dir'; } break; case 'name': case 'description': $this->file = $this->file->loadByObscureId($obscure); if (!$this->file->isOK()) { return 'error: file'; } $data['name'] = $this->file->getName(); $data['description'] = $this->file->getDescription(); break; default: return $this->showElement($obscure, $element, $data); break; } return $this->showElement($obscure, $element, $data, 'toolbox/editInPlace'); } /** * save element * * Process form and save either file name, file's description or folder's name. * When done, display altered element with click-to-edit functionality * * @return string */ public function saveElement() { $element = $this->req->get('element', ''); $obscure = $this->req->get('obscureid', 0); $data = array(); $value = $this->req->get('value', ''); switch ($element) { case 'dir': if ($element == 'name' && empty($value)) { $data = $this->expl->getDir($obscure); return $this->showElement($obscure, $element, $data); } $this->expl->renameDir($obscure, $value); $data = $this->expl->getDir($obscure); break; case 'name': case 'description': $this->file = $this->file->loadByObscureId($obscure); if (!$this->file->isOK()) { return 'error file'; } // don't allow to save empty file names if ($element == 'name' && empty($value)) { $data['name'] = $this->file->getName(); $data['description'] = $this->file->getDescription(); return $this->showElement($obscure, $element, $data); } $data = array( $element => $value ); $this->file->save($data); $data['name'] = $this->file->getName(); $data['description'] = $this->file->getDescription(); break; default: break; } return $this->showElement($obscure, $element, $data); } /** * cancel edit element * * Discard changes and display element as is including edit-in-place functionality * * @return string */ public function cancelElement() { $element = $this->req->get('element', ''); $obscure = $this->req->get('obscureid', 0); $data = array(); switch ($element) { case 'dir': $data = $this->expl->getDir($obscure); if (empty($data['name'])) { return 'error: dir'; } break; default: case 'name': case 'description': $this->file = $this->file->loadByObscureId($obscure); if (!$this->file->isOK()) { return 'error file'; } $data['name'] = $this->file->getName(); $data['description'] = $this->file->getDescription(); break; } return $this->showElement($obscure, $element, $data); } /** * show element * * Auxilliary method to load and fill template to show either edit-in-place * element waiting to be activated or edit-in-place form. * * @param string $obscure * @param string $element * @param array $data * @param string $tmpl * @return string */ private function showElement($obscure, $element, $data, $tmpl = 'toolbox/editInPlaceDone') { $this->loadTemplates($tmpl); $this->tmpl->addGlobalVars($data); $this->tmpl->addGlobalVar('element', $element); $this->tmpl->addGlobalVar('obscureid', $obscure); $this->tmpl->addGlobalVar('content_path', $this->req->get('content_path', '')); $path = $this->req->get('path'. ''); $out = $this->tmpl->getParsedTemplate('snippet'); WBClass::load('WBString'); return WBString::replaceSuperPlaceholders($out, $path); } /** * Delete VFS File * * * @param string $obscure * @return string */ public function delete() { $obscure = $this->req->get('obscureid', 0); $this->file = $this->file->loadByObscureId($obscure); if (!$this->file->isOK()) { return 'error file'; } if($this->file->delete()) { return 'OK'; } return 'error delete'; } /** * Undelete VFS File * * @param string $obscure * @return string */ public function undelete() { $obscure = $this->req->get('obscureid', 0); $this->file = $this->file->loadByObscureId($obscure, true); if (!$this->file->isOK()) { return 'error file'; } if($this->file->undelete()) { return 'OK'; } return 'error undelete'; } }