Merge pull request #6757 from yashodha/CRM-17227
[civicrm-core.git] / CRM / Member / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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_Member_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
6a488035
TO
48 */
49 static $_links = NULL;
50
51 /**
100fef9d 52 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
53 *
54 * @var array
6a488035
TO
55 */
56 static $_columnHeaders;
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
6a488035
TO
61 */
62 static $_properties = array(
63 'contact_id',
64 'membership_id',
65 'contact_type',
66 'sort_name',
67 'membership_type',
68 'join_date',
69 'membership_start_date',
70 'membership_end_date',
71 'membership_source',
72 'status_id',
73 'member_is_test',
74 'owner_membership_id',
75 'membership_status',
76 'member_campaign_id',
77 );
78
79 /**
100fef9d 80 * Are we restricting ourselves to a single contact
6a488035 81 *
6a488035
TO
82 * @var boolean
83 */
84 protected $_single = FALSE;
85
86 /**
100fef9d 87 * Are we restricting ourselves to a single contact
6a488035 88 *
6a488035
TO
89 * @var boolean
90 */
91 protected $_limit = NULL;
92
93 /**
100fef9d 94 * What context are we being invoked from
6a488035 95 *
6a488035
TO
96 * @var string
97 */
98 protected $_context = NULL;
99
100 /**
100fef9d 101 * QueryParams is the array returned by exportValues called on
6a488035
TO
102 * the HTML_QuickForm_Controller for that page.
103 *
104 * @var array
6a488035
TO
105 */
106 public $_queryParams;
107
108 /**
fe482240 109 * Represent the type of selector.
6a488035
TO
110 *
111 * @var int
6a488035
TO
112 */
113 protected $_action;
114
115 /**
fe482240 116 * The additional clause that we restrict the search with.
6a488035
TO
117 *
118 * @var string
119 */
120 protected $_memberClause = NULL;
121
122 /**
fe482240 123 * The query object.
6a488035
TO
124 *
125 * @var string
126 */
127 protected $_query;
128
129 /**
fe482240 130 * Class constructor.
6a488035 131 *
b2363ea8
TO
132 * @param array $queryParams
133 * Array of parameters for query.
fd31fa4c 134 * @param \const|int $action - action of search basic or advanced.
b2363ea8
TO
135 * @param string $memberClause
136 * If the caller wants to further restrict the search (used in memberships).
137 * @param bool $single
138 * Are we dealing only with one contact?.
139 * @param int $limit
140 * How many memberships do we want returned.
6a488035 141 *
fd31fa4c
EM
142 * @param string $context
143 *
144 * @return \CRM_Member_Selector_Search
fd31fa4c 145 */
317fceb4 146 public function __construct(
500cfe81 147 &$queryParams,
b09fe5ed 148 $action = CRM_Core_Action::NONE,
6a488035 149 $memberClause = NULL,
b09fe5ed
TO
150 $single = FALSE,
151 $limit = NULL,
152 $context = 'search'
6a488035
TO
153 ) {
154 // submitted form values
155 $this->_queryParams = &$queryParams;
156
353ffa53
TO
157 $this->_single = $single;
158 $this->_limit = $limit;
6a488035
TO
159 $this->_context = $context;
160
161 $this->_memberClause = $memberClause;
162
163 // type of selector
164 $this->_action = $action;
165
166 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
167 CRM_Member_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_MEMBER,
168 FALSE
169 ),
170 NULL, FALSE, FALSE,
171 CRM_Contact_BAO_Query::MODE_MEMBER
172 );
173 $this->_query->_distinctComponentClause = " civicrm_membership.id";
174 $this->_query->_groupByComponentClause = " GROUP BY civicrm_membership.id ";
175 }
6a488035
TO
176
177 /**
178 * This method returns the links that are given for each search row.
179 * currently the links added for each row are
180 *
181 * - View
182 * - Edit
183 *
da6b46f4
EM
184 * @param string $status
185 * @param null $isPaymentProcessor
186 * @param null $accessContribution
187 * @param null $qfKey
188 * @param null $context
189 * @param bool $isCancelSupported
190 *
6a488035 191 * @return array
6a488035 192 */
317fceb4 193 public static function &links(
500cfe81 194 $status = 'all',
6a488035
TO
195 $isPaymentProcessor = NULL,
196 $accessContribution = NULL,
b09fe5ed
TO
197 $qfKey = NULL,
198 $context = NULL,
199 $isCancelSupported = FALSE
6a488035
TO
200 ) {
201 $extraParams = NULL;
202 if ($context == 'search') {
203 $extraParams .= '&compContext=membership';
204 }
205 if ($qfKey) {
206 $extraParams .= "&key={$qfKey}";
207 }
208
209 if (!self::$_links['view']) {
210 self::$_links['view'] = array(
211 CRM_Core_Action::VIEW => array(
212 'name' => ts('View'),
213 'url' => 'civicrm/contact/view/membership',
214 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=member' . $extraParams,
215 'title' => ts('View Membership'),
216 ),
217 );
218 }
219 if (!isset(self::$_links['all']) || !self::$_links['all']) {
220 $extraLinks = array(
221 CRM_Core_Action::UPDATE => array(
222 'name' => ts('Edit'),
223 'url' => 'civicrm/contact/view/membership',
224 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
225 'title' => ts('Edit Membership'),
226 ),
227 CRM_Core_Action::DELETE => array(
228 'name' => ts('Delete'),
229 'url' => 'civicrm/contact/view/membership',
230 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
231 'title' => ts('Delete Membership'),
232 ),
233 CRM_Core_Action::RENEW => array(
234 'name' => ts('Renew'),
235 'url' => 'civicrm/contact/view/membership',
236 'qs' => 'reset=1&action=renew&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
237 'title' => ts('Renew Membership'),
238 ),
239 CRM_Core_Action::FOLLOWUP => array(
240 'name' => ts('Renew-Credit Card'),
241 'url' => 'civicrm/contact/view/membership',
242 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=%%cxt%%&mode=live' . $extraParams,
243 'title' => ts('Renew Membership Using Credit Card'),
244 ),
245 );
246 if (!$isPaymentProcessor || !$accessContribution) {
247 //unset the renew with credit card when payment
248 //processor is not available or user not permitted to make contributions
249 unset($extraLinks[CRM_Core_Action::FOLLOWUP]);
250 }
251
252 self::$_links['all'] = self::$_links['view'] + $extraLinks;
253 }
254
255 if ($isCancelSupported) {
256 self::$_links['all'][CRM_Core_Action::DISABLE] = array(
257 'name' => ts('Cancel Auto-renewal'),
258 'url' => 'civicrm/contribute/unsubscribe',
259 'qs' => 'reset=1&mid=%%id%%&context=%%cxt%%' . $extraParams,
260 'title' => 'Cancel Auto Renew Subscription',
261 );
262 }
263 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
264 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
265 }
266
267 return self::$_links[$status];
268 }
6a488035
TO
269
270 /**
100fef9d 271 * Getter for array of the parameters required for creating pager.
6a488035 272 *
da6b46f4 273 * @param $action
c490a46a 274 * @param array $params
6a488035 275 */
00be9182 276 public function getPagerParams($action, &$params) {
6a488035
TO
277 $params['status'] = ts('Member') . ' %%StatusMessage%%';
278 $params['csvString'] = NULL;
279 if ($this->_limit) {
280 $params['rowCount'] = $this->_limit;
281 }
282 else {
283 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
284 }
285
286 $params['buttonTop'] = 'PagerTopButton';
287 $params['buttonBottom'] = 'PagerBottomButton';
288 }
6a488035
TO
289
290 /**
291 * Returns total number of rows for the query.
292 *
293 * @param
294 *
a6c01b45
CW
295 * @return int
296 * Total number of rows
6a488035 297 */
00be9182 298 public function getTotalCount($action) {
6a488035
TO
299 return $this->_query->searchQuery(0, 0, NULL,
300 TRUE, FALSE,
301 FALSE, FALSE,
302 FALSE,
303 $this->_memberClause
304 );
305 }
306
307 /**
fe482240 308 * Returns all the rows in the given offset and rowCount.
6a488035 309 *
3f8d2862 310 * @param string $action
b2363ea8
TO
311 * The action being performed.
312 * @param int $offset
313 * The row number to start from.
314 * @param int $rowCount
315 * The number of rows to return.
316 * @param string $sort
317 * The sql string that describes the sort order.
3f8d2862 318 * @param string $output
b2363ea8 319 * What should the result set include (web/email/csv).
6a488035 320 *
a6c01b45
CW
321 * @return int
322 * the total number of rows for this action
6a488035 323 */
00be9182 324 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
325 // check if we can process credit card registration
326 $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE,
327 "billing_mode IN ( 1, 3 )"
328 );
329 if (count($processors) > 0) {
330 $this->_isPaymentProcessor = TRUE;
331 }
332 else {
333 $this->_isPaymentProcessor = FALSE;
334 }
335
336 // Only show credit card membership signup and renewal if user has CiviContribute permission
337 if (CRM_Core_Permission::access('CiviContribute')) {
338 $this->_accessContribution = TRUE;
339 }
340 else {
341 $this->_accessContribution = FALSE;
342 }
343
344 //get all campaigns.
345 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
346
347 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
348 FALSE, FALSE,
349 FALSE, FALSE,
350 FALSE,
351 $this->_memberClause
352 );
353
354 // process the result of the query
355 $rows = array();
356
357 //CRM-4418 check for view, edit, delete
358 $permissions = array(CRM_Core_Permission::VIEW);
359 if (CRM_Core_Permission::check('edit memberships')) {
360 $permissions[] = CRM_Core_Permission::EDIT;
361 }
362 if (CRM_Core_Permission::check('delete in CiviMember')) {
363 $permissions[] = CRM_Core_Permission::DELETE;
364 }
365 $mask = CRM_Core_Action::mask($permissions);
366
367 while ($result->fetch()) {
368 $row = array();
369
370 // the columns we are interested in
371 foreach (self::$_properties as $property) {
372 if (property_exists($result, $property)) {
373 $row[$property] = $result->$property;
374 }
375 }
376
377 //carry campaign on selectors.
378 $row['campaign'] = CRM_Utils_Array::value($result->member_campaign_id, $allCampaigns);
379 $row['campaign_id'] = $result->member_campaign_id;
380
a7488080 381 if (!empty($row['member_is_test'])) {
6a488035
TO
382 $row['membership_type'] = $row['membership_type'] . " (test)";
383 }
384
385 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->membership_id;
386
387 if (!isset($result->owner_membership_id)) {
388 // unset renew and followup link for deceased membership
389 $currentMask = $mask;
390 if ($result->membership_status == 'Deceased') {
391 $currentMask = $currentMask & ~CRM_Core_Action::RENEW & ~CRM_Core_Action::FOLLOWUP;
392 }
393
394 $isCancelSupported = CRM_Member_BAO_Membership::isCancelSubscriptionSupported($row['membership_id']);
395 $row['action'] = CRM_Core_Action::formLink(self::links('all',
396 $this->_isPaymentProcessor,
397 $this->_accessContribution,
398 $this->_key,
399 $this->_context,
400 $isCancelSupported
401 ),
402 $currentMask,
403 array(
404 'id' => $result->membership_id,
405 'cid' => $result->contact_id,
406 'cxt' => $this->_context,
87dab4a4
AH
407 ),
408 ts('more'),
409 FALSE,
410 'membership.selector.row',
411 'Membership',
412 $result->membership_id
6a488035
TO
413 );
414 }
415 else {
416 $row['action'] = CRM_Core_Action::formLink(self::links('view'), $mask,
417 array(
418 'id' => $result->membership_id,
419 'cid' => $result->contact_id,
420 'cxt' => $this->_context,
87dab4a4
AH
421 ),
422 ts('more'),
423 FALSE,
424 'membership.selector.row',
425 'Membership',
426 $result->membership_id
6a488035
TO
427 );
428 }
429
430 //does membership have auto renew CRM-7137.
431 $autoRenew = FALSE;
432 if (isset($result->membership_recur_id) && $result->membership_recur_id &&
353ffa53 433 !CRM_Member_BAO_Membership::isSubscriptionCancelled($row['membership_id'])
6a488035
TO
434 ) {
435 $autoRenew = TRUE;
436 }
437 $row['auto_renew'] = $autoRenew;
438
b09fe5ed 439 $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
440 );
441
442 $rows[] = $row;
443 }
444
445 return $rows;
446 }
447
448 /**
1054415f 449 * @inheritDoc
6a488035 450 */
6a488035
TO
451 public function getQILL() {
452 return $this->_query->qill();
453 }
454
455 /**
100fef9d 456 * Returns the column headers as an array of tuples:
6a488035
TO
457 * (name, sortName (key to the sort array))
458 *
b2363ea8
TO
459 * @param string $action
460 * The action being performed.
3f8d2862 461 * @param string $output
b2363ea8 462 * What should the result set include (web/email/csv).
6a488035 463 *
a6c01b45
CW
464 * @return array
465 * the column headers that need to be displayed
6a488035
TO
466 */
467 public function &getColumnHeaders($action = NULL, $output = NULL) {
468 if (!isset(self::$_columnHeaders)) {
469 self::$_columnHeaders = array(
470 array(
471 'name' => ts('Type'),
33a5a53d 472 'sort' => 'membership_type',
6a488035
TO
473 'direction' => CRM_Utils_Sort::DONTCARE,
474 ),
b09fe5ed 475 array(
353ffa53 476 'name' => ts('Member Since'),
6a488035
TO
477 'sort' => 'join_date',
478 'direction' => CRM_Utils_Sort::DESCENDING,
479 ),
480 array(
481 'name' => ts('Start Date'),
482 'sort' => 'membership_start_date',
483 'direction' => CRM_Utils_Sort::DONTCARE,
484 ),
485 array(
486 'name' => ts('End Date'),
487 'sort' => 'membership_end_date',
488 'direction' => CRM_Utils_Sort::DONTCARE,
489 ),
490 array(
491 'name' => ts('Source'),
492 'sort' => 'membership_source',
493 'direction' => CRM_Utils_Sort::DONTCARE,
494 ),
495 array(
496 'name' => ts('Status'),
33a5a53d 497 'sort' => 'membership_status',
6a488035
TO
498 'direction' => CRM_Utils_Sort::DONTCARE,
499 ),
500 array(
501 'name' => ts('Auto-renew?'),
502 ),
503 array('desc' => ts('Actions')),
504 );
505
506 if (!$this->_single) {
507 $pre = array(
508 array('desc' => ts('Contact Type')),
509 array(
510 'name' => ts('Name'),
511 'sort' => 'sort_name',
512 'direction' => CRM_Utils_Sort::DONTCARE,
513 ),
514 );
515 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
516 }
517 }
518 return self::$_columnHeaders;
519 }
520
bb3a214a
EM
521 /**
522 * @return mixed
523 */
00be9182 524 public function alphabetQuery() {
6a488035
TO
525 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
526 }
527
bb3a214a
EM
528 /**
529 * @return string
530 */
00be9182 531 public function &getQuery() {
6a488035
TO
532 return $this->_query;
533 }
534
535 /**
100fef9d 536 * Name of export file.
6a488035 537 *
b2363ea8
TO
538 * @param string $output
539 * Type of output.
6a488035 540 *
a6c01b45
CW
541 * @return string
542 * name of the file
6a488035 543 */
00be9182 544 public function getExportFileName($output = 'csv') {
6a488035
TO
545 return ts('CiviCRM Member Search');
546 }
96025800 547
6a488035 548}