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