Merge pull request #10198 from eileenmcnaughton/iida3
[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 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Report_Form_Grant_Detail extends CRM_Report_Form {
34
35 protected $_addressField = FALSE;
36
37 protected $_customGroupExtends = array(
21dfd5f5 38 'Grant',
9d72cede 39 );
6a488035 40
74cf4551 41 /**
73b448bf 42 * Class constructor.
74cf4551 43 */
00be9182 44 public function __construct() {
6a488035 45 $this->_columns = array(
9d72cede 46 'civicrm_contact' => array(
6a488035 47 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
48 'fields' => array(
49 'sort_name' => array(
50 'title' => ts('Contact Name'),
6a488035
TO
51 'required' => TRUE,
52 'no_repeat' => TRUE,
53 ),
9d72cede 54 'id' => array(
6a488035
TO
55 'no_display' => TRUE,
56 'required' => TRUE,
57 ),
58 ),
59 'grouping' => 'contact-fields',
9d72cede
EM
60 'filters' => array(
61 'sort_name' => array(
62 'title' => ts('Contact Name'),
6a488035
TO
63 'operator' => 'like',
64 ),
9d72cede
EM
65 'gender_id' => array(
66 'title' => ts('Gender'),
6a488035 67 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
26cf88b5 68 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
6a488035 69 ),
9d72cede
EM
70 'id' => array(
71 'title' => ts('Contact ID'),
6a488035
TO
72 'no_display' => TRUE,
73 ),
74 ),
75 ),
9d72cede 76 'civicrm_address' => array(
6a488035 77 'dao' => 'CRM_Core_DAO_Address',
9d72cede
EM
78 'filters' => array(
79 'country_id' => array(
80 'title' => ts('Country'),
09b5cdcf 81 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
82 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
83 'options' => CRM_Core_PseudoConstant::country(),
84 ),
9d72cede
EM
85 'state_province_id' => array(
86 'title' => ts('State/Province'),
09b5cdcf 87 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
88 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
89 'options' => CRM_Core_PseudoConstant::stateProvince(),
90 ),
91 ),
92 ),
9d72cede 93 'civicrm_grant' => array(
6a488035 94 'dao' => 'CRM_Grant_DAO_Grant',
9d72cede
EM
95 'fields' => array(
96 'grant_type_id' => array(
6a488035
TO
97 'name' => 'grant_type_id',
98 'title' => ts('Grant Type'),
99 ),
9d72cede 100 'status_id' => array(
6a488035
TO
101 'name' => 'status_id',
102 'title' => ts('Grant Status'),
103 ),
9d72cede 104 'amount_total' => array(
6a488035
TO
105 'name' => 'amount_total',
106 'title' => ts('Amount Requested'),
107 'type' => CRM_Utils_Type::T_MONEY,
108 ),
9d72cede 109 'amount_granted' => array(
6a488035
TO
110 'name' => 'amount_granted',
111 'title' => ts('Amount Granted'),
112 ),
9d72cede 113 'application_received_date' => array(
6a488035
TO
114 'name' => 'application_received_date',
115 'title' => ts('Application Received'),
116 'default' => TRUE,
117 ),
9d72cede 118 'money_transfer_date' => array(
6a488035
TO
119 'name' => 'money_transfer_date',
120 'title' => ts('Money Transfer Date'),
121 'type' => CRM_Utils_Type::T_DATE,
122 ),
9d72cede 123 'grant_due_date' => array(
6a488035
TO
124 'name' => 'grant_due_date',
125 'title' => ts('Grant Report Due'),
126 'type' => CRM_Utils_Type::T_DATE,
127 ),
9d72cede 128 'decision_date' => array(
6a488035
TO
129 'name' => 'decision_date',
130 'title' => ts('Grant Decision Date'),
131 'type' => CRM_Utils_Type::T_DATE,
132 ),
9d72cede 133 'rationale' => array(
6a488035
TO
134 'name' => 'rationale',
135 'title' => ts('Rationale'),
136 ),
9d72cede 137 'grant_report_received' => array(
6a488035
TO
138 'name' => 'grant_report_received',
139 'title' => ts('Grant Report Received'),
140 ),
141 ),
9d72cede
EM
142 'filters' => array(
143 'grant_type' => array(
6a488035
TO
144 'name' => 'grant_type_id',
145 'title' => ts('Grant Type'),
146 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
fb1fd730 147 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'),
6a488035 148 ),
9d72cede 149 'status_id' => array(
6a488035
TO
150 'name' => 'status_id',
151 'title' => ts('Grant Status'),
09b5cdcf 152 'type' => CRM_Utils_Type::T_INT,
6a488035 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}