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