ICM-18 Fixed issues that were reported by Jenkins test
[civicrm-core.git] / CRM / Member / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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_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
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 '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 /**
80 * Are we restricting ourselves to a single contact
81 *
82 * @var boolean
83 */
84 protected $_single = FALSE;
85
86 /**
87 * Are we restricting ourselves to a single contact
88 *
89 * @var boolean
90 */
91 protected $_limit = NULL;
92
93 /**
94 * What context are we being invoked from
95 *
96 * @var string
97 */
98 protected $_context = NULL;
99
100 /**
101 * QueryParams is the array returned by exportValues called on
102 * the HTML_QuickForm_Controller for that page.
103 *
104 * @var array
105 */
106 public $_queryParams;
107
108 /**
109 * Represent the type of selector.
110 *
111 * @var int
112 */
113 protected $_action;
114
115 /**
116 * The additional clause that we restrict the search with.
117 *
118 * @var string
119 */
120 protected $_memberClause = NULL;
121
122 /**
123 * The query object.
124 *
125 * @var string
126 */
127 protected $_query;
128
129 /**
130 * Class constructor.
131 *
132 * @param array $queryParams
133 * Array of parameters for query.
134 * @param \const|int $action - action of search basic or advanced.
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.
141 *
142 * @param string $context
143 *
144 * @return \CRM_Member_Selector_Search
145 */
146 public function __construct(
147 &$queryParams,
148 $action = CRM_Core_Action::NONE,
149 $memberClause = NULL,
150 $single = FALSE,
151 $limit = NULL,
152 $context = 'search'
153 ) {
154 // submitted form values
155 $this->_queryParams = &$queryParams;
156
157 $this->_single = $single;
158 $this->_limit = $limit;
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 }
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 *
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 *
191 * @return array
192 */
193 public static function &links(
194 $status = 'all',
195 $isPaymentProcessor = NULL,
196 $accessContribution = NULL,
197 $qfKey = NULL,
198 $context = NULL,
199 $isCancelSupported = FALSE
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 }
269
270 /**
271 * Getter for array of the parameters required for creating pager.
272 *
273 * @param $action
274 * @param array $params
275 */
276 public function getPagerParams($action, &$params) {
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 }
289
290 /**
291 * Returns total number of rows for the query.
292 *
293 * @param
294 *
295 * @return int
296 * Total number of rows
297 */
298 public function getTotalCount($action) {
299 return $this->_query->searchQuery(0, 0, NULL,
300 TRUE, FALSE,
301 FALSE, FALSE,
302 FALSE,
303 $this->_memberClause
304 );
305 }
306
307 /**
308 * Returns all the rows in the given offset and rowCount.
309 *
310 * @param string $action
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.
318 * @param string $output
319 * What should the result set include (web/email/csv).
320 *
321 * @return int
322 * the total number of rows for this action
323 */
324 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
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
381 if (!empty($row['member_is_test'])) {
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 if (!isset($result->owner_membership_id)) {
396 $links = self::links('all',
397 $this->_isPaymentProcessor,
398 $this->_accessContribution,
399 $this->_key,
400 $this->_context,
401 $isCancelSupported
402 );
403 }
404 else {
405 $links = self::links('view');
406 }
407 // check permissions
408 $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $result->membership_type_id, 'financial_type_id');
409 $finType = CRM_Contribute_PseudoConstant::financialType($finTypeId);
410 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
411 && !CRM_Core_Permission::check('edit contributions of type ' . $finType)
412 ) {
413 unset($links[CRM_Core_Action::UPDATE]);
414 }
415 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() &&
416 !CRM_Core_Permission::check('delete contributions of type ' . $finType)
417 ) {
418 unset($links[CRM_Core_Action::DELETE]);
419 }
420 $row['action'] = CRM_Core_Action::formLink($links,
421 $currentMask,
422 array(
423 'id' => $result->membership_id,
424 'cid' => $result->contact_id,
425 'cxt' => $this->_context,
426 ),
427 ts('more'),
428 FALSE,
429 'membership.selector.row',
430 'Membership',
431 $result->membership_id
432 );
433 }
434 else {
435 $row['action'] = CRM_Core_Action::formLink($links, $mask,
436 array(
437 'id' => $result->membership_id,
438 'cid' => $result->contact_id,
439 'cxt' => $this->_context,
440 ),
441 ts('more'),
442 FALSE,
443 'membership.selector.row',
444 'Membership',
445 $result->membership_id
446 );
447 }
448
449 //does membership have auto renew CRM-7137.
450 $autoRenew = FALSE;
451 if (isset($result->membership_recur_id) && $result->membership_recur_id &&
452 !CRM_Member_BAO_Membership::isSubscriptionCancelled($row['membership_id'])
453 ) {
454 $autoRenew = TRUE;
455 }
456 $row['auto_renew'] = $autoRenew;
457
458 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
459 );
460
461 $rows[] = $row;
462 }
463
464 return $rows;
465 }
466
467 /**
468 * @inheritDoc
469 */
470 public function getQILL() {
471 return $this->_query->qill();
472 }
473
474 /**
475 * Returns the column headers as an array of tuples:
476 * (name, sortName (key to the sort array))
477 *
478 * @param string $action
479 * The action being performed.
480 * @param string $output
481 * What should the result set include (web/email/csv).
482 *
483 * @return array
484 * the column headers that need to be displayed
485 */
486 public function &getColumnHeaders($action = NULL, $output = NULL) {
487 if (!isset(self::$_columnHeaders)) {
488 self::$_columnHeaders = array(
489 array(
490 'name' => ts('Type'),
491 'sort' => 'membership_type',
492 'direction' => CRM_Utils_Sort::DONTCARE,
493 ),
494 array(
495 'name' => ts('Member Since'),
496 'sort' => 'join_date',
497 'direction' => CRM_Utils_Sort::DESCENDING,
498 ),
499 array(
500 'name' => ts('Start Date'),
501 'sort' => 'membership_start_date',
502 'direction' => CRM_Utils_Sort::DONTCARE,
503 ),
504 array(
505 'name' => ts('End Date'),
506 'sort' => 'membership_end_date',
507 'direction' => CRM_Utils_Sort::DONTCARE,
508 ),
509 array(
510 'name' => ts('Source'),
511 'sort' => 'membership_source',
512 'direction' => CRM_Utils_Sort::DONTCARE,
513 ),
514 array(
515 'name' => ts('Status'),
516 'sort' => 'membership_status',
517 'direction' => CRM_Utils_Sort::DONTCARE,
518 ),
519 array(
520 'name' => ts('Auto-renew?'),
521 ),
522 array('desc' => ts('Actions')),
523 );
524
525 if (!$this->_single) {
526 $pre = array(
527 array('desc' => ts('Contact Type')),
528 array(
529 'name' => ts('Name'),
530 'sort' => 'sort_name',
531 'direction' => CRM_Utils_Sort::DONTCARE,
532 ),
533 );
534 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
535 }
536 }
537 return self::$_columnHeaders;
538 }
539
540 /**
541 * @return mixed
542 */
543 public function alphabetQuery() {
544 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
545 }
546
547 /**
548 * @return string
549 */
550 public function &getQuery() {
551 return $this->_query;
552 }
553
554 /**
555 * Name of export file.
556 *
557 * @param string $output
558 * Type of output.
559 *
560 * @return string
561 * name of the file
562 */
563 public function getExportFileName($output = 'csv') {
564 return ts('CiviCRM Member Search');
565 }
566
567 }