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