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