示例#1
0
 /**
  * Save an attachment
  *
  * @return     void
  */
 public function saveTask()
 {
     // Incoming
     $pid = Request::getInt('pid', 0);
     if (!$pid) {
         $this->setError(Lang::txt('COM_TOOLS_CONTRIBUTE_NO_ID'));
         $this->displayTask($pid);
         return;
     }
     // get tool object
     $obj = new \Components\Tools\Tables\Tool($this->database);
     $this->_toolid = $obj->getToolIdFromResource($pid);
     // make sure user is authorized to go further
     if (!$this->_checkAccess($this->_toolid)) {
         App::abort(403, Lang::txt('COM_TOOLS_ALERTNOTAUTH'));
         return;
     }
     // Incoming file
     $file = Request::getVar('upload', '', 'files', 'array');
     if (!$file['name']) {
         $this->setError(Lang::txt('COM_TOOLS_CONTRIBUTE_NO_FILE'));
         $this->displayTask($pid);
         return;
     }
     // Make the filename safe
     $file['name'] = Filesystem::clean($file['name']);
     // Ensure file names fit.
     $ext = Filesystem::extension($file['name']);
     $file['name'] = str_replace(' ', '_', $file['name']);
     if (strlen($file['name']) > 230) {
         $file['name'] = substr($file['name'], 0, 230);
         $file['name'] .= '.' . $ext;
     }
     // Instantiate a new resource object
     $row = new \Components\Resources\Tables\Resource($this->database);
     if (!$row->bind($_POST)) {
         $this->setError($row->getError());
         $this->displayTask($pid);
         return;
     }
     $row->title = $row->title ? $row->title : $file['name'];
     $row->introtext = $row->title;
     $row->created = Date::toSql();
     $row->created_by = User::get('id');
     $row->published = 1;
     $row->publish_up = Date::toSql();
     $row->publish_down = '0000-00-00 00:00:00';
     $row->standalone = 0;
     $row->access = 0;
     $row->path = '';
     // make sure no path is specified just yet
     // Check content
     if (!$row->check()) {
         $this->setError($row->getError());
         $this->displayTask($pid);
         return;
     }
     // Store new content
     if (!$row->store()) {
         $this->setError($row->getError());
         $this->displayTask($pid);
         return;
     }
     if (!$row->id) {
         $row->id = $row->insertid();
     }
     // Build the path
     include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'html.php';
     $listdir = \Components\Resources\Tables\Html::build_path($row->created, $row->id, '');
     $path = $this->_buildUploadPath($listdir, '');
     // Make sure the upload path exist
     if (!is_dir($path)) {
         if (!Filesystem::makeDirectory($path)) {
             $this->setError(Lang::txt('COM_TOOLS_UNABLE_TO_CREATE_UPLOAD_PATH'));
             $this->displayTask($pid);
             return;
         }
     }
     // Perform the upload
     if (!Filesystem::upload($file['tmp_name'], $path . DS . $file['name'])) {
         $this->setError(Lang::txt('COM_TOOLS_ERROR_UPLOADING'));
     } else {
         // File was uploaded
         // Check the file type
         $row->type = $this->_getChildType($file['name']);
     }
     if (!$row->path) {
         $row->path = $listdir . DS . $file['name'];
     }
     if (substr($row->path, 0, 1) == DS) {
         $row->path = substr($row->path, 1, strlen($row->path));
     }
     // Store new content
     if (!$row->store()) {
         $this->setError($row->getError());
         $this->displayTask($pid);
         return;
     }
     // Instantiate a Resources Assoc object
     $assoc = new \Components\Resources\Tables\Assoc($this->database);
     // Get the last child in the ordering
     $order = $assoc->getLastOrder($pid);
     $order = $order ? $order : 0;
     // Increase the ordering - new items are always last
     $order = $order + 1;
     // Create new parent/child association
     $assoc->parent_id = $pid;
     $assoc->child_id = $row->id;
     $assoc->ordering = $order;
     $assoc->grouping = 0;
     if (!$assoc->check()) {
         $this->setError($assoc->getError());
     }
     if (!$assoc->store(true)) {
         $this->setError($assoc->getError());
     }
     $this->_rid = $pid;
     // Push through to the attachments view
     $this->displayTask($pid);
 }