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