Skip to content

Commit

Permalink
Merge pull request #13746 from eileenmcnaughton/activity_search
Browse files Browse the repository at this point in the history
Convert activity_date_time field to datepicker and add support for url input
  • Loading branch information
seamuslee001 authored Mar 5, 2019
2 parents 6a9757e + 8e80025 commit f043815
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 85 deletions.
31 changes: 4 additions & 27 deletions CRM/Activity/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ public static function whereClauseSingle(&$values, &$query) {
case 'activity_date':
case 'activity_date_low':
case 'activity_date_high':
case 'activity_date_time_low':
case 'activity_date_time_high':
$query->dateQueryBuilder($values,
'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
'civicrm_activity', str_replace(['_high', '_low'], '', $name), 'activity_date_time', ts('Activity Date')
);
break;

Expand Down Expand Up @@ -440,7 +442,7 @@ public static function from($name, $mode, $side) {
* rather than a static function.
*/
public static function getSearchFieldMetadata() {
$fields = ['activity_type_id'];
$fields = ['activity_type_id', 'activity_date_time'];
$metadata = civicrm_api3('Activity', 'getfields', [])['values'];
return array_intersect_key($metadata, array_flip($fields));
}
Expand All @@ -454,10 +456,6 @@ public static function buildSearchForm(&$form) {
$form->addSearchFieldMetadata(['Activity' => self::getSearchFieldMetadata()]);
$form->addFormFieldsFromMetadata();

CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
$form->addElement('hidden', 'activity_date_range_error');
$form->addFormRule(array('CRM_Activity_BAO_Query', 'formRule'), $form);

$followUpActivity = array(
1 => ts('Yes'),
2 => ts('No'),
Expand Down Expand Up @@ -631,27 +629,6 @@ public static function selectorReturnProperties() {
return $properties;
}

/**
* 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 = array();

if (empty($fields['activity_date_low']) || empty($fields['activity_date_high'])) {
return TRUE;
}

CRM_Utils_Rule::validDateRange($fields, 'activity_date', $errors, ts('Activity Date'));

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

/**
* Where/qill clause for notes
*
Expand Down
25 changes: 0 additions & 25 deletions CRM/Activity/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,31 +322,6 @@ public function fixFormValues() {
}
}

$dateLow = CRM_Utils_Request::retrieve('dateLow', 'String');

if ($dateLow) {
$dateLow = date('m/d/Y', strtotime($dateLow));
$this->_formValues['activity_date_relative'] = 0;
$this->_defaults['activity_date_relative'] = 0;
$this->_formValues['activity_date_low'] = $dateLow;
$this->_defaults['activity_date_low'] = $dateLow;
}

$dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String');

if ($dateHigh) {
// Activity date time assumes midnight at the beginning of the date
// This sets it to almost midnight at the end of the date
/* if ($dateHigh <= 99999999) {
$dateHigh = 1000000 * $dateHigh + 235959;
} */
$dateHigh = date('m/d/Y', strtotime($dateHigh));
$this->_formValues['activity_date_relative'] = 0;
$this->_defaults['activity_date_relative'] = 0;
$this->_formValues['activity_date_high'] = $dateHigh;
$this->_defaults['activity_date_high'] = $dateHigh;
}

// Enable search activity by custom value
// @todo this is not good security practice. Instead define entity fields in metadata &
// use getEntity Defaults
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 @@ -1899,6 +1899,8 @@ public function whereClauseSingle(&$values, $apiEntity = NULL) {
case 'activity_date':
case 'activity_date_low':
case 'activity_date_high':
case 'activity_date_time_low':
case 'activity_date_time_high':
case 'activity_role':
case 'activity_status_id':
case 'activity_status':
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Search/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function postProcess() {
*
* @return array|bool
*/
public static function formRule($fields) {
public static function formRule($fields, $files, $form) {
// check actionName and if next, then do not repeat a search, since we are going to the next page
if (array_key_exists('_qf_Search_next', $fields)) {
if (empty($fields['task'])) {
Expand Down
6 changes: 4 additions & 2 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1295,11 +1295,13 @@ public function addDateRange($name, $from = '_from', $to = '_to', $label = 'From
*
* @param string $fieldName
* @param string $label
* @param bool $isDateTime
* Is this a date-time field (not just date).
* @param bool $required
* @param string $fromLabel
* @param string $toLabel
*/
public function addDatePickerRange($fieldName, $label, $required = FALSE, $fromLabel = 'From', $toLabel = 'To') {
public function addDatePickerRange($fieldName, $label, $isDateTime = FALSE, $required = FALSE, $fromLabel = 'From', $toLabel = 'To') {

$options = array(
'' => ts('- any -'),
Expand All @@ -1314,7 +1316,7 @@ public function addDatePickerRange($fieldName, $label, $required = FALSE, $fromL
NULL
);
$attributes = ['format' => 'searchDate'];
$extra = ['time' => FALSE];
$extra = ['time' => $isDateTime];
$this->add('datepicker', $fieldName . '_low', ts($fromLabel), $attributes, $required, $extra);
$this->add('datepicker', $fieldName . '_high', ts($toLabel), $attributes, $required, $extra);
}
Expand Down
44 changes: 41 additions & 3 deletions CRM/Core/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ public function buildQuickform() {
* than existing ad hoc handling.
*/
public function addFormFieldsFromMetadata() {
$this->addFormRule(['CRM_Core_Form_Search', 'formRule'], $this);
$this->_action = CRM_Core_Action::ADVANCED;
foreach ($this->getSearchFieldMetadata() as $entity => $fields) {
foreach ($fields as $fieldName => $fieldSpec) {
if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE) {
// Assuming time is false for now as we are not checking for date-time fields as yet.
$this->addDatePickerRange($fieldName, $fieldSpec['title'], FALSE);
if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) {
$this->addDatePickerRange($fieldName, $fieldSpec['title'], ($fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)));
}
else {
$this->addField($fieldName, ['entity' => $entity]);
Expand All @@ -165,6 +165,31 @@ public function addFormFieldsFromMetadata() {
}
}

/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($fields, $files, $form) {
$errors = [];
foreach ($form->getSearchFieldMetadata() as $entity => $spec) {
foreach ($spec as $fieldName => $fieldSpec) {
if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) {
if (isset($fields[$fieldName . '_high']) && isset($fields[$fieldName . '_low']) && empty($fields[$fieldName . '_relative'])) {
if (strtotime($fields[$fieldName . '_low']) > strtotime($fields[$fieldName . '_high'])) {
$errors[$fieldName . '_low'] = ts('%1: Please check that your date range is in correct chronological order.', [1 => $fieldSpec['title']]);
}
}
}
}
}
return $errors;
}

/**
* Get the validation rule to apply to a function.
*
Expand All @@ -184,6 +209,10 @@ protected function getValidationTypeForField($entity, $fieldName) {
case CRM_Utils_Type::T_INT:
return 'CommaSeparatedIntegers';

case CRM_Utils_Type::T_DATE:
case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
return 'Timestamp';

default:
return 'Alphanumeric';
}
Expand All @@ -204,6 +233,15 @@ protected function getEntityDefaults($entity) {
if ($value !== FALSE) {
$defaults[$fieldSpec['name']] = $value;
}
if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || ($fieldSpec['type'] === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) {
$low = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_low', 'Timestamp', FALSE, NULL, 'GET');
$high = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_high', 'Timestamp', FALSE, NULL, 'GET');
if ($low !== FALSE || $high !== FALSE) {
$defaults[$fieldSpec['name'] . '_relative'] = 0;
$defaults[$fieldSpec['name'] . '_low'] = $low ? date('Y-m-d H:i:s', strtotime($low)) : NULL;
$defaults[$fieldSpec['name'] . '_high'] = $high ? date('Y-m-d H:i:s', strtotime($high)) : NULL;
}
}
}
}
return $defaults;
Expand Down
18 changes: 0 additions & 18 deletions CRM/Pledge/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,24 +313,6 @@ public function addRules() {
$this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
}

/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @return array|bool
*/
public static function formRule($fields) {
$errors = array();

if (!empty($errors)) {
return $errors;
}

return TRUE;
}

/**
* Set the default form values.
*
Expand Down
79 changes: 76 additions & 3 deletions CRM/Upgrade/Incremental/SmartGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,46 @@ public function updateGroups($actions) {
* @param array $fields
*/
public function datePickerConversion($fields) {
$fieldPossibilities = [];
$fieldPossibilities = $relativeFieldNames = [];
foreach ($fields as $field) {
$fieldPossibilities[] = $field;
$fieldPossibilities[] = $field . '_high';
$fieldPossibilities[] = $field . '_low';
}
$relativeDateMappings = ['activity_date_time' => 'activity'];

foreach ($fields as $field) {
foreach ($this->getSearchesWithField($field) as $savedSearch) {
$formValues = $savedSearch['form_values'];
$isRelative = $hasRelative = FALSE;
$relativeFieldName = $field . '_relative';

if (!empty($relativeDateMappings[$field]) && isset($formValues['relative_dates'])) {
if (!empty($formValues['relative_dates'][$relativeDateMappings[$field]])) {
$formValues[] = [$relativeFieldName, '=', $savedSearch['form_values']['relative_dates'][$relativeDateMappings[$field]]];
unset($formValues['relative_dates'][$relativeDateMappings[$field]]);
$isRelative = TRUE;
}
}
foreach ($formValues as $index => $formValue) {
if (!isset($formValue[0])) {
// Any actual criteria will have this key set but skip any weird lines
continue;
}
if (in_array($formValue[0], $fieldPossibilities)) {
$formValues[$index][2] = $this->getConvertedDateValue($formValue[2]);
if ($isRelative) {
unset($formValues[$index]);
}
else {
$isHigh = substr($formValue[0], -5, 5) === '_high';
$formValues[$index][2] = $this->getConvertedDateValue($formValue[2], $isHigh);
}
}
}
if (!$isRelative) {
if (!in_array($relativeFieldName, $relativeFieldNames)) {
$relativeFieldNames[] = $relativeFieldName;
$formValues[] = [$relativeFieldName, '=', 0];
}
}
if ($formValues !== $savedSearch['form_values']) {
Expand Down Expand Up @@ -98,19 +125,64 @@ public function convertEqualsStringToInArray($field) {
* Get converted date value.
*
* @param string $dateValue
* @param bool $isEndOfDay
* Is this the upper value in a search range? If so alter the time to
* get the end of day if none set.
*
* @return string
* $dateValue
*/
protected function getConvertedDateValue($dateValue) {
protected function getConvertedDateValue($dateValue, $isEndOfDay) {
if (date('Y-m-d', strtotime($dateValue)) !== $dateValue
&& date('Y-m-d H:i:s', strtotime($dateValue)) !== $dateValue
) {
$dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date::processDate($dateValue)));
if ($isEndOfDay) {
$dateValue = str_replace('00:00:00', '23:59:59', $dateValue);
}
}
return $dateValue;
}

/**
* Rename a smartgroup field.
*
* @param string $oldName
* @param string $newName
*/
public function renameField($oldName, $newName) {
foreach ($this->getSearchesWithField($oldName) as $savedSearch) {
$formValues = $savedSearch['form_values'];
foreach ($formValues as $index => $formValue) {
if ($formValue[0] === $oldName) {
$formValues[$index][0] = $newName;
}
}

if ($formValues !== $savedSearch['form_values']) {
civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
}
}
}

/**
* Rename pairs of fields
*
* @param array $pairs
* Array or arrays of pairs - e.g
* [
* ['old' => 'activity_date', 'new' => 'activity_date_time'],
* ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
* ['old' => 'activity_date_high', 'new' => 'activity_date_time_high'],
* ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
* ]
*/
public function renameFields($pairs) {
foreach ($pairs as $pair) {
$this->renameField($pair['old'], $pair['new']);
}
}

/**
* @param $field
* @return mixed
Expand All @@ -121,6 +193,7 @@ protected function getSearchesWithField($field) {
'form_values' => ['LIKE' => "%{$field}%"],
])['values'];
return $savedSearches;

}

}
11 changes: 10 additions & 1 deletion CRM/Upgrade/Incremental/php/FiveTwelve.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
* @param string $rev
*/
public function upgrade_5_12_alpha1($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Update smart groups to rename filters on activity_date to activity_date_time', 'updateSmartGroups', [
'renameFields' => [
['old' => 'activity_date', 'new' => 'activity_date_time'],
['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
['old' => 'activity_date_high', 'new' => 'activity_date_time_high'],
['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
],
]);
$this->addTask('Update smart groups where jcalendar fields have been converted to datepicker', 'updateSmartGroups', [
'datepickerConversion' => [
'age_asof_date',
'activity_date_time'
]
]);
}
Expand Down
7 changes: 3 additions & 4 deletions templates/CRM/Activity/Form/Search/Common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@
</tr>

<tr>
<td><label>{ts}Activity Dates{/ts}</label></td>
</tr>
<tr>
{include file="CRM/Core/DateRange.tpl" fieldName="activity_date" from='_low' to='_high'}
<td>
{include file="CRM/Core/DatePickerRange.tpl" fieldName="activity_date_time" from='_low' to='_high'}
</td>
</tr>
<tr>
<td>
Expand Down
Loading

0 comments on commit f043815

Please sign in to comment.