Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CATL-1114: Support Cases In Scheduled Reminders #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CRM/Admin/Form/ScheduleReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ public function listTokens() {
$tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
$tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens);
$tokens = array_merge(CRM_Core_SelectValues::contributionTokens(), $tokens);
$tokens = array_merge(CRM_Core_SelectValues::caseTokens(), $tokens);
return $tokens;
}

Expand Down
138 changes: 138 additions & 0 deletions CRM/Case/ActionMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Class CRM_Event_ActionMapping
*
* This defines the scheduled-reminder functionality for CiviCase entities.
*/
class CRM_Case_ActionMapping extends \Civi\ActionSchedule\Mapping {

/**
* The value for civicrm_action_schedule.mapping_id which identifies the
* "Case" mapping.
*
* Note: This value is chosen to match legacy DB IDs.
*/
const CASE_MAPPING_ID = 99;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you arrive at this particular value for the ID? Will it clash with some other value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a random unique id in this case.


/**
* Register Case-related action mappings.
*
* @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
*/
public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) {
$registrations->register(CRM_Case_ActionMapping::create(array(
'id' => CRM_Case_ActionMapping::CASE_MAPPING_ID,
'entity' => 'civicrm_case',
'entity_label' => ts('Case'),
'entity_value' => 'case_type',
'entity_value_label' => ts('Case Type'),
'entity_status' => 'case_status',
'entity_status_label' => ts('Case Status'),
'entity_date_start' => 'case_start_date',
'entity_date_end' => 'case_end_date',
)));
}

/**
* @inheritdoc
*/
public function getRecipientListing($type) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use short array syntax in this function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if ($type == 'case_roles') {
$result = civicrm_api3('RelationshipType', 'get', array(
'sequential' => 1,
'options' => array('limit' => 0, 'sort' => "label_b_a"),
))['values'];
$cRoles = array();
foreach ($result as $each) {
$cRoles[$each['id']] = $each['label_b_a'];
}
return $cRoles;
}
return array();
}

/**
* @inheritdoc
*/
public function getRecipientTypes() {
return array('case_roles' => 'Case Roles');
}

/**
* @inheritdoc
*/
public function createQuery($schedule, $phase, $defaultParams) {
$selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
$selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);

$query = \CRM_Utils_SQL_Select::from("civicrm_case c")->param($defaultParams);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't something like CRM_Utils_SQL_Select::from("{$this->entity} e") this work as I see in many core Action Mapping classes rather than hardcoding the table name?
At worst if the above does not work, we can get the table name from the BAO/DAO.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following CRM/Contribute/ActionMapping/ByPage.php
I have updated it to {$this->entity} now

$query->join('r', "INNER JOIN civicrm_relationship r ON c.id = r.case_id");
$query->join('cs', "INNER JOIN civicrm_contact ct ON r.contact_id_b = ct.id");
$query->join('ce', "INNER JOIN civicrm_email ce ON ce.contact_id = ct.id");

if (!empty($selectedValues)) {
$query->where("c.case_type_id IN (#caseId)")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the param placeholder name not be '#caseTypeId' ? Since we are searching in the case_type_id column

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right

->param('caseId', $selectedValues);
}

if (!empty($selectedStatuses)) {
$query->where("c.status_id IN (#selectedStatuses)")
->param('selectedStatuses', $selectedStatuses);
}

if (!empty($caseRoles)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't seem to find where the $caseRoles parameter is set in this function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was not required here and is handled later in the function. I have removed this now.

$query->where("r.relationship_type_id IN (#caseRoles)")
->param('caseRoles', $caseRoles);
}

if ($schedule->recipient_listing && $schedule->limit_to) {
switch ($schedule->recipient) {
case 'case_roles':
$caseRoles = (array) \CRM_Utils_Array::explodePadded($schedule->recipient_listing);
$query->where("r.relationship_type_id IN (#caseRoles)")
->param('caseRoles', $caseRoles);
break;
}
}

// $schedule->start_action_date is user-supplied data. validate.
if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) {
throw new CRM_Core_Exception("Invalid date field");
}

$query['casDateField'] = $schedule->start_action_date;
$query['casAddlCheckFrom'] = 'civicrm_case c';
$query['casContactIdField'] = 'ct.id';
$query['casEntityIdField'] = 'r.case_id';
$query['casContactTableAlias'] = 'ct';
$query->where('r.is_active = 1 AND c.is_deleted = 0');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An active relationship is more than checking if the relationship is active alone, We will also need to check the start and end date of the relationship to verify that start date is <= today OR NULL and end date >= today OR NULL.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. Fixed this now.


return $query;
}
}
2 changes: 2 additions & 0 deletions Civi/ActionSchedule/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ protected static function getValueLabelMap($name) {
$valueLabelMap['auto_renew_options'] = \CRM_Core_OptionGroup::values('auto_renew_options');
$valueLabelMap['contact_date_reminder_options'] = \CRM_Core_OptionGroup::values('contact_date_reminder_options');
$valueLabelMap['civicrm_membership_type'] = \CRM_Member_PseudoConstant::membershipType();
$valueLabelMap['case_type'] = \CRM_Case_PseudoConstant::caseType();
$valueLabelMap['case_status'] = \CRM_Case_PseudoConstant::caseStatus();

$allCustomFields = \CRM_Core_BAO_CustomField::getFields('');
$dateFields = [
Expand Down
1 change: 1 addition & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ public function createEventDispatcher($container) {
$dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Contribute_ActionMapping_ByType', 'onRegisterActionMappings']);
$dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Event_ActionMapping', 'onRegisterActionMappings']);
$dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Member_ActionMapping', 'onRegisterActionMappings']);
$dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, ['CRM_Case_ActionMapping', 'onRegisterActionMappings']);

if (\CRM_Utils_Constant::value('CIVICRM_FLEXMAILER_HACK_LISTENERS')) {
\Civi\Core\Resolver::singleton()->call(CIVICRM_FLEXMAILER_HACK_LISTENERS, [$dispatcher]);
Expand Down
34 changes: 34 additions & 0 deletions templates/CRM/Admin/Form/ScheduleReminders.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,40 @@
showHideLimitTo();
}

function caseRolesFilter() {
if(!$("#entity_1", $form).val()){
return;
}
var caseType = $("#entity_1", $form).val();
CRM.api3('CaseType', 'get', {
"id": {"IN":caseType}
}).done(function(result) {
if (!CRM._.isEmpty(result.values)) {
var values = [];
CRM._.each(result.values, function(v, i) {
CRM._.each(v.definition.caseRoles, function(v2, j) {
values.push(v2.name);
});
});
// filter options
$("#recipient_listing > option").each(function(i, val) {
var text = $(val).text();
if ($.inArray(text, values) == -1) {
$(this).remove();
}
});
}
$("#recipientList", $form).show();
});
}

$('#entity_1', $form).change(function () {
var recipient = $("#recipient", $form).val();
if (recipient == 'case_roles') {
populateRecipient();
}
});

// CRM-14070 Hide limit-to when entity is activity
function showHideLimitTo() {
$('#limit_to', $form).toggle(!($('#entity_0', $form).val() == '1'));
Expand Down