Skip to content

Commit

Permalink
Add batch update via profile for cases and refactor to use shared cor…
Browse files Browse the repository at this point in the history
…e classes
  • Loading branch information
mattwire committed Jan 28, 2018
1 parent 56d3977 commit 888da08
Show file tree
Hide file tree
Showing 14 changed files with 941 additions and 142 deletions.
139 changes: 6 additions & 133 deletions CRM/Case/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,145 +26,18 @@
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/

/**
* This class generates task actions for CiviEvent.
* This class generates form task actions for CiviCase.
*/
class CRM_Case_Form_Task extends CRM_Core_Form {
class CRM_Case_Form_Task extends CRM_Core_Form_Task {

/**
* The task being performed
*
* @var int
*/
protected $_task;

/**
* The additional clause that we restrict the search with
*
* @var string
*/
protected $_componentClause = NULL;

/**
* The array that holds all the component ids
*
* @var array
*/
protected $_componentIds;

/**
* The array that holds all the case ids
*
* @var array
*/
public $_caseIds;

/**
* Build all the data structures needed to build the form.
*/
public function preProcess() {
self::preProcessCommon($this);
}

/**
* @param CRM_Core_Form $form
* @param bool $useTable
*/
public static function preProcessCommon(&$form, $useTable = FALSE) {
$form->_caseIds = array();

$values = $form->controller->exportValues($form->get('searchFormName'));

$form->_task = $values['task'];
$caseTasks = CRM_Case_Task::tasks();
$form->assign('taskName', $caseTasks[$form->_task]);

$ids = array();
if ($values['radio_ts'] == 'ts_sel') {
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
else {
$queryParams = $form->get('queryParams');
$query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
CRM_Contact_BAO_Query::MODE_CASE
);
$query->_distinctComponentClause = " ( civicrm_case.id )";
$query->_groupByComponentClause = " GROUP BY civicrm_case.id ";
$result = $query->searchQuery(0, 0, NULL);
while ($result->fetch()) {
$ids[] = $result->case_id;
}
}

if (!empty($ids)) {
$form->_componentClause = ' civicrm_case.id IN ( ' . implode(',', $ids) . ' ) ';
$form->assign('totalSelectedCases', count($ids));
}

$form->_caseIds = $form->_componentIds = $ids;

//set the context for redirection for any task actions
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
$urlParams = 'force=1';
if (CRM_Utils_Rule::qfKey($qfKey)) {
$urlParams .= "&qfKey=$qfKey";
}

$session = CRM_Core_Session::singleton();
$searchFormName = strtolower($form->get('searchFormName'));
if ($searchFormName == 'search') {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/case/search', $urlParams));
}
else {
$session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
$urlParams
));
}
}

/**
* Given the signer id, compute the contact id
* since its used for things like send email
*/
public function setContactIDs() {
$this->_contactIds = &CRM_Core_DAO::getContactIDsFromComponent($this->_caseIds,
'civicrm_case_contact'
);
}

/**
* Simple shell that derived classes can call to add buttons to
* the form with a customized title for the main Submit
*
* @param string $title
* Title of the main button.
* @param string $nextType
* Button type for the form after processing.
* @param string $backType
* @param bool $submitOnce
*/
public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
$this->addButtons(array(
array(
'type' => $nextType,
'name' => $title,
'isDefault' => TRUE,
),
array(
'type' => $backType,
'name' => ts('Cancel'),
),
)
);
}
// Must be set to entity table name (eg. civicrm_participant) by child class
static $tableName = 'civicrm_case';
// Must be set to entity shortname (eg. event)
static $entityShortname = 'case';

}
118 changes: 118 additions & 0 deletions CRM/Case/Form/Task/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| 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 |
+--------------------------------------------------------------------+
*/

/**
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/

/**
* This class provides the functionality for batch profile update for cases
*/
class CRM_Case_Form_Task_Batch extends CRM_Core_Form_Task_Batch {

// Must be set to entity table name (eg. civicrm_participant) by child class
static $tableName = 'civicrm_case';
// Must be set to entity shortname (eg. event)
static $entityShortname = 'case';

/**
* Process the form after the input has been submitted and validated.
*
* @return void
*/
public function postProcess() {
$params = $this->exportValues();

if (!isset($params['field'])) {
CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
return;
}

$customFields = array();
$dateFields = array(
'case_created_date',
'case_start_date',
'case_end_date',
'case_modified_date',
);
foreach ($params['field'] as $key => $value) {
$value['id'] = $key;

if (!empty($value['case_type'])) {
$caseTypeId = $value['case_type_id'] = $value['case_type'][1];
}
unset($value['case_type']);

// Get the case status
$daoClass = 'CRM_Case_DAO_Case';
$caseStatus = CRM_Utils_Array::value('case_status', $value);
if (!$caseStatus) {
// default to existing status ID
$caseStatus = CRM_Core_DAO::getFieldValue($daoClass, $key, 'status_id');
}
$value['status_id'] = $caseStatus;
unset($value['case_status']);

foreach ($dateFields as $val) {
if (isset($value[$val])) {
$value[$val] = CRM_Utils_Date::processDate($value[$val]);
}
}
if (empty($customFields)) {
if (empty($value['case_type_id'])) {
$caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $key, 'case_type_id');
}

// case type custom data
$customFields = CRM_Core_BAO_CustomField::getFields('Case', FALSE, FALSE, $caseTypeId);

$customFields = CRM_Utils_Array::crmArrayMerge($customFields,
CRM_Core_BAO_CustomField::getFields('Case',
FALSE, FALSE, NULL, NULL, TRUE
)
);
}
//check for custom data
$value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
$key,
'Case',
$caseTypeId
);

$case = CRM_Case_BAO_Case::add($value);

// add custom field values
if (!empty($value['custom']) && is_array($value['custom'])) {
CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_case', $case->id);
}
}

CRM_Core_Session::setStatus(ts('Your updates have been saved.'), ts('Saved'), 'success');
}

}
52 changes: 52 additions & 0 deletions CRM/Case/Form/Task/PickProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| 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 |
+--------------------------------------------------------------------+
*/

/**
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/

/**
* This class provides the functionality for batch profile update for case
*/
class CRM_Case_Form_Task_PickProfile extends CRM_Core_Form_Task_PickProfile {

/**
* Must be set to entity table name (eg. civicrm_participant) by child class
*
* @var string
*/
static $tableName = 'civicrm_case';

/**
* Must be set to entity shortname (eg. event)
*
* @var string
*/
static $entityShortname = 'case';

}
8 changes: 8 additions & 0 deletions CRM/Case/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public static function &tasks() {
'class' => 'CRM_Case_Form_Task_PDF',
'result' => FALSE,
),
6 => array(
'title' => ts('Update multiple cases'),
'class' => array(
'CRM_Case_Form_Task_PickProfile',
'CRM_Case_Form_Task_Batch',
),
'result' => FALSE,
),
);

//CRM-4418, check for delete
Expand Down
11 changes: 10 additions & 1 deletion CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public static function formatContactIDSToLinks($contactIDs, $addViewLink = TRUE,
public static function contactDetails($componentIds, $componentName, $returnProperties = array()) {
$contactDetails = array();
if (empty($componentIds) ||
!in_array($componentName, array('CiviContribute', 'CiviMember', 'CiviEvent', 'Activity'))
!in_array($componentName, array('CiviContribute', 'CiviMember', 'CiviEvent', 'Activity', 'CiviCase'))
) {
return $contactDetails;
}
Expand All @@ -704,6 +704,9 @@ public static function contactDetails($componentIds, $componentName, $returnProp
$compTable = 'civicrm_activity';
$activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
}
elseif ($componentName == 'CiviCase') {
$compTable = 'civicrm_case';
}
else {
$compTable = 'civicrm_participant';
}
Expand All @@ -723,6 +726,12 @@ public static function contactDetails($componentIds, $componentName, $returnProp
$from[$value] = "
INNER JOIN civicrm_activity_contact acs ON (acs.activity_id = {$compTable}.id AND acs.record_type_id = {$sourceID})
INNER JOIN civicrm_contact contact ON ( contact.id = acs.contact_id )";
}
elseif ($componentName == 'CiviCase') {
$select[] = "contact.$property as $property";
$from[$value] = "
INNER JOIN civicrm_case_contact ccs ON (ccs.case_id = {$compTable}.id)
INNER JOIN civicrm_contact contact ON ( contact.id = ccs.contact_id )";
}
else {
$select[] = "$property as $property";
Expand Down
3 changes: 0 additions & 3 deletions CRM/Core/BAO/UFField.php
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,7 @@ public static function getAvailableFields($gid = NULL, $defaults = array()) {
CRM_Utils_Array::remove($caseFields,
'case_id',
'case_type',
'case_start_date',
'case_end_date',
'case_role',
'case_status',
'case_deleted'
);
}
Expand Down
Loading

0 comments on commit 888da08

Please sign in to comment.