* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBXinha_Dialog'); /** * Xinha Dialog Module * * * * @version 0.4.0 * @package WB * @subpackage base */ class WBXinha_Dialog_Vfsfile extends WBXinha_Dialog { /** * file * @var WBVFS_File */ protected $file; /** * explorer * @var WBVFS_Explorer */ protected $expl; /** * current working directory * @var array */ protected $cwd = array( 'position' => 'block', 'width' => '100', 'height' => '75' ); /** * Run dialog * * @param bool $submit * @return bool whehther dialog was successful and may be closed */ public function run($submit = true) { if (!$this->requireUser()) { return false; } $this->startVFS(); $dir = $this->req->get('dir', '0'); if ($dir === '0' || empty($dir)) { $dir = 0; } $query = $this->req->get('query', ''); $this->tmpl->addGlobalVar('find_query', $query); $this->cwd['position'] = $this->getAttribute('position', $this->cwd['position']); $this->cwd['width'] = $this->getStyle('width', 'px', $this->cwd['width']); $this->cwd['height'] = $this->getStyle('height', 'px', $this->cwd['height']); $selected = array(); if (!empty($this->value)) { $selected[] = $this->value; } if (!$submit) { if (!empty($this->value)) { $this->file = $this->file->loadByObscureId($this->value); if ($this->file->isOK()) { $dir = $this->file->getDirId(); } } return $this->listFiles($dir, $selected); } switch (strtolower($this->req->get('action'))) { case 'select': return $this->selectFiles($dir); break; case 'find': $this->value = ''; if (empty($query)) { return $this->selectFiles($dir); } $this->file = $this->file->loadByName($query); if (!$this->file->isOK()) { return $this->selectFiles($dir); } $selected[] = $this->file->getObscureId(); $dir = $this->file->getDirId(); break; default: case 'cd': break; } return $this->listFiles($dir, $selected); } /** * select files * * Collect selected files from request and insert in editor * * @param string $dir * @return bool always false */ protected function selectFiles($dir) { $selected = $this->req->get('file'); if (!is_array($selected) || count($selected) < 1) { $selected = array(); if (!empty($this->value)) { $selected[] = $this->value; } $this->listFiles($dir, $selected); return false; } $this->loadTemplates('insert'); $files = $this->file->listObscureId($selected); if (empty($files)) { $this->listFiles($dir, array()); return false; } $this->cwd['position'] = $this->req->get('position', $this->cwd['position']); $this->cwd['width'] = $this->req->get('width', $this->cwd['width']); $this->cwd['height'] = $this->req->get('height', $this->cwd['height']); $this->tmpl->addGLobalVars($this->cwd, 'cwd_'); $this->tmpl->addRows('file_list_entry', $files); $this->insert = $this->tmpl->getParsedTemplate('snippet'); return true; } /** * list files * * * @param string $dir * @param array $selected * @return bool always false */ protected function listFiles($dir, $selected = array()) { 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 = 0; } $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['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->loadTemplates('list'); $this->tmpl->addGLobalVars($this->cwd, 'cwd_'); $this->tmpl->addRows('file_list_entry', $files); $this->tmpl->addRows('dir_list_entry', $dirs); return false; } /** * start virtual file system * * Initialize explorer and file object */ protected function startVFS() { $sess = WBClass::create('patSession'); $param = array( 'user' => $this->user->getId() ); if ($sess->has('vfsfile_user')) { $param['user'] = $sess->get('vfsfile_user'); } $this->expl = WBClass::create('WBVFS_Explorer', $param); $this->file = WBClass::create('WBVFS_File', $param); // load default values from config $config = WBClass::create('WBConfig'); if (!$config->load('wxml/converter/wb/vfsfile', true)) { return; } $this->cwd['width'] = $config->get('defaultwidth', $this->cwd['width']); $this->cwd['height'] = $config->get('defaultheight', $this->cwd['height']); } }