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