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