Merge pull request #17976 from civicrm/5.28
[civicrm-core.git] / CRM / Event / 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.
22 *
23 */
24class CRM_Event_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
25
26 /**
27 * This defines two actions- View and Edit.
28 *
29 * @var array
6a488035 30 */
90b461f1 31 public static $_links = NULL;
6a488035
TO
32
33 /**
100fef9d 34 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
35 *
36 * @var array
6a488035 37 */
90b461f1 38 public static $_columnHeaders;
6a488035
TO
39
40 /**
41 * Properties of contact we're interested in displaying
42 * @var array
6a488035 43 */
90b461f1 44 public static $_properties = [
6a488035
TO
45 'contact_id',
46 'contact_type',
47 'sort_name',
48 'event_id',
49 'participant_status_id',
50 'event_title',
51 'participant_fee_level',
52 'participant_id',
53 'event_start_date',
54 'event_end_date',
55 'event_type_id',
56 'modified_date',
57 'participant_is_test',
58 'participant_role_id',
59 'participant_register_date',
60 'participant_fee_amount',
61 'participant_fee_currency',
62 'participant_status',
63 'participant_role',
64 'participant_campaign_id',
be2fb01f 65 ];
6a488035
TO
66
67 /**
100fef9d 68 * Are we restricting ourselves to a single contact
6a488035 69 *
d51c6add 70 * @var bool
6a488035
TO
71 */
72 protected $_single = FALSE;
73
74 /**
100fef9d 75 * Are we restricting ourselves to a single contact
6a488035 76 *
d51c6add 77 * @var bool
6a488035
TO
78 */
79 protected $_limit = NULL;
80
81 /**
100fef9d 82 * What context are we being invoked from
6a488035 83 *
6a488035
TO
84 * @var string
85 */
86 protected $_context = NULL;
87
88 /**
100fef9d 89 * What component context are we being invoked from
6a488035 90 *
6a488035
TO
91 * @var string
92 */
93 protected $_compContext = NULL;
94
95 /**
100fef9d 96 * QueryParams is the array returned by exportValues called on
6a488035
TO
97 * the HTML_QuickForm_Controller for that page.
98 *
99 * @var array
6a488035
TO
100 */
101 public $_queryParams;
102
103 /**
fe482240 104 * Represent the type of selector.
6a488035
TO
105 *
106 * @var int
6a488035
TO
107 */
108 protected $_action;
109
110 /**
fe482240 111 * The additional clause that we restrict the search with.
6a488035
TO
112 *
113 * @var string
114 */
115 protected $_eventClause = NULL;
116
117 /**
fe482240 118 * The query object.
6a488035
TO
119 *
120 * @var string
121 */
122 protected $_query;
123
124 /**
fe482240 125 * Class constructor.
6a488035 126 *
d4dd1e85
TO
127 * @param array $queryParams
128 * Array of parameters for query.
da6b46f4 129 * @param \const|int $action - action of search basic or advanced.
d4dd1e85
TO
130 * @param string $eventClause
131 * If the caller wants to further restrict the search (used in participations).
132 * @param bool $single
133 * Are we dealing only with one contact?.
134 * @param int $limit
135 * How many participations do we want returned.
6a488035 136 *
da6b46f4
EM
137 * @param string $context
138 * @param null $compContext
139 *
140 * @return \CRM_Event_Selector_Search
6a488035 141 */
2da40d21 142 public function __construct(
ddca8f33 143 &$queryParams,
0479b4c8 144 $action = CRM_Core_Action::NONE,
6a488035 145 $eventClause = NULL,
0479b4c8
TO
146 $single = FALSE,
147 $limit = NULL,
148 $context = 'search',
6a488035
TO
149 $compContext = NULL
150 ) {
151 // submitted form values
152 $this->_queryParams = &$queryParams;
153
353ffa53
TO
154 $this->_single = $single;
155 $this->_limit = $limit;
156 $this->_context = $context;
6a488035
TO
157 $this->_compContext = $compContext;
158
159 $this->_eventClause = $eventClause;
160
161 // type of selector
162 $this->_action = $action;
163
164 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
165 CRM_Event_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_EVENT,
166 FALSE
167 ),
168 NULL, FALSE, FALSE,
169 CRM_Contact_BAO_Query::MODE_EVENT
170 );
171 $this->_query->_distinctComponentClause = " civicrm_participant.id";
172 $this->_query->_groupByComponentClause = " GROUP BY civicrm_participant.id ";
173 }
6a488035
TO
174
175 /**
fe482240 176 * Can be used to alter the number of participation returned from a buildForm hook.
6a488035 177 *
d4dd1e85
TO
178 * @param int $limit
179 * How many participations do we want returned.
6a488035 180 */
00be9182 181 public function setLimit($limit) {
6a488035
TO
182 $this->_limit = $limit;
183 }
184
185 /**
186 * This method returns the links that are given for each search row.
187 * currently the links added for each row are
188 *
189 * - View
190 * - Edit
191 *
77b97be7
EM
192 * @param null $qfKey
193 * @param null $context
194 * @param null $compContext
195 *
6a488035 196 * @return array
6a488035 197 */
00be9182 198 public static function &links($qfKey = NULL, $context = NULL, $compContext = NULL) {
6a488035
TO
199 $extraParams = NULL;
200 if ($compContext) {
201 $extraParams .= "&compContext={$compContext}";
202 }
203 elseif ($context == 'search') {
204 $extraParams .= '&compContext=participant';
205 }
206
207 if ($qfKey) {
208 $extraParams .= "&key={$qfKey}";
209 }
210
6a488035 211 if (!(self::$_links)) {
be2fb01f
CW
212 self::$_links = [
213 CRM_Core_Action::VIEW => [
6a488035
TO
214 'name' => ts('View'),
215 'url' => 'civicrm/contact/view/participant',
216 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=event' . $extraParams,
217 'title' => ts('View Participation'),
be2fb01f
CW
218 ],
219 CRM_Core_Action::UPDATE => [
6a488035
TO
220 'name' => ts('Edit'),
221 'url' => 'civicrm/contact/view/participant',
222 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
223 'title' => ts('Edit Participation'),
be2fb01f
CW
224 ],
225 CRM_Core_Action::DELETE => [
6a488035
TO
226 'name' => ts('Delete'),
227 'url' => 'civicrm/contact/view/participant',
228 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
229 'title' => ts('Delete Participation'),
be2fb01f
CW
230 ],
231 ];
6a488035
TO
232 }
233 return self::$_links;
234 }
6a488035
TO
235
236 /**
100fef9d 237 * Getter for array of the parameters required for creating pager.
6a488035 238 *
fd31fa4c 239 * @param $action
c490a46a 240 * @param array $params
6a488035 241 */
00be9182 242 public function getPagerParams($action, &$params) {
6a488035
TO
243 $params['status'] = ts('Event') . ' %%StatusMessage%%';
244 $params['csvString'] = NULL;
245 if ($this->_limit) {
246 $params['rowCount'] = $this->_limit;
247 }
248 else {
249 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
250 }
251
252 $params['buttonTop'] = 'PagerTopButton';
253 $params['buttonBottom'] = 'PagerBottomButton';
254 }
6a488035
TO
255
256 /**
257 * Returns total number of rows for the query.
258 *
ad37ac8e 259 * @param int $action
6a488035 260 *
a6c01b45
CW
261 * @return int
262 * Total number of rows
6a488035 263 */
00be9182 264 public function getTotalCount($action) {
6a488035
TO
265 return $this->_query->searchQuery(0, 0, NULL,
266 TRUE, FALSE,
267 FALSE, FALSE,
268 FALSE,
269 $this->_eventClause
270 );
271 }
272
273 /**
fe482240 274 * Returns all the rows in the given offset and rowCount.
6a488035 275 *
3f8d2862 276 * @param string $action
d4dd1e85
TO
277 * The action being performed.
278 * @param int $offset
279 * The row number to start from.
280 * @param int $rowCount
281 * The number of rows to return.
282 * @param string $sort
283 * The sql string that describes the sort order.
3f8d2862 284 * @param string $output
d4dd1e85 285 * What should the result set include (web/email/csv).
6a488035 286 *
a6c01b45
CW
287 * @return array
288 * rows in the given offset and rowCount
6a488035 289 */
00be9182 290 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
291 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
292 FALSE, FALSE,
293 FALSE, FALSE,
294 FALSE,
295 $this->_eventClause
296 );
297 // process the result of the query
be2fb01f 298 $rows = [];
6a488035
TO
299
300 //lets handle view, edit and delete separately. CRM-4418
be2fb01f 301 $permissions = [CRM_Core_Permission::VIEW];
6a488035
TO
302 if (CRM_Core_Permission::check('edit event participants')) {
303 $permissions[] = CRM_Core_Permission::EDIT;
304 }
305 if (CRM_Core_Permission::check('delete in CiviEvent')) {
306 $permissions[] = CRM_Core_Permission::DELETE;
307 }
308 $mask = CRM_Core_Action::mask($permissions);
309
353ffa53
TO
310 $statusTypes = CRM_Event_PseudoConstant::participantStatus();
311 $statusClasses = CRM_Event_PseudoConstant::participantStatusClass();
6a488035 312 $participantRoles = CRM_Event_PseudoConstant::participantRole();
353ffa53 313 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
314
315 //get all campaigns.
316 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
317
318 while ($result->fetch()) {
be2fb01f 319 $row = [];
6a488035
TO
320 // the columns we are interested in
321 foreach (self::$_properties as $property) {
322 if (isset($result->$property)) {
323 $row[$property] = $result->$property;
324 }
325 }
326
cf79ac58
MW
327 // Skip registration if event_id is NULL
328 if (empty($row['event_id'])) {
23380ec7 329 Civi::log()->warning('Participant record without event ID. You have invalid data in your database!');
cf79ac58
MW
330 continue;
331 }
332
6a488035 333 //carry campaign on selectors.
9c1bc317 334 $row['campaign'] = $allCampaigns[$result->participant_campaign_id] ?? NULL;
6a488035
TO
335 $row['campaign_id'] = $result->participant_campaign_id;
336
337 // gross hack to show extra information for pending status
338 $statusClass = NULL;
339 if ((isset($row['participant_status_id'])) &&
340 ($statusId = array_search($row['participant_status_id'], $statusTypes))
341 ) {
342 $statusClass = $statusClasses[$statusId];
343 }
344
f7dbf5d9 345 $row['showConfirmUrl'] = $statusClass == 'Pending';
6a488035 346
a7488080 347 if (!empty($row['participant_is_test'])) {
cd355351 348 $row['participant_status'] = CRM_Core_TestEntity::appendTestText($row['participant_status']);
6a488035
TO
349 }
350
351 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->participant_id;
fbc54416
PJ
352 $links = self::links($this->_key, $this->_context, $this->_compContext);
353
354 if ($statusTypes[$row['participant_status_id']] == 'Partially paid') {
be2fb01f 355 $links[CRM_Core_Action::ADD] = [
fbc54416 356 'name' => ts('Record Payment'),
81f3d017 357 'url' => 'civicrm/payment',
fbc54416
PJ
358 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
359 'title' => ts('Record Payment'),
be2fb01f 360 ];
059326f3 361 if (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) {
be2fb01f 362 $links[CRM_Core_Action::BASIC] = [
059326f3
E
363 'name' => ts('Submit Credit Card payment'),
364 'url' => 'civicrm/payment/add',
365 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event&mode=live',
366 'title' => ts('Submit Credit Card payment'),
be2fb01f 367 ];
059326f3 368 }
fbc54416 369 }
6a488035 370
e8cf3013 371 if ($statusTypes[$row['participant_status_id']] == 'Pending refund') {
be2fb01f 372 $links[CRM_Core_Action::ADD] = [
e8cf3013 373 'name' => ts('Record Refund'),
81f3d017 374 'url' => 'civicrm/payment',
e8cf3013
PJ
375 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
376 'title' => ts('Record Refund'),
be2fb01f 377 ];
e8cf3013
PJ
378 }
379
e219d53b 380 // CRM-20879: Show 'Transfer or Cancel' action only if logged in user
381 // have 'edit event participants' permission and participant status
382 // is not Cancelled or Transferred
383 if (in_array(CRM_Core_Permission::EDIT, $permissions) &&
be2fb01f 384 !in_array($statusTypes[$row['participant_status_id']], ['Cancelled', 'Transferred'])
e219d53b 385 ) {
be2fb01f 386 $links[] = [
e219d53b 387 'name' => ts('Transfer or Cancel'),
388 'url' => 'civicrm/event/selfsvcupdate',
389 'qs' => 'reset=1&pid=%%id%%&is_backoffice=1&cs=' . CRM_Contact_BAO_Contact_Utils::generateChecksum($result->contact_id, NULL, 'inf'),
390 'title' => ts('Transfer or Cancel'),
be2fb01f 391 ];
e219d53b 392 }
393
fbc54416 394 $row['action'] = CRM_Core_Action::formLink($links,
6a488035 395 $mask,
be2fb01f 396 [
6a488035
TO
397 'id' => $result->participant_id,
398 'cid' => $result->contact_id,
399 'cxt' => $this->_context,
be2fb01f 400 ],
87dab4a4
AH
401 ts('more'),
402 FALSE,
403 'participant.selector.row',
404 'Participant',
405 $result->participant_id
6a488035
TO
406 );
407
0479b4c8 408 $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
409 );
410
411 $row['paid'] = CRM_Event_BAO_Event::isMonetary($row['event_id']);
412
a7488080 413 if (!empty($row['participant_fee_level'])) {
6a488035
TO
414 CRM_Event_BAO_Participant::fixEventLevel($row['participant_fee_level']);
415 }
416
417 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
418 // add line item details if applicable
419 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
420 }
421
422 if (!empty($row['participant_role_id'])) {
be2fb01f 423 $viewRoles = [];
6a488035
TO
424 foreach (explode($sep, $row['participant_role_id']) as $k => $v) {
425 $viewRoles[] = $participantRoles[$v];
426 }
427 $row['participant_role_id'] = implode(', ', $viewRoles);
428 }
429 $rows[] = $row;
430 }
431 CRM_Core_Selector_Controller::$_template->assign_by_ref('lineItems', $lineItems);
432
433 return $rows;
434 }
435
436 /**
1054415f 437 * @inheritDoc
6a488035 438 */
6a488035
TO
439 public function getQILL() {
440 return $this->_query->qill();
441 }
442
443 /**
100fef9d 444 * Returns the column headers as an array of tuples:
6a488035
TO
445 * (name, sortName (key to the sort array))
446 *
d4dd1e85
TO
447 * @param string $action
448 * The action being performed.
3f8d2862 449 * @param string $output
d4dd1e85 450 * What should the result set include (web/email/csv).
6a488035 451 *
a6c01b45
CW
452 * @return array
453 * the column headers that need to be displayed
6a488035
TO
454 */
455 public function &getColumnHeaders($action = NULL, $output = NULL) {
456 if (!isset(self::$_columnHeaders)) {
be2fb01f
CW
457 self::$_columnHeaders = [
458 [
353ffa53 459 'name' => ts('Event'),
6a488035
TO
460 'sort' => 'event_title',
461 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
462 ],
463 [
6a488035 464 'name' => ts('Fee Level'),
33a5a53d 465 'sort' => 'participant_fee_level',
6a488035 466 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
467 ],
468 [
6a488035 469 'name' => ts('Amount'),
33a5a53d 470 'sort' => 'participant_fee_amount',
6a488035 471 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
472 ],
473 [
6a488035
TO
474 'name' => ts('Registered'),
475 'sort' => 'participant_register_date',
476 'direction' => CRM_Utils_Sort::DESCENDING,
be2fb01f
CW
477 ],
478 [
6a488035
TO
479 'name' => ts('Event Date(s)'),
480 'sort' => 'event_start_date',
481 'direction' => CRM_Utils_Sort::DESCENDING,
be2fb01f
CW
482 ],
483 [
6a488035
TO
484 'name' => ts('Status'),
485 'sort' => 'participant_status',
486 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
487 ],
488 [
6a488035
TO
489 'name' => ts('Role'),
490 'sort' => 'participant_role_id',
491 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
492 ],
493 ['desc' => ts('Actions')],
494 ];
6a488035
TO
495
496 if (!$this->_single) {
be2fb01f
CW
497 $pre = [
498 ['desc' => ts('Contact Type')],
499 [
6a488035
TO
500 'name' => ts('Participant'),
501 'sort' => 'sort_name',
502 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
503 ],
504 ];
6a488035
TO
505 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
506 }
507 }
508 return self::$_columnHeaders;
509 }
510
0cf587a7
EM
511 /**
512 * @return mixed
513 */
00be9182 514 public function alphabetQuery() {
52cda5dc 515 return $this->_query->alphabetQuery();
6a488035
TO
516 }
517
0cf587a7
EM
518 /**
519 * @return string
520 */
00be9182 521 public function &getQuery() {
6a488035
TO
522 return $this->_query;
523 }
524
525 /**
100fef9d 526 * Name of export file.
6a488035 527 *
d4dd1e85
TO
528 * @param string $output
529 * Type of output.
6a488035 530 *
a6c01b45
CW
531 * @return string
532 * name of the file
6a488035 533 */
00be9182 534 public function getExportFileName($output = 'csv') {
6a488035
TO
535 return ts('CiviCRM Event Search');
536 }
96025800 537
6a488035 538}