INFRA-132 - CRM/Contribute - phpcbf
[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 * @var boolean
96 */
97 protected $_single = FALSE;
98
99 /**
100 * Are we restricting ourselves to a single contact
101 *
102 * @var boolean
103 */
104 protected $_limit = NULL;
105
106 /**
107 * What context are we being invoked from
108 *
109 * @var string
110 */
111 protected $_context = NULL;
112
113 /**
114 * What component context are we being invoked from
115 *
116 * @var string
117 */
118 protected $_compContext = NULL;
119
120 /**
121 * QueryParams is the array returned by exportValues called on
122 * the HTML_QuickForm_Controller for that page.
123 *
124 * @var array
125 */
126 public $_queryParams;
127
128 /**
129 * Represent the type of selector
130 *
131 * @var int
132 */
133 protected $_action;
134
135 /**
136 * The additional clause that we restrict the search with
137 *
138 * @var string
139 */
140 protected $_contributionClause = NULL;
141
142 /**
143 * The query object
144 *
145 * @var string
146 */
147 protected $_query;
148
149 protected $_includeSoftCredits = FALSE;
150
151 /**
152 * Class constructor
153 *
154 * @param array $queryParams
155 * Array of parameters for query.
156 * @param \const|int $action - action of search basic or advanced.
157 * @param string $contributionClause
158 * If the caller wants to further restrict the search (used in contributions).
159 * @param bool $single
160 * Are we dealing only with one contact?.
161 * @param int $limit
162 * How many contributions do we want returned.
163 *
164 * @param string $context
165 * @param null $compContext
166 *
167 * @return \CRM_Contribute_Selector_Search
168 @access public
169 */
170 function __construct(&$queryParams,
171 $action = CRM_Core_Action::NONE,
172 $contributionClause = NULL,
173 $single = FALSE,
174 $limit = NULL,
175 $context = 'search',
176 $compContext = NULL
177 ) {
178
179 // submitted form values
180 $this->_queryParams = &$queryParams;
181
182 $this->_single = $single;
183 $this->_limit = $limit;
184 $this->_context = $context;
185 $this->_compContext = $compContext;
186
187 $this->_contributionClause = $contributionClause;
188
189 // type of selector
190 $this->_action = $action;
191
192 $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
193 $this->_query = new CRM_Contact_BAO_Query(
194 $this->_queryParams,
195 CRM_Contribute_BAO_Query::defaultReturnProperties(
196 CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
197 FALSE
198 ),
199 NULL, FALSE, FALSE,
200 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
201 );
202 if ($this->_includeSoftCredits) {
203 $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
204 $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
205 } else {
206 $this->_query->_distinctComponentClause = " civicrm_contribution.id";
207 $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
208 }
209 }
210
211 /**
212 * This method returns the links that are given for each search row.
213 * currently the links added for each row are
214 *
215 * - View
216 * - Edit
217 *
218 * @param int $componentId
219 * @param null $componentAction
220 * @param null $key
221 * @param null $compContext
222 *
223 * @return array
224 */
225 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
226 $extraParams = NULL;
227 if ($componentId) {
228 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
229 }
230 if ($compContext) {
231 $extraParams .= "&compContext={$compContext}";
232 }
233 if ($key) {
234 $extraParams .= "&key={$key}";
235 }
236
237 if (!(self::$_links)) {
238 self::$_links = array(
239 CRM_Core_Action::VIEW => array(
240 'name' => ts('View'),
241 'url' => 'civicrm/contact/view/contribution',
242 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
243 'title' => ts('View Contribution'),
244 ),
245 CRM_Core_Action::UPDATE => array(
246 'name' => ts('Edit'),
247 'url' => 'civicrm/contact/view/contribution',
248 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
249 'title' => ts('Edit Contribution'),
250 ),
251 CRM_Core_Action::DELETE => array(
252 'name' => ts('Delete'),
253 'url' => 'civicrm/contact/view/contribution',
254 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
255 'title' => ts('Delete Contribution'),
256 ),
257 );
258 }
259 return self::$_links;
260 }
261
262 /**
263 * Getter for array of the parameters required for creating pager.
264 *
265 * @param $action
266 * @param array $params
267 */
268 public function getPagerParams($action, &$params) {
269 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
270 $params['csvString'] = NULL;
271 if ($this->_limit) {
272 $params['rowCount'] = $this->_limit;
273 }
274 else {
275 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
276 }
277
278 $params['buttonTop'] = 'PagerTopButton';
279 $params['buttonBottom'] = 'PagerBottomButton';
280 }
281
282 /**
283 * Returns total number of rows for the query.
284 *
285 * @param
286 *
287 * @return int Total number of rows
288 */
289 public function getTotalCount($action) {
290 return $this->_query->searchQuery(0, 0, NULL,
291 TRUE, FALSE,
292 FALSE, FALSE,
293 FALSE,
294 $this->_contributionClause
295 );
296 }
297
298 /**
299 * Returns all the rows in the given offset and rowCount
300 *
301 * @param enum $action
302 * The action being performed.
303 * @param int $offset
304 * The row number to start from.
305 * @param int $rowCount
306 * The number of rows to return.
307 * @param string $sort
308 * The sql string that describes the sort order.
309 * @param enum $output
310 * What should the result set include (web/email/csv).
311 *
312 * @return int the total number of rows for this action
313 */
314 public 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 ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
414 );
415
416 if (!empty($row['amount_level'])) {
417 CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']);
418 }
419
420 $rows[] = $row;
421 }
422
423 return $rows;
424 }
425
426 /**
427 * @return array $qill which contains an array of strings
428 */
429
430 // the current internationalisation is bad, but should more or less work
431 // for most of "European" languages
432 public function getQILL() {
433 return $this->_query->qill();
434 }
435
436 /**
437 * Returns the column headers as an array of tuples:
438 * (name, sortName (key to the sort array))
439 *
440 * @param string $action
441 * The action being performed.
442 * @param enum $output
443 * What should the result set include (web/email/csv).
444 *
445 * @return array the column headers that need to be displayed
446 */
447 public function &getColumnHeaders($action = NULL, $output = NULL) {
448 self::$_columnHeaders = array(
449 array(
450 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
451 'sort' => 'total_amount',
452 'direction' => CRM_Utils_Sort::DONTCARE,
453 ),
454 );
455 if ($this->_includeSoftCredits) {
456 self::$_columnHeaders =
457 array_merge(
458 self::$_columnHeaders,
459 array(
460 array(
461 'name' => ts('Soft Credit Amount'),
462 'sort' => 'contribution_soft_credit_amount',
463 'direction' => CRM_Utils_Sort::DONTCARE,
464 )
465 )
466 );
467 }
468 self::$_columnHeaders =
469 array_merge(
470 self::$_columnHeaders,
471 array(
472 array(
473 'name' => ts('Type'),
474 'sort' => 'financial_type',
475 'direction' => CRM_Utils_Sort::DONTCARE,
476 ),
477 array(
478 'name' => ts('Source'),
479 'sort' => 'contribution_source',
480 'direction' => CRM_Utils_Sort::DONTCARE,
481 ),
482 array(
483 'name' => ts('Received'),
484 'sort' => 'receive_date',
485 'direction' => CRM_Utils_Sort::DESCENDING,
486 ),
487 array(
488 'name' => ts('Thank-you Sent'),
489 'sort' => 'thankyou_date',
490 'direction' => CRM_Utils_Sort::DONTCARE,
491 ),
492 array(
493 'name' => ts('Status'),
494 'sort' => 'contribution_status',
495 'direction' => CRM_Utils_Sort::DONTCARE,
496 ),
497 array(
498 'name' => ts('Premium'),
499 'sort' => 'product_name',
500 'direction' => CRM_Utils_Sort::DONTCARE,
501 ),
502 )
503 );
504 if (!$this->_single) {
505 $pre = array(
506 array('desc' => ts('Contact Type')),
507 array(
508 'name' => ts('Name'),
509 'sort' => 'sort_name',
510 'direction' => CRM_Utils_Sort::DONTCARE,
511 ),
512 );
513 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
514 }
515 if ($this->_includeSoftCredits) {
516 self::$_columnHeaders =
517 array_merge(
518 self::$_columnHeaders,
519 array(
520 array(
521 'name' => ts('Soft Credit For'),
522 'sort' => 'contribution_soft_credit_name',
523 'direction' => CRM_Utils_Sort::DONTCARE,
524 ),
525 array(
526 'name' => ts('Soft Credit Type'),
527 'sort' => 'contribution_soft_credit_type',
528 'direction' => CRM_Utils_Sort::ASCENDING,
529 ),
530 )
531 );
532 }
533 self::$_columnHeaders =
534 array_merge(
535 self::$_columnHeaders, array(
536 array('desc' => ts('Actions'))
537 )
538 );
539 return self::$_columnHeaders;
540 }
541
542 /**
543 * @return mixed
544 */
545 public function alphabetQuery() {
546 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
547 }
548
549 /**
550 * @return string
551 */
552 public function &getQuery() {
553 return $this->_query;
554 }
555
556 /**
557 * Name of export file.
558 *
559 * @param string $output
560 * Type of output.
561 *
562 * @return string name of the file
563 */
564 public function getExportFileName($output = 'csv') {
565 return ts('CiviCRM Contribution Search');
566 }
567
568 /**
569 * @return mixed
570 */
571 public function getSummary() {
572 return $this->_query->summaryContribution($this->_context);
573 }
574 }