Skip to content

Instantly share code, notes, and snippets.

@Edzelopez
Created February 16, 2018 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Edzelopez/34df7a9254668579b1011900653f5c2c to your computer and use it in GitHub Desktop.
Save Edzelopez/34df7a9254668579b1011900653f5c2c to your computer and use it in GitHub Desktop.
<?php
/**
* Form controller class
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
*/
class CRM_Afabc_Form_SocialWorker extends CRM_Core_Form {
public $_cid;
public $_pid;
public $_swid;
public $_event;
public $_eid;
public function preProcess() {
$this->_pid = (int) CRM_Utils_Request::retrieve('pid', 'Positive');
$this->_swid = (int) CRM_Utils_Request::retrieve('swid', 'Positive');
$this->_cid = CRM_Core_DAO::singleValueQuery("SELECT c.id FROM civicrm_contact c INNER JOIN civicrm_participant p ON p.contact_id = c.id WHERE p.id = %1", array(1 => array($this->_pid, "Integer")));
$this->_event = CRM_Core_DAO::singleValueQuery("SELECT e.title FROM civicrm_event e INNER JOIN civicrm_participant p ON p.event_id = e.id WHERE p.id = %1", array(1 => array($this->_pid, "Integer")));
$this->_eid = CRM_Core_DAO::singleValueQuery("SELECT e.id FROM civicrm_event e INNER JOIN civicrm_participant p ON p.event_id = e.id WHERE p.id = %1", array(1 => array($this->_pid, "Integer")));
}
public function buildQuickForm() {
$status = CRM_Core_DAO::singleValueQuery("SELECT s.name FROM civicrm_participant p INNER JOIN civicrm_participant_status_type s ON s.id = p.status_id WHERE p.id = %1",
array(1 => array($this->_pid, "Integer")));
$name = CRM_Contact_BAO_Contact::displayName($this->_cid);
$this->addYesNo('participant_confirm', ts("Is {$name} a valid participant registration?"), FALSE, TRUE);
$this->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Confirm'),
'isDefault' => TRUE,
),
));
$this->assign('contactName', $name);
$this->assign('event', $this->_event);
$this->assign('status', $status);
$this->assign('elementNames', $this->getRenderableElementNames());
if ($status != "Confirmed") {
CRM_Utils_System::setTitle("Confirm registration for {$name}");
}
else {
CRM_Utils_System::setTitle("Registration Confirmed");
}
parent::buildQuickForm();
}
public function postProcess() {
$values = $this->controller->exportValues($this->_name);
$status = CRM_Event_PseudoConstant::participantStatus();
$updatedStatus = "";
if ($values['participant_confirm']) {
$updatedStatus = array_search("Confirmed", $status);
$activityType = "Social Worker Approval";
}
else {
$updatedStatus = array_search("Cancelled", $status);
$activityType = "Social Worker Disapproval";
}
if ($updatedStatus) {
try {
civicrm_api3("Participant", "create", array(
"id" => $this->_pid,
"contact_id" => $this->_cid,
"event_id" => $this->_eid,
"status_id" => $updatedStatus,
));
if ($this->_swid && $this->_cid) {
// Create the activity.
$activityParams = array(
"activity_type_id" => $activityType,
"source_contact_id" => $this->_swid,
"target_contact_id" => $this->_cid,
"subject" => ts("Registration confirmed"),
"description" => ts("Registration for $this->_event confirmed by social worker."),
"status_id" => "Completed",
);
civicrm_api3("Activity", "create", $activityParams);
}
CRM_Core_Session::setStatus(ts('Participant status updated successfully!'));
}
catch (CiviCRM_API3_Exception $e) {
CRM_Core_Error::debug( '$e', $e );
throw new CRM_Core_Exception('Error while updating participant status. Please contact the site administrator.');
}
}
parent::postProcess();
}
/**
* Get the fields/elements defined in this form.
*
* @return array (string)
*/
function getRenderableElementNames() {
$elementNames = array();
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment