Example #1
0
 /**
  * called from ajax.
  */
 public function importAction()
 {
     $this->requireAdminPrivileges();
     // This is necessary in order to avoid session lock and being able to run two
     // ajax requests simultaneously
     session_write_close();
     $this->nfilesprocessed = 0;
     $this->disableLayout();
     $this->disableView();
     $this->assetstores = $this->Assetstore->getAll();
     $form = $this->Form->Import->createImportForm($this->assetstores);
     if ($this->getRequest()->isPost() && !$form->isValid($_POST)) {
         echo json_encode(array('error' => $this->t('The form is invalid. Missing values.')));
         return false;
     }
     // If we just validate the form we return
     if ($this->getRequest()->getPost('validate')) {
         echo json_encode(array('stage' => 'validate'));
         return false;
     } elseif ($this->getRequest()->getPost('initialize')) {
         // Count the total number of files in the directory and return it
         $this->ntotalfiles = $this->_recursiveCountFiles($form->inputdirectory->getValue());
         echo json_encode(array('stage' => 'initialize', 'totalfiles' => $this->ntotalfiles));
         return false;
     } elseif ($this->getRequest()->isPost() && $form->isValid($_POST)) {
         $this->ntotalfiles = $this->getRequest()->getPost('totalfiles');
         // Parse the directory
         $pathName = $form->inputdirectory->getValue();
         $currentdirid = $form->importFolder->getValue();
         // we just start with te initial dir
         $currentdir = new FolderDao();
         $currentdir->setFolderId($currentdirid);
         // Set the file locations used to handle the async requests
         $this->progressfile = $this->getTempDirectory() . '/importprogress_' . $form->uploadid->getValue();
         $this->stopfile = $this->getTempDirectory() . '/importstop_' . $form->uploadid->getValue();
         $this->assetstoreid = $form->assetstore->getValue();
         $this->importemptydirectories = $form->importemptydirectories->getValue();
         try {
             if ($this->_recursiveParseDirectory($pathName, $currentdir)) {
                 echo json_encode(array('message' => $this->t('Import successful.')));
             } else {
                 echo json_encode(array('error' => $this->t('Problem occured while importing. ' . 'Check the log files or contact an ' . 'administrator.')));
             }
         } catch (Exception $e) {
             echo json_encode(array('error' => $this->t($e->getMessage())));
         }
         // Remove the temp and stop files
         UtilityComponent::safedelete($this->progressfile);
         UtilityComponent::safedelete($this->stopfile);
         return true;
     }
     echo json_encode(array('error' => $this->t('The request should be a post.')));
     return false;
 }