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