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