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