Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-04-00-08-28
[civicrm-core.git] / CRM / Contribute / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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 * @return CRM_Contact_Selector
167 * @access public
168 */
169 function __construct(&$queryParams,
170 $action = CRM_Core_Action::NONE,
171 $contributionClause = NULL,
172 $single = FALSE,
173 $limit = NULL,
174 $context = 'search',
175 $compContext = NULL
176 ) {
177
178 // submitted form values
179 $this->_queryParams = &$queryParams;
180
181 $this->_single = $single;
182 $this->_limit = $limit;
183 $this->_context = $context;
184 $this->_compContext = $compContext;
185
186 $this->_contributionClause = $contributionClause;
187
188 // type of selector
189 $this->_action = $action;
190
191 $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
192 $this->_query = new CRM_Contact_BAO_Query(
193 $this->_queryParams,
194 CRM_Contribute_BAO_Query::defaultReturnProperties(
195 CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
196 FALSE
197 ),
198 NULL, FALSE, FALSE,
199 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
200 );
201 if ($this->_includeSoftCredits) {
202 $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
203 $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
204 } else {
205 $this->_query->_distinctComponentClause = " civicrm_contribution.id";
206 $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
207 }
208 }
209 //end of constructor
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 * @return array
219 * @access public
220 *
221 */
222 static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
223 $extraParams = NULL;
224 if ($componentId) {
225 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
226 }
227 if ($compContext) {
228 $extraParams .= "&compContext={$compContext}";
229 }
230 if ($key) {
231 $extraParams .= "&key={$key}";
232 }
233
234 if (!(self::$_links)) {
235 self::$_links = array(
236 CRM_Core_Action::VIEW => array(
237 'name' => ts('View'),
238 'url' => 'civicrm/contact/view/contribution',
239 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
240 'title' => ts('View Contribution'),
241 ),
242 CRM_Core_Action::UPDATE => array(
243 'name' => ts('Edit'),
244 'url' => 'civicrm/contact/view/contribution',
245 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
246 'title' => ts('Edit Contribution'),
247 ),
248 CRM_Core_Action::DELETE => array(
249 'name' => ts('Delete'),
250 'url' => 'civicrm/contact/view/contribution',
251 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
252 'title' => ts('Delete Contribution'),
253 ),
254 );
255 }
256 return self::$_links;
257 }
258 //end of function
259
260 /**
261 * getter for array of the parameters required for creating pager.
262 *
263 * @param
264 * @access public
265 */
266 function getPagerParams($action, &$params) {
267 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
268 $params['csvString'] = NULL;
269 if ($this->_limit) {
270 $params['rowCount'] = $this->_limit;
271 }
272 else {
273 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
274 }
275
276 $params['buttonTop'] = 'PagerTopButton';
277 $params['buttonBottom'] = 'PagerBottomButton';
278 }
279 //end of function
280
281 /**
282 * Returns total number of rows for the query.
283 *
284 * @param
285 *
286 * @return int Total number of rows
287 * @access public
288 */
289 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 the action being performed
302 * @param int $offset the row number to start from
303 * @param int $rowCount the number of rows to return
304 * @param string $sort the sql string that describes the sort order
305 * @param enum $output what should the result set include (web/email/csv)
306 *
307 * @return int the total number of rows for this action
308 */
309 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
310 if ($this->_includeSoftCredits) {
311 // especial sort order when rows include soft credits
312 $sort = "civicrm_contribution.receive_date DESC, civicrm_contribution.id, civicrm_contribution_soft.id";
313 }
314 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
315 FALSE, FALSE,
316 FALSE, FALSE,
317 FALSE,
318 $this->_contributionClause
319 );
320 // process the result of the query
321 $rows = array();
322
323 //CRM-4418 check for view/edit/delete
324 $permissions = array(CRM_Core_Permission::VIEW);
325 if (CRM_Core_Permission::check('edit contributions')) {
326 $permissions[] = CRM_Core_Permission::EDIT;
327 }
328 if (CRM_Core_Permission::check('delete in CiviContribute')) {
329 $permissions[] = CRM_Core_Permission::DELETE;
330 }
331 $mask = CRM_Core_Action::mask($permissions);
332
333 $qfKey = $this->_key;
334 $componentId = $componentContext = NULL;
335 if ($this->_context != 'contribute') {
336 $qfKey = CRM_Utils_Request::retrieve('key', 'String', CRM_Core_DAO::$_nullObject);
337 $componentId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject);
338 $componentAction = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject);
339 $componentContext = CRM_Utils_Request::retrieve('compContext', 'String', CRM_Core_DAO::$_nullObject);
340
341 if (!$componentContext &&
342 $this->_compContext
343 ) {
344 $componentContext = $this->_compContext;
345 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
346 }
347 }
348
349 // get all contribution status
350 $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status',
351 FALSE, FALSE, FALSE, NULL, 'name', FALSE
352 );
353
354 //get all campaigns.
355 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
356
357 While ($result->fetch()) {
358 $row = array();
359 // the columns we are interested in
360 foreach (self::$_properties as $property) {
361 if (property_exists($result, $property)) {
362 $row[$property] = $result->$property;
363 }
364 }
365
366 //carry campaign on selectors.
367 $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns);
368 $row['campaign_id'] = $result->contribution_campaign_id;
369
370 // add contribution status name
371 $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'],
372 $contributionStatuses
373 );
374
375 if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
376 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
377 }
378 elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
379 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
380 }
381
382 if ($row['is_test']) {
383 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
384 }
385
386 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id;
387
388 $actions = array(
389 'id' => $result->contribution_id,
390 'cid' => $result->contact_id,
391 'cxt' => $this->_context,
392 );
393
394 $row['action'] = CRM_Core_Action::formLink(
395 self::links($componentId,
396 $componentAction,
397 $qfKey,
398 $componentContext
399 ),
400 $mask, $actions,
401 ts('more'),
402 FALSE,
403 'contribution.selector.row',
404 'Contribution',
405 $result->contribution_id
406 );
407
408 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
409 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
410 );
411
412 if (!empty($row['amount_level'])) {
413 CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']);
414 }
415
416 $rows[] = $row;
417 }
418
419 return $rows;
420 }
421
422 /**
423 *
424 * @return array $qill which contains an array of strings
425 * @access public
426 */
427
428 // the current internationalisation is bad, but should more or less work
429 // for most of "European" languages
430 public function getQILL() {
431 return $this->_query->qill();
432 }
433
434 /**
435 * returns the column headers as an array of tuples:
436 * (name, sortName (key to the sort array))
437 *
438 * @param string $action the action being performed
439 * @param enum $output what should the result set include (web/email/csv)
440 *
441 * @return array the column headers that need to be displayed
442 * @access public
443 */
444 public function &getColumnHeaders($action = NULL, $output = NULL) {
445 self::$_columnHeaders = array(
446 array(
447 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
448 'sort' => 'total_amount',
449 'direction' => CRM_Utils_Sort::DONTCARE,
450 ),
451 );
452 if ($this->_includeSoftCredits) {
453 self::$_columnHeaders =
454 array_merge(
455 self::$_columnHeaders,
456 array(
457 array(
458 'name' => ts('Soft Credit Amount'),
459 'sort' => 'contribution_soft_credit_amount',
460 'direction' => CRM_Utils_Sort::DONTCARE,
461 )
462 )
463 );
464 }
465 self::$_columnHeaders =
466 array_merge(
467 self::$_columnHeaders,
468 array(
469 array(
470 'name' => ts('Type'),
471 'sort' => 'financial_type_id',
472 'direction' => CRM_Utils_Sort::DONTCARE,
473 ),
474 array(
475 'name' => ts('Source'),
476 'sort' => 'contribution_source',
477 'direction' => CRM_Utils_Sort::DONTCARE,
478 ),
479 array(
480 'name' => ts('Received'),
481 'sort' => 'receive_date',
482 'direction' => CRM_Utils_Sort::DESCENDING,
483 ),
484 array(
485 'name' => ts('Thank-you Sent'),
486 'sort' => 'thankyou_date',
487 'direction' => CRM_Utils_Sort::DONTCARE,
488 ),
489 array(
490 'name' => ts('Status'),
491 'sort' => 'contribution_status_id',
492 'direction' => CRM_Utils_Sort::DONTCARE,
493 ),
494 array(
495 'name' => ts('Premium'),
496 'sort' => 'product_name',
497 'direction' => CRM_Utils_Sort::DONTCARE,
498 ),
499 )
500 );
501 if (!$this->_single) {
502 $pre = array(
503 array('desc' => ts('Contact Type')),
504 array(
505 'name' => ts('Name'),
506 'sort' => 'sort_name',
507 'direction' => CRM_Utils_Sort::DONTCARE,
508 ),
509 );
510 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
511 }
512 if ($this->_includeSoftCredits) {
513 self::$_columnHeaders =
514 array_merge(
515 self::$_columnHeaders,
516 array(
517 array(
518 'name' => ts('Soft Credit For'),
519 'sort' => 'contribution_soft_credit_name',
520 'direction' => CRM_Utils_Sort::DONTCARE,
521 ),
522 array(
523 'name' => ts('Soft Credit Type'),
524 'sort' => 'contribution_soft_credit_type',
525 'direction' => CRM_Utils_Sort::ASCENDING,
526 ),
527 )
528 );
529 }
530 self::$_columnHeaders =
531 array_merge(
532 self::$_columnHeaders, array(
533 array('desc' => ts('Actions'))
534 )
535 );
536 return self::$_columnHeaders;
537 }
538
539 function alphabetQuery() {
540 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
541 }
542
543 function &getQuery() {
544 return $this->_query;
545 }
546
547 /**
548 * name of export file.
549 *
550 * @param string $output type of output
551 *
552 * @return string name of the file
553 */
554 function getExportFileName($output = 'csv') {
555 return ts('CiviCRM Contribution Search');
556 }
557
558 function getSummary() {
559 return $this->_query->summaryContribution($this->_context);
560 }
561 }
562