fix comment blocks
[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
2cc569f2
PJ
48 */
49 static $_links = NULL;
50
51 /**
100fef9d 52 * We use desc to remind us what that column is, name is used in the tpl
2cc569f2
PJ
53 *
54 * @var array
2cc569f2
PJ
55 */
56 static $_columnHeaders;
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
2cc569f2
PJ
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',
21dfd5f5 72 'mailing_job_end_date',
2cc569f2
PJ
73 );
74
75 /**
100fef9d 76 * Are we restricting ourselves to a single contact
2cc569f2 77 *
2cc569f2
PJ
78 * @var boolean
79 */
80 protected $_single = FALSE;
81
82 /**
100fef9d 83 * Are we restricting ourselves to a single contact
2cc569f2 84 *
2cc569f2
PJ
85 * @var boolean
86 */
87 protected $_limit = NULL;
88
89 /**
100fef9d 90 * What context are we being invoked from
2cc569f2 91 *
2cc569f2
PJ
92 * @var string
93 */
94 protected $_context = NULL;
95
96 /**
100fef9d 97 * What component context are we being invoked from
2cc569f2 98 *
2cc569f2
PJ
99 * @var string
100 */
101 protected $_compContext = NULL;
102
103 /**
100fef9d 104 * QueryParams is the array returned by exportValues called on
2cc569f2
PJ
105 * the HTML_QuickForm_Controller for that page.
106 *
107 * @var array
2cc569f2
PJ
108 */
109 public $_queryParams;
110
111 /**
100fef9d 112 * Represent the type of selector
2cc569f2
PJ
113 *
114 * @var int
2cc569f2
PJ
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 *
90c8230e
TO
135 * @param array $queryParams
136 * Array of parameters for query.
da6b46f4 137 * @param \const|int $action - action of search basic or advanced.
90c8230e
TO
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.
2cc569f2 144 *
da6b46f4
EM
145 * @param string $context
146 * @param null $compContext
147 *
148 * @return \CRM_Mailing_Selector_Search
2cc569f2 149 */
a3d7e8ee
TO
150 function __construct(
151 &$queryParams,
2cc569f2
PJ
152 $action = CRM_Core_Action::NONE,
153 $mailingClause = NULL,
154 $single = FALSE,
155 $limit = NULL,
156 $context = 'search',
157 $compContext = NULL
158 ) {
159 // submitted form values
160 $this->_queryParams = &$queryParams;
161
353ffa53
TO
162 $this->_single = $single;
163 $this->_limit = $limit;
164 $this->_context = $context;
2cc569f2
PJ
165 $this->_compContext = $compContext;
166
167 $this->_mailingClause = $mailingClause;
168
169 // type of selector
170 $this->_action = $action;
2cc569f2
PJ
171 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
172 CRM_Mailing_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_MAILING,
173 FALSE
174 ),
175 NULL, FALSE, FALSE,
176 CRM_Contact_BAO_Query::MODE_MAILING
177 );
335038eb
PJ
178
179 $this->_query->_distinctComponentClause = " civicrm_mailing_recipients.id ";
2cc569f2 180 }
2cc569f2
PJ
181
182 /**
183 * This method returns the links that are given for each search row.
184 * currently the links added for each row are
185 *
186 * - View
187 * - Edit
188 *
189 * @return array
2cc569f2 190 */
00be9182 191 public static function &links() {
2cc569f2 192 if (!(self::$_links)) {
35f7561f
TO
193 list($context, $key) = func_get_args();
194 $extraParams = ($key) ? "&key={$key}" : NULL;
195 $searchContext = ($context) ? "&context=$context" : NULL;
2cc569f2 196
35f7561f 197 self::$_links = array(
353ffa53
TO
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 ),
35f7561f 216 );
2cc569f2
PJ
217 }
218 return self::$_links;
219 }
2cc569f2
PJ
220
221 /**
100fef9d 222 * Getter for array of the parameters required for creating pager.
2cc569f2 223 *
dd244018 224 * @param $action
c490a46a 225 * @param array $params
2cc569f2 226 */
00be9182 227 public function getPagerParams($action, &$params) {
2cc569f2
PJ
228 $params['status'] = ts('Mailing Recipient') . ' %%StatusMessage%%';
229 $params['csvString'] = NULL;
230 if ($this->_limit) {
231 $params['rowCount'] = $this->_limit;
232 }
233 else {
234 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
235 }
236
237 $params['buttonTop'] = 'PagerTopButton';
238 $params['buttonBottom'] = 'PagerBottomButton';
239 }
2cc569f2
PJ
240
241 /**
242 * Returns total number of rows for the query.
243 *
244 * @param
245 *
a6c01b45
CW
246 * @return int
247 * Total number of rows
2cc569f2 248 */
00be9182 249 public function getTotalCount($action) {
2cc569f2
PJ
250 return $this->_query->searchQuery(0, 0, NULL,
251 TRUE, FALSE,
252 FALSE, FALSE,
253 FALSE,
254 $this->_mailingClause
255 );
256 }
257
258 /**
100fef9d 259 * Returns all the rows in the given offset and rowCount
2cc569f2 260 *
3f8d2862 261 * @param string $action
90c8230e
TO
262 * The action being performed.
263 * @param int $offset
264 * The row number to start from.
265 * @param int $rowCount
266 * The number of rows to return.
267 * @param string $sort
268 * The sql string that describes the sort order.
3f8d2862 269 * @param string $output
90c8230e 270 * What should the result set include (web/email/csv).
2cc569f2 271 *
a6c01b45
CW
272 * @return int
273 * the total number of rows for this action
2cc569f2 274 */
00be9182 275 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
2cc569f2
PJ
276 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
277 FALSE, FALSE,
278 FALSE, FALSE,
279 FALSE,
280 $this->_mailingClause
281 );
282
283 // process the result of the query
284 $rows = array();
285 $permissions = array(CRM_Core_Permission::getPermission());
286 if (CRM_Core_Permission::check('delete contacts')) {
287 $permissions[] = CRM_Core_Permission::DELETE;
288 }
289 $mask = CRM_Core_Action::mask($permissions);
290 $qfKey = $this->_key;
291
35f7561f 292 while ($result->fetch()) {
2cc569f2
PJ
293 $row = array();
294 // the columns we are interested in
295 foreach (self::$_properties as $property) {
296 if (property_exists($result, $property)) {
297 $row[$property] = $result->$property;
298 }
299 }
300
335038eb 301 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->mailing_recipients_id;
2cc569f2
PJ
302
303 $actions = array(
304 'cid' => $result->contact_id,
305 'cxt' => $this->_context,
306 );
307
308 $row['action'] = CRM_Core_Action::formLink(
309 self::links($qfKey, $this->_context),
dd244018 310 $mask,
87dab4a4
AH
311 $actions,
312 ts('more'),
313 FALSE,
314 'contact.mailing.row',
315 'Contact',
316 $result->contact_id
2cc569f2 317 );
35f7561f 318 $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
319 );
320
321 $rows[] = $row;
322 }
323 return $rows;
324 }
325
326 /**
1054415f 327 * @inheritDoc
2cc569f2 328 */
2cc569f2
PJ
329 public function getQILL() {
330 return $this->_query->qill();
331 }
332
333 /**
100fef9d 334 * Returns the column headers as an array of tuples:
2cc569f2
PJ
335 * (name, sortName (key to the sort array))
336 *
90c8230e
TO
337 * @param string $action
338 * The action being performed.
3f8d2862 339 * @param string $output
90c8230e 340 * What should the result set include (web/email/csv).
2cc569f2 341 *
a6c01b45
CW
342 * @return array
343 * the column headers that need to be displayed
2cc569f2
PJ
344 */
345 public function &getColumnHeaders($action = NULL, $output = NULL) {
346 if (!isset(self::$_columnHeaders)) {
347 self::$_columnHeaders = array(
348 array('desc' => ts('Contact Type')),
349 array(
350 'name' => ts('Name'),
351 'sort' => 'sort_name',
352 'direction' => CRM_Utils_Sort::DONTCARE,
353 ),
354 array(
355 'name' => ts('Email'),
356 'sort' => 'email',
357 'direction' => CRM_Utils_Sort::DONTCARE,
358 ),
359 array(
360 'name' => ts('Mailing Name'),
361 'sort' => 'mailing_name',
362 'direction' => CRM_Utils_Sort::DONTCARE,
363 ),
364 array(
365 'name' => ts('Mailing Subject'),
366 'sort' => 'mailing_subject',
367 'direction' => CRM_Utils_Sort::DONTCARE,
368 ),
369 array(
370 'name' => ts('Mailing Status'),
371 'sort' => 'mailing_job_status',
372 'direction' => CRM_Utils_Sort::DONTCARE,
373 ),
374 array(
375 'name' => ts('Completed Date'),
376 'sort' => 'mailing_job_end_date',
377 'direction' => CRM_Utils_Sort::DONTCARE,
378 ),
379 array('desc' => ts('Actions')),
380 );
381 }
382 return self::$_columnHeaders;
383 }
384
e0ef6999
EM
385 /**
386 * @return mixed
387 */
00be9182 388 public function alphabetQuery() {
2cc569f2
PJ
389 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
390 }
391
e0ef6999
EM
392 /**
393 * @return string
394 */
00be9182 395 public function &getQuery() {
2cc569f2
PJ
396 return $this->_query;
397 }
398
399 /**
100fef9d 400 * Name of export file.
2cc569f2 401 *
90c8230e
TO
402 * @param string $output
403 * Type of output.
2cc569f2 404 *
a6c01b45
CW
405 * @return string
406 * name of the file
2cc569f2 407 */
00be9182 408 public function getExportFileName($output = 'csv') {
2cc569f2
PJ
409 return ts('CiviCRM Mailing Search');
410 }
dd244018 411}