3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 class CRM_Grant_BAO_Query
extends CRM_Core_BAO_Query
{
40 public static function &getFields() {
42 $fields = CRM_Grant_BAO_Grant
::exportableFields();
47 * Build select for CiviGrant.
53 public static function select(&$query) {
54 if (!empty($query->_returnProperties
['grant_status_id'])) {
55 $query->_select
['grant_status_id'] = 'grant_status.id as grant_status_id';
56 $query->_element
['grant_status'] = 1;
57 $query->_tables
['grant_status'] = $query->_whereTables
['grant_status'] = 1;
58 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
61 if (!empty($query->_returnProperties
['grant_status'])) {
62 $query->_select
['grant_status'] = 'grant_status.label as grant_status';
63 $query->_element
['grant_status'] = 1;
64 $query->_tables
['grant_status'] = $query->_whereTables
['grant_status'] = 1;
65 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
68 if (!empty($query->_returnProperties
['grant_type_id'])) {
69 $query->_select
['grant_type_id'] = 'grant_type.id as grant_type_id';
70 $query->_element
['grant_type'] = 1;
71 $query->_tables
['grant_type'] = $query->_whereTables
['grant_type'] = 1;
72 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
75 if (!empty($query->_returnProperties
['grant_type'])) {
76 $query->_select
['grant_type'] = 'grant_type.label as grant_type';
77 $query->_element
['grant_type'] = 1;
78 $query->_tables
['grant_type'] = $query->_whereTables
['grant_type'] = 1;
79 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
82 if (!empty($query->_returnProperties
['grant_note'])) {
83 $query->_select
['grant_note'] = "civicrm_note.note as grant_note";
84 $query->_element
['grant_note'] = 1;
85 $query->_tables
['grant_note'] = 1;
88 if ($query->_mode
& CRM_Contact_BAO_Query
::MODE_GRANT
) {
89 $query->_select
['grant_amount_requested'] = 'civicrm_grant.amount_requested as grant_amount_requested';
90 $query->_select
['grant_amount_granted'] = 'civicrm_grant.amount_granted as grant_amount_granted';
91 $query->_select
['grant_amount_total'] = 'civicrm_grant.amount_total as grant_amount_total';
92 $query->_select
['grant_application_received_date'] = 'civicrm_grant.application_received_date as grant_application_received_date ';
93 $query->_select
['grant_report_received'] = 'civicrm_grant.grant_report_received as grant_report_received';
94 $query->_select
['grant_money_transfer_date'] = 'civicrm_grant.money_transfer_date as grant_money_transfer_date';
95 $query->_element
['grant_type_id'] = 1;
96 $query->_element
['grant_status_id'] = 1;
97 $query->_tables
['civicrm_grant'] = 1;
98 $query->_whereTables
['civicrm_grant'] = 1;
103 * Given a list of conditions in params generate the required.
110 public static function where(&$query) {
111 foreach ($query->_params
as $id => $values) {
112 if (!is_array($values) ||
count($values) != 5) {
116 if (substr($values[0], 0, 6) == 'grant_') {
117 self
::whereClauseSingle($values, $query);
124 * @param \CRM_Contact_BAO_Query $query
126 public static function whereClauseSingle(&$values, &$query) {
127 list($name, $op, $value, $grouping, $wildcard) = $values;
129 case 'grant_money_transfer_date_low':
130 case 'grant_money_transfer_date_high':
131 $query->dateQueryBuilder($values, 'civicrm_grant',
132 'grant_money_transfer_date', 'money_transfer_date',
133 'Money Transfer Date'
137 case 'grant_money_transfer_date_notset':
138 $query->_where
[$grouping][] = "civicrm_grant.money_transfer_date IS NULL";
139 $query->_qill
[$grouping][] = ts("Grant Money Transfer Date is NULL");
140 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
143 case 'grant_application_received_date_low':
144 case 'grant_application_received_date_high':
145 $query->dateQueryBuilder($values, 'civicrm_grant',
146 'grant_application_received_date',
147 'application_received_date', 'Application Received Date'
151 case 'grant_application_received_date_notset':
152 $query->_where
[$grouping][] = "civicrm_grant.application_received_date IS NULL";
153 $query->_qill
[$grouping][] = ts("Grant Application Received Date is NULL");
154 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
157 case 'grant_due_date_low':
158 case 'grant_due_date_high':
159 $query->dateQueryBuilder($values, 'civicrm_grant',
161 'grant_due_date', ts('Grant Due Date')
165 case 'grant_due_date_notset':
166 $query->_where
[$grouping][] = "civicrm_grant.grant_due_date IS NULL";
167 $query->_qill
[$grouping][] = ts("Grant Due Date is NULL");
168 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
171 case 'grant_decision_date_low':
172 case 'grant_decision_date_high':
173 $query->dateQueryBuilder($values, 'civicrm_grant',
174 'grant_decision_date',
175 'decision_date', ts('Grant Decision Date')
179 case 'grant_decision_date_notset':
180 $query->_where
[$grouping][] = "civicrm_grant.decision_date IS NULL";
181 $query->_qill
[$grouping][] = ts("Grant Decision Date is NULL");
182 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
185 case 'grant_type_id':
187 case 'grant_status_id':
190 if (strstr($name, 'type')) {
191 $name = 'grant_type_id';
192 $label = ts('Grant Type(s)');
196 $label = ts('Grant Status(s)');
199 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_grant.$name", $op, $value, "Integer");
201 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Grant_DAO_Grant', $name, $value, $op);
202 $query->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $label, 2 => $qillop, 3 => $qillVal]);
203 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
207 case 'grant_report_received':
211 $query->_where
[$grouping][] = "civicrm_grant.grant_report_received $op $value";
213 elseif ($value == 0) {
215 $query->_where
[$grouping][] = "civicrm_grant.grant_report_received IS NULL";
218 $query->_qill
[$grouping][] = ts('Grant Report Received = %1', [1 => $yesNo]);
219 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
224 case 'grant_amount_low':
225 case 'grant_amount_high':
226 $query->numberRangeBuilder($values,
227 'civicrm_grant', 'grant_amount', 'amount_total', 'Total Amount'
233 * @param string $name
237 * @return null|string
239 public static function from($name, $mode, $side) {
242 case 'civicrm_grant':
243 $from = " $side JOIN civicrm_grant ON civicrm_grant.contact_id = contact_a.id ";
247 $from .= " $side JOIN civicrm_option_group option_group_grant_status ON (option_group_grant_status.name = 'grant_status')";
248 $from .= " $side JOIN civicrm_option_value grant_status ON (civicrm_grant.status_id = grant_status.value AND option_group_grant_status.id = grant_status.option_group_id ) ";
252 $from .= " $side JOIN civicrm_option_group option_group_grant_type ON (option_group_grant_type.name = 'grant_type')";
253 if ($mode & CRM_Contact_BAO_Query
::MODE_GRANT
) {
254 $from .= " INNER JOIN civicrm_option_value grant_type ON (civicrm_grant.grant_type_id = grant_type.value AND option_group_grant_type.id = grant_type.option_group_id ) ";
257 $from .= " $side JOIN civicrm_option_value grant_type ON (civicrm_grant.grant_type_id = grant_type.value AND option_group_grant_type.id = grant_type.option_group_id ) ";
262 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_grant' AND
263 civicrm_grant.id = civicrm_note.entity_id )";
271 * @param bool $includeCustomFields
275 public static function defaultReturnProperties(
277 $includeCustomFields = TRUE
280 if ($mode & CRM_Contact_BAO_Query
::MODE_GRANT
) {
283 'contact_sub_type' => 1,
288 'grant_amount_requested' => 1,
289 'grant_application_received_date' => 1,
290 'grant_report_received' => 1,
291 'grant_money_transfer_date' => 1,
300 * Get the metadata for fields to be included on the grant search form.
302 public static function getSearchFieldMetadata() {
304 'grant_report_received',
305 'grant_application_received_date',
306 'grant_decision_date',
307 'grant_money_transfer_date',
310 $metadata = civicrm_api3('Grant', 'getfields', [])['values'];
311 return array_intersect_key($metadata, array_flip($fields));
315 * Transitional function for specifying which fields the tpl can iterate through.
317 public static function getTemplateHandlableSearchFields() {
318 return array_diff_key(self
::getSearchFieldMetadata(), ['grant_report_received' => 1]);
322 * Add all the elements shared between grant search and advanaced search.
325 * @param \CRM_Grant_Form_Search $form
329 public static function buildSearchForm(&$form) {
331 $grantType = CRM_Core_OptionGroup
::values('grant_type');
332 $form->addSearchFieldMetadata(['Grant' => self
::getSearchFieldMetadata()]);
333 $form->addFormFieldsFromMetadata();
334 $form->assign('grantSearchFields', self
::getTemplateHandlableSearchFields());
335 $form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE,
336 ['id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2']
339 $grantStatus = CRM_Core_OptionGroup
::values('grant_status');
340 $form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE,
341 ['id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2']
343 $form->addElement('checkbox', 'grant_application_received_date_notset', ts('Date is not set'), NULL);
344 $form->addElement('checkbox', 'grant_money_transfer_date_notset', ts('Date is not set'), NULL);
345 $form->addElement('checkbox', 'grant_due_date_notset', ts('Date is not set'), NULL);
346 $form->addElement('checkbox', 'grant_decision_date_notset', ts('Date is not set'), NULL);
348 $form->add('text', 'grant_amount_low', ts('Minimum Amount'), ['size' => 8, 'maxlength' => 8]);
349 $form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money
::format('9.99', ' ')]), 'money');
351 $form->add('text', 'grant_amount_high', ts('Maximum Amount'), ['size' => 8, 'maxlength' => 8]);
352 $form->addRule('grant_amount_high', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money
::format('99.99', ' ')]), 'money');
354 self
::addCustomFormFields($form, ['Grant']);
356 $form->assign('validGrant', TRUE);