Example #1
0
 /**
  * Process data
  *
  * @return  void
  */
 protected function _process()
 {
     // New project?
     $new = $this->model->exists() ? false : true;
     // Are we in setup?
     $setup = $new || $this->model->inSetup() ? true : false;
     // Incoming
     $private = Request::getInt('private', 1);
     // Save section
     switch ($this->section) {
         case 'describe':
         case 'info':
         case 'info_custom':
             // Incoming
             $name = trim(Request::getVar('name', '', 'post'));
             $title = trim(Request::getVar('title', '', 'post'));
             $name = preg_replace('/ /', '', $name);
             $name = strtolower($name);
             // Clean up title from any scripting
             $title = preg_replace('/\\s+/', ' ', $title);
             $title = $this->_txtClean($title);
             // Check incoming data
             if ($setup && $new && !$this->model->check($name, $this->model->get('id'))) {
                 $this->setError(Lang::txt('COM_PROJECTS_ERROR_NAME_INVALID_OR_EMPTY'));
                 return false;
             } elseif (!$title) {
                 $this->setError(Lang::txt('COM_PROJECTS_ERROR_TITLE_SHORT_OR_EMPTY'));
                 return false;
             }
             if ($this->model->exists()) {
                 $this->model->set('modified', Date::toSql());
                 $this->model->set('modified_by', User::get('id'));
             } else {
                 $this->model->set('alias', $name);
                 $this->model->set('created', Date::toSql());
                 $this->model->set('created_by_user', User::get('id'));
                 $this->model->set('owned_by_group', $this->_gid);
                 $this->model->set('owned_by_user', User::get('id'));
                 $this->model->set('private', $this->config->get('privacy', 1));
             }
             $this->model->set('title', \Hubzero\Utility\String::truncate($title, 250));
             $this->model->set('about', trim(Request::getVar('about', '', 'post', 'none', 2)));
             $this->model->set('type', Request::getInt('type', 1, 'post'));
             // save advanced permissions
             if (isset($_POST['private'])) {
                 $this->model->set('private', $private);
             }
             if ($setup && !$this->model->exists()) {
                 // Copy params from default project type
                 $objT = $this->model->table('Type');
                 $this->model->set('params', $objT->getParams($this->model->get('type')));
             }
             // Save changes
             if (!$this->model->store()) {
                 $this->setError($this->model->getError());
                 return false;
             }
             // Save custom description
             if ($this->section == 'info_custom') {
                 $newInfo = Request::getVar('description', array());
                 $projectID = $this->model->get('id');
                 $project = ProjectORM::one($this->model->get('id'));
                 $old = Description::collect($project->descriptions);
                 $formFields = array_merge($old, $newInfo);
                 $knownFields = Field::all()->rows()->toObject();
                 foreach ($knownFields as $kField) {
                     $existingField = Description::all()->whereEquals('project_id', $this->model->get('id'))->whereEquals('description_key', $kField->name)->limit(1)->row();
                     if ($existingField->id != NULL) {
                         $existingField->set('description_value', $formFields[$kField->name]);
                         $existingField->set('ordering', $kField->ordering);
                         $existingField->save();
                     } else {
                         // Create a new field
                         $newField = new Description();
                         $newField->set('description_key', $kField->name)->set('description_value', $formFields[$kField->name])->set('project_id', $this->model->get('id'))->set('ordering', $kField->ordering);
                         if (!$newField->save()) {
                             $this->setError($newField->getError());
                         }
                     }
                 }
             }
             // Save owners for new projects
             if ($new && $this->section != 'info_custom') {
                 $this->_identifier = $this->model->get('alias');
                 // Group owners
                 $objO = $this->model->table('Owner');
                 if ($this->_gid) {
                     // Only add the creator
                     // They'll choose if they want to sync the entire group or not in the next step
                     if (!$objO->saveOwners($this->model->get('id'), User::get('id'), User::get('id'), $this->_gid, 0, 1, 1, '', $split_group_roles = 0)) {
                         $this->setError(Lang::txt('COM_PROJECTS_ERROR_SAVING_AUTHORS') . ': ' . $objO->getError());
                         return false;
                     }
                     // Make sure project creator is manager
                     $objO->reassignRole($this->model->get('id'), $users = array(User::get('id')), 0, 1);
                 } elseif (!$objO->saveOwners($this->model->get('id'), User::get('id'), User::get('id'), $this->_gid, 1, 1, 1)) {
                     $this->setError(Lang::txt('COM_PROJECTS_ERROR_SAVING_AUTHORS') . ': ' . $objO->getError());
                     return false;
                 }
             }
             // Record activity
             $this->model->recordActivity(Lang::txt('COM_PROJECTS_PROJECT_INFO_UPDATED'));
             break;
         case 'team':
             if ($new) {
                 return false;
             }
             if ($this->model->groupOwner()) {
                 // Save group sync settings
                 $this->model->set('sync_group', Request::getInt('sync_group', 0, 'post'));
                 if (!$this->model->store()) {
                     $this->setError($this->model->getError());
                     return false;
                 }
                 // Are we syncing group membership?
                 if ($this->model->get('sync_group')) {
                     $objO = $this->model->table('Owner');
                     $objO->saveOwners($this->model->get('id'), User::get('id'), 0, $this->_gid, 0, 1, 1, '', $split_group_roles = 0);
                 }
             }
             // Save team
             $content = Event::trigger('projects.onProject', array($this->model, 'save', array('team')));
             if (isset($content[0]) && $this->next == $this->section) {
                 if (isset($content[0]['msg']) && !empty($content[0]['msg'])) {
                     $this->_setNotification($content[0]['msg']['message'], $content[0]['msg']['type']);
                 }
             }
             break;
         case 'settings':
             if ($new) {
                 return false;
             }
             // Save privacy
             if (isset($_POST['private'])) {
                 $this->model->set('private', $private);
                 // Save changes
                 if (!$this->model->store()) {
                     $this->setError($this->model->getError());
                     return false;
                 }
             }
             // Save params
             $incoming = Request::getVar('params', array());
             if (!empty($incoming)) {
                 foreach ($incoming as $key => $value) {
                     $this->model->saveParam($key, $value);
                     $this->model->params->set($key, $value);
                     // If grant information changed
                     if ($key == 'grant_status') {
                         // Meta data for comment
                         $meta = '<meta>' . Date::of('now')->toLocal('M d, Y') . ' - ' . User::get('name') . '</meta>';
                         $cbase = $this->model->get('admin_notes');
                         $cbase .= '<nb:sponsored>' . Lang::txt('COM_PROJECTS_PROJECT_MANAGER_GRANT_INFO_UPDATE') . $meta . '</nb:sponsored>';
                         $this->model->set('admin_notes', $cbase);
                         // Save admin notes
                         if (!$this->model->store()) {
                             $this->setError($this->model->getError());
                             return false;
                         }
                         $admingroup = $this->config->get('ginfo_group', '');
                         if (\Hubzero\User\Group::getInstance($admingroup)) {
                             $admins = Helpers\Html::getGroupMembers($admingroup);
                             // Send out email to admins
                             if (!empty($admins)) {
                                 Helpers\Html::sendHUBMessage($this->_option, $this->model, $admins, Lang::txt('COM_PROJECTS_EMAIL_ADMIN_REVIEWER_NOTIFICATION'), 'projects_new_project_admin', 'admin', Lang::txt('COM_PROJECTS_PROJECT_MANAGER_GRANT_INFO_UPDATE'), 'sponsored');
                             }
                         }
                     }
                 }
                 // Record activity
                 $this->model->recordActivity(Lang::txt('COM_PROJECTS_PROJECT_SETTINGS_UPDATED'));
             }
             break;
     }
 }