Skip to content

Commit

Permalink
dev/core#561 Convert Case date fields on search forms from jcalendar …
Browse files Browse the repository at this point in the history
…to datepicker
  • Loading branch information
seamuslee001 committed Oct 26, 2019
1 parent 55af4b2 commit 91ffa2d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 51 deletions.
70 changes: 42 additions & 28 deletions CRM/Case/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@ public static function where(&$query) {
*/
public static function whereClauseSingle(&$values, &$query) {
list($name, $op, $value, $grouping, $wildcard) = $values;
$quoteValue = NULL;
$fields = self::getFields();

if (!empty($value) && !is_array($value)) {
$quoteValue = "\"$value\"";
}

$fieldAliases = self::getLegacySupportedFields();

$fieldName = $name = self::getFieldName($values);
$qillName = $name;
if (in_array($name, $fieldAliases)) {
$qillName = array_search($name, $fieldAliases);
}
$pseudoExtraParam = [];
$fieldSpec = CRM_Utils_Array::value($fieldName, $fields, []);
$tableName = CRM_Utils_Array::value('table_name', $fieldSpec, 'civicrm_case');
$dataType = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $fieldSpec));
if ($dataType === 'Timestamp' || $dataType === 'Date') {
$title = empty($fieldSpec['unique_title']) ? $fieldSpec['title'] : $fieldSpec['unique_title'];
$query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
$query->dateQueryBuilder($values,
$tableName, $fieldName, $fieldSpec['name'], $title
);
return;
}
$val = $names = [];
switch ($name) {

Expand Down Expand Up @@ -677,6 +703,20 @@ public static function tableNames(&$tables) {
}
}

/**
* Get the metadata for fields to be included on the search form.
*
* @throws \CiviCRM_API3_Exception
*/
public static function getSearchFieldMetadata() {
$fields = [
'case_start_date',
'case_end_date',
];
$metadata = civicrm_api3('Case', 'getfields', [])['values'];
return array_intersect_key($metadata, array_flip($fields));
}

/**
* Add all the elements shared between case search and advanced search.
*
Expand All @@ -689,12 +729,8 @@ public static function buildSearchForm(&$form) {

$form->addField('case_type_id', ['context' => 'search', 'entity' => 'Case']);
$form->addField('case_status_id', ['context' => 'search', 'entity' => 'Case']);

CRM_Core_Form_Date::buildDateRange($form, 'case_from', 1, '_start_date_low', '_start_date_high', ts('From'), FALSE);
CRM_Core_Form_Date::buildDateRange($form, 'case_to', 1, '_end_date_low', '_end_date_high', ts('From'), FALSE);
$form->addElement('hidden', 'case_from_start_date_range_error');
$form->addElement('hidden', 'case_to_end_date_range_error');
$form->addFormRule(['CRM_Case_BAO_Query', 'formRule'], $form);
$form->addSearchFieldMetadata(['Case' => self::getSearchFieldMetadata()]);
$form->addFormFieldsFromMetadata();

$form->assign('validCiviCase', TRUE);

Expand Down Expand Up @@ -738,26 +774,4 @@ public static function buildSearchForm(&$form) {
$form->setDefaults(['case_owner' => 1]);
}

/**
* Custom form rules.
*
* @param array $fields
* @param array $files
* @param CRM_Core_Form $form
*
* @return bool|array
*/
public static function formRule($fields, $files, $form) {
$errors = [];

if ((empty($fields['case_from_start_date_low']) || empty($fields['case_from_start_date_high'])) && (empty($fields['case_to_end_date_low']) || empty($fields['case_to_end_date_high']))) {
return TRUE;
}

CRM_Utils_Rule::validDateRange($fields, 'case_from_start_date', $errors, ts('Case Start Date'));
CRM_Utils_Rule::validDateRange($fields, 'case_to_end_date', $errors, ts('Case End Date'));

return empty($errors) ? TRUE : $errors;
}

}
16 changes: 1 addition & 15 deletions CRM/Case/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public function preProcess() {
$this->loadFormValues();

if ($this->_force) {
$this->postProcess();
$this->set('force', 0);
$this->handleForcedSearch();
}

$sortID = NULL;
Expand Down Expand Up @@ -314,19 +313,6 @@ public static function formRule($fields, $files, $form) {
return TRUE;
}

/**
* Set the default form values.
*
*
* @return array
* the default array reference
*/
public function setDefaultValues() {
$defaults = [];
$defaults = $this->_formValues;
return $defaults;
}

public function fixFormValues() {
if (!$this->_force) {
return;
Expand Down
2 changes: 2 additions & 0 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,8 @@ public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals
'membership_join_date_relative',
'membership_start_date_relative',
'membership_end_date_relative',
'case_start_date_relative',
'case_end_date_relative',
];
// Handle relative dates first
foreach (array_keys($formValues) as $id) {
Expand Down
2 changes: 2 additions & 0 deletions CRM/Upgrade/Incremental/SmartGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function datePickerConversion($fields) {
'pledge_create_date' => 'pledge_create',
'pledge_end_date' => 'pledge_end',
'pledge_start_date' => 'pledge_start',
'case_start_date' => 'case_from',
'case_end_date' => 'case_to',
];

foreach ($fields as $field) {
Expand Down
16 changes: 16 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveTwenty.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public function upgrade_5_20_alpha1($rev) {
}
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Add "Template" contribution status', 'templateStatus');
$this->addTask('Update smart groups to rename filters on case_from and case_to to case_start_date and case_end_date', 'updateSmartGroups', [
'renameField' => [
['old' => 'case_from_relative', 'new' => 'case_start_date_relative'],
['old' => 'case_from_start_date_high', 'new' => 'case_start_date_high'],
['old' => 'case_from_start_date_low', 'new' => 'case_start_date_low'],
['old' => 'case_to_relative', 'new' => 'case_end_date_relative'],
['old' => 'case_to_end_date_high', 'new' => 'case_end_date_high'],
['old' => 'case_to_end_date_low', 'new' => 'case_end_date_low'],
],
]);
$this->addTask('Update smart groups where jcalendar fields have been converted to datepicker', 'updateSmartGroups', [
'datepickerConversion' => [
'case_start_date',
'case_end_date',
],
]);
}

public static function templateStatus(CRM_Queue_TaskContext $ctx) {
Expand Down
10 changes: 2 additions & 8 deletions templates/CRM/Case/Form/Search/Common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@
</tr>

<tr>
<td>
<label>{ts}Case Start Date{/ts}</label>
</td>
{include file="CRM/Core/DateRange.tpl" fieldName="case_from" from='_start_date_low' to='_start_date_high'}
{include file="CRM/Core/DatePickerRangeWrapper.tpl" fieldName="case_start_date"}
</tr>
<tr>
<td>
<label>{ts}Case End Date{/ts}</label>
</td>
{include file="CRM/Core/DateRange.tpl" fieldName="case_to" from='_end_date_low' to='_end_date_high'}
{include file="CRM/Core/DatePickerRangeWrapper.tpl" fieldName="case_end_date"}
</tr>

<tr id='case_search_form'>
Expand Down

0 comments on commit 91ffa2d

Please sign in to comment.