bulk comment fix
[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 * @param null $componentId
219 * @param null $componentAction
220 * @param null $key
221 * @param null $compContext
222 *
223 * @return array
224 * @access public
225 */
226 static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
227 $extraParams = NULL;
228 if ($componentId) {
229 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
230 }
231 if ($compContext) {
232 $extraParams .= "&compContext={$compContext}";
233 }
234 if ($key) {
235 $extraParams .= "&key={$key}";
236 }
237
238 if (!(self::$_links)) {
239 self::$_links = array(
240 CRM_Core_Action::VIEW => array(
241 'name' => ts('View'),
242 'url' => 'civicrm/contact/view/contribution',
243 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
244 'title' => ts('View Contribution'),
245 ),
246 CRM_Core_Action::UPDATE => array(
247 'name' => ts('Edit'),
248 'url' => 'civicrm/contact/view/contribution',
249 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
250 'title' => ts('Edit Contribution'),
251 ),
252 CRM_Core_Action::DELETE => array(
253 'name' => ts('Delete'),
254 'url' => 'civicrm/contact/view/contribution',
255 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
256 'title' => ts('Delete Contribution'),
257 ),
258 );
259 }
260 return self::$_links;
261 }
262 //end of function
263
264 /**
265 * getter for array of the parameters required for creating pager.
266 *
267 * @param
268 * @access public
269 */
270 function getPagerParams($action, &$params) {
271 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
272 $params['csvString'] = NULL;
273 if ($this->_limit) {
274 $params['rowCount'] = $this->_limit;
275 }
276 else {
277 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
278 }
279
280 $params['buttonTop'] = 'PagerTopButton';
281 $params['buttonBottom'] = 'PagerBottomButton';
282 }
283 //end of function
284
285 /**
286 * Returns total number of rows for the query.
287 *
288 * @param
289 *
290 * @return int Total number of rows
291 * @access public
292 */
293 function getTotalCount($action) {
294 return $this->_query->searchQuery(0, 0, NULL,
295 TRUE, FALSE,
296 FALSE, FALSE,
297 FALSE,
298 $this->_contributionClause
299 );
300 }
301
302 /**
303 * returns all the rows in the given offset and rowCount
304 *
305 * @param enum $action the action being performed
306 * @param int $offset the row number to start from
307 * @param int $rowCount the number of rows to return
308 * @param string $sort the sql string that describes the sort order
309 * @param enum $output what should the result set include (web/email/csv)
310 *
311 * @return int the total number of rows for this action
312 */
313 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
314 if ($this->_includeSoftCredits) {
315 // especial sort order when rows include soft credits
316 $sort = "civicrm_contribution.receive_date DESC, civicrm_contribution.id, civicrm_contribution_soft.id";
317 }
318 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
319 FALSE, FALSE,
320 FALSE, FALSE,
321 FALSE,
322 $this->_contributionClause
323 );
324 // process the result of the query
325 $rows = array();
326
327 //CRM-4418 check for view/edit/delete
328 $permissions = array(CRM_Core_Permission::VIEW);
329 if (CRM_Core_Permission::check('edit contributions')) {
330 $permissions[] = CRM_Core_Permission::EDIT;
331 }
332 if (CRM_Core_Permission::check('delete in CiviContribute')) {
333 $permissions[] = CRM_Core_Permission::DELETE;
334 }
335 $mask = CRM_Core_Action::mask($permissions);
336
337 $qfKey = $this->_key;
338 $componentId = $componentContext = NULL;
339 if ($this->_context != 'contribute') {
340 $qfKey = CRM_Utils_Request::retrieve('key', 'String', CRM_Core_DAO::$_nullObject);
341 $componentId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject);
342 $componentAction = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject);
343 $componentContext = CRM_Utils_Request::retrieve('compContext', 'String', CRM_Core_DAO::$_nullObject);
344
345 if (!$componentContext &&
346 $this->_compContext
347 ) {
348 $componentContext = $this->_compContext;
349 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
350 }
351 }
352
353 // get all contribution status
354 $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status',
355 FALSE, FALSE, FALSE, NULL, 'name', FALSE
356 );
357
358 //get all campaigns.
359 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
360
361 While ($result->fetch()) {
362 $row = array();
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->contribution_campaign_id, $allCampaigns);
372 $row['campaign_id'] = $result->contribution_campaign_id;
373
374 // add contribution status name
375 $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'],
376 $contributionStatuses
377 );
378
379 if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
380 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
381 }
382 elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
383 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
384 }
385
386 if ($row['is_test']) {
387 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
388 }
389
390 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id;
391
392 $actions = array(
393 'id' => $result->contribution_id,
394 'cid' => $result->contact_id,
395 'cxt' => $this->_context,
396 );
397
398 $row['action'] = CRM_Core_Action::formLink(
399 self::links($componentId,
400 $componentAction,
401 $qfKey,
402 $componentContext
403 ),
404 $mask, $actions,
405 ts('more'),
406 FALSE,
407 'contribution.selector.row',
408 'Contribution',
409 $result->contribution_id
410 );
411
412 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
413 $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 *
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_id',
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_id',
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 function alphabetQuery() {
544 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
545 }
546
547 function &getQuery() {
548 return $this->_query;
549 }
550
551 /**
552 * name of export file.
553 *
554 * @param string $output type of output
555 *
556 * @return string name of the file
557 */
558 function getExportFileName($output = 'csv') {
559 return ts('CiviCRM Contribution Search');
560 }
561
562 function getSummary() {
563 return $this->_query->summaryContribution($this->_context);
564 }
565 }
566