Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-10-28-14-52-15
[civicrm-core.git] / CRM / Mailing / 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_Mailing_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 'mailing_id',
68 'mailing_name',
69 'sort_name',
70 'email',
71 'mailing_subject',
72 'email_on_hold',
73 'contact_opt_out',
74 'mailing_job_status',
75 'mailing_job_end_date'
76 );
77
78 /**
79 * are we restricting ourselves to a single contact
80 *
81 * @access protected
82 * @var boolean
83 */
84 protected $_single = FALSE;
85
86 /**
87 * are we restricting ourselves to a single contact
88 *
89 * @access protected
90 * @var boolean
91 */
92 protected $_limit = NULL;
93
94 /**
95 * what context are we being invoked from
96 *
97 * @access protected
98 * @var string
99 */
100 protected $_context = NULL;
101
102 /**
103 * what component context are we being invoked from
104 *
105 * @access protected
106 * @var string
107 */
108 protected $_compContext = NULL;
109
110 /**
111 * queryParams is the array returned by exportValues called on
112 * the HTML_QuickForm_Controller for that page.
113 *
114 * @var array
115 * @access protected
116 */
117 public $_queryParams;
118
119 /**
120 * represent the type of selector
121 *
122 * @var int
123 * @access protected
124 */
125 protected $_action;
126
127 /**
128 * The additional clause that we restrict the search with
129 *
130 * @var string
131 */
132 protected $_mailingClause = NULL;
133
134 /**
135 * The query object
136 *
137 * @var string
138 */
139 protected $_query;
140
141 /**
142 * Class constructor
143 *
144 * @param array $queryParams array of parameters for query
145 * @param int $action - action of search basic or advanced.
146 * @param string $mailingClause if the caller wants to further restrict the search
147 * @param boolean $single are we dealing only with one contact?
148 * @param int $limit how many mailing do we want returned
149 *
150 * @return CRM_Contact_Selector
151 * @access public
152 */
153 function __construct(&$queryParams,
154 $action = CRM_Core_Action::NONE,
155 $mailingClause = NULL,
156 $single = FALSE,
157 $limit = NULL,
158 $context = 'search',
159 $compContext = NULL
160 ) {
161 // submitted form values
162 $this->_queryParams = &$queryParams;
163
164 $this->_single = $single;
165 $this->_limit = $limit;
166 $this->_context = $context;
167 $this->_compContext = $compContext;
168
169 $this->_mailingClause = $mailingClause;
170
171 // type of selector
172 $this->_action = $action;
173 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
174 CRM_Mailing_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_MAILING,
175 FALSE
176 ),
177 NULL, FALSE, FALSE,
178 CRM_Contact_BAO_Query::MODE_MAILING
179 );
180
181 $this->_query->_distinctComponentClause = " civicrm_mailing_recipients.id ";
182 }
183 //end of constructor
184
185 /**
186 * This method returns the links that are given for each search row.
187 * currently the links added for each row are
188 *
189 * - View
190 * - Edit
191 *
192 * @return array
193 * @access public
194 *
195 */
196 static function &links() {
197 if (!(self::$_links)) {
198 list($context, $key) = func_get_args();
199 $extraParams = ($key) ? "&key={$key}" : NULL;
200 $searchContext = ($context) ? "&context=$context" : NULL;
201
202 self::$_links = array(
203 CRM_Core_Action::VIEW => array(
204 'name' => ts('View'),
205 'url' => 'civicrm/contact/view',
206 'qs' => "reset=1&cid=%%cid%%{$searchContext}{$extraParams}",
207 'title' => ts('View Contact Details'),
208 ),
209 CRM_Core_Action::UPDATE => array(
210 'name' => ts('Edit'),
211 'url' => 'civicrm/contact/add',
212 'qs' => "reset=1&action=update&cid=%%cid%%{$searchContext}{$extraParams}",
213 'title' => ts('Edit Contact Details'),
214 ),
215 CRM_Core_Action::DELETE => array(
216 'name' => ts('Delete'),
217 'url' => 'civicrm/contact/view/delete',
218 'qs' => "reset=1&delete=1&cid=%%cid%%{$searchContext}{$extraParams}",
219 'title' => ts('Delete Contact'),
220 ),
221 );
222 }
223 return self::$_links;
224 }
225 //end of function
226
227 /**
228 * getter for array of the parameters required for creating pager.
229 *
230 * @param
231 * @access public
232 */
233 function getPagerParams($action, &$params) {
234 $params['status'] = ts('Mailing Recipient') . ' %%StatusMessage%%';
235 $params['csvString'] = NULL;
236 if ($this->_limit) {
237 $params['rowCount'] = $this->_limit;
238 }
239 else {
240 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
241 }
242
243 $params['buttonTop'] = 'PagerTopButton';
244 $params['buttonBottom'] = 'PagerBottomButton';
245 }
246 //end of function
247
248 /**
249 * Returns total number of rows for the query.
250 *
251 * @param
252 *
253 * @return int Total number of rows
254 * @access public
255 */
256 function getTotalCount($action) {
257 return $this->_query->searchQuery(0, 0, NULL,
258 TRUE, FALSE,
259 FALSE, FALSE,
260 FALSE,
261 $this->_mailingClause
262 );
263 }
264
265 /**
266 * returns all the rows in the given offset and rowCount
267 *
268 * @param enum $action the action being performed
269 * @param int $offset the row number to start from
270 * @param int $rowCount the number of rows to return
271 * @param string $sort the sql string that describes the sort order
272 * @param enum $output what should the result set include (web/email/csv)
273 *
274 * @return int the total number of rows for this action
275 */
276 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
277 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
278 FALSE, FALSE,
279 FALSE, FALSE,
280 FALSE,
281 $this->_mailingClause
282 );
283
284 // process the result of the query
285 $rows = array();
286 $permissions = array(CRM_Core_Permission::getPermission());
287 if (CRM_Core_Permission::check('delete contacts')) {
288 $permissions[] = CRM_Core_Permission::DELETE;
289 }
290 $mask = CRM_Core_Action::mask($permissions);
291 $qfKey = $this->_key;
292
293 While ($result->fetch()) {
294 $row = array();
295 // the columns we are interested in
296 foreach (self::$_properties as $property) {
297 if (property_exists($result, $property)) {
298 $row[$property] = $result->$property;
299 }
300 }
301
302 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->mailing_recipients_id;
303
304 $actions = array(
305 'cid' => $result->contact_id,
306 'cxt' => $this->_context,
307 );
308
309 $row['action'] = CRM_Core_Action::formLink(
310 self::links($qfKey, $this->_context),
311 $mask, $actions
312 );
313 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
314 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
315 );
316
317 $rows[] = $row;
318 }
319 return $rows;
320 }
321
322 /**
323 *
324 * @return array $qill which contains an array of strings
325 * @access public
326 */
327
328 // the current internationalisation is bad, but should more or less work
329 // for most of "European" languages
330 public function getQILL() {
331 return $this->_query->qill();
332 }
333
334 /**
335 * returns the column headers as an array of tuples:
336 * (name, sortName (key to the sort array))
337 *
338 * @param string $action the action being performed
339 * @param enum $output what should the result set include (web/email/csv)
340 *
341 * @return array the column headers that need to be displayed
342 * @access public
343 */
344 public function &getColumnHeaders($action = NULL, $output = NULL) {
345 if (!isset(self::$_columnHeaders)) {
346 self::$_columnHeaders = array(
347 array('desc' => ts('Contact Type')),
348 array(
349 'name' => ts('Name'),
350 'sort' => 'sort_name',
351 'direction' => CRM_Utils_Sort::DONTCARE,
352 ),
353 array(
354 'name' => ts('Email'),
355 'sort' => 'email',
356 'direction' => CRM_Utils_Sort::DONTCARE,
357 ),
358 array(
359 'name' => ts('Mailing Name'),
360 'sort' => 'mailing_name',
361 'direction' => CRM_Utils_Sort::DONTCARE,
362 ),
363 array(
364 'name' => ts('Mailing Subject'),
365 'sort' => 'mailing_subject',
366 'direction' => CRM_Utils_Sort::DONTCARE,
367 ),
368 array(
369 'name' => ts('Mailing Status'),
370 'sort' => 'mailing_job_status',
371 'direction' => CRM_Utils_Sort::DONTCARE,
372 ),
373 array(
374 'name' => ts('Completed Date'),
375 'sort' => 'mailing_job_end_date',
376 'direction' => CRM_Utils_Sort::DONTCARE,
377 ),
378 array('desc' => ts('Actions')),
379 );
380 }
381 return self::$_columnHeaders;
382 }
383
384 function alphabetQuery() {
385 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
386 }
387
388 function &getQuery() {
389 return $this->_query;
390 }
391
392 /**
393 * name of export file.
394 *
395 * @param string $output type of output
396 *
397 * @return string name of the file
398 */
399 function getExportFileName($output = 'csv') {
400 return ts('CiviCRM Mailing Search');
401 }
402 }