Esempio n. 1
0
 /**
  * Method to save a record.
  *
  * @param     string     $key       The name of the primary key of the URL variable.
  * @param     string     $urlVar    The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  *
  * @return    boolean               True if successful, false otherwise.
  */
 public function save($key = 'id', $urlVar = null)
 {
     // Check for request forgeries.
     JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     // Initialise variables.
     $model = $this->getModel();
     $data = JRequest::getVar('jform', array(), 'post', 'array');
     $file_form = JRequest::getVar('jform', '', 'files', 'array');
     $context = $this->option . ".edit." . $this->context;
     $layout = JRequest::getVar('layout');
     $files = array();
     if (empty($urlVar)) {
         $urlVar = $key;
     }
     // Setup redirect links
     $record_id = JRequest::getInt($urlVar);
     $link_base = 'index.php?option=' . $this->option . '&view=';
     $link_list = $link_base . $this->view_list . $this->getRedirectToListAppend();
     $link_item = $link_base . $this->view_item . $this->getRedirectToItemAppend($record_id, $urlVar);
     // Get project id from directory if missing
     if ((!isset($data['project_id']) || empty($data['project_id'])) && isset($data['dir_id'])) {
         $data['project_id'] = PFrepoHelper::getProjectFromDir($data['dir_id']);
     }
     // Check edit id
     if (!$this->checkEditId($context, $record_id)) {
         // Somehow the person just went to the form and tried to save it. We don't allow that.
         $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $record_id));
         $this->setMessage($this->getError(), 'error');
         $this->setRedirect(JRoute::_($layout != 'modal' ? $link_item : $link_list, false));
         return false;
     }
     // Get file info
     $files = $this->getFormFiles($file_form);
     // Check for upload errors
     if (!$this->checkFileError($files, $record_id)) {
         $this->setRedirect(JRoute::_($layout != 'modal' ? $link_item : $link_list, false));
         return false;
     }
     // Upload file if we have any
     if (count($files) && !empty($files[0]['tmp_name'])) {
         $file = $files[0];
         if ($record_id) {
             // File extension must be the same as the original
             if (!$this->checkFileExtension($record_id, $file['name'])) {
                 $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_UPLOAD_ERROR_10'));
                 $this->setMessage($this->getError(), 'error');
                 $this->setRedirect(JRoute::_($layout != 'modal' ? $link_item : $link_list, false));
                 return false;
             }
         }
         // Upload the file
         $result = $model->upload($file, $data['dir_id'], false, $record_id);
         if (is_array($result)) {
             $data['file'] = $result;
         } else {
             $error = $model->getError();
             $this->setError($error);
             $this->setMessage($error, 'error');
             $this->setRedirect(JRoute::_($layout != 'modal' ? $link_item : $link_list, false));
             return false;
         }
     }
     // Inject file info into the form post data
     if (version_compare(JVERSION, '3.0.0', 'ge')) {
         $this->input->post->set('jform', $data);
     } else {
         JRequest::setVar('jform', $data, 'post');
     }
     // Store data
     return parent::save($key, $urlVar);
 }