Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-30-00-43-32
[civicrm-core.git] / CRM / Contribute / 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_Contribute_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 'contribution_id',
68 'contact_type',
69 'sort_name',
70 'amount_level',
71 'total_amount',
72 'financial_type',
73 'contribution_source',
74 'receive_date',
75 'thankyou_date',
76 'contribution_status_id',
77 'contribution_status',
78 'cancel_date',
79 'product_name',
80 'is_test',
81 'contribution_recur_id',
82 'receipt_date',
83 'membership_id',
84 'currency',
85 'contribution_campaign_id',
86 'contribution_soft_credit_name',
87 'contribution_soft_credit_contact_id',
88 'contribution_soft_credit_amount',
89 'contribution_soft_credit_type',
90 );
91
92 /**
93 * Are we restricting ourselves to a single contact
94 *
95 * @access protected
96 * @var boolean
97 */
98 protected $_single = FALSE;
99
100 /**
101 * Are we restricting ourselves to a single contact
102 *
103 * @access protected
104 * @var boolean
105 */
106 protected $_limit = NULL;
107
108 /**
109 * What context are we being invoked from
110 *
111 * @access protected
112 * @var string
113 */
114 protected $_context = NULL;
115
116 /**
117 * What component context are we being invoked from
118 *
119 * @access protected
120 * @var string
121 */
122 protected $_compContext = NULL;
123
124 /**
125 * QueryParams is the array returned by exportValues called on
126 * the HTML_QuickForm_Controller for that page.
127 *
128 * @var array
129 * @access protected
130 */
131 public $_queryParams;
132
133 /**
134 * Represent the type of selector
135 *
136 * @var int
137 * @access protected
138 */
139 protected $_action;
140
141 /**
142 * The additional clause that we restrict the search with
143 *
144 * @var string
145 */
146 protected $_contributionClause = NULL;
147
148 /**
149 * The query object
150 *
151 * @var string
152 */
153 protected $_query;
154
155 protected $_includeSoftCredits = FALSE;
156
157 /**
158 * Class constructor
159 *
160 * @param array $queryParams array of parameters for query
161 * @param \const|int $action - action of search basic or advanced.
162 * @param string $contributionClause if the caller wants to further restrict the search (used in contributions)
163 * @param boolean $single are we dealing only with one contact?
164 * @param int $limit how many contributions do we want returned
165 *
166 * @param string $context
167 * @param null $compContext
168 *
169 * @return \CRM_Contribute_Selector_Search
170 @access public
171 */
172 function __construct(&$queryParams,
173 $action = CRM_Core_Action::NONE,
174 $contributionClause = NULL,
175 $single = FALSE,
176 $limit = NULL,
177 $context = 'search',
178 $compContext = NULL
179 ) {
180
181 // submitted form values
182 $this->_queryParams = &$queryParams;
183
184 $this->_single = $single;
185 $this->_limit = $limit;
186 $this->_context = $context;
187 $this->_compContext = $compContext;
188
189 $this->_contributionClause = $contributionClause;
190
191 // type of selector
192 $this->_action = $action;
193
194 $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
195 $this->_query = new CRM_Contact_BAO_Query(
196 $this->_queryParams,
197 CRM_Contribute_BAO_Query::defaultReturnProperties(
198 CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
199 FALSE
200 ),
201 NULL, FALSE, FALSE,
202 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
203 );
204 if ($this->_includeSoftCredits) {
205 $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
206 $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
207 } else {
208 $this->_query->_distinctComponentClause = " civicrm_contribution.id";
209 $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
210 }
211 }
212
213 /**
214 * This method returns the links that are given for each search row.
215 * currently the links added for each row are
216 *
217 * - View
218 * - Edit
219 *
220 * @param int $componentId
221 * @param null $componentAction
222 * @param null $key
223 * @param null $compContext
224 *
225 * @return array
226 * @access public
227 */
228 static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
229 $extraParams = NULL;
230 if ($componentId) {
231 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
232 }
233 if ($compContext) {
234 $extraParams .= "&compContext={$compContext}";
235 }
236 if ($key) {
237 $extraParams .= "&key={$key}";
238 }
239
240 if (!(self::$_links)) {
241 self::$_links = array(
242 CRM_Core_Action::VIEW => array(
243 'name' => ts('View'),
244 'url' => 'civicrm/contact/view/contribution',
245 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
246 'title' => ts('View Contribution'),
247 ),
248 CRM_Core_Action::UPDATE => array(
249 'name' => ts('Edit'),
250 'url' => 'civicrm/contact/view/contribution',
251 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
252 'title' => ts('Edit Contribution'),
253 ),
254 CRM_Core_Action::DELETE => array(
255 'name' => ts('Delete'),
256 'url' => 'civicrm/contact/view/contribution',
257 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
258 'title' => ts('Delete Contribution'),
259 ),
260 );
261 }
262 return self::$_links;
263 }
264
265 /**
266 * Getter for array of the parameters required for creating pager.
267 *
268 * @param $action
269 * @param array $params
270 * @access public
271 */
272 function getPagerParams($action, &$params) {
273 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
274 $params['csvString'] = NULL;
275 if ($this->_limit) {
276 $params['rowCount'] = $this->_limit;
277 }
278 else {
279 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
280 }
281
282 $params['buttonTop'] = 'PagerTopButton';
283 $params['buttonBottom'] = 'PagerBottomButton';
284 }
285
286 /**
287 * Returns total number of rows for the query.
288 *
289 * @param
290 *
291 * @return int Total number of rows
292 * @access public
293 */
294 function getTotalCount($action) {
295 return $this->_query->searchQuery(0, 0, NULL,
296 TRUE, FALSE,
297 FALSE, FALSE,
298 FALSE,
299 $this->_contributionClause
300 );
301 }
302
303 /**
304 * Returns all the rows in the given offset and rowCount
305 *
306 * @param enum $action the action being performed
307 * @param int $offset the row number to start from
308 * @param int $rowCount the number of rows to return
309 * @param string $sort the sql string that describes the sort order
310 * @param enum $output what should the result set include (web/email/csv)
311 *
312 * @return int the total number of rows for this action
313 */
314 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
315 if ($this->_includeSoftCredits) {
316 // especial sort order when rows include soft credits
317 $sort = "civicrm_contribution.receive_date DESC, civicrm_contribution.id, civicrm_contribution_soft.id";
318 }
319 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
320 FALSE, FALSE,
321 FALSE, FALSE,
322 FALSE,
323 $this->_contributionClause
324 );
325 // process the result of the query
326 $rows = array();
327
328 //CRM-4418 check for view/edit/delete
329 $permissions = array(CRM_Core_Permission::VIEW);
330 if (CRM_Core_Permission::check('edit contributions')) {
331 $permissions[] = CRM_Core_Permission::EDIT;
332 }
333 if (CRM_Core_Permission::check('delete in CiviContribute')) {
334 $permissions[] = CRM_Core_Permission::DELETE;
335 }
336 $mask = CRM_Core_Action::mask($permissions);
337
338 $qfKey = $this->_key;
339 $componentId = $componentContext = NULL;
340 if ($this->_context != 'contribute') {
341 $qfKey = CRM_Utils_Request::retrieve('key', 'String', CRM_Core_DAO::$_nullObject);
342 $componentId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject);
343 $componentAction = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject);
344 $componentContext = CRM_Utils_Request::retrieve('compContext', 'String', CRM_Core_DAO::$_nullObject);
345
346 if (!$componentContext &&
347 $this->_compContext
348 ) {
349 $componentContext = $this->_compContext;
350 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
351 }
352 }
353
354 // get all contribution status
355 $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status',
356 FALSE, FALSE, FALSE, NULL, 'name', FALSE
357 );
358
359 //get all campaigns.
360 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
361
362 While ($result->fetch()) {
363 $row = array();
364 // the columns we are interested in
365 foreach (self::$_properties as $property) {
366 if (property_exists($result, $property)) {
367 $row[$property] = $result->$property;
368 }
369 }
370
371 //carry campaign on selectors.
372 $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns);
373 $row['campaign_id'] = $result->contribution_campaign_id;
374
375 // add contribution status name
376 $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'],
377 $contributionStatuses
378 );
379
380 if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
381 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
382 }
383 elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
384 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
385 }
386
387 if ($row['is_test']) {
388 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
389 }
390
391 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id;
392
393 $actions = array(
394 'id' => $result->contribution_id,
395 'cid' => $result->contact_id,
396 'cxt' => $this->_context,
397 );
398
399 $row['action'] = CRM_Core_Action::formLink(
400 self::links($componentId,
401 $componentAction,
402 $qfKey,
403 $componentContext
404 ),
405 $mask, $actions,
406 ts('more'),
407 FALSE,
408 'contribution.selector.row',
409 'Contribution',
410 $result->contribution_id
411 );
412
413 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
414 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
415 );
416
417 if (!empty($row['amount_level'])) {
418 CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']);
419 }
420
421 $rows[] = $row;
422 }
423
424 return $rows;
425 }
426
427 /**
428 * @return array $qill which contains an array of strings
429 * @access public
430 */
431
432 // the current internationalisation is bad, but should more or less work
433 // for most of "European" languages
434 public function getQILL() {
435 return $this->_query->qill();
436 }
437
438 /**
439 * Returns the column headers as an array of tuples:
440 * (name, sortName (key to the sort array))
441 *
442 * @param string $action the action being performed
443 * @param enum $output what should the result set include (web/email/csv)
444 *
445 * @return array the column headers that need to be displayed
446 * @access public
447 */
448 public function &getColumnHeaders($action = NULL, $output = NULL) {
449 self::$_columnHeaders = array(
450 array(
451 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
452 'sort' => 'total_amount',
453 'direction' => CRM_Utils_Sort::DONTCARE,
454 ),
455 );
456 if ($this->_includeSoftCredits) {
457 self::$_columnHeaders =
458 array_merge(
459 self::$_columnHeaders,
460 array(
461 array(
462 'name' => ts('Soft Credit Amount'),
463 'sort' => 'contribution_soft_credit_amount',
464 'direction' => CRM_Utils_Sort::DONTCARE,
465 )
466 )
467 );
468 }
469 self::$_columnHeaders =
470 array_merge(
471 self::$_columnHeaders,
472 array(
473 array(
474 'name' => ts('Type'),
475 'sort' => 'financial_type',
476 'direction' => CRM_Utils_Sort::DONTCARE,
477 ),
478 array(
479 'name' => ts('Source'),
480 'sort' => 'contribution_source',
481 'direction' => CRM_Utils_Sort::DONTCARE,
482 ),
483 array(
484 'name' => ts('Received'),
485 'sort' => 'receive_date',
486 'direction' => CRM_Utils_Sort::DESCENDING,
487 ),
488 array(
489 'name' => ts('Thank-you Sent'),
490 'sort' => 'thankyou_date',
491 'direction' => CRM_Utils_Sort::DONTCARE,
492 ),
493 array(
494 'name' => ts('Status'),
495 'sort' => 'contribution_status',
496 'direction' => CRM_Utils_Sort::DONTCARE,
497 ),
498 array(
499 'name' => ts('Premium'),
500 'sort' => 'product_name',
501 'direction' => CRM_Utils_Sort::DONTCARE,
502 ),
503 )
504 );
505 if (!$this->_single) {
506 $pre = array(
507 array('desc' => ts('Contact Type')),
508 array(
509 'name' => ts('Name'),
510 'sort' => 'sort_name',
511 'direction' => CRM_Utils_Sort::DONTCARE,
512 ),
513 );
514 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
515 }
516 if ($this->_includeSoftCredits) {
517 self::$_columnHeaders =
518 array_merge(
519 self::$_columnHeaders,
520 array(
521 array(
522 'name' => ts('Soft Credit For'),
523 'sort' => 'contribution_soft_credit_name',
524 'direction' => CRM_Utils_Sort::DONTCARE,
525 ),
526 array(
527 'name' => ts('Soft Credit Type'),
528 'sort' => 'contribution_soft_credit_type',
529 'direction' => CRM_Utils_Sort::ASCENDING,
530 ),
531 )
532 );
533 }
534 self::$_columnHeaders =
535 array_merge(
536 self::$_columnHeaders, array(
537 array('desc' => ts('Actions'))
538 )
539 );
540 return self::$_columnHeaders;
541 }
542
543 /**
544 * @return mixed
545 */
546 function alphabetQuery() {
547 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
548 }
549
550 /**
551 * @return string
552 */
553 function &getQuery() {
554 return $this->_query;
555 }
556
557 /**
558 * Name of export file.
559 *
560 * @param string $output type of output
561 *
562 * @return string name of the file
563 */
564 function getExportFileName($output = 'csv') {
565 return ts('CiviCRM Contribution Search');
566 }
567
568 /**
569 * @return mixed
570 */
571 function getSummary() {
572 return $this->_query->summaryContribution($this->_context);
573 }
574 }
575