Merge branch 4.5 into master
[civicrm-core.git] / CRM / Pledge / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 87 *
6a488035
TO
88 * @var boolean
89 */
90 protected $_single = FALSE;
91
92 /**
100fef9d 93 * Are we restricting ourselves to a single contact
6a488035 94 *
6a488035
TO
95 * @var boolean
96 */
97 protected $_limit = NULL;
98
99 /**
100fef9d 100 * What context are we being invoked from
6a488035 101 *
6a488035
TO
102 * @var string
103 */
104 protected $_context = NULL;
105
106 /**
100fef9d 107 * QueryParams is the array returned by exportValues called on
6a488035
TO
108 * the HTML_QuickForm_Controller for that page.
109 *
110 * @var array
6a488035
TO
111 */
112 public $_queryParams;
113
114 /**
100fef9d 115 * Represent the type of selector
6a488035
TO
116 *
117 * @var int
6a488035
TO
118 */
119 protected $_action;
120
121 /**
122 * The additional clause that we restrict the search with
123 *
124 * @var string
125 */
126 protected $_additionalClause = NULL;
127
128 /**
129 * The query object
130 *
131 * @var string
132 */
133 protected $_query;
134
135 /**
136 * Class constructor
137 *
3a1617b6
TO
138 * @param array $queryParams
139 * Array of parameters for query.
fd31fa4c 140 * @param \const|int $action - action of search basic or advanced.
3a1617b6
TO
141 * @param string $additionalClause
142 * If the caller wants to further restrict the search (used in participations).
143 * @param bool $single
144 * Are we dealing only with one contact?.
145 * @param int $limit
146 * How many signers do we want returned.
6a488035 147 *
fd31fa4c
EM
148 * @param string $context
149 *
150 * @return \CRM_Pledge_Selector_Search
151 @access public
6a488035 152 */
3295515a
TO
153 function __construct(
154 &$queryParams,
098201d8 155 $action = CRM_Core_Action::NONE,
6a488035 156 $additionalClause = NULL,
098201d8
TO
157 $single = FALSE,
158 $limit = NULL,
159 $context = 'search'
6a488035
TO
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
6a488035 189 */
00be9182 190 public static function &links() {
9f6f062c
DL
191 $args = func_get_args();
192 $hideOption = CRM_Utils_Array::value(0, $args);
193 $key = CRM_Utils_Array::value(1, $args);
194
6a488035
TO
195 $extraParams = ($key) ? "&key={$key}" : NULL;
196
197 $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?');
198 self::$_links = array(
199 CRM_Core_Action::VIEW => array(
200 'name' => ts('View'),
201 'url' => 'civicrm/contact/view/pledge',
202 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=pledge' . $extraParams,
203 'title' => ts('View Pledge'),
204 ),
205 CRM_Core_Action::UPDATE => array(
206 'name' => ts('Edit'),
207 'url' => 'civicrm/contact/view/pledge',
208 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
209 'title' => ts('Edit Pledge'),
210 ),
211 CRM_Core_Action::DETACH => array(
212 'name' => ts('Cancel'),
213 'url' => 'civicrm/contact/view/pledge',
214 'qs' => 'reset=1&action=detach&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
215 'extra' => 'onclick = "return confirm(\'' . $cancelExtra . '\');"',
216 'title' => ts('Cancel Pledge'),
217 ),
218 CRM_Core_Action::DELETE => array(
219 'name' => ts('Delete'),
220 'url' => 'civicrm/contact/view/pledge',
221 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
222 'title' => ts('Delete Pledge'),
223 ),
224 );
225
6a488035
TO
226 if (in_array('Cancel', $hideOption)) {
227 unset(self::$_links[CRM_Core_Action::DETACH]);
228 }
229
230 return self::$_links;
231 }
6a488035
TO
232
233 /**
100fef9d 234 * Getter for array of the parameters required for creating pager.
6a488035 235 *
da6b46f4 236 * @param $action
c490a46a 237 * @param array $params
6a488035 238 */
00be9182 239 public function getPagerParams($action, &$params) {
6a488035
TO
240 $params['status'] = ts('Pledge') . ' %%StatusMessage%%';
241 $params['csvString'] = NULL;
242 if ($this->_limit) {
243 $params['rowCount'] = $this->_limit;
244 }
245 else {
246 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
247 }
248
249 $params['buttonTop'] = 'PagerTopButton';
250 $params['buttonBottom'] = 'PagerBottomButton';
251 }
6a488035
TO
252
253 /**
254 * Returns total number of rows for the query.
255 *
256 * @param
257 *
a6c01b45
CW
258 * @return int
259 * Total number of rows
6a488035 260 */
00be9182 261 public function getTotalCount($action) {
6a488035
TO
262 return $this->_query->searchQuery(0, 0, NULL,
263 TRUE, FALSE,
264 FALSE, FALSE,
265 FALSE,
266 $this->_additionalClause
267 );
268 }
269
270 /**
100fef9d 271 * Returns all the rows in the given offset and rowCount
6a488035 272 *
3a1617b6
TO
273 * @param enum $action
274 * The action being performed.
275 * @param int $offset
276 * The row number to start from.
277 * @param int $rowCount
278 * The number of rows to return.
279 * @param string $sort
280 * The sql string that describes the sort order.
281 * @param enum $output
282 * What should the result set include (web/email/csv).
6a488035 283 *
a6c01b45
CW
284 * @return int
285 * the total number of rows for this action
6a488035 286 */
00be9182 287 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
288 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
289 FALSE, FALSE,
290 FALSE, FALSE,
291 FALSE,
292 $this->_additionalClause
293 );
294
295 // process the result of the query
296 $rows = array();
297
298 // get all pledge status
299 $pledgeStatuses = CRM_Core_OptionGroup::values('contribution_status',
300 FALSE, FALSE, FALSE, NULL, 'name', FALSE
301 );
302
303 //get all campaigns.
304 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
305
306 //4418 check for view, edit and delete
307 $permissions = array(CRM_Core_Permission::VIEW);
308 if (CRM_Core_Permission::check('edit pledges')) {
309 $permissions[] = CRM_Core_Permission::EDIT;
310 }
311 if (CRM_Core_Permission::check('delete in CiviPledge')) {
312 $permissions[] = CRM_Core_Permission::DELETE;
313 }
314 $mask = CRM_Core_Action::mask($permissions);
315
316 while ($result->fetch()) {
317 $row = array();
318 // the columns we are interested in
319 foreach (self::$_properties as $property) {
320 if (isset($result->$property)) {
321 $row[$property] = $result->$property;
322 }
323 }
324
325 //carry campaign on selectors.
326 $row['campaign'] = CRM_Utils_Array::value($result->pledge_campaign_id, $allCampaigns);
327 $row['campaign_id'] = $result->pledge_campaign_id;
328
329 // add pledge status name
330 $row['pledge_status_name'] = CRM_Utils_Array::value($row['pledge_status_id'],
331 $pledgeStatuses
332 );
333 // append (test) to status label
a7488080 334 if (!empty($row['pledge_is_test'])) {
6a488035
TO
335 $row['pledge_status'] .= ' (test)';
336 }
337
338 $hideOption = array();
339 if (CRM_Utils_Array::key('Cancelled', $row) ||
340 CRM_Utils_Array::key('Completed', $row)
341 ) {
342 $hideOption[] = 'Cancel';
343 }
344
345 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->pledge_id;
346
347 $row['action'] = CRM_Core_Action::formLink(self::links($hideOption, $this->_key),
348 $mask,
349 array(
350 'id' => $result->pledge_id,
351 'cid' => $result->contact_id,
352 'cxt' => $this->_context,
87dab4a4
AH
353 ),
354 ts('more'),
355 FALSE,
356 'pledge.selector.row',
357 'Pledge',
358 $result->pledge_id
6a488035
TO
359 );
360
098201d8 361 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
6a488035
TO
362 );
363 $rows[] = $row;
364 }
365 return $rows;
366 }
367
368 /**
a6c01b45
CW
369 * @return array
370 * which contains an array of strings
6a488035
TO
371 */
372
373 // the current internationalisation is bad, but should more or less work
374 // for most of "European" languages
375 public function getQILL() {
376 return $this->_query->qill();
377 }
378
379 /**
100fef9d 380 * Returns the column headers as an array of tuples:
6a488035
TO
381 * (name, sortName (key to the sort array))
382 *
3a1617b6
TO
383 * @param string $action
384 * The action being performed.
385 * @param enum $output
386 * What should the result set include (web/email/csv).
6a488035 387 *
a6c01b45
CW
388 * @return array
389 * the column headers that need to be displayed
6a488035
TO
390 */
391 public function &getColumnHeaders($action = NULL, $output = NULL) {
392 if (!isset(self::$_columnHeaders)) {
393 self::$_columnHeaders = array(
394 array(
395 'name' => ts('Pledged'),
396 'sort' => 'pledge_amount',
397 'direction' => CRM_Utils_Sort::DONTCARE,
398 ),
399 array(
400 'name' => ts('Total Paid'),
401 'sort' => 'pledge_total_paid',
402 'direction' => CRM_Utils_Sort::DONTCARE,
403 ),
404 array(
405 'name' => ts('Balance'),
406 ),
407 array(
408 'name' => ts('Pledged For'),
6d3864ef 409 'sort' => 'pledge_financial_type',
6a488035
TO
410 'direction' => CRM_Utils_Sort::DONTCARE,
411 ),
412 array(
413 'name' => ts('Pledge Made'),
414 'sort' => 'pledge_create_date',
415 'direction' => CRM_Utils_Sort::DESCENDING,
416 ),
417 array(
418 'name' => ts('Next Pay Date'),
419 'sort' => 'pledge_next_pay_date',
420 'direction' => CRM_Utils_Sort::DONTCARE,
421 ),
422 array(
423 'name' => ts('Next Amount'),
424 'sort' => 'pledge_next_pay_amount',
425 'direction' => CRM_Utils_Sort::DONTCARE,
426 ),
427 array(
428 'name' => ts('Status'),
429 'sort' => 'pledge_status',
430 'direction' => CRM_Utils_Sort::DONTCARE,
431 ),
432 array('desc' => ts('Actions')),
433 );
434
435 if (!$this->_single) {
436 $pre = array(
7b99ead3 437 array('desc' => ts('Contact ID')),
6a488035
TO
438 array(
439 'name' => ts('Name'),
440 'sort' => 'sort_name',
441 'direction' => CRM_Utils_Sort::DONTCARE,
442 ),
443 );
444
445 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
446 }
447 }
448 return self::$_columnHeaders;
449 }
450
ffd93213
EM
451 /**
452 * @return string
453 */
00be9182 454 public function &getQuery() {
6a488035
TO
455 return $this->_query;
456 }
457
458 /**
100fef9d 459 * Name of export file.
6a488035 460 *
3a1617b6
TO
461 * @param string $output
462 * Type of output.
6a488035 463 *
a6c01b45
CW
464 * @return string
465 * name of the file
6a488035 466 */
00be9182 467 public function getExportFileName($output = 'csv') {
6a488035
TO
468 return ts('Pledge Search');
469 }
470}