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