INFRA-132 - Remove @static annotation
[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 */
49 static $_links = NULL;
50
51 /**
52 * We use desc to remind us what that column is, name is used in the tpl
53 *
54 * @var array
55 */
56 static $_columnHeaders;
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
61 */
62 static $_properties = array(
63 'contact_id',
64 'mailing_id',
65 'mailing_name',
66 'sort_name',
67 'email',
68 'mailing_subject',
69 'email_on_hold',
70 'contact_opt_out',
71 'mailing_job_status',
72 'mailing_job_end_date',
73 );
74
75 /**
76 * Are we restricting ourselves to a single contact
77 *
78 * @var boolean
79 */
80 protected $_single = FALSE;
81
82 /**
83 * Are we restricting ourselves to a single contact
84 *
85 * @var boolean
86 */
87 protected $_limit = NULL;
88
89 /**
90 * What context are we being invoked from
91 *
92 * @var string
93 */
94 protected $_context = NULL;
95
96 /**
97 * What component context are we being invoked from
98 *
99 * @var string
100 */
101 protected $_compContext = NULL;
102
103 /**
104 * QueryParams is the array returned by exportValues called on
105 * the HTML_QuickForm_Controller for that page.
106 *
107 * @var array
108 */
109 public $_queryParams;
110
111 /**
112 * Represent the type of selector
113 *
114 * @var int
115 */
116 protected $_action;
117
118 /**
119 * The additional clause that we restrict the search with
120 *
121 * @var string
122 */
123 protected $_mailingClause = NULL;
124
125 /**
126 * The query object
127 *
128 * @var string
129 */
130 protected $_query;
131
132 /**
133 * Class constructor
134 *
135 * @param array $queryParams
136 * Array of parameters for query.
137 * @param \const|int $action - action of search basic or advanced.
138 * @param string $mailingClause
139 * If the caller wants to further restrict the search.
140 * @param bool $single
141 * Are we dealing only with one contact?.
142 * @param int $limit
143 * How many mailing do we want returned.
144 *
145 * @param string $context
146 * @param null $compContext
147 *
148 * @return \CRM_Mailing_Selector_Search
149 * @access public
150 */
151 function __construct(
152 &$queryParams,
153 $action = CRM_Core_Action::NONE,
154 $mailingClause = NULL,
155 $single = FALSE,
156 $limit = NULL,
157 $context = 'search',
158 $compContext = NULL
159 ) {
160 // submitted form values
161 $this->_queryParams = &$queryParams;
162
163 $this->_single = $single;
164 $this->_limit = $limit;
165 $this->_context = $context;
166 $this->_compContext = $compContext;
167
168 $this->_mailingClause = $mailingClause;
169
170 // type of selector
171 $this->_action = $action;
172 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
173 CRM_Mailing_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_MAILING,
174 FALSE
175 ),
176 NULL, FALSE, FALSE,
177 CRM_Contact_BAO_Query::MODE_MAILING
178 );
179
180 $this->_query->_distinctComponentClause = " civicrm_mailing_recipients.id ";
181 }
182
183 /**
184 * This method returns the links that are given for each search row.
185 * currently the links added for each row are
186 *
187 * - View
188 * - Edit
189 *
190 * @return array
191 */
192 public static function &links() {
193 if (!(self::$_links)) {
194 list($context, $key) = func_get_args();
195 $extraParams = ($key) ? "&key={$key}" : NULL;
196 $searchContext = ($context) ? "&context=$context" : NULL;
197
198 self::$_links = array(
199 CRM_Core_Action::VIEW => array(
200 'name' => ts('View'),
201 'url' => 'civicrm/contact/view',
202 'qs' => "reset=1&cid=%%cid%%{$searchContext}{$extraParams}",
203 'title' => ts('View Contact Details'),
204 ),
205 CRM_Core_Action::UPDATE => array(
206 'name' => ts('Edit'),
207 'url' => 'civicrm/contact/add',
208 'qs' => "reset=1&action=update&cid=%%cid%%{$searchContext}{$extraParams}",
209 'title' => ts('Edit Contact Details'),
210 ),
211 CRM_Core_Action::DELETE => array(
212 'name' => ts('Delete'),
213 'url' => 'civicrm/contact/view/delete',
214 'qs' => "reset=1&delete=1&cid=%%cid%%{$searchContext}{$extraParams}",
215 'title' => ts('Delete Contact'),
216 ),
217 );
218 }
219 return self::$_links;
220 }
221
222 /**
223 * Getter for array of the parameters required for creating pager.
224 *
225 * @param $action
226 * @param array $params
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
248 * Total number of rows
249 */
250 public function getTotalCount($action) {
251 return $this->_query->searchQuery(0, 0, NULL,
252 TRUE, FALSE,
253 FALSE, FALSE,
254 FALSE,
255 $this->_mailingClause
256 );
257 }
258
259 /**
260 * Returns all the rows in the given offset and rowCount
261 *
262 * @param string $action
263 * The action being performed.
264 * @param int $offset
265 * The row number to start from.
266 * @param int $rowCount
267 * The number of rows to return.
268 * @param string $sort
269 * The sql string that describes the sort order.
270 * @param string $output
271 * What should the result set include (web/email/csv).
272 *
273 * @return int
274 * the total number of rows for this action
275 */
276 public 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,
312 $actions,
313 ts('more'),
314 FALSE,
315 'contact.mailing.row',
316 'Contact',
317 $result->contact_id
318 );
319 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
320 );
321
322 $rows[] = $row;
323 }
324 return $rows;
325 }
326
327 /**
328 * @inheritDoc
329 */
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
339 * The action being performed.
340 * @param string $output
341 * What should the result set include (web/email/csv).
342 *
343 * @return array
344 * the column headers that need to be displayed
345 */
346 public function &getColumnHeaders($action = NULL, $output = NULL) {
347 if (!isset(self::$_columnHeaders)) {
348 self::$_columnHeaders = array(
349 array('desc' => ts('Contact Type')),
350 array(
351 'name' => ts('Name'),
352 'sort' => 'sort_name',
353 'direction' => CRM_Utils_Sort::DONTCARE,
354 ),
355 array(
356 'name' => ts('Email'),
357 'sort' => 'email',
358 'direction' => CRM_Utils_Sort::DONTCARE,
359 ),
360 array(
361 'name' => ts('Mailing Name'),
362 'sort' => 'mailing_name',
363 'direction' => CRM_Utils_Sort::DONTCARE,
364 ),
365 array(
366 'name' => ts('Mailing Subject'),
367 'sort' => 'mailing_subject',
368 'direction' => CRM_Utils_Sort::DONTCARE,
369 ),
370 array(
371 'name' => ts('Mailing Status'),
372 'sort' => 'mailing_job_status',
373 'direction' => CRM_Utils_Sort::DONTCARE,
374 ),
375 array(
376 'name' => ts('Completed Date'),
377 'sort' => 'mailing_job_end_date',
378 'direction' => CRM_Utils_Sort::DONTCARE,
379 ),
380 array('desc' => ts('Actions')),
381 );
382 }
383 return self::$_columnHeaders;
384 }
385
386 /**
387 * @return mixed
388 */
389 public function alphabetQuery() {
390 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
391 }
392
393 /**
394 * @return string
395 */
396 public function &getQuery() {
397 return $this->_query;
398 }
399
400 /**
401 * Name of export file.
402 *
403 * @param string $output
404 * Type of output.
405 *
406 * @return string
407 * name of the file
408 */
409 public function getExportFileName($output = 'csv') {
410 return ts('CiviCRM Mailing Search');
411 }
412 }