Example #1
0
 /**
  * Execute the navigator and return the exit code of the application.
  *
  * @return int
  * @throws \Exception
  */
 public function render()
 {
     if (!$this->_view) {
         throw new \Exception('No view provided, please set one first!');
     }
     if (!($this->canupload || $this->canaccess)) {
         return \View::ERROR_ACCESSDENIED;
     }
     $resolved = \Core\resolve_link($this->_baseurl);
     // Tack on the system-defined uri-based options.
     $resolved .= (strpos($resolved, '?') === false ? '?' : '&') . 'controls=' . ($this->usecontrols ? '1' : '0') . '&uploader=' . ($this->useuploader ? '1' : '0');
     // This is meant to be called from most of the change directory methods, so it needs to drop that argument.
     $resolvedwmode = $resolved . '&mode=' . $this->_mode;
     // Get a list of the view modes along with current directories and titles.
     $viewmodes = array(['mode' => 'icon', 'title' => 'View as Icons', 'icon' => 'th-large', 'link' => $resolved . '&dir=' . urlencode($this->_cwd) . '&mode=icon'], ['mode' => 'list', 'title' => 'View as List', 'icon' => 'th-list', 'link' => $resolved . '&dir=' . urlencode($this->_cwd) . '&mode=list']);
     // This will create a navigatable tree of directory listings for the user.
     //$tree = explode('/', substr($dirname, strlen($basedirname)));
     $tree = explode('/', $this->_cwd);
     $treestack = '';
     foreach ($tree as $k => $v) {
         if (!trim($v)) {
             unset($tree[$k]);
         } else {
             $treestack .= '/' . $v;
             $tree[$k] = array('name' => $v, 'stack' => $treestack, 'href' => $resolvedwmode . '&dir=' . urlencode($treestack));
         }
     }
     // The base directory, because it may not be in /public necessarily.
     $base = Filestore\Factory::Directory($this->_basedir);
     $dir = Filestore\Factory::Directory($this->_basedir . $this->_cwd);
     // Allow automatic creation of the root directory.
     if ($this->_cwd == '' && !$dir->exists()) {
         $dir->mkdir();
     }
     if (!$dir->exists()) {
         return \View::ERROR_NOTFOUND;
     }
     $dirlen = strlen($dir->getPath());
     $baselen = strlen($base->getPath());
     $directories = array();
     $files = array();
     foreach ($dir->ls() as $file) {
         if ($file instanceof Filestore\Directory) {
             // Give me a count of children in that directory.  I need to do the logic custom here because I only want directories and images.
             $count = 0;
             foreach ($file->ls() as $subfile) {
                 if ($file instanceof Filestore\Directory) {
                     $count++;
                 } elseif ($file instanceof Filestore\File && Filestore\check_file_mimetype($this->_accept, $file->getMimetype(), $file->getExtension()) == '') {
                     $count++;
                 }
             }
             $directories[$file->getBasename()] = array('object' => $file, 'name' => $file->getBasename(), 'browsename' => substr($file->getPath(), $dirlen), 'children' => $count, 'href' => $resolvedwmode . '&dir=' . urlencode(substr($file->getPath(), $baselen)));
         } elseif ($file instanceof Filestore\File) {
             // I only want images
             if (Filestore\check_file_mimetype($this->_accept, $file->getMimetype(), $file->getExtension()) != '') {
                 continue;
             }
             $files[$file->getBaseFilename()] = array('object' => $file, 'name' => $file->getBaseFilename(), 'browsename' => substr($file->getFilename(), $baselen), 'selectname' => $file->getURL(), 'corename' => $file->getFilename(false));
         }
     }
     // Sorting would be nice!
     ksort($directories, SORT_STRING | SORT_FLAG_CASE);
     ksort($files, SORT_STRING | SORT_FLAG_CASE);
     // If it's a nested directory, provide a link back to the parent.
     if ($this->_cwd == '') {
         $uplink = null;
     } elseif ($this->_cwd == '/') {
         $uplink = null;
     } else {
         $uplink = $resolvedwmode . '&dir=' . urlencode(dirname(substr($dir->getPath(), $baselen)));
     }
     // Only certain people are allowed the rights to upload here.
     if ($this->canupload && $this->useuploader) {
         $uploadform = new \Form();
         //$uploadform->set('action', \Core\resolve_link('/mediamanagernavigator/upload'));
         $uploadform->addElement('multifile', array('basedir' => $this->_basedir . $this->_cwd, 'title' => 'Upload Files', 'name' => 'files', 'accept' => $this->_accept));
         //$uploadform->addElement('submit', array('value' => 'Upload Files'));
         $uploadform->clearFromSession();
     } else {
         $uploadform = false;
     }
     if ($this->canupload && $this->usecontrols) {
         $this->_view->addControl(['link' => '#', 'title' => 'Create Directory', 'icon' => 'folder-close', 'class' => 'directory-create']);
     }
     if ($this->usecontrols) {
         foreach ($viewmodes as $viewmode) {
             if ($viewmode['mode'] == $this->_mode) {
                 continue;
             }
             $this->_view->addControl($viewmode);
         }
     }
     switch ($this->_mode) {
         case 'list':
             $this->_view->templatename = 'pages/mediamanagernavigator/index/list.tpl';
             break;
         default:
             $this->_view->templatename = 'pages/mediamanagernavigator/index/icons.tpl';
     }
     $this->_view->assign('directories', array_values($directories));
     $this->_view->assign('files', array_values($files));
     $this->_view->assign('location_tree', $tree);
     $this->_view->assign('location', $treestack);
     $this->_view->assign('uploadform', $uploadform);
     $this->_view->assign('baseurl', $resolvedwmode);
     $this->_view->assign('mode', $this->_mode);
     $this->_view->assign('uplink', $uplink);
     $this->_view->assign('canupload', $this->canupload);
     $this->_view->assign('usecontrols', $this->usecontrols);
     return \View::ERROR_NOERROR;
 }