65147a91b7d9ced7d1113a7e8b9442e196cb7055
[civicrm-core.git] / CRM / Pledge / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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_Pledge_Selector_Search extends CRM_Core_Selector_Base {
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 'sort_name',
68 'display_name',
69 'pledge_id',
70 'pledge_amount',
71 'pledge_create_date',
72 'pledge_total_paid',
73 'pledge_next_pay_date',
74 'pledge_next_pay_amount',
75 'pledge_outstanding_amount',
76 'pledge_status_id',
77 'pledge_status',
78 'pledge_is_test',
79 'pledge_contribution_page_id',
80 'pledge_contribution_type',
81 'pledge_campaign_id',
82 'pledge_currency',
83 );
84
85 /**
86 * are we restricting ourselves to a single contact
87 *
88 * @access protected
89 * @var boolean
90 */
91 protected $_single = FALSE;
92
93 /**
94 * are we restricting ourselves to a single contact
95 *
96 * @access protected
97 * @var boolean
98 */
99 protected $_limit = NULL;
100
101 /**
102 * what context are we being invoked from
103 *
104 * @access protected
105 * @var string
106 */
107 protected $_context = NULL;
108
109 /**
110 * queryParams is the array returned by exportValues called on
111 * the HTML_QuickForm_Controller for that page.
112 *
113 * @var array
114 * @access protected
115 */
116 public $_queryParams;
117
118 /**
119 * represent the type of selector
120 *
121 * @var int
122 * @access protected
123 */
124 protected $_action;
125
126 /**
127 * The additional clause that we restrict the search with
128 *
129 * @var string
130 */
131 protected $_additionalClause = NULL;
132
133 /**
134 * The query object
135 *
136 * @var string
137 */
138 protected $_query;
139
140 /**
141 * Class constructor
142 *
143 * @param array $queryParams array of parameters for query
144 * @param int $action - action of search basic or advanced.
145 * @param string $additionalClause if the caller wants to further restrict the search (used in participations)
146 * @param boolean $single are we dealing only with one contact?
147 * @param int $limit how many signers do we want returned
148 *
149 * @return CRM_Contact_Selector
150 * @access public
151 */
152 function __construct(&$queryParams,
153 $action = CRM_Core_Action::NONE,
154 $additionalClause = NULL,
155 $single = FALSE,
156 $limit = NULL,
157 $context = 'search'
158 ) {
159 // submitted form values
160 $this->_queryParams = &$queryParams;
161
162 $this->_single = $single;
163 $this->_limit = $limit;
164 $this->_context = $context;
165
166 $this->_additionalClause = $additionalClause;
167
168 // type of selector
169 $this->_action = $action;
170
171 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE,
172 CRM_Contact_BAO_Query::MODE_PLEDGE
173 );
174
175 $this->_query->_distinctComponentClause = "civicrm_pledge.id";
176 $this->_query->_groupByComponentClause = " GROUP BY civicrm_pledge.id ";
177 }
178 //end of constructor
179
180 /**
181 * This method returns the links that are given for each search row.
182 * currently the links added for each row are
183 *
184 * - View
185 * - Edit
186 *
187 * @return array
188 * @access public
189 *
190 */
191 static function &links($hideOption, $key = NULL) {
192 $extraParams = ($key) ? "&key={$key}" : NULL;
193
194 $cancelExtra = ts('Cancelling this pledge will also cancel any scheduled (and not completed) pledge payments.') . ' ' . ts('This action cannot be undone.') . ' ' . ts('Do you want to continue?');
195 self::$_links = array(
196 CRM_Core_Action::VIEW => array(
197 'name' => ts('View'),
198 'url' => 'civicrm/contact/view/pledge',
199 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=pledge' . $extraParams,
200 'title' => ts('View Pledge'),
201 ),
202 CRM_Core_Action::UPDATE => array(
203 'name' => ts('Edit'),
204 'url' => 'civicrm/contact/view/pledge',
205 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
206 'title' => ts('Edit Pledge'),
207 ),
208 CRM_Core_Action::DETACH => array(
209 'name' => ts('Cancel'),
210 'url' => 'civicrm/contact/view/pledge',
211 'qs' => 'reset=1&action=detach&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
212 'extra' => 'onclick = "return confirm(\'' . $cancelExtra . '\');"',
213 'title' => ts('Cancel Pledge'),
214 ),
215 CRM_Core_Action::DELETE => array(
216 'name' => ts('Delete'),
217 'url' => 'civicrm/contact/view/pledge',
218 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
219 'title' => ts('Delete Pledge'),
220 ),
221 );
222
223
224 if (in_array('Cancel', $hideOption)) {
225 unset(self::$_links[CRM_Core_Action::DETACH]);
226 }
227
228 return self::$_links;
229 }
230 //end of function
231
232 /**
233 * getter for array of the parameters required for creating pager.
234 *
235 * @param
236 * @access public
237 */
238 function getPagerParams($action, &$params) {
239 $params['status'] = ts('Pledge') . ' %%StatusMessage%%';
240 $params['csvString'] = NULL;
241 if ($this->_limit) {
242 $params['rowCount'] = $this->_limit;
243 }
244 else {
245 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
246 }
247
248 $params['buttonTop'] = 'PagerTopButton';
249 $params['buttonBottom'] = 'PagerBottomButton';
250 }
251 //end of function
252
253 /**
254 * Returns total number of rows for the query.
255 *
256 * @param
257 *
258 * @return int Total number of rows
259 * @access public
260 */
261 function getTotalCount($action) {
262 return $this->_query->searchQuery(0, 0, NULL,
263 TRUE, FALSE,
264 FALSE, FALSE,
265 FALSE,
266 $this->_additionalClause
267 );
268 }
269
270 /**
271 * returns all the rows in the given offset and rowCount
272 *
273 * @param enum $action the action being performed
274 * @param int $offset the row number to start from
275 * @param int $rowCount the number of rows to return
276 * @param string $sort the sql string that describes the sort order
277 * @param enum $output what should the result set include (web/email/csv)
278 *
279 * @return int the total number of rows for this action
280 */
281 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
282 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
283 FALSE, FALSE,
284 FALSE, FALSE,
285 FALSE,
286 $this->_additionalClause
287 );
288
289 // process the result of the query
290 $rows = array();
291
292 // get all pledge status
293 $pledgeStatuses = CRM_Core_OptionGroup::values('contribution_status',
294 FALSE, FALSE, FALSE, NULL, 'name', FALSE
295 );
296
297 //get all campaigns.
298 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
299
300 //4418 check for view, edit and delete
301 $permissions = array(CRM_Core_Permission::VIEW);
302 if (CRM_Core_Permission::check('edit pledges')) {
303 $permissions[] = CRM_Core_Permission::EDIT;
304 }
305 if (CRM_Core_Permission::check('delete in CiviPledge')) {
306 $permissions[] = CRM_Core_Permission::DELETE;
307 }
308 $mask = CRM_Core_Action::mask($permissions);
309
310 while ($result->fetch()) {
311 $row = array();
312 // the columns we are interested in
313 foreach (self::$_properties as $property) {
314 if (isset($result->$property)) {
315 $row[$property] = $result->$property;
316 }
317 }
318
319 //carry campaign on selectors.
320 $row['campaign'] = CRM_Utils_Array::value($result->pledge_campaign_id, $allCampaigns);
321 $row['campaign_id'] = $result->pledge_campaign_id;
322
323 // add pledge status name
324 $row['pledge_status_name'] = CRM_Utils_Array::value($row['pledge_status_id'],
325 $pledgeStatuses
326 );
327 // append (test) to status label
328 if (CRM_Utils_Array::value('pledge_is_test', $row)) {
329 $row['pledge_status'] .= ' (test)';
330 }
331
332 $hideOption = array();
333 if (CRM_Utils_Array::key('Cancelled', $row) ||
334 CRM_Utils_Array::key('Completed', $row)
335 ) {
336 $hideOption[] = 'Cancel';
337 }
338
339 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->pledge_id;
340
341 $row['action'] = CRM_Core_Action::formLink(self::links($hideOption, $this->_key),
342 $mask,
343 array(
344 'id' => $result->pledge_id,
345 'cid' => $result->contact_id,
346 'cxt' => $this->_context,
347 )
348 );
349
350
351 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
352 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
353 );
354 $rows[] = $row;
355 }
356 return $rows;
357 }
358
359 /**
360 *
361 * @return array $qill which contains an array of strings
362 * @access public
363 */
364
365 // the current internationalisation is bad, but should more or less work
366 // for most of "European" languages
367 public function getQILL() {
368 return $this->_query->qill();
369 }
370
371 /**
372 * returns the column headers as an array of tuples:
373 * (name, sortName (key to the sort array))
374 *
375 * @param string $action the action being performed
376 * @param enum $output what should the result set include (web/email/csv)
377 *
378 * @return array the column headers that need to be displayed
379 * @access public
380 */
381 public function &getColumnHeaders($action = NULL, $output = NULL) {
382 if (!isset(self::$_columnHeaders)) {
383 self::$_columnHeaders = array(
384 array(
385 'name' => ts('Pledged'),
386 'sort' => 'pledge_amount',
387 'direction' => CRM_Utils_Sort::DONTCARE,
388 ),
389 array(
390 'name' => ts('Total Paid'),
391 'sort' => 'pledge_total_paid',
392 'direction' => CRM_Utils_Sort::DONTCARE,
393 ),
394 array(
395 'name' => ts('Balance'),
396 ),
397 array(
398 'name' => ts('Pledged For'),
399 'sort' => 'pledge_contribution_type',
400 'direction' => CRM_Utils_Sort::DONTCARE,
401 ),
402 array(
403 'name' => ts('Pledge Made'),
404 'sort' => 'pledge_create_date',
405 'direction' => CRM_Utils_Sort::DESCENDING,
406 ),
407 array(
408 'name' => ts('Next Pay Date'),
409 'sort' => 'pledge_next_pay_date',
410 'direction' => CRM_Utils_Sort::DONTCARE,
411 ),
412 array(
413 'name' => ts('Next Amount'),
414 'sort' => 'pledge_next_pay_amount',
415 'direction' => CRM_Utils_Sort::DONTCARE,
416 ),
417 array(
418 'name' => ts('Status'),
419 'sort' => 'pledge_status',
420 'direction' => CRM_Utils_Sort::DONTCARE,
421 ),
422 array('desc' => ts('Actions')),
423 );
424
425 if (!$this->_single) {
426 $pre = array(
427 array('desc' => ts('Contact Id')),
428 array(
429 'name' => ts('Name'),
430 'sort' => 'sort_name',
431 'direction' => CRM_Utils_Sort::DONTCARE,
432 ),
433 );
434
435 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
436 }
437 }
438 return self::$_columnHeaders;
439 }
440
441 function &getQuery() {
442 return $this->_query;
443 }
444
445 /**
446 * name of export file.
447 *
448 * @param string $output type of output
449 *
450 * @return string name of the file
451 */
452 function getExportFileName($output = 'csv') {
453 return ts('Pledge Search');
454 }
455 }
456 //end of class
457