Example #1
0
 /**
  * Save data in directory.
  * @throws SystemException 'ERR_NO_DATA'
  * @throws SystemException 'ERR_BAD_PID'
  */
 protected function saveDir()
 {
     $transactionStarted = $this->dbh->beginTransaction();
     try {
         if (!isset($_POST[$this->getTableName()]) || !isset($_POST[$this->getTableName()][$this->getPK()]) || !isset($_POST[$this->getTableName()]['upl_title']) || !isset($_POST[$this->getTableName()]['upl_pid'])) {
             throw new SystemException('ERR_NO_DATA');
         }
         $data = $_POST[$this->getTableName()];
         if (!$data['upl_pid']) {
             throw new SystemException('ERR_BAD_PID');
         }
         // получаем instance IFileRepository
         $repository = $this->repoinfo->getRepositoryInstanceById($data['upl_pid']);
         $mode = empty($data[$this->getPK()]) ? QAL::INSERT : QAL::UPDATE;
         if ($mode == QAL::INSERT) {
             $parentPath = $this->dbh->getScalar($this->getTableName(), ['upl_path'], ['upl_id' => $data['upl_pid']]);
             if (!$parentPath) {
                 throw new SystemException('ERR_BAD_PID');
             }
             unset($data[$this->getPK()]);
             $data['upl_name'] = $data['upl_filename'] = Translit::asURLSegment($data['upl_title']);
             $data['upl_mime_type'] = 'unknown/mime-type';
             $data['upl_internal_type'] = FileRepoInfo::META_TYPE_FOLDER;
             $data['upl_childs_count'] = 0;
             $data['upl_publication_date'] = date('Y-m-d H:i:s');
             $data['upl_path'] = $parentPath . (substr($parentPath, -1) != '/' ? '/' : '') . $data['upl_filename'];
             $where = false;
             $repository->createDir($data['upl_path']);
         } else {
             $where = ['upl_id' => $data['upl_id']];
             /*$currentUplPath = simplifyDBResult($this->dbh->select($this->getTableName(), array('upl_path'), array('upl_id' => $data['upl_id'])), 'upl_path', true);
               $data['upl_name'] = $data['upl_filename'] = Translit::asURLSegment($data['upl_title']);
               if($currentUplPath != $data['upl_path']){
                   rename($currentUplPath, $data['upl_path']);
               }*/
         }
         $result = $this->dbh->modify($mode, $this->getTableName(), $data, $where);
         $transactionStarted = !$this->dbh->commit();
         $uplID = is_int($result) ? $result : (int) $_POST[$this->getTableName()][$this->getPK()];
         $args = [$uplID, date('Y-m-d H:i:s')];
         $this->dbh->call('proc_update_dir_date', $args);
         $b = new JSONCustomBuilder();
         $b->setProperties(['data' => $uplID, 'result' => true, 'mode' => is_int($result) ? 'insert' : 'update']);
         $this->setBuilder($b);
     } catch (SystemException $e) {
         if ($transactionStarted) {
             $this->dbh->rollback();
         }
         throw $e;
     }
 }
 protected function prepare()
 {
     parent::prepare();
     if (in_array($this->getState(), ['add', 'edit'])) {
         $fd = $this->getDataDescription()->getFieldDescriptionByName('upl_id');
         $fd->setType(FieldDescription::FIELD_TYPE_INT);
         if ($this->getState() == 'edit' && ($uplPath = $this->dbh->getScalar('share_uploads', 'upl_path', ['upl_id' => $this->getData()->getFieldByName('upl_id')->getRowData(0)]))) {
             $fd->setProperty('upl_path', $uplPath);
             $info = new FileRepoInfo();
             $fileInfo = $info->analyze($uplPath);
             $fd->setProperty('media_type', $fileInfo->type);
         }
         $f = $this->getData()->getFieldByName($this->getParam('pk'));
         $f->setData($this->getParam('linkedID'), true);
         $f = $this->getData()->getFieldByName('session_id');
         $f->setData(session_id(), true);
     }
 }