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