Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-05-23-28-33
[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 *
fd31fa4c
EM
138 * @param array $queryParams array of parameters for query
139 * @param \const|int $action - action of search basic or advanced.
140 * @param string $additionalClause if the caller wants to further restrict the search (used in participations)
6a488035 141 * @param boolean $single are we dealing only with one contact?
fd31fa4c 142 * @param int $limit how many signers do we want returned
6a488035 143 *
fd31fa4c
EM
144 * @param string $context
145 *
146 * @return \CRM_Pledge_Selector_Search
147 @access public
6a488035
TO
148 */
149 function __construct(&$queryParams,
150 $action = CRM_Core_Action::NONE,
151 $additionalClause = NULL,
152 $single = FALSE,
153 $limit = NULL,
154 $context = 'search'
155 ) {
156 // submitted form values
157 $this->_queryParams = &$queryParams;
158
159 $this->_single = $single;
160 $this->_limit = $limit;
161 $this->_context = $context;
162
163 $this->_additionalClause = $additionalClause;
164
165 // type of selector
166 $this->_action = $action;
167
168 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE,
169 CRM_Contact_BAO_Query::MODE_PLEDGE
170 );
171
172 $this->_query->_distinctComponentClause = "civicrm_pledge.id";
173 $this->_query->_groupByComponentClause = " GROUP BY civicrm_pledge.id ";
174 }
6a488035
TO
175
176 /**
177 * This method returns the links that are given for each search row.
178 * currently the links added for each row are
179 *
180 * - View
181 * - Edit
182 *
183 * @return array
6a488035
TO
184 *
185 */
00be9182 186 public static function &links() {
9f6f062c
DL
187 $args = func_get_args();
188 $hideOption = CRM_Utils_Array::value(0, $args);
189 $key = CRM_Utils_Array::value(1, $args);
190
6a488035
TO
191 $extraParams = ($key) ? "&key={$key}" : NULL;
192
193 $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?');
194 self::$_links = array(
195 CRM_Core_Action::VIEW => array(
196 'name' => ts('View'),
197 'url' => 'civicrm/contact/view/pledge',
198 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=pledge' . $extraParams,
199 'title' => ts('View Pledge'),
200 ),
201 CRM_Core_Action::UPDATE => array(
202 'name' => ts('Edit'),
203 'url' => 'civicrm/contact/view/pledge',
204 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
205 'title' => ts('Edit Pledge'),
206 ),
207 CRM_Core_Action::DETACH => array(
208 'name' => ts('Cancel'),
209 'url' => 'civicrm/contact/view/pledge',
210 'qs' => 'reset=1&action=detach&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
211 'extra' => 'onclick = "return confirm(\'' . $cancelExtra . '\');"',
212 'title' => ts('Cancel Pledge'),
213 ),
214 CRM_Core_Action::DELETE => array(
215 'name' => ts('Delete'),
216 'url' => 'civicrm/contact/view/pledge',
217 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
218 'title' => ts('Delete Pledge'),
219 ),
220 );
221
222
223 if (in_array('Cancel', $hideOption)) {
224 unset(self::$_links[CRM_Core_Action::DETACH]);
225 }
226
227 return self::$_links;
228 }
6a488035
TO
229
230 /**
100fef9d 231 * Getter for array of the parameters required for creating pager.
6a488035 232 *
da6b46f4 233 * @param $action
c490a46a 234 * @param array $params
da6b46f4 235 *
6a488035 236 */
00be9182 237 public function getPagerParams($action, &$params) {
6a488035
TO
238 $params['status'] = ts('Pledge') . ' %%StatusMessage%%';
239 $params['csvString'] = NULL;
240 if ($this->_limit) {
241 $params['rowCount'] = $this->_limit;
242 }
243 else {
244 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
245 }
246
247 $params['buttonTop'] = 'PagerTopButton';
248 $params['buttonBottom'] = 'PagerBottomButton';
249 }
6a488035
TO
250
251 /**
252 * Returns total number of rows for the query.
253 *
254 * @param
255 *
256 * @return int Total number of rows
6a488035 257 */
00be9182 258 public function getTotalCount($action) {
6a488035
TO
259 return $this->_query->searchQuery(0, 0, NULL,
260 TRUE, FALSE,
261 FALSE, FALSE,
262 FALSE,
263 $this->_additionalClause
264 );
265 }
266
267 /**
100fef9d 268 * Returns all the rows in the given offset and rowCount
6a488035
TO
269 *
270 * @param enum $action the action being performed
271 * @param int $offset the row number to start from
272 * @param int $rowCount the number of rows to return
273 * @param string $sort the sql string that describes the sort order
274 * @param enum $output what should the result set include (web/email/csv)
275 *
276 * @return int the total number of rows for this action
277 */
00be9182 278 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
279 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
280 FALSE, FALSE,
281 FALSE, FALSE,
282 FALSE,
283 $this->_additionalClause
284 );
285
286 // process the result of the query
287 $rows = array();
288
289 // get all pledge status
290 $pledgeStatuses = CRM_Core_OptionGroup::values('contribution_status',
291 FALSE, FALSE, FALSE, NULL, 'name', FALSE
292 );
293
294 //get all campaigns.
295 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
296
297 //4418 check for view, edit and delete
298 $permissions = array(CRM_Core_Permission::VIEW);
299 if (CRM_Core_Permission::check('edit pledges')) {
300 $permissions[] = CRM_Core_Permission::EDIT;
301 }
302 if (CRM_Core_Permission::check('delete in CiviPledge')) {
303 $permissions[] = CRM_Core_Permission::DELETE;
304 }
305 $mask = CRM_Core_Action::mask($permissions);
306
307 while ($result->fetch()) {
308 $row = array();
309 // the columns we are interested in
310 foreach (self::$_properties as $property) {
311 if (isset($result->$property)) {
312 $row[$property] = $result->$property;
313 }
314 }
315
316 //carry campaign on selectors.
317 $row['campaign'] = CRM_Utils_Array::value($result->pledge_campaign_id, $allCampaigns);
318 $row['campaign_id'] = $result->pledge_campaign_id;
319
320 // add pledge status name
321 $row['pledge_status_name'] = CRM_Utils_Array::value($row['pledge_status_id'],
322 $pledgeStatuses
323 );
324 // append (test) to status label
a7488080 325 if (!empty($row['pledge_is_test'])) {
6a488035
TO
326 $row['pledge_status'] .= ' (test)';
327 }
328
329 $hideOption = array();
330 if (CRM_Utils_Array::key('Cancelled', $row) ||
331 CRM_Utils_Array::key('Completed', $row)
332 ) {
333 $hideOption[] = 'Cancel';
334 }
335
336 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->pledge_id;
337
338 $row['action'] = CRM_Core_Action::formLink(self::links($hideOption, $this->_key),
339 $mask,
340 array(
341 'id' => $result->pledge_id,
342 'cid' => $result->contact_id,
343 'cxt' => $this->_context,
87dab4a4
AH
344 ),
345 ts('more'),
346 FALSE,
347 'pledge.selector.row',
348 'Pledge',
349 $result->pledge_id
6a488035
TO
350 );
351
352
353 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
354 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
355 );
356 $rows[] = $row;
357 }
358 return $rows;
359 }
360
361 /**
6a488035 362 * @return array $qill which contains an array of strings
6a488035
TO
363 */
364
365 // the current internationalisation is bad, but should more or less work
366 // for most of "European" languages
367 public function getQILL() {
368 return $this->_query->qill();
369 }
370
371 /**
100fef9d 372 * Returns the column headers as an array of tuples:
6a488035
TO
373 * (name, sortName (key to the sort array))
374 *
375 * @param string $action the action being performed
376 * @param enum $output what should the result set include (web/email/csv)
377 *
378 * @return array the column headers that need to be displayed
6a488035
TO
379 */
380 public function &getColumnHeaders($action = NULL, $output = NULL) {
381 if (!isset(self::$_columnHeaders)) {
382 self::$_columnHeaders = array(
383 array(
384 'name' => ts('Pledged'),
385 'sort' => 'pledge_amount',
386 'direction' => CRM_Utils_Sort::DONTCARE,
387 ),
388 array(
389 'name' => ts('Total Paid'),
390 'sort' => 'pledge_total_paid',
391 'direction' => CRM_Utils_Sort::DONTCARE,
392 ),
393 array(
394 'name' => ts('Balance'),
395 ),
396 array(
397 'name' => ts('Pledged For'),
6d3864ef 398 'sort' => 'pledge_financial_type',
6a488035
TO
399 'direction' => CRM_Utils_Sort::DONTCARE,
400 ),
401 array(
402 'name' => ts('Pledge Made'),
403 'sort' => 'pledge_create_date',
404 'direction' => CRM_Utils_Sort::DESCENDING,
405 ),
406 array(
407 'name' => ts('Next Pay Date'),
408 'sort' => 'pledge_next_pay_date',
409 'direction' => CRM_Utils_Sort::DONTCARE,
410 ),
411 array(
412 'name' => ts('Next Amount'),
413 'sort' => 'pledge_next_pay_amount',
414 'direction' => CRM_Utils_Sort::DONTCARE,
415 ),
416 array(
417 'name' => ts('Status'),
418 'sort' => 'pledge_status',
419 'direction' => CRM_Utils_Sort::DONTCARE,
420 ),
421 array('desc' => ts('Actions')),
422 );
423
424 if (!$this->_single) {
425 $pre = array(
7b99ead3 426 array('desc' => ts('Contact ID')),
6a488035
TO
427 array(
428 'name' => ts('Name'),
429 'sort' => 'sort_name',
430 'direction' => CRM_Utils_Sort::DONTCARE,
431 ),
432 );
433
434 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
435 }
436 }
437 return self::$_columnHeaders;
438 }
439
ffd93213
EM
440 /**
441 * @return string
442 */
00be9182 443 public function &getQuery() {
6a488035
TO
444 return $this->_query;
445 }
446
447 /**
100fef9d 448 * Name of export file.
6a488035
TO
449 *
450 * @param string $output type of output
451 *
452 * @return string name of the file
453 */
00be9182 454 public function getExportFileName($output = 'csv') {
6a488035
TO
455 return ts('Pledge Search');
456 }
457}