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