Merge pull request #17396 from seamuslee001/use_util_mail_events
[civicrm-core.git] / CRM / Event / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 */
24 class 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
30 */
31 public static $_links = NULL;
32
33 /**
34 * We use desc to remind us what that column is, name is used in the tpl
35 *
36 * @var array
37 */
38 public static $_columnHeaders;
39
40 /**
41 * Properties of contact we're interested in displaying
42 * @var array
43 */
44 public static $_properties = [
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',
65 ];
66
67 /**
68 * Are we restricting ourselves to a single contact
69 *
70 * @var bool
71 */
72 protected $_single = FALSE;
73
74 /**
75 * Are we restricting ourselves to a single contact
76 *
77 * @var bool
78 */
79 protected $_limit = NULL;
80
81 /**
82 * What context are we being invoked from
83 *
84 * @var string
85 */
86 protected $_context = NULL;
87
88 /**
89 * What component context are we being invoked from
90 *
91 * @var string
92 */
93 protected $_compContext = NULL;
94
95 /**
96 * QueryParams is the array returned by exportValues called on
97 * the HTML_QuickForm_Controller for that page.
98 *
99 * @var array
100 */
101 public $_queryParams;
102
103 /**
104 * Represent the type of selector.
105 *
106 * @var int
107 */
108 protected $_action;
109
110 /**
111 * The additional clause that we restrict the search with.
112 *
113 * @var string
114 */
115 protected $_eventClause = NULL;
116
117 /**
118 * The query object.
119 *
120 * @var string
121 */
122 protected $_query;
123
124 /**
125 * Class constructor.
126 *
127 * @param array $queryParams
128 * Array of parameters for query.
129 * @param \const|int $action - action of search basic or advanced.
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.
136 *
137 * @param string $context
138 * @param null $compContext
139 *
140 * @return \CRM_Event_Selector_Search
141 */
142 public function __construct(
143 &$queryParams,
144 $action = CRM_Core_Action::NONE,
145 $eventClause = NULL,
146 $single = FALSE,
147 $limit = NULL,
148 $context = 'search',
149 $compContext = NULL
150 ) {
151 // submitted form values
152 $this->_queryParams = &$queryParams;
153
154 $this->_single = $single;
155 $this->_limit = $limit;
156 $this->_context = $context;
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 }
174
175 /**
176 * Can be used to alter the number of participation returned from a buildForm hook.
177 *
178 * @param int $limit
179 * How many participations do we want returned.
180 */
181 public function setLimit($limit) {
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 *
192 * @param null $qfKey
193 * @param null $context
194 * @param null $compContext
195 *
196 * @return array
197 */
198 public static function &links($qfKey = NULL, $context = NULL, $compContext = NULL) {
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
211 if (!(self::$_links)) {
212 self::$_links = [
213 CRM_Core_Action::VIEW => [
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'),
218 ],
219 CRM_Core_Action::UPDATE => [
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'),
224 ],
225 CRM_Core_Action::DELETE => [
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'),
230 ],
231 ];
232 }
233 return self::$_links;
234 }
235
236 /**
237 * Getter for array of the parameters required for creating pager.
238 *
239 * @param $action
240 * @param array $params
241 */
242 public function getPagerParams($action, &$params) {
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 }
255
256 /**
257 * Returns total number of rows for the query.
258 *
259 * @param int $action
260 *
261 * @return int
262 * Total number of rows
263 */
264 public function getTotalCount($action) {
265 return $this->_query->searchQuery(0, 0, NULL,
266 TRUE, FALSE,
267 FALSE, FALSE,
268 FALSE,
269 $this->_eventClause
270 );
271 }
272
273 /**
274 * Returns all the rows in the given offset and rowCount.
275 *
276 * @param string $action
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.
284 * @param string $output
285 * What should the result set include (web/email/csv).
286 *
287 * @return array
288 * rows in the given offset and rowCount
289 */
290 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
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
298 $rows = [];
299
300 //lets handle view, edit and delete separately. CRM-4418
301 $permissions = [CRM_Core_Permission::VIEW];
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
310 $statusTypes = CRM_Event_PseudoConstant::participantStatus();
311 $statusClasses = CRM_Event_PseudoConstant::participantStatusClass();
312 $participantRoles = CRM_Event_PseudoConstant::participantRole();
313 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
314
315 //get all campaigns.
316 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
317
318 while ($result->fetch()) {
319 $row = [];
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
327 // Skip registration if event_id is NULL
328 if (empty($row['event_id'])) {
329 Civi::log()->warning('Participant record without event ID. You have invalid data in your database!');
330 continue;
331 }
332
333 //carry campaign on selectors.
334 $row['campaign'] = $allCampaigns[$result->participant_campaign_id] ?? NULL;
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
345 $row['showConfirmUrl'] = $statusClass == 'Pending';
346
347 if (!empty($row['participant_is_test'])) {
348 $row['participant_status'] = CRM_Core_TestEntity::appendTestText($row['participant_status']);
349 }
350
351 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->participant_id;
352 $links = self::links($this->_key, $this->_context, $this->_compContext);
353
354 if ($statusTypes[$row['participant_status_id']] == 'Partially paid') {
355 $links[CRM_Core_Action::ADD] = [
356 'name' => ts('Record Payment'),
357 'url' => 'civicrm/payment',
358 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
359 'title' => ts('Record Payment'),
360 ];
361 if (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) {
362 $links[CRM_Core_Action::BASIC] = [
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'),
367 ];
368 }
369 }
370
371 if ($statusTypes[$row['participant_status_id']] == 'Pending refund') {
372 $links[CRM_Core_Action::ADD] = [
373 'name' => ts('Record Refund'),
374 'url' => 'civicrm/payment',
375 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
376 'title' => ts('Record Refund'),
377 ];
378 }
379
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) &&
384 !in_array($statusTypes[$row['participant_status_id']], ['Cancelled', 'Transferred'])
385 ) {
386 $links[] = [
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'),
391 ];
392 }
393
394 $row['action'] = CRM_Core_Action::formLink($links,
395 $mask,
396 [
397 'id' => $result->participant_id,
398 'cid' => $result->contact_id,
399 'cxt' => $this->_context,
400 ],
401 ts('more'),
402 FALSE,
403 'participant.selector.row',
404 'Participant',
405 $result->participant_id
406 );
407
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
409 );
410
411 $row['paid'] = CRM_Event_BAO_Event::isMonetary($row['event_id']);
412
413 if (!empty($row['participant_fee_level'])) {
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'])) {
423 $viewRoles = [];
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 /**
437 * @inheritDoc
438 */
439 public function getQILL() {
440 return $this->_query->qill();
441 }
442
443 /**
444 * Returns the column headers as an array of tuples:
445 * (name, sortName (key to the sort array))
446 *
447 * @param string $action
448 * The action being performed.
449 * @param string $output
450 * What should the result set include (web/email/csv).
451 *
452 * @return array
453 * the column headers that need to be displayed
454 */
455 public function &getColumnHeaders($action = NULL, $output = NULL) {
456 if (!isset(self::$_columnHeaders)) {
457 self::$_columnHeaders = [
458 [
459 'name' => ts('Event'),
460 'sort' => 'event_title',
461 'direction' => CRM_Utils_Sort::DONTCARE,
462 ],
463 [
464 'name' => ts('Fee Level'),
465 'sort' => 'participant_fee_level',
466 'direction' => CRM_Utils_Sort::DONTCARE,
467 ],
468 [
469 'name' => ts('Amount'),
470 'sort' => 'participant_fee_amount',
471 'direction' => CRM_Utils_Sort::DONTCARE,
472 ],
473 [
474 'name' => ts('Registered'),
475 'sort' => 'participant_register_date',
476 'direction' => CRM_Utils_Sort::DESCENDING,
477 ],
478 [
479 'name' => ts('Event Date(s)'),
480 'sort' => 'event_start_date',
481 'direction' => CRM_Utils_Sort::DESCENDING,
482 ],
483 [
484 'name' => ts('Status'),
485 'sort' => 'participant_status',
486 'direction' => CRM_Utils_Sort::DONTCARE,
487 ],
488 [
489 'name' => ts('Role'),
490 'sort' => 'participant_role_id',
491 'direction' => CRM_Utils_Sort::DONTCARE,
492 ],
493 ['desc' => ts('Actions')],
494 ];
495
496 if (!$this->_single) {
497 $pre = [
498 ['desc' => ts('Contact Type')],
499 [
500 'name' => ts('Participant'),
501 'sort' => 'sort_name',
502 'direction' => CRM_Utils_Sort::DONTCARE,
503 ],
504 ];
505 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
506 }
507 }
508 return self::$_columnHeaders;
509 }
510
511 /**
512 * @return mixed
513 */
514 public function alphabetQuery() {
515 return $this->_query->alphabetQuery();
516 }
517
518 /**
519 * @return string
520 */
521 public function &getQuery() {
522 return $this->_query;
523 }
524
525 /**
526 * Name of export file.
527 *
528 * @param string $output
529 * Type of output.
530 *
531 * @return string
532 * name of the file
533 */
534 public function getExportFileName($output = 'csv') {
535 return ts('CiviCRM Event Search');
536 }
537
538 }