/**
  * Save an entry
  *
  * @param      boolean $redirect Redirect after save?
  * @return     void
  */
 public function saveTask($redirect = true)
 {
     // [SECURITY] Check for request forgeries
     Request::checkToken();
     // Incoming
     $fields = Request::getVar('fields', array(), 'post', 'none', 2);
     $team = Request::getVar('team', '', 'post', 2, 'none');
     // Bind the incoming data to our mdoel
     $row = new Models\Api\Application($fields);
     // Validate and save the data
     if (!$row->store(true)) {
         $this->setError($row->getError());
         $this->editTask($row);
         return;
     }
     // parse incoming team
     $team = array_map('trim', explode(',', $team));
     // clean up team
     foreach ($team as $k => $t) {
         // handle usernames & emails
         if (!is_numeric($t)) {
             // handle emails
             if (strpos($t, '@')) {
                 // load profile by email
                 $profile = \Hubzero\User\Profile\Helper::find_by_email($t);
             } else {
                 // load profile by username
                 $profile = \Hubzero\User\Profile::getInstance($t);
             }
             // swap usernames for uidnumbers
             if ($profile) {
                 $team[$k] = $profile->get('uidNumber');
             } else {
                 unset($team[$k]);
             }
         }
     }
     // add creator if new
     // will only ever get added once
     $team[] = User::get('id');
     // get current team
     $currentTeam = $row->team()->lists('uidNumber');
     // remove members not included now
     foreach (array_diff($currentTeam, $team) as $uidNumber) {
         $member = $row->team($uidNumber);
         $member->delete();
     }
     // add each non-team member to team
     foreach (array_diff($team, $currentTeam) as $uidNumber) {
         if ($uidNumber < 1) {
             continue;
         }
         // new team member object
         $teamMember = new Models\Api\Application\Team\Member(array('uidNumber' => $uidNumber, 'application_id' => $row->get('id')));
         $teamMember->store();
     }
     // Are we redirecting?
     // This will happen if a user clicks the "save & close" button.
     if ($redirect) {
         // Set the redirect
         App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_DEVELOPER_APPLICATION_SAVED'));
         return;
     }
     // Display the edit form. This will happen if the user clicked
     // the "save" or "apply" button.
     $this->editTask($row);
 }
 /**
  * Save developer application details
  * 
  * @return  void
  */
 public function saveTask()
 {
     // CSRF check
     Request::checkToken();
     // get request vars
     $data = Request::getVar('application', array(), 'post', 2, 'none');
     $team = Request::getVar('team', array(), 'post', 2, 'none');
     // bind data to model
     $model = new Models\Api\Application($data);
     // must be logged in
     if (User::isGuest()) {
         $return = Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&task=edit&id=' . $data['id'], false, true);
         App::redirect(Route::url('index.php?option=com_users&view=login&return=' . base64_encode($return)));
         return;
     }
     // is the app available
     if ($model->isDeleted()) {
         App::redirect(Route::url('index.php?option=com_developer&controller=applications'), Lang::txt('COM_DEVELOPER_API_APPLICATION_DOES_NOT_EXIST'), 'warning');
         return;
     }
     // make sure its ours
     if (!$this->config->get('access-edit-application', 0) && (!$this->config->get('access-create-application', 0) && $data['id'] > 0)) {
         App::redirect(Route::url('index.php?option=com_developer&controller=applications'), Lang::txt('COM_DEVELOPER_API_APPLICATION_NOT_AUTHORIZED'), 'warning');
         return;
     }
     // attempt to save model
     if (!$model->store(true)) {
         $this->setError($model->getError());
         $this->editTask($model);
         return;
     }
     // parse incoming team
     $team = array_map('trim', explode(',', $team));
     // clean up team
     foreach ($team as $k => $t) {
         // handle usernames & emails
         if (!is_numeric($t)) {
             // handle emails
             if (strpos($t, '@')) {
                 // load profile by email
                 $profile = \Hubzero\User\Profile\Helper::find_by_email($t);
             } else {
                 // load profile by username
                 $profile = \Hubzero\User\Profile::getInstance($t);
             }
             // swap usernames for uidnumbers
             if ($profile) {
                 $team[$k] = $profile->get('uidNumber');
             } else {
                 unset($team[$k]);
             }
         }
     }
     // add creator if new
     // will only ever get added once
     $team[] = User::get('id');
     // get current team
     $currentTeam = $model->team()->lists('uidNumber');
     // add each non-team member to team
     foreach (array_diff($team, $currentTeam) as $uidNumber) {
         if ($uidNumber < 1) {
             continue;
         }
         // new team member object
         $teamMember = new Models\Api\Application\Team\Member(array('uidNumber' => $uidNumber, 'application_id' => $model->get('id')));
         $teamMember->store();
     }
     // Redirect back to the main listing with a success message
     App::redirect(Route::url($model->link()), Lang::txt('COM_DEVELOPER_API_APPLICATION_SAVED'), 'passed');
 }