Skip to content

Commit

Permalink
Convert grant date fields to use datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Dec 2, 2018
1 parent 09e4739 commit ec8ec53
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 47 deletions.
53 changes: 53 additions & 0 deletions CRM/Core/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ class CRM_Core_Form_Search extends CRM_Core_Form {
*/
protected $entityReferenceFields = array();

/**
* Metadata for fields on the search form.
*
* @var array
*/
protected $searchFieldMetadata = [];

/**
* @return array
*/
public function getSearchFieldMetadata() {
return $this->searchFieldMetadata;
}

/**
* Builds the list of tasks or actions that a searcher can perform on a result set.
*
Expand Down Expand Up @@ -270,4 +284,43 @@ protected function getFormVariables() {
}
}

/**
* Add any fields described in metadata to the form.
*
* The goal is to describe all fields in metadata and handle from metadata rather
* than existing ad hoc handling.
*/
public function addFormFieldsFromMetadata() {
$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->add('datepicker', $fieldName . '_low', $fieldSpec['title'], [], FALSE, ['time' => FALSE]);
$this->add('datepicker', $fieldName . '_high', $fieldSpec['title'], [], FALSE, ['time' => FALSE]);
}
else {
$this->addField($fieldName, ['entity' => $entity]);
}
}
}
}

/**
* @param array $searchFieldMetadata
*/
public function addSearchFieldMetadata($searchFieldMetadata) {
$existingFieldsForEntity = isset($this->searchFieldMetadata[$this->getDefaultEntity()]) ? $this->searchFieldMetadata[$this->getDefaultEntity()] : [];
$this->searchFieldMetadata[$this->getDefaultEntity()] = array_merge($existingFieldsForEntity, $searchFieldMetadata);
}

/**
* Get the metadata for fields to be included on the activity search form.
*/
protected function getEntityMetadata() {
$fields = $this->searchFields;
$metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', [])['values'];
return array_intersect_key($metadata, array_flip($fields));
}

}
24 changes: 0 additions & 24 deletions CRM/Grant/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ public static function where(&$query) {
* @param $query
*/
public static function whereClauseSingle(&$values, &$query) {
$strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
list($name, $op, $value, $grouping, $wildcard) = $values;
$val = $names = array();
switch ($name) {
case 'grant_money_transfer_date_low':
case 'grant_money_transfer_date_high':
Expand Down Expand Up @@ -317,28 +315,6 @@ public static function buildSearchForm(&$form) {
array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
);

$form->addDate('grant_application_received_date_low', ts('App. Received Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_application_received_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));

$form->addElement('checkbox', 'grant_application_received_notset', ts('Date is not set'), NULL);

$form->addDate('grant_money_transfer_date_low', ts('Money Sent Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_money_transfer_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));

$form->addElement('checkbox', 'grant_money_transfer_date_notset', ts('Date is not set'), NULL);

$form->addDate('grant_due_date_low', ts('Report Due Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_due_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));

$form->addElement('checkbox', 'grant_due_date_notset', ts('Date is not set'), NULL);

$form->addDate('grant_decision_date_low', ts('Grant Decision Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_decision_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));

$form->addElement('checkbox', 'grant_decision_date_notset', ts('Date is not set'), NULL);

$form->addYesNo('grant_report_received', ts('Grant report received?'), TRUE);

$form->add('text', 'grant_amount_low', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8));
$form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');

Expand Down
6 changes: 3 additions & 3 deletions CRM/Grant/DAO/Grant.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Grant/Grant.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:43c5de526491b3e385bec6d129dba6e3)
* (GenCodeChecksum:6ae1e39d3a16c7bae46965892ea5e323)
*/

/**
Expand Down Expand Up @@ -200,7 +200,7 @@ public static function &fields() {
'type' => 'EntityRef',
],
],
'application_received_date' => [
'grant_application_received_date' => [
'name' => 'application_received_date',
'type' => CRM_Utils_Type::T_DATE,
'title' => ts('Application received date'),
Expand All @@ -219,7 +219,7 @@ public static function &fields() {
'formatType' => 'activityDate',
],
],
'decision_date' => [
'grant_decision_date' => [
'name' => 'decision_date',
'type' => CRM_Utils_Type::T_DATE,
'title' => ts('Decision date'),
Expand Down
24 changes: 22 additions & 2 deletions CRM/Grant/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,26 @@ class CRM_Grant_Form_Search extends CRM_Core_Form_Search {
*/
protected $_prefix = "grant_";

protected $entity = 'grant';
/**
* Search fields to include on the form.
*
* @var array
*/
protected $searchFields = ['grant_application_received_date', 'grant_money_transfer_date', 'grant_money_transfer_date', 'grant_report_received', 'grant_due_date', 'grant_decision_date'];

/**
* Metadata of all fields to include on the form.
*
* @var array
*/
protected $searchFieldMetadata = [];

/**
* @return string
*/
public function getDefaultEntity() {
return 'Grant';
}

/**
* Processing needed for buildForm and later.
Expand All @@ -81,7 +100,7 @@ public function preProcess() {

$this->getUrlVariables();
$this->getFormVariables();

$this->addSearchFieldMetadata($this->getEntityMetadata());
if ($this->_force) {
$this->postProcess();
$this->set('force', 0);
Expand Down Expand Up @@ -130,6 +149,7 @@ public function preProcess() {
public function buildQuickForm() {
parent::buildQuickForm();
$this->addSortNameField();
$this->addFormFieldsFromMetadata();

CRM_Grant_BAO_Query::buildSearchForm($this);

Expand Down
32 changes: 14 additions & 18 deletions templates/CRM/Grant/Form/Search/Common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,41 @@
<tr>
<td>
{$form.grant_application_received_date_low.label}<br />
{include file="CRM/common/jcalendar.tpl" elementName=grant_application_received_date_low}
{$form.grant_application_received_date_low.html}
</td>
<td colspan="2">
{$form.grant_application_received_date_high.label}<br />
{include file="CRM/common/jcalendar.tpl" elementName=grant_application_received_date_high}
&nbsp;{$form.grant_application_received_notset.html}&nbsp;&nbsp;{$form.grant_application_received_notset.label}
{$form.grant_application_received_date_high.label}<br />
{$form.grant_application_received_date_high.html}
</td>
</tr>
<tr>
<td>
{$form.grant_decision_date_low.label}<br />
{include file="CRM/common/jcalendar.tpl" elementName=grant_decision_date_low}
{$form.grant_decision_date_low.label}<br />
{$form.grant_decision_date_low.html}
</td>
<td colspan="2">
{$form.grant_decision_date_high.label}<br />
{include file="CRM/common/jcalendar.tpl" elementName=grant_decision_date_high}
&nbsp;{$form.grant_decision_date_notset.html}&nbsp;&nbsp;{$form.grant_decision_date_notset.label}
{$form.grant_decision_date_high.html}
</td>
</tr>
<tr>
<td>
{$form.grant_money_transfer_date_low.label}<br />
{include file="CRM/common/jcalendar.tpl" elementName=grant_money_transfer_date_low}
{$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 />
{include file="CRM/common/jcalendar.tpl" elementName=grant_money_transfer_date_high}
&nbsp;{$form.grant_money_transfer_date_notset.html}&nbsp;&nbsp;{$form.grant_money_transfer_date_notset.label}
{$form.grant_money_transfer_date_high.label}<br />
{$form.grant_money_transfer_date_high.html}
</td>
</tr>
<tr>
<td>
{$form.grant_due_date_low.label}<br />
{include file="CRM/common/jcalendar.tpl" elementName=grant_due_date_low}
{$form.grant_due_date_low.label}<br />
{$form.grant_due_date_low.html}
</td>
<td colspan="2">
{$form.grant_due_date_high.label}<br />
{include file="CRM/common/jcalendar.tpl" elementName=grant_due_date_high}
&nbsp;{$form.grant_due_date_notset.html}&nbsp;&nbsp;{$form.grant_due_date_notset.label}
{$form.grant_due_date_high.label}<br />
{$form.grant_due_date_high.html}
</td>
</tr>
{if $grantGroupTree}
Expand Down
2 changes: 2 additions & 0 deletions xml/schema/Grant/Grant.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<field>
<name>application_received_date</name>
<title>Application received date</title>
<uniqueName>grant_application_received_date</uniqueName>
<type>date</type>
<export>true</export>
<import>true</import>
Expand All @@ -56,6 +57,7 @@
<field>
<name>decision_date</name>
<title>Decision date</title>
<uniqueName>grant_decision_date</uniqueName>
<type>date</type>
<comment>Date on which grant decision was made.</comment>
<import>true</import>
Expand Down

0 comments on commit ec8ec53

Please sign in to comment.