Update version numbers to 4.4 and lint php
[civicrm-core.git] / CRM / Report / Form / Pledge / Pbnp.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Pledge_Pbnp extends CRM_Report_Form {
36 protected $_charts = array(
37 '' => 'Tabular',
38 'barChart' => 'Bar Chart',
39 'pieChart' => 'Pie Chart',
40 );
41 public $_drilldownReport = array('pledge/summary' => 'Link to Detail Report');
42
43 protected $_customGroupExtends = array(
44 'Pledge');
45
46 function __construct() {
47
48 // Check if CiviCampaign is a) enabled and b) has active campaigns
49 $config = CRM_Core_Config::singleton();
50 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
51 if ($campaignEnabled) {
52 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
53 $this->activeCampaigns = $getCampaigns['campaigns'];
54 asort($this->activeCampaigns);
55 }
56
57 $this->_columns = array(
58 'civicrm_contact' =>
59 array(
60 'dao' => 'CRM_Contact_DAO_Contact',
61 'fields' =>
62 array(
63 'sort_name' =>
64 array('title' => ts('Constituent Name'),
65 'required' => TRUE,
66 'no_repeat' => TRUE,
67 ),
68 'id' =>
69 array(
70 'no_display' => TRUE,
71 'required' => TRUE,
72 ),
73 ),
74 'grouping' => 'contact-fields',
75 ),
76 'civicrm_pledge' =>
77 array(
78 'dao' => 'CRM_Pledge_DAO_Pledge',
79 'fields' =>
80 array(
81 'pledge_create_date' =>
82 array('title' => ts('Pledge Made'),
83 'required' => TRUE,
84 ),
85 'financial_type_id' =>
86 array('title' => ts('Financial Type'),
87 'requried' => TRUE,
88 ),
89 'amount' =>
90 array('title' => ts('Amount'),
91 'required' => TRUE,
92 'type' => CRM_Utils_Type::T_MONEY,
93 ),
94 'currency' =>
95 array(
96 'required' => TRUE,
97 'no_display' => TRUE,
98 ),
99 'status_id' =>
100 array('title' => ts('Status'),
101 ),
102 ),
103 'filters' =>
104 array(
105 'pledge_create_date' =>
106 array(
107 'title' => ts('Pledge Made'),
108 'operatorType' => CRM_Report_Form::OP_DATE,
109 ),
110 'currency' =>
111 array('title' => 'Currency',
112 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
113 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
114 'default' => NULL,
115 'type' => CRM_Utils_Type::T_STRING,
116 ),
117 'financial_type_id' =>
118 array( 'title' => ts('Financial Type'),
119 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
120 'options' => CRM_Contribute_PseudoConstant::financialType(),
121 ),
122 ),
123 'grouping' => 'pledge-fields',
124 ),
125 'civicrm_pledge_payment' =>
126 array(
127 'dao' => 'CRM_Pledge_DAO_PledgePayment',
128 'fields' =>
129 array(
130 'scheduled_date' =>
131 array('title' => ts('Next Payment Due'),
132 'type' => CRM_Utils_Type::T_DATE,
133 'required' => TRUE,
134 ),
135 ),
136 'filters' =>
137 array(
138 'scheduled_date' =>
139 array(
140 'title' => ts('Next Payment Due'),
141 'operatorType' => CRM_Report_Form::OP_DATE,
142 ),
143 ),
144 'grouping' => 'pledge-fields',
145 ),
146 'civicrm_address' =>
147 array(
148 'dao' => 'CRM_Core_DAO_Address',
149 'fields' =>
150 array(
151 'street_address' => NULL,
152 'city' => NULL,
153 'postal_code' => NULL,
154 'state_province_id' =>
155 array('title' => ts('State/Province'),
156 ),
157 'country_id' =>
158 array('title' => ts('Country'),
159 'default' => TRUE,
160 ),
161 ),
162 'grouping' => 'contact-fields',
163 ),
164 'civicrm_email' =>
165 array(
166 'dao' => 'CRM_Core_DAO_Email',
167 'fields' =>
168 array('email' => NULL),
169 'grouping' => 'contact-fields',
170 ),
171 'civicrm_group' =>
172 array(
173 'dao' => 'CRM_Contact_DAO_Group',
174 'alias' => 'cgroup',
175 'filters' =>
176 array(
177 'gid' =>
178 array(
179 'name' => 'group_id',
180 'title' => ts('Group'),
181 'group' => TRUE,
182 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
183 'options' => CRM_Core_PseudoConstant::staticGroup(),
184 ),
185 ),
186 ),
187 );
188
189 // If we have a campaign, build out the relevant elements
190 if ($campaignEnabled && !empty($this->activeCampaigns)) {
191 $this->_columns['civicrm_pledge']['fields']['campaign_id'] = array(
192 'title' => 'Campaign',
193 'default' => 'false',
194 );
195 $this->_columns['civicrm_pledge']['filters']['campaign_id'] = array('title' => ts('Campaign'),
196 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
197 'options' => $this->activeCampaigns,
198 );
199 }
200
201 $this->_tagFilter = TRUE;
202 $this->_currencyColumn = 'civicrm_pledge_currency';
203 parent::__construct();
204 }
205
206 function preProcess() {
207 $this->assign('reportTitle', ts('Pledge But Not Paid Report'));
208 parent::preProcess();
209 }
210
211 function select() {
212 $select = array();
213 $this->_columnHeaders = array();
214 foreach ($this->_columns as $tableName => $table) {
215 if (array_key_exists('fields', $table)) {
216 foreach ($table['fields'] as $fieldName => $field) {
217 if (CRM_Utils_Array::value('required', $field) ||
218 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
219 ) {
220 // to include optional columns address and email, only if checked
221 if ($tableName == 'civicrm_address') {
222 $this->_addressField = TRUE;
223 $this->_emailField = TRUE;
224 }
225 elseif ($tableName == 'civicrm_email') {
226 $this->_emailField = TRUE;
227 }
228 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
229 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
230 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
231 }
232 }
233 }
234 }
235 $this->_select = "SELECT " . implode(', ', $select) . " ";
236 }
237
238 function from() {
239 $this->_from = NULL;
240
241 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
242 $pendingStatus = array_search('Pending', $allStatus);
243 foreach (array(
244 'Pending', 'In Progress', 'Overdue') as $statusKey) {
245 if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
246 $unpaidStatus[] = $key;
247 }
248 }
249
250 $statusIds = implode(', ', $unpaidStatus);
251
252 $this->_from = "
253 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
254 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
255 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
256 {$this->_aliases['civicrm_contact']}.id) AND
257 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )
258 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']}
259 ON ({$this->_aliases['civicrm_pledge']}.id =
260 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
261
262 // include address field if address column is to be included
263 if ($this->_addressField) {
264 $this->_from .= "
265 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
266 ON ({$this->_aliases['civicrm_contact']}.id =
267 {$this->_aliases['civicrm_address']}.contact_id) AND
268 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
269 }
270
271 // include email field if email column is to be included
272 if ($this->_emailField) {
273 $this->_from .= "
274 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
275 ON ({$this->_aliases['civicrm_contact']}.id =
276 {$this->_aliases['civicrm_email']}.contact_id) AND
277 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
278 }
279 }
280
281 function groupBy() {
282 $this->_groupBy = "
283 GROUP BY {$this->_aliases['civicrm_pledge']}.contact_id,
284 {$this->_aliases['civicrm_pledge']}.id,
285 {$this->_aliases['civicrm_pledge']}.currency";
286 }
287
288 function orderBy() {
289 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
290 }
291
292 function postProcess() {
293 // get the acl clauses built before we assemble the query
294 $this->buildACLClause($this->_aliases['civicrm_contact']);
295 parent::PostProcess();
296 }
297
298 function alterDisplay(&$rows) {
299 // custom code to alter rows
300 $entryFound = FALSE;
301 $checkList = array();
302 $display_flag = $prev_cid = $cid = 0;
303
304 foreach ($rows as $rowNum => $row) {
305 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
306 // don't repeat contact details if its same as the previous row
307 if (array_key_exists('civicrm_contact_id', $row)) {
308 if ($cid = $row['civicrm_contact_id']) {
309 if ($rowNum == 0) {
310 $prev_cid = $cid;
311 }
312 else {
313 if ($prev_cid == $cid) {
314 $display_flag = 1;
315 $prev_cid = $cid;
316 }
317 else {
318 $display_flag = 0;
319 $prev_cid = $cid;
320 }
321 }
322
323 if ($display_flag) {
324 foreach ($row as $colName => $colVal) {
325 if (in_array($colName, $this->_noRepeats)) {
326 unset($rows[$rowNum][$colName]);
327 }
328 }
329 }
330 $entryFound = TRUE;
331 }
332 }
333 }
334
335 //handle the Financial Type Ids
336 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
337 if ($value = $row['civicrm_pledge_financial_type_id']) {
338 $rows[$rowNum]['civicrm_pledge_financial_type_id'] =
339 CRM_Contribute_PseudoConstant::financialType($value, false);
340 }
341 $entryFound = TRUE;
342 }
343
344 //handle the Status Ids
345 if (array_key_exists('civicrm_pledge_status_id', $row)) {
346 if ($value = $row['civicrm_pledge_status_id']) {
347 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
348 }
349 $entryFound = TRUE;
350 }
351
352 // handle state province
353 if (array_key_exists('civicrm_address_state_province_id', $row)) {
354 if ($value = $row['civicrm_address_state_province_id']) {
355 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
356 }
357 $entryFound = TRUE;
358 }
359
360 // handle country
361 if (array_key_exists('civicrm_address_country_id', $row)) {
362 if ($value = $row['civicrm_address_country_id']) {
363 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
364 }
365 $entryFound = TRUE;
366 }
367
368 // convert display name to links
369 if (array_key_exists('civicrm_contact_sort_name', $row) &&
370 array_key_exists('civicrm_contact_id', $row)
371 ) {
372 $url = CRM_Report_Utils_Report::getNextUrl('pledge/summary',
373 'reset=1&force=1&id_op=eq&id_value=' .
374 $row['civicrm_contact_id'],
375 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
376 );
377 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
378 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
379 $entryFound = TRUE;
380 }
381
382 // If using campaigns, convert campaign_id to campaign title
383 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
384 if ($value = $row['civicrm_pledge_campaign_id']) {
385 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->activeCampaigns[$value];
386 }
387 $entryFound = TRUE;
388 }
389
390 // skip looking further in rows, if first row itself doesn't
391 // have the column we need
392 if (!$entryFound) {
393 break;
394 }
395 }
396 }
397 }
398