Merge pull request #7806 from eileenmcnaughton/CRM-18033
[civicrm-core.git] / CRM / Report / Form / Grant / Detail.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Grant_Detail extends CRM_Report_Form {
36
37 protected $_addressField = FALSE;
38
39 protected $_customGroupExtends = array(
21dfd5f5 40 'Grant',
9d72cede 41 );
6a488035 42
74cf4551 43 /**
74cf4551
EM
44 */
45 /**
74cf4551 46 */
00be9182 47 public function __construct() {
6a488035 48 $this->_columns = array(
9d72cede 49 'civicrm_contact' => array(
6a488035 50 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
51 'fields' => array(
52 'sort_name' => array(
53 'title' => ts('Contact Name'),
6a488035
TO
54 'required' => TRUE,
55 'no_repeat' => TRUE,
56 ),
9d72cede 57 'id' => array(
6a488035
TO
58 'no_display' => TRUE,
59 'required' => TRUE,
60 ),
61 ),
62 'grouping' => 'contact-fields',
9d72cede
EM
63 'filters' => array(
64 'sort_name' => array(
65 'title' => ts('Contact Name'),
6a488035
TO
66 'operator' => 'like',
67 ),
9d72cede
EM
68 'gender_id' => array(
69 'title' => ts('Gender'),
6a488035 70 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
26cf88b5 71 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
6a488035 72 ),
9d72cede
EM
73 'id' => array(
74 'title' => ts('Contact ID'),
6a488035
TO
75 'no_display' => TRUE,
76 ),
77 ),
78 ),
9d72cede 79 'civicrm_address' => array(
6a488035 80 'dao' => 'CRM_Core_DAO_Address',
9d72cede
EM
81 'filters' => array(
82 'country_id' => array(
83 'title' => ts('Country'),
6a488035
TO
84 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
85 'options' => CRM_Core_PseudoConstant::country(),
86 ),
9d72cede
EM
87 'state_province_id' => array(
88 'title' => ts('State/Province'),
6a488035
TO
89 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
90 'options' => CRM_Core_PseudoConstant::stateProvince(),
91 ),
92 ),
93 ),
9d72cede 94 'civicrm_grant' => array(
6a488035 95 'dao' => 'CRM_Grant_DAO_Grant',
9d72cede
EM
96 'fields' => array(
97 'grant_type_id' => array(
6a488035
TO
98 'name' => 'grant_type_id',
99 'title' => ts('Grant Type'),
100 ),
9d72cede 101 'status_id' => array(
6a488035
TO
102 'name' => 'status_id',
103 'title' => ts('Grant Status'),
104 ),
9d72cede 105 'amount_total' => array(
6a488035
TO
106 'name' => 'amount_total',
107 'title' => ts('Amount Requested'),
108 'type' => CRM_Utils_Type::T_MONEY,
109 ),
9d72cede 110 'amount_granted' => array(
6a488035
TO
111 'name' => 'amount_granted',
112 'title' => ts('Amount Granted'),
113 ),
9d72cede 114 'application_received_date' => array(
6a488035
TO
115 'name' => 'application_received_date',
116 'title' => ts('Application Received'),
117 'default' => TRUE,
118 ),
9d72cede 119 'money_transfer_date' => array(
6a488035
TO
120 'name' => 'money_transfer_date',
121 'title' => ts('Money Transfer Date'),
122 'type' => CRM_Utils_Type::T_DATE,
123 ),
9d72cede 124 'grant_due_date' => array(
6a488035
TO
125 'name' => 'grant_due_date',
126 'title' => ts('Grant Report Due'),
127 'type' => CRM_Utils_Type::T_DATE,
128 ),
9d72cede 129 'decision_date' => array(
6a488035
TO
130 'name' => 'decision_date',
131 'title' => ts('Grant Decision Date'),
132 'type' => CRM_Utils_Type::T_DATE,
133 ),
9d72cede 134 'rationale' => array(
6a488035
TO
135 'name' => 'rationale',
136 'title' => ts('Rationale'),
137 ),
9d72cede 138 'grant_report_received' => array(
6a488035
TO
139 'name' => 'grant_report_received',
140 'title' => ts('Grant Report Received'),
141 ),
142 ),
9d72cede
EM
143 'filters' => array(
144 'grant_type' => array(
6a488035
TO
145 'name' => 'grant_type_id',
146 'title' => ts('Grant Type'),
147 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
fb1fd730 148 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'),
6a488035 149 ),
9d72cede 150 'status_id' => array(
6a488035
TO
151 'name' => 'status_id',
152 'title' => ts('Grant Status'),
153 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
fb1fd730 154 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'),
6a488035 155 ),
9d72cede 156 'amount_granted' => array(
6a488035
TO
157 'title' => ts('Amount Granted'),
158 'operatorType' => CRM_Report_Form::OP_INT,
159 ),
9d72cede 160 'amount_total' => array(
6a488035
TO
161 'title' => ts('Amount Requested'),
162 'operatorType' => CRM_Report_Form::OP_INT,
163 ),
9d72cede 164 'application_received_date' => array(
6a488035
TO
165 'title' => ts('Application Received'),
166 'operatorType' => CRM_Report_Form::OP_DATE,
0e214fe2 167 'type' => CRM_Utils_Type::T_DATE,
6a488035 168 ),
9d72cede 169 'money_transfer_date' => array(
6a488035
TO
170 'title' => ts('Money Transfer Date'),
171 'operatorType' => CRM_Report_Form::OP_DATE,
0e214fe2 172 'type' => CRM_Utils_Type::T_DATE,
6a488035 173 ),
9d72cede 174 'grant_due_date' => array(
6a488035
TO
175 'title' => ts('Grant Report Due'),
176 'operatorType' => CRM_Report_Form::OP_DATE,
0e214fe2 177 'type' => CRM_Utils_Type::T_DATE,
6a488035 178 ),
9d72cede 179 'decision_date' => array(
6a488035
TO
180 'title' => ts('Grant Decision Date'),
181 'operatorType' => CRM_Report_Form::OP_DATE,
182 'type' => CRM_Utils_Type::T_DATE,
183 ),
184 ),
9d72cede
EM
185 'group_bys' => array(
186 'grant_type_id' => array(
6a488035
TO
187 'title' => ts('Grant Type'),
188 ),
9d72cede 189 'status_id' => array(
6a488035
TO
190 'title' => ts('Grant Status'),
191 ),
9d72cede 192 'amount_total' => array(
6a488035
TO
193 'title' => ts('Amount Requested'),
194 ),
9d72cede 195 'amount_granted' => array(
6a488035
TO
196 'title' => ts('Amount Granted'),
197 ),
9d72cede 198 'application_received_date' => array(
6a488035
TO
199 'title' => ts('Application Received Date'),
200 ),
9d72cede 201 'money_transfer_date' => array(
6a488035
TO
202 'title' => ts('Money Transfer Date'),
203 ),
9d72cede 204 'decision_date' => array(
6a488035
TO
205 'title' => ts('Grant Decision Date'),
206 ),
207 ),
208 ),
209 );
210
211 parent::__construct();
212 }
213
00be9182 214 public function select() {
6a488035
TO
215 $select = array();
216
217 $this->_columnHeaders = array();
218 foreach ($this->_columns as $tableName => $table) {
219 if ($tableName == 'civicrm_address') {
220 $this->_addressField = TRUE;
221 }
222 if (array_key_exists('fields', $table)) {
223 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
224 if (!empty($field['required']) ||
225 !empty($this->_params['fields'][$fieldName])
226 ) {
6a488035
TO
227
228 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
229
230 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
231 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
232 }
233 }
234 }
235 }
236
237 $this->_select = "SELECT " . implode(', ', $select) . " ";
238 }
239
00be9182 240 public function from() {
6a488035
TO
241 $this->_from = "
242 FROM civicrm_grant {$this->_aliases['civicrm_grant']}
b2708486 243 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
6a488035
TO
244 ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) ";
245 if ($this->_addressField) {
246 $this->_from .= "
b2708486
DL
247 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
248 ON {$this->_aliases['civicrm_contact']}.id =
249 {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
250 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
251 }
252 }
253
00be9182 254 public function where() {
6a488035
TO
255 $clauses = array();
256 $this->_where = '';
257 foreach ($this->_columns as $tableName => $table) {
258 if (array_key_exists('filters', $table)) {
259 foreach ($table['filters'] as $fieldName => $field) {
260
261 $clause = NULL;
262 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
263 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
264 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
265 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
266
267 if ($relative || $from || $to) {
268 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
269 }
270 }
271 else {
272 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
273 if ($op) {
274 $clause = $this->whereClause($field,
275 $op,
276 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
277 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
278 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
279 );
280 }
281 }
282 if (!empty($clause)) {
283 $clauses[] = $clause;
284 $this->_where = "WHERE " . implode(' AND ', $clauses);
285 }
286 }
287 }
288 }
289 }
290
00be9182 291 public function groupBy() {
6a488035 292 $this->_groupBy = "";
a7488080 293 if (!empty($this->_params['group_bys']) &&
6a488035
TO
294 is_array($this->_params['group_bys']) &&
295 !empty($this->_params['group_bys'])
296 ) {
297 foreach ($this->_columns as $tableName => $table) {
298 if (array_key_exists('group_bys', $table)) {
299 foreach ($table['group_bys'] as $fieldName => $field) {
a7488080 300 if (!empty($this->_params['group_bys'][$fieldName])) {
6a488035
TO
301 $this->_groupBy[] = $field['dbAlias'];
302 }
303 }
304 }
305 }
306 }
307 if (!empty($this->_groupBy)) {
9d72cede
EM
308 $this->_groupBy = "ORDER BY " . implode(', ', $this->_groupBy) .
309 ", {$this->_aliases['civicrm_contact']}.sort_name";
6a488035
TO
310 }
311 else {
312 $this->_groupBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name";
313 }
314 }
315
74cf4551 316 /**
ced9bfed
EM
317 * Alter display of rows.
318 *
319 * Iterate through the rows retrieved via SQL and make changes for display purposes,
320 * such as rendering contacts as links.
321 *
322 * @param array $rows
323 * Rows generated by SQL, with an array for each row.
74cf4551 324 */
00be9182 325 public function alterDisplay(&$rows) {
6a488035
TO
326 $entryFound = FALSE;
327 foreach ($rows as $rowNum => $row) {
328 // convert display name to links
329 if (array_key_exists('civicrm_contact_sort_name', $row) &&
330 array_key_exists('civicrm_contact_id', $row)
331 ) {
332 $url = CRM_Utils_System::url('civicrm/contact/view',
b2708486 333 'reset=1&cid=' . $row['civicrm_contact_id'],
6a488035
TO
334 $this->_absoluteUrl
335 );
336 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
337 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View contact details for this record.");
338 $entryFound = TRUE;
339 }
340
341 if (array_key_exists('civicrm_grant_grant_type_id', $row)) {
342 if ($value = $row['civicrm_grant_grant_type_id']) {
a8c23526 343 $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'grant_type_id', $value);
6a488035
TO
344 }
345 $entryFound = TRUE;
346 }
6a488035
TO
347 if (array_key_exists('civicrm_grant_status_id', $row)) {
348 if ($value = $row['civicrm_grant_status_id']) {
a8c23526 349 $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'status_id', $value);
6a488035
TO
350 }
351 $entryFound = TRUE;
352 }
353 if (array_key_exists('civicrm_grant_grant_report_received', $row)) {
354 if ($value = $row['civicrm_grant_grant_report_received']) {
355 if ($value == 1) {
356 $value = 'Yes';
357 }
358 else {
359 $value = 'No';
360 }
361 $rows[$rowNum]['civicrm_grant_grant_report_received'] = $value;
362 }
363 $entryFound = TRUE;
364 }
365 if (!$entryFound) {
366 break;
367 }
368 }
369 }
96025800 370
6a488035 371}