Merge branch 'phpunit-ob-fix' of https://github.com/giant-rabbit/civicrm-core into...
[civicrm-core.git] / CRM / Pledge / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42class CRM_Pledge_Selector_Search extends CRM_Core_Selector_Base {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
48 * @static
49 */
50 static $_links = NULL;
51
52 /**
100fef9d 53 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
54 *
55 * @var array
56 * @static
57 */
58 static $_columnHeaders;
59
60 /**
61 * Properties of contact we're interested in displaying
62 * @var array
63 * @static
64 */
65 static $_properties = array(
66 'contact_id',
67 'sort_name',
68 'display_name',
69 'pledge_id',
70 'pledge_amount',
71 'pledge_create_date',
72 'pledge_total_paid',
73 'pledge_next_pay_date',
74 'pledge_next_pay_amount',
75 'pledge_outstanding_amount',
76 'pledge_status_id',
77 'pledge_status',
78 'pledge_is_test',
79 'pledge_contribution_page_id',
6d3864ef 80 'pledge_financial_type',
6a488035
TO
81 'pledge_campaign_id',
82 'pledge_currency',
83 );
84
85 /**
100fef9d 86 * Are we restricting ourselves to a single contact
6a488035
TO
87 *
88 * @access protected
89 * @var boolean
90 */
91 protected $_single = FALSE;
92
93 /**
100fef9d 94 * Are we restricting ourselves to a single contact
6a488035
TO
95 *
96 * @access protected
97 * @var boolean
98 */
99 protected $_limit = NULL;
100
101 /**
100fef9d 102 * What context are we being invoked from
6a488035
TO
103 *
104 * @access protected
105 * @var string
106 */
107 protected $_context = NULL;
108
109 /**
100fef9d 110 * QueryParams is the array returned by exportValues called on
6a488035
TO
111 * the HTML_QuickForm_Controller for that page.
112 *
113 * @var array
114 * @access protected
115 */
116 public $_queryParams;
117
118 /**
100fef9d 119 * Represent the type of selector
6a488035
TO
120 *
121 * @var int
122 * @access protected
123 */
124 protected $_action;
125
126 /**
127 * The additional clause that we restrict the search with
128 *
129 * @var string
130 */
131 protected $_additionalClause = NULL;
132
133 /**
134 * The query object
135 *
136 * @var string
137 */
138 protected $_query;
139
140 /**
141 * Class constructor
142 *
fd31fa4c
EM
143 * @param array $queryParams array of parameters for query
144 * @param \const|int $action - action of search basic or advanced.
145 * @param string $additionalClause if the caller wants to further restrict the search (used in participations)
6a488035 146 * @param boolean $single are we dealing only with one contact?
fd31fa4c 147 * @param int $limit how many signers do we want returned
6a488035 148 *
fd31fa4c
EM
149 * @param string $context
150 *
151 * @return \CRM_Pledge_Selector_Search
152 @access public
6a488035
TO
153 */
154 function __construct(&$queryParams,
155 $action = CRM_Core_Action::NONE,
156 $additionalClause = NULL,
157 $single = FALSE,
158 $limit = NULL,
159 $context = 'search'
160 ) {
161 // submitted form values
162 $this->_queryParams = &$queryParams;
163
164 $this->_single = $single;
165 $this->_limit = $limit;
166 $this->_context = $context;
167
168 $this->_additionalClause = $additionalClause;
169
170 // type of selector
171 $this->_action = $action;
172
173 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE,
174 CRM_Contact_BAO_Query::MODE_PLEDGE
175 );
176
177 $this->_query->_distinctComponentClause = "civicrm_pledge.id";
178 $this->_query->_groupByComponentClause = " GROUP BY civicrm_pledge.id ";
179 }
6a488035
TO
180
181 /**
182 * This method returns the links that are given for each search row.
183 * currently the links added for each row are
184 *
185 * - View
186 * - Edit
187 *
188 * @return array
189 * @access public
190 *
191 */
9f6f062c
DL
192 static function &links() {
193 $args = func_get_args();
194 $hideOption = CRM_Utils_Array::value(0, $args);
195 $key = CRM_Utils_Array::value(1, $args);
196
6a488035
TO
197 $extraParams = ($key) ? "&key={$key}" : NULL;
198
199 $cancelExtra = ts('Cancelling this pledge will also cancel any scheduled (and not completed) pledge payments.') . ' ' . ts('This action cannot be undone.') . ' ' . ts('Do you want to continue?');
200 self::$_links = array(
201 CRM_Core_Action::VIEW => array(
202 'name' => ts('View'),
203 'url' => 'civicrm/contact/view/pledge',
204 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=pledge' . $extraParams,
205 'title' => ts('View Pledge'),
206 ),
207 CRM_Core_Action::UPDATE => array(
208 'name' => ts('Edit'),
209 'url' => 'civicrm/contact/view/pledge',
210 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
211 'title' => ts('Edit Pledge'),
212 ),
213 CRM_Core_Action::DETACH => array(
214 'name' => ts('Cancel'),
215 'url' => 'civicrm/contact/view/pledge',
216 'qs' => 'reset=1&action=detach&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
217 'extra' => 'onclick = "return confirm(\'' . $cancelExtra . '\');"',
218 'title' => ts('Cancel Pledge'),
219 ),
220 CRM_Core_Action::DELETE => array(
221 'name' => ts('Delete'),
222 'url' => 'civicrm/contact/view/pledge',
223 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
224 'title' => ts('Delete Pledge'),
225 ),
226 );
227
228
229 if (in_array('Cancel', $hideOption)) {
230 unset(self::$_links[CRM_Core_Action::DETACH]);
231 }
232
233 return self::$_links;
234 }
6a488035
TO
235
236 /**
100fef9d 237 * Getter for array of the parameters required for creating pager.
6a488035 238 *
da6b46f4 239 * @param $action
c490a46a 240 * @param array $params
da6b46f4 241 *
6a488035
TO
242 * @access public
243 */
244 function getPagerParams($action, &$params) {
245 $params['status'] = ts('Pledge') . ' %%StatusMessage%%';
246 $params['csvString'] = NULL;
247 if ($this->_limit) {
248 $params['rowCount'] = $this->_limit;
249 }
250 else {
251 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
252 }
253
254 $params['buttonTop'] = 'PagerTopButton';
255 $params['buttonBottom'] = 'PagerBottomButton';
256 }
6a488035
TO
257
258 /**
259 * Returns total number of rows for the query.
260 *
261 * @param
262 *
263 * @return int Total number of rows
264 * @access public
265 */
266 function getTotalCount($action) {
267 return $this->_query->searchQuery(0, 0, NULL,
268 TRUE, FALSE,
269 FALSE, FALSE,
270 FALSE,
271 $this->_additionalClause
272 );
273 }
274
275 /**
100fef9d 276 * Returns all the rows in the given offset and rowCount
6a488035
TO
277 *
278 * @param enum $action the action being performed
279 * @param int $offset the row number to start from
280 * @param int $rowCount the number of rows to return
281 * @param string $sort the sql string that describes the sort order
282 * @param enum $output what should the result set include (web/email/csv)
283 *
284 * @return int the total number of rows for this action
285 */
286 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
287 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
288 FALSE, FALSE,
289 FALSE, FALSE,
290 FALSE,
291 $this->_additionalClause
292 );
293
294 // process the result of the query
295 $rows = array();
296
297 // get all pledge status
298 $pledgeStatuses = CRM_Core_OptionGroup::values('contribution_status',
299 FALSE, FALSE, FALSE, NULL, 'name', FALSE
300 );
301
302 //get all campaigns.
303 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
304
305 //4418 check for view, edit and delete
306 $permissions = array(CRM_Core_Permission::VIEW);
307 if (CRM_Core_Permission::check('edit pledges')) {
308 $permissions[] = CRM_Core_Permission::EDIT;
309 }
310 if (CRM_Core_Permission::check('delete in CiviPledge')) {
311 $permissions[] = CRM_Core_Permission::DELETE;
312 }
313 $mask = CRM_Core_Action::mask($permissions);
314
315 while ($result->fetch()) {
316 $row = array();
317 // the columns we are interested in
318 foreach (self::$_properties as $property) {
319 if (isset($result->$property)) {
320 $row[$property] = $result->$property;
321 }
322 }
323
324 //carry campaign on selectors.
325 $row['campaign'] = CRM_Utils_Array::value($result->pledge_campaign_id, $allCampaigns);
326 $row['campaign_id'] = $result->pledge_campaign_id;
327
328 // add pledge status name
329 $row['pledge_status_name'] = CRM_Utils_Array::value($row['pledge_status_id'],
330 $pledgeStatuses
331 );
332 // append (test) to status label
a7488080 333 if (!empty($row['pledge_is_test'])) {
6a488035
TO
334 $row['pledge_status'] .= ' (test)';
335 }
336
337 $hideOption = array();
338 if (CRM_Utils_Array::key('Cancelled', $row) ||
339 CRM_Utils_Array::key('Completed', $row)
340 ) {
341 $hideOption[] = 'Cancel';
342 }
343
344 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->pledge_id;
345
346 $row['action'] = CRM_Core_Action::formLink(self::links($hideOption, $this->_key),
347 $mask,
348 array(
349 'id' => $result->pledge_id,
350 'cid' => $result->contact_id,
351 'cxt' => $this->_context,
87dab4a4
AH
352 ),
353 ts('more'),
354 FALSE,
355 'pledge.selector.row',
356 'Pledge',
357 $result->pledge_id
6a488035
TO
358 );
359
360
361 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
362 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
363 );
364 $rows[] = $row;
365 }
366 return $rows;
367 }
368
369 /**
6a488035
TO
370 * @return array $qill which contains an array of strings
371 * @access public
372 */
373
374 // the current internationalisation is bad, but should more or less work
375 // for most of "European" languages
376 public function getQILL() {
377 return $this->_query->qill();
378 }
379
380 /**
100fef9d 381 * Returns the column headers as an array of tuples:
6a488035
TO
382 * (name, sortName (key to the sort array))
383 *
384 * @param string $action the action being performed
385 * @param enum $output what should the result set include (web/email/csv)
386 *
387 * @return array the column headers that need to be displayed
388 * @access public
389 */
390 public function &getColumnHeaders($action = NULL, $output = NULL) {
391 if (!isset(self::$_columnHeaders)) {
392 self::$_columnHeaders = array(
393 array(
394 'name' => ts('Pledged'),
395 'sort' => 'pledge_amount',
396 'direction' => CRM_Utils_Sort::DONTCARE,
397 ),
398 array(
399 'name' => ts('Total Paid'),
400 'sort' => 'pledge_total_paid',
401 'direction' => CRM_Utils_Sort::DONTCARE,
402 ),
403 array(
404 'name' => ts('Balance'),
405 ),
406 array(
407 'name' => ts('Pledged For'),
6d3864ef 408 'sort' => 'pledge_financial_type',
6a488035
TO
409 'direction' => CRM_Utils_Sort::DONTCARE,
410 ),
411 array(
412 'name' => ts('Pledge Made'),
413 'sort' => 'pledge_create_date',
414 'direction' => CRM_Utils_Sort::DESCENDING,
415 ),
416 array(
417 'name' => ts('Next Pay Date'),
418 'sort' => 'pledge_next_pay_date',
419 'direction' => CRM_Utils_Sort::DONTCARE,
420 ),
421 array(
422 'name' => ts('Next Amount'),
423 'sort' => 'pledge_next_pay_amount',
424 'direction' => CRM_Utils_Sort::DONTCARE,
425 ),
426 array(
427 'name' => ts('Status'),
428 'sort' => 'pledge_status',
429 'direction' => CRM_Utils_Sort::DONTCARE,
430 ),
431 array('desc' => ts('Actions')),
432 );
433
434 if (!$this->_single) {
435 $pre = array(
7b99ead3 436 array('desc' => ts('Contact ID')),
6a488035
TO
437 array(
438 'name' => ts('Name'),
439 'sort' => 'sort_name',
440 'direction' => CRM_Utils_Sort::DONTCARE,
441 ),
442 );
443
444 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
445 }
446 }
447 return self::$_columnHeaders;
448 }
449
ffd93213
EM
450 /**
451 * @return string
452 */
6a488035
TO
453 function &getQuery() {
454 return $this->_query;
455 }
456
457 /**
100fef9d 458 * Name of export file.
6a488035
TO
459 *
460 * @param string $output type of output
461 *
462 * @return string name of the file
463 */
464 function getExportFileName($output = 'csv') {
465 return ts('Pledge Search');
466 }
467}
6a488035 468