/**
  * Set variables up before form is built based on participant ID from URL
  *
  * @return void
  */
 public function preProcess()
 {
     $config = CRM_Core_Config::singleton();
     $session = CRM_Core_Session::singleton();
     $this->_userContext = $session->readUserContext();
     $participant = $values = array();
     $this->_participant_id = CRM_Utils_Request::retrieve('pid', 'Positive', $this, FALSE, NULL, 'REQUEST');
     $params = array('id' => $this->_participant_id);
     $this->_participant = CRM_Event_BAO_Participant::getValues($params, $values, $participant);
     $this->_part_values = $values[$this->_participant_id];
     $this->set('values', $this->_part_values);
     //fetch Event by event_id, verify that this event can still be xferred/cancelled
     $this->_event_id = $this->_part_values['event_id'];
     $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}&noFullMsg=true");
     $this->_contact_id = $this->_part_values['participant_contact_id'];
     $this->assign('action', $this->_action);
     if ($this->_participant_id) {
         $this->assign('participantId', $this->_participant_id);
     }
     $event = array();
     $daoName = 'title';
     $this->_event_title = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
     $daoName = 'start_date';
     $this->_event_start_date = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
     list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contact_id);
     $this->_contact_name = $displayName;
     $this->_contact_email = $email;
     $details = array();
     $details = CRM_Event_BAO_Participant::participantDetails($this->_participant_id);
     $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'participant_role', 'id', 'name');
     $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participant_id, 'contribution_id', 'participant_id');
     $this->assign('contributionId', $contributionId);
     $query = "\n      SELECT cpst.name as status, cov.name as role, cp.fee_level, cp.fee_amount, cp.register_date, cp.status_id\n      FROM civicrm_participant cp\n      LEFT JOIN civicrm_participant_status_type cpst ON cpst.id = cp.status_id\n      LEFT JOIN civicrm_option_value cov ON cov.value = cp.role_id and cov.option_group_id = {$optionGroupId}\n      WHERE cp.id = {$this->_participant_id}";
     $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
     while ($dao->fetch()) {
         $details['status'] = $dao->status;
         $details['role'] = $dao->role;
         $details['fee_level'] = $dao->fee_level;
         $details['fee_amount'] = $dao->fee_amount;
         $details['register_date'] = $dao->register_date;
     }
     //verify participant status is still Registered
     if ($details['status'] != "Registered") {
         $status = "You are no longer registered for " . $this->_event_title;
         CRM_Core_Session::setStatus($status, ts('Event status error.'), 'alert');
         CRM_Utils_System::redirect($url);
     }
     $query = "select start_date as start, selfcancelxfer_time as time from civicrm_event where id = " . $this->_event_id;
     $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
     while ($dao->fetch()) {
         $time_limit = $dao->time;
         $start_date = $dao->start;
     }
     $start_time = new Datetime($start_date);
     $timenow = new Datetime();
     if (!empty($start_time) && $start_time < $timenow) {
         $status = ts("The event has been started, cannot transfer or cancel this event");
         $session->setStatus($status, ts('Oops.'), 'alert');
         CRM_Utils_System::redirect($url);
     }
     if (!empty($time_limit) && $time_limit > 0) {
         $interval = $timenow->diff($start_time);
         $days = $interval->format('%d');
         $hours = $interval->format('%h');
         if ($hours <= $time_limit && $days < 1) {
             $status = ts("Less than %1 hours to start time, cannot transfer or cancel this event", array(1 => $time_limit));
             $session->setStatus($status, ts('Oops.'), 'alert');
             CRM_Utils_System::redirect($url);
         }
     }
     $this->assign('details', $details);
     $this->selfsvcupdateUrl = CRM_Utils_System::url('civicrm/event/selfsvcupdate', "reset=1&id={$this->_participant_id}&id=0");
     $this->selfsvcupdateText = ts('Update');
     $this->selfsvcupdateButtonText = ts('Update');
     // Based on those ids retrieve event and verify it is eligible
     // for self update (event.start_date > today, event can be 'self_updated'
     // retrieve contact name and email, and let user verify his/her identity
 }
 /**
  * Get source values for transfer based on participant id in URL. Line items will
  * be transferred to this participant - at this point no transaction changes processed
  *
  * return @void
  */
 public function preProcess()
 {
     $config = CRM_Core_Config::singleton();
     $session = CRM_Core_Session::singleton();
     $this->_userContext = $session->readUserContext();
     $this->_from_participant_id = CRM_Utils_Request::retrieve('pid', 'Positive', $this, FALSE, NULL, 'REQUEST');
     $this->_userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this, FALSE, NULL, 'REQUEST');
     $params = array('id' => $this->_from_participant_id);
     $participant = $values = array();
     $this->_participant = CRM_Event_BAO_Participant::getValues($params, $values, $participant);
     $this->_part_values = $values[$this->_from_participant_id];
     $this->set('values', $this->_part_values);
     $this->_event_id = $this->_part_values['event_id'];
     $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
     $this->_from_contact_id = $this->_part_values['participant_contact_id'];
     $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($this->_from_contact_id, $this->_userChecksum);
     if (!$validUser && !CRM_Core_Permission::check('edit all events')) {
         CRM_Core_Error::statusBounce(ts('You do not have sufficient permission to transfer/cancel this participant.'), $url);
     }
     $this->assign('action', $this->_action);
     if ($this->_from_participant_id) {
         $this->assign('participantId', $this->_from_participant_id);
     }
     $event = array();
     $daoName = 'title';
     $this->_event_title = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
     $daoName = 'start_date';
     $this->_event_start_date = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
     list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_from_contact_id);
     $this->_contact_name = $displayName;
     $this->_contact_email = $email;
     $details = array();
     $details = CRM_Event_BAO_Participant::participantDetails($this->_from_participant_id);
     $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'participant_role', 'id', 'name');
     $query = "\n      SELECT cpst.name as status, cov.name as role, cp.fee_level, cp.fee_amount, cp.register_date, civicrm_event.start_date\n      FROM civicrm_participant cp\n      LEFT JOIN civicrm_participant_status_type cpst ON cpst.id = cp.status_id\n      LEFT JOIN civicrm_option_value cov ON cov.value = cp.role_id and cov.option_group_id = {$optionGroupId}\n      LEFT JOIN civicrm_event ON civicrm_event.id = cp.event_id\n      WHERE cp.id = {$this->_from_participant_id}";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         $details['status'] = $dao->status;
         $details['role'] = $dao->role;
         $details['fee_level'] = $dao->fee_level;
         $details['fee_amount'] = $dao->fee_amount;
         $details['register_date'] = $dao->register_date;
         $details['event_start_date'] = $dao->start_date;
     }
     $this->assign('details', $details);
     //This participant row will be cancelled.  Get line item(s) to cancel
     $this->selfsvctransferUrl = CRM_Utils_System::url('civicrm/event/selfsvcupdate', "reset=1&id={$this->_from_participant_id}&id=0");
     $this->selfsvctransferText = ts('Update');
     $this->selfsvctransferButtonText = ts('Update');
 }