4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Grant_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 (($query->_mode
& CRM_Contact_BAO_Query
::MODE_GRANT
) ||
!empty($query->_returnProperties
)) {
55 if (!empty($query->_returnProperties
['grant_status_id'])) {
56 $query->_select
['grant_status_id'] = 'grant_status.id as grant_status_id';
57 $query->_element
['grant_status'] = 1;
58 $query->_tables
['grant_status'] = $query->_whereTables
['grant_status'] = 1;
59 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
62 if (!empty($query->_returnProperties
['grant_status'])) {
63 $query->_select
['grant_status'] = 'grant_status.label as grant_status';
64 $query->_element
['grant_status'] = 1;
65 $query->_tables
['grant_status'] = $query->_whereTables
['grant_status'] = 1;
66 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
69 if (!empty($query->_returnProperties
['grant_type_id'])) {
70 $query->_select
['grant_type_id'] = 'grant_type.id as grant_type_id';
71 $query->_element
['grant_type'] = 1;
72 $query->_tables
['grant_type'] = $query->_whereTables
['grant_type'] = 1;
73 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
76 if (!empty($query->_returnProperties
['grant_type'])) {
77 $query->_select
['grant_type'] = 'grant_type.label as grant_type';
78 $query->_element
['grant_type'] = 1;
79 $query->_tables
['grant_type'] = $query->_whereTables
['grant_type'] = 1;
80 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
83 if (!empty($query->_returnProperties
['grant_note'])) {
84 $query->_select
['grant_note'] = "civicrm_note.note as grant_note";
85 $query->_element
['grant_note'] = 1;
86 $query->_tables
['grant_note'] = 1;
88 $query->_select
['grant_amount_requested'] = 'civicrm_grant.amount_requested as grant_amount_requested';
89 $query->_select
['grant_amount_granted'] = 'civicrm_grant.amount_granted as grant_amount_granted';
90 $query->_select
['grant_amount_total'] = 'civicrm_grant.amount_total as grant_amount_total';
91 $query->_select
['grant_application_received_date'] = 'civicrm_grant.application_received_date as grant_application_received_date ';
92 $query->_select
['grant_report_received'] = 'civicrm_grant.grant_report_received as grant_report_received';
93 $query->_select
['grant_money_transfer_date'] = 'civicrm_grant.money_transfer_date as grant_money_transfer_date';
94 $query->_element
['grant_type_id'] = 1;
95 $query->_element
['grant_status_id'] = 1;
96 $query->_tables
['civicrm_grant'] = 1;
97 $query->_whereTables
['civicrm_grant'] = 1;
102 * Given a list of conditions in params generate the required
109 public static function where(&$query) {
110 foreach ($query->_params
as $id => $values) {
111 if (!is_array($values) ||
count($values) != 5) {
115 if (substr($values[0], 0, 6) == 'grant_') {
116 self
::whereClauseSingle($values, $query);
125 public static function whereClauseSingle(&$values, &$query) {
126 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
127 list($name, $op, $value, $grouping, $wildcard) = $values;
128 $val = $names = array();
130 case 'grant_money_transfer_date_low':
131 case 'grant_money_transfer_date_high':
132 $query->dateQueryBuilder($values, 'civicrm_grant',
133 'grant_money_transfer_date', 'money_transfer_date',
134 'Money Transfer Date'
138 case 'grant_money_transfer_date_notset':
139 $query->_where
[$grouping][] = "civicrm_grant.money_transfer_date IS NULL";
140 $query->_qill
[$grouping][] = ts("Grant Money Transfer Date is NULL");
141 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
144 case 'grant_application_received_date_low':
145 case 'grant_application_received_date_high':
146 $query->dateQueryBuilder($values, 'civicrm_grant',
147 'grant_application_received_date',
148 'application_received_date', 'Application Received Date'
152 case 'grant_application_received_notset':
153 $query->_where
[$grouping][] = "civicrm_grant.application_received_date IS NULL";
154 $query->_qill
[$grouping][] = ts("Grant Application Received Date is NULL");
155 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
158 case 'grant_due_date_low':
159 case 'grant_due_date_high':
160 $query->dateQueryBuilder($values, 'civicrm_grant',
162 'grant_due_date', 'Grant Due Date'
166 case 'grant_due_date_notset':
167 $query->_where
[$grouping][] = "civicrm_grant.grant_due_date IS NULL";
168 $query->_qill
[$grouping][] = ts("Grant Due Date is NULL");
169 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
172 case 'grant_decision_date_low':
173 case 'grant_decision_date_high':
174 $query->dateQueryBuilder($values, 'civicrm_grant',
175 'grant_decision_date',
176 'decision_date', 'Grant Decision Date'
180 case 'grant_decision_date_notset':
181 $query->_where
[$grouping][] = "civicrm_grant.decision_date IS NULL";
182 $query->_qill
[$grouping][] = ts("Grant Decision Date is NULL");
183 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
186 case 'grant_type_id':
188 $grantTypes = CRM_Core_OptionGroup
::values('grant_type');
189 if (is_array($value)) {
190 foreach ($value as $k => $v) {
195 if (count($val) > 0) {
196 // Overwrite $value so it works with an IN where statement.
198 $value = '(' . implode(',', $val) . ')';
202 foreach ($val as $id) {
203 $names[] = CRM_Utils_Array
::value($id, $grantTypes);
207 if (!empty($value)) {
208 $names[] = $grantTypes[$value];
212 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause('civicrm_grant.grant_type_id', $op, $value, "Integer");
214 $query->_qill
[$grouping][] = ts('Grant Type %2 %1', array(1 => implode(' ' . ts('or') . ' ', $names), 2 => $op));
215 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
219 case 'grant_status_id':
221 $grantStatus = CRM_Core_OptionGroup
::values('grant_status');
222 if (is_array($value)) {
223 foreach ($value as $k => $v) {
228 if (count($val) > 0) {
229 // Overwrite $value so it works with an IN where statement.
231 $value = '(' . implode(',', $val) . ')';
235 foreach ($val as $id) {
236 $names[] = CRM_Utils_Array
::value($id, $grantStatus);
240 if (!empty($value)) {
241 $names[] = $grantStatus[$value];
245 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause('civicrm_grant.status_id', $op, $value, "Integer");
247 $query->_qill
[$grouping][] = ts('Grant Status %2 %1', array(1 => implode(' ' . ts('or') . ' ', $names), 2 => $op));
248 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
252 case 'grant_report_received':
256 $query->_where
[$grouping][] = "civicrm_grant.grant_report_received $op $value";
258 elseif ($value == 0) {
260 $query->_where
[$grouping][] = "civicrm_grant.grant_report_received IS NULL";
263 $query->_qill
[$grouping][] = "Grant Report Received = $yesNo ";
264 $query->_tables
['civicrm_grant'] = $query->_whereTables
['civicrm_grant'] = 1;
269 case 'grant_amount_low':
270 case 'grant_amount_high':
271 $query->numberRangeBuilder($values,
272 'civicrm_grant', 'grant_amount', 'amount_total', 'Total Amount'
278 * @param string $name
282 * @return null|string
284 public static function from($name, $mode, $side) {
287 case 'civicrm_grant':
288 $from = " $side JOIN civicrm_grant ON civicrm_grant.contact_id = contact_a.id ";
292 $from .= " $side JOIN civicrm_option_group option_group_grant_status ON (option_group_grant_status.name = 'grant_status')";
293 $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 ) ";
297 $from .= " $side JOIN civicrm_option_group option_group_grant_type ON (option_group_grant_type.name = 'grant_type')";
298 if ($mode & CRM_Contact_BAO_Query
::MODE_GRANT
) {
299 $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 ) ";
302 $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 ) ";
307 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_grant' AND
308 civicrm_grant.id = civicrm_note.entity_id )";
315 * Getter for the qill object
319 public function qill() {
320 return (isset($this->_qill
)) ?
$this->_qill
: "";
325 * @param bool $includeCustomFields
329 static function defaultReturnProperties(
331 $includeCustomFields = TRUE
334 if ($mode & CRM_Contact_BAO_Query
::MODE_GRANT
) {
337 'contact_sub_type' => 1,
342 'grant_amount_requested' => 1,
343 'grant_application_received_date' => 1,
344 'grant_report_received' => 1,
345 'grant_money_transfer_date' => 1,
354 * Add all the elements shared between grant search and advanaced search
357 * @param CRM_Core_Form $form
362 public static function buildSearchForm(&$form) {
364 $grantType = CRM_Core_OptionGroup
::values('grant_type');
365 $form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE,
366 array('id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
369 $grantStatus = CRM_Core_OptionGroup
::values('grant_status');
370 $form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE,
371 array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
374 $form->addDate('grant_application_received_date_low', ts('App. Received Date - From'), FALSE, array('formatType' => 'searchDate'));
375 $form->addDate('grant_application_received_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
377 $form->addElement('checkbox', 'grant_application_received_notset', ts(''), NULL);
379 $form->addDate('grant_money_transfer_date_low', ts('Money Sent Date - From'), FALSE, array('formatType' => 'searchDate'));
380 $form->addDate('grant_money_transfer_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
382 $form->addElement('checkbox', 'grant_money_transfer_date_notset', ts(''), NULL);
384 $form->addDate('grant_due_date_low', ts('Report Due Date - From'), FALSE, array('formatType' => 'searchDate'));
385 $form->addDate('grant_due_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
387 $form->addElement('checkbox', 'grant_due_date_notset', ts(''), NULL);
389 $form->addDate('grant_decision_date_low', ts('Grant Decision Date - From'), FALSE, array('formatType' => 'searchDate'));
390 $form->addDate('grant_decision_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
392 $form->addElement('checkbox', 'grant_decision_date_notset', ts(''), NULL);
394 $form->addYesNo('grant_report_received', ts('Grant report received?'), TRUE);
396 $form->add('text', 'grant_amount_low', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8));
397 $form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money
::format('9.99', ' '))), 'money');
399 $form->add('text', 'grant_amount_high', ts('Maximum Amount'), array('size' => 8, 'maxlength' => 8));
400 $form->addRule('grant_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money
::format('99.99', ' '))), 'money');
402 // add all the custom searchable fields
403 $grant = array('Grant');
404 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $grant);
406 $form->assign('grantGroupTree', $groupDetails);
407 foreach ($groupDetails as $group) {
408 foreach ($group['fields'] as $field) {
409 $fieldId = $field['id'];
410 $elementName = 'custom_' . $fieldId;
411 CRM_Core_BAO_CustomField
::addQuickFormElement($form,
420 $form->assign('validGrant', TRUE);
427 public static function searchAction(&$row, $id) {
433 public static function tableNames(&$tables) {