/** * Upload single file * * @param void * @return null */ function upload_single() { if ($this->request->isSubmitted()) { if (!File::canAdd($this->logged_user, $this->active_project)) { if ($this->request->isApiCall()) { $this->httpError(HTTP_ERR_FORBIDDEN, null, true, true); } else { die('error - upload not permitted'); } // if } // if $file_data = $this->request->post('file'); if (!is_array($file_data)) { $file_data = array('milestone_id' => $this->request->get('milestone_id'), 'visibility' => $this->active_project->getDefaultVisibility()); if (instance_of($this->active_category, 'Category')) { $file_data['parent_id'] = $this->active_category->getId(); } // if } // if $this->smarty->assign('file_data', $file_data); if ($this->request->isSubmitted()) { db_begin_work(); $this->active_file = new File(); $attached = attach_from_files($this->active_file, $this->logged_user); // Do we have an upload error? if (is_error($attached) || $attached != 1) { if ($this->request->isApiCall()) { $this->serveData(is_error($attached) ? $attached : new Error('0 files uploaded')); } else { die('error - nothing uploaded'); } // if } // if $this->active_file->setAttributes($file_data); if ($this->active_file->getName() == '') { $this->active_file->setName($this->active_file->pending_files[0]['name']); } // if $this->active_file->setRevision(1); $this->active_file->setProjectId($this->active_project->getId()); if (trim($this->active_file->getCreatedByName()) == '' || trim($this->active_file->getCreatedByEmail()) == '') { $this->active_file->setCreatedBy($this->logged_user); } // if $this->active_file->setState(STATE_VISIBLE); $save = $this->active_file->save(); if ($save && !is_error($save)) { if ($this->active_file->countRevisions() > 0) { $subscribers = array($this->logged_user->getId()); if (is_foreachable($this->request->post('notify_users'))) { $subscribers = array_merge($subscribers, $this->request->post('notify_users')); } else { $subscribers[] = $this->active_project->getLeaderId(); } // if if (!in_array($this->active_project->getLeaderId(), $subscribers)) { $subscribers[] = $this->active_project->getLeaderId(); } // if Subscriptions::subscribeUsers($subscribers, $this->active_file); db_commit(); $this->active_file->ready(); if ($this->request->isApiCall()) { $this->serveData($this->active_file, 'file'); } else { die('success'); // async } // if } else { if ($this->request->isApiCall()) { $this->httpError(HTTP_ERR_OPERATION_FAILED, null, true, true); } else { die('error - unable to attach file'); } // if } // if } else { if ($this->request->isApiCall()) { $this->serveData($save); } else { die('error - could not save file object'); // async } // if } // if } // if } else { if ($this->request->isApiCall()) { $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true); } else { die('error - request is not POST request'); // async } // if } // if }