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