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