Skip to content

Instantly share code, notes, and snippets.

@pradpnayak
Last active June 17, 2020 12:31
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 pradpnayak/f9589797dca332ca6e87a2b8fc3af64e to your computer and use it in GitHub Desktop.
Save pradpnayak/f9589797dca332ca6e87a2b8fc3af64e to your computer and use it in GitHub Desktop.
<?php
class CRM_ActivityDetailReport_Report_Form_Contact_ActivityDetailsWithCaseData extends CRM_Report_Form_Activity {
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
$this->_columns['civicrm_contact']['filters']['current_user_contact'] = [
'name' => 'current_user_contact',
'title' => ts('Limit to contact(s)'),
'type' => CRM_Report_Form::OP_ENTITYREF,
'operatorType' => CRM_Report_Form::OP_ENTITYREF,
'attributes' => ['entity' => 'Contact'],
'dbAlias' => 'current_contact.contact_id',
];
$this->_columns['civicrm_case']['fields']['duration_hours'] = [
'title' => ts('Duration (Hours)'),
'dbAlias' => 'duration/60',
'type' => CRM_Utils_Type::T_FLOAT,
];
$this->_columns['civicrm_case']['fields']['case_id'] = [
'title' => ts('Case ID'),
'name' => 'id',
];
$this->_columns['civicrm_case']['fields']['case_client_name'] = [
'title' => ts('Case Client Name'),
'dbAlias' => 'civicrm_contact_case.sort_name',
];
$this->_columns['civicrm_case']['fields']['case_client_id'] = [
'title' => ts('Case Client ID'),
'dbAlias' => 'civicrm_contact_case.id',
'default' => TRUE,
'no_display' => TRUE,
'required' => TRUE,
];
$this->_columns['civicrm_case']['fields']['case_status_id'] = [
'title' => ts('Case Status'),
'name' => 'status_id'
];
$this->_columns['civicrm_case']['fields']['case_type_id'] = [
'title' => ts('Case Type'),
'dbAlias' => 'civicrm_case_type.title'
];
$this->_columns['civicrm_activitydetailreport']['fields']['manage_activity_link'] = [
'title' => ts('Manage'),
'dbAlias' => 'CONCAT(activity_civireport.activity_type_id, ",", IFNULL(activity_civireport.source_record_id, ""))',
];
$this->_columns['civicrm_case']['filters']['case_type_id'] = [
'title' => ts('Case Type'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'type' => CRM_Utils_Type::T_STRING,
'options' => CRM_Case_PseudoConstant::caseType(),
];
}
/**
* Alter display of rows.
*
* Iterate through the rows retrieved via SQL and make changes for display purposes,
* such as rendering contacts as links.
*
* @param array $rows
* Rows generated by SQL, with an array for each row.
*/
public function alterDisplay(&$rows) {
$caseStatuses = CRM_Core_OptionGroup::values('case_status');
parent::alterDisplay($rows);
foreach ($rows as &$row) {
if (array_key_exists('civicrm_case_case_status_id', $row)) {
$row['civicrm_case_case_status_id'] = $caseStatuses[$row['civicrm_case_case_status_id']] ?? '';
}
if (array_key_exists('civicrm_case_case_client_name', $row)) {
$url = CRM_Utils_System::url("civicrm/contact/view",
'reset=1&cid=' . $row['civicrm_case_case_client_id'],
$this->_absoluteUrl
);
$row['civicrm_case_case_client_name_link'] = $url;
$row['civicrm_case_case_client_name_hover'] = ts('View Contact');
}
if (array_key_exists('civicrm_case_case_id', $row)) {
$url = CRM_Utils_System::url(
'civicrm/contact/view/case',
"action=view&reset=1&cid={$row['civicrm_case_case_client_id']}&id={$row['civicrm_case_case_id']}",
$this->_absoluteUrl
);
$row['civicrm_case_case_id'] = "<a class=' crm-popup ' href='$url'>" . $row['civicrm_case_case_id'] . '</a>';
}
CRM_ActivityDetailReport_Utils::alterRows($row);
}
}
/**
* Build from clause.
* @todo remove this function & declare the 3 contact tables separately
*/
public function from() {
parent::from();
$this->joinCase();
}
/**
* Main build form function.
*/
public function buildQuickForm() {
$this->assign('reportContext', $_REQUEST['context'] ?? NULL);
parent::buildQuickForm();
$activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'get');
if (!empty($this->_formValues['show_add_activity']) && CRM_Core_Permission::check('edit all contacts')) {
$activityLinks = $activityTypes;
if (!empty($this->_formValues['show_activity_types'])) {
$activityLinks = array_intersect_key($activityLinks, array_flip($this->_formValues['show_activity_types']));
}
$activityLinks = $this->generateActivityLinks($activityLinks);
$this->add(
'select', 'add_activity_type_id', '',
$activityLinks, FALSE, [
'class' => 'crm-select2 crm-action-menu fa-calendar-check-o twenty',
'placeholder' => ts('Add Activity'),
]
);
}
$this->addElement('checkbox', 'show_add_activity', ts("Show 'Add New Activity' option?"));
$this->add('select', 'show_activity_types',
ts('Restrict Activity Types to'), $activityTypes, FALSE,
[
'class' => 'crm-select2 huge',
'multiple' => 'multiple',
'placeholder' => ts('--select activity type--'),
]
);
}
public function generateActivityLinks($activityTypes) {
$allActTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate');
$emailActivityType = array_search('Email', $allActTypes);
$pdfActivityType = array_search('Print PDF Letter', $allActTypes);
$activityLinks = [];
foreach ($activityTypes as $type => $label) {
if ($type == $emailActivityType) {
$url = CRM_Utils_System::url('civicrm/activity/email/add',
"action=add&context=standalone&reset=1&atype={$type}",
FALSE, NULL, FALSE
);
}
elseif ($type == $pdfActivityType) {
$url = CRM_Utils_System::url('civicrm/activity/pdf/add',
"action=add&context=standalone&reset=1&atype={$type}",
FALSE, NULL, FALSE);
}
else {
$url = CRM_Utils_System::url('civicrm/activity/add',
"action=add&context=standalone&reset=1&atype={$type}",
FALSE, NULL, FALSE
);
}
$activityLinks[$url] = $label;
}
return $activityLinks;
}
/**
* @todo remove this function & declare the 3 contact tables separately
*
* (Currently the construct method incorrectly melds them - this is an interim
* refactor in order to get this under ReportTemplateTests)
*/
protected function buildSourceFrom() {
parent::buildSourceFrom();
$this->joinCase();
}
/**
* @todo remove this function & declare the 3 contact tables separately
*
* (Currently the construct method incorrectly melds them - this is an interim
* refactor in order to get this under ReportTemplateTests)
*/
protected function buildAssigneeFrom() {
parent::buildAssigneeFrom();
$this->joinCase();
}
public function joinCase() {
$this->_from .= "
INNER JOIN civicrm_activity_contact current_contact
ON current_contact.activity_id = {$this->_aliases['civicrm_activity']}.id
LEFT JOIN civicrm_case_activity caseactivity
ON caseactivity.activity_id = {$this->_aliases['civicrm_activity']}.id
LEFT JOIN civicrm_case {$this->_aliases['civicrm_case']}
ON {$this->_aliases['civicrm_case']}.id = caseactivity.case_id
LEFT JOIN civicrm_case_type civicrm_case_type
ON civicrm_case_type.id = {$this->_aliases['civicrm_case']}.case_type_id
LEFT JOIN civicrm_case_contact civicrm_case_contact
ON civicrm_case_contact.case_id = {$this->_aliases['civicrm_case']}.id
LEFT JOIN civicrm_contact civicrm_contact_case
ON civicrm_contact_case.id = civicrm_case_contact.contact_id
";
CRM_Utils_Hook::alterReportVar('fromClause', $this, $this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment