Skip to content

Commit

Permalink
Migrate to a datepicker range
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jan 4, 2019
1 parent 0bcac7e commit e897889
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 47 deletions.
28 changes: 28 additions & 0 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,34 @@ public function addDateRange($name, $from = '_from', $to = '_to', $label = 'From
}
}

/**
* Add a search for a range using date picker fields.
*
* @param string $fieldName
* @param string $label
* @param string $dateFormat
* @param bool $required
*/
public function addDatePickerRange($fieldName, $label, $required = FALSE, $fromLabel = 'From', $toLabel = 'To', $dateFormat ='') {

$options = array(
'' => ts('- any -'),
0 => ts('Choose Date Range'),
) + CRM_Core_OptionGroup::values('relative_date_filters');

$this->add('select',
"{$fieldName}_relative",
$label,
$options,
$required,
NULL
);
$attributes = ['format' => 'searchDate'];
$extra = ['time' => FALSE];
$this->add('datepicker', $fieldName . '_low', ts($fromLabel), $attributes, $required, $extra);
$this->add('datepicker', $fieldName . '_high', ts($toLabel), $attributes, $required, $extra);
}

/**
* Based on form action, return a string representing the api action.
* Used by addField method.
Expand Down
3 changes: 1 addition & 2 deletions CRM/Core/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ public function addFormFieldsFromMetadata() {
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->add('datepicker', $fieldName . '_low', $fieldSpec['title'], [], FALSE, ['time' => FALSE]);
$this->add('datepicker', $fieldName . '_high', $fieldSpec['title'], [], FALSE, ['time' => FALSE]);
$this->addDatePickerRange($fieldName, $fieldSpec['title'], FALSE);
}
else {
$this->addField($fieldName, ['entity' => $entity]);
Expand Down
14 changes: 11 additions & 3 deletions CRM/Grant/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static function where(&$query) {

/**
* @param $values
* @param $query
* @param \CRM_Contact_BAO_Query $query
*/
public static function whereClauseSingle(&$values, &$query) {
list($name, $op, $value, $grouping, $wildcard) = $values;
Expand All @@ -147,7 +147,7 @@ public static function whereClauseSingle(&$values, &$query) {
);
return;

case 'grant_application_received_notset':
case 'grant_application_received_date_notset':
$query->_where[$grouping][] = "civicrm_grant.application_received_date IS NULL";
$query->_qill[$grouping][] = ts("Grant Application Received Date is NULL");
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
Expand Down Expand Up @@ -310,6 +310,13 @@ public static function getSearchFieldMetadata() {
return array_intersect_key($metadata, array_flip($fields));
}

/**
* Transitional function for specifying which fields the tpl can iterate through.
*/
public static function getTemplateHandlableSearchFields() {
return array_diff_key(self::getSearchFieldMetadata(), ['grant_report_received' => 1]);
}

/**
* Add all the elements shared between grant search and advanaced search.
*
Expand All @@ -323,6 +330,7 @@ public static function buildSearchForm(&$form) {
$grantType = CRM_Core_OptionGroup::values('grant_type');
$form->addSearchFieldMetadata(['Grant' => self::getSearchFieldMetadata()]);
$form->addFormFieldsFromMetadata();
$form->assign('grantSearchFields', self::getTemplateHandlableSearchFields());
$form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE,
array('id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
Expand All @@ -331,7 +339,7 @@ public static function buildSearchForm(&$form) {
$form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE,
array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
$form->addElement('checkbox', 'grant_application_received_notset', ts('Date is not set'), NULL);
$form->addElement('checkbox', 'grant_application_received_date_notset', ts('Date is not set'), NULL);
$form->addElement('checkbox', 'grant_money_transfer_date_notset', ts('Date is not set'), NULL);
$form->addElement('checkbox', 'grant_due_date_notset', ts('Date is not set'), NULL);
$form->addElement('checkbox', 'grant_decision_date_notset', ts('Date is not set'), NULL);
Expand Down
58 changes: 58 additions & 0 deletions templates/CRM/Core/DatePickerRange.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| 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 |
+--------------------------------------------------------------------+
*}
{*this is included inside a table row*}
{assign var=relativeName value=$fieldName|cat:"_relative"}

{$form.$relativeName.label}<br />
{$form.$relativeName.html}<br />
<span class="crm-absolute-date-range">
<span class="crm-absolute-date-from">
{assign var=fromName value=$fieldName|cat:$from}
{$form.$fromName.label}
{$form.$fromName.html}
</span>
<span class="crm-absolute-date-to">
{assign var=toName value=$fieldName|cat:$to}
{$form.$toName.label}
{$form.$toName.html}
</span>
</span>
{literal}
<script type="text/javascript">
CRM.$(function($) {
$("#{/literal}{$relativeName}{literal}").change(function() {
var n = cj(this).parent().parent();
if ($(this).val() == "0") {
$(".crm-absolute-date-range", n).show();
} else {
$(".crm-absolute-date-range", n).hide();
$(':text', n).val('');
}
}).change();
});
</script>
{/literal}

51 changes: 9 additions & 42 deletions templates/CRM/Grant/Form/Search/Common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,50 +49,17 @@
{$form.grant_amount_high.html}
</td>
</tr>
{foreach from=$grantSearchFields key=fieldName item=fieldSpec}
{assign var=notSetFieldName value=$fieldName|cat:'_notset'}
<tr>
<td>
{$form.grant_application_received_date_low.label}<br />
{$form.grant_application_received_date_low.html}
</td>
<td colspan="2">
{$form.grant_application_received_date_high.label}<br />
{$form.grant_application_received_date_high.html}
&nbsp;{$form.grant_application_received_notset.html}&nbsp;&nbsp;{$form.grant_application_received_notset.label}
</td>
</tr>
<tr>
<td>
{$form.grant_decision_date_low.label}<br />
{$form.grant_decision_date_low.html}
</td>
<td colspan="2">
{$form.grant_decision_date_high.label}<br />
{$form.grant_decision_date_high.html}
&nbsp;{$form.grant_decision_date_notset.html}&nbsp;&nbsp;{$form.grant_decision_date_notset.label}
</td>
</tr>
<tr>
<td>
{$form.grant_money_transfer_date_low.label}<br />
{$form.grant_money_transfer_date_low.html}
</td>
<td colspan="2">
{$form.grant_money_transfer_date_high.label}<br />
{$form.grant_money_transfer_date_high.html}
&nbsp;{$form.grant_money_transfer_date_notset.html}&nbsp;&nbsp;{$form.grant_money_transfer_date_notset.label}
</td>
</tr>
<tr>
<td>
{$form.grant_due_date_low.label}<br />
{$form.grant_due_date_low.html}
</td>
<td colspan="2">
{$form.grant_due_date_high.label}<br />
{$form.grant_due_date_high.html}
&nbsp;{$form.grant_due_date_notset.html}&nbsp;&nbsp;{$form.grant_due_date_notset.label}
</td>
<td>
{include file="CRM/Core/DatePickerRange.tpl" from='_low' to='_high'}
</td>
<td>
&nbsp;{$form.$notSetFieldName.html}&nbsp;&nbsp;{$form.$notSetFieldName.label}
</td>
</tr>
{/foreach}
{if $grantGroupTree}
<tr>
<td colspan="3">
Expand Down

0 comments on commit e897889

Please sign in to comment.