INFRA-132 - Remove extra newlines from the bottom of docblocks
[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
139 * Array of parameters for query.
140 * @param \const|int $action - action of search basic or advanced.
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.
147 *
148 * @param string $context
149 * @param null $compContext
150 *
151 * @return \CRM_Mailing_Selector_Search
152 @access public
153 */
154 function __construct(
155 &$queryParams,
156 $action = CRM_Core_Action::NONE,
157 $mailingClause = NULL,
158 $single = FALSE,
159 $limit = NULL,
160 $context = 'search',
161 $compContext = NULL
162 ) {
163 // submitted form values
164 $this->_queryParams = &$queryParams;
165
166 $this->_single = $single;
167 $this->_limit = $limit;
168 $this->_context = $context;
169 $this->_compContext = $compContext;
170
171 $this->_mailingClause = $mailingClause;
172
173 // type of selector
174 $this->_action = $action;
175 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
176 CRM_Mailing_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_MAILING,
177 FALSE
178 ),
179 NULL, FALSE, FALSE,
180 CRM_Contact_BAO_Query::MODE_MAILING
181 );
182
183 $this->_query->_distinctComponentClause = " civicrm_mailing_recipients.id ";
184 }
185
186 /**
187 * This method returns the links that are given for each search row.
188 * currently the links added for each row are
189 *
190 * - View
191 * - Edit
192 *
193 * @return array
194 */
195 public static function &links() {
196 if (!(self::$_links)) {
197 list($context, $key) = func_get_args();
198 $extraParams = ($key) ? "&key={$key}" : NULL;
199 $searchContext = ($context) ? "&context=$context" : NULL;
200
201 self::$_links = array(
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 ),
220 );
221 }
222 return self::$_links;
223 }
224
225 /**
226 * Getter for array of the parameters required for creating pager.
227 *
228 * @param $action
229 * @param array $params
230 */
231 public function getPagerParams($action, &$params) {
232 $params['status'] = ts('Mailing Recipient') . ' %%StatusMessage%%';
233 $params['csvString'] = NULL;
234 if ($this->_limit) {
235 $params['rowCount'] = $this->_limit;
236 }
237 else {
238 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
239 }
240
241 $params['buttonTop'] = 'PagerTopButton';
242 $params['buttonBottom'] = 'PagerBottomButton';
243 }
244
245 /**
246 * Returns total number of rows for the query.
247 *
248 * @param
249 *
250 * @return int
251 * Total number of rows
252 */
253 public function getTotalCount($action) {
254 return $this->_query->searchQuery(0, 0, NULL,
255 TRUE, FALSE,
256 FALSE, FALSE,
257 FALSE,
258 $this->_mailingClause
259 );
260 }
261
262 /**
263 * Returns all the rows in the given offset and rowCount
264 *
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).
275 *
276 * @return int
277 * the total number of rows for this action
278 */
279 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
280 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
281 FALSE, FALSE,
282 FALSE, FALSE,
283 FALSE,
284 $this->_mailingClause
285 );
286
287 // process the result of the query
288 $rows = array();
289 $permissions = array(CRM_Core_Permission::getPermission());
290 if (CRM_Core_Permission::check('delete contacts')) {
291 $permissions[] = CRM_Core_Permission::DELETE;
292 }
293 $mask = CRM_Core_Action::mask($permissions);
294 $qfKey = $this->_key;
295
296 while ($result->fetch()) {
297 $row = array();
298 // the columns we are interested in
299 foreach (self::$_properties as $property) {
300 if (property_exists($result, $property)) {
301 $row[$property] = $result->$property;
302 }
303 }
304
305 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->mailing_recipients_id;
306
307 $actions = array(
308 'cid' => $result->contact_id,
309 'cxt' => $this->_context,
310 );
311
312 $row['action'] = CRM_Core_Action::formLink(
313 self::links($qfKey, $this->_context),
314 $mask,
315 $actions,
316 ts('more'),
317 FALSE,
318 'contact.mailing.row',
319 'Contact',
320 $result->contact_id
321 );
322 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
323 );
324
325 $rows[] = $row;
326 }
327 return $rows;
328 }
329
330 /**
331 * @return array
332 * which contains an array of strings
333 */
334
335 // the current internationalisation is bad, but should more or less work
336 // for most of "European" languages
337 public function getQILL() {
338 return $this->_query->qill();
339 }
340
341 /**
342 * Returns the column headers as an array of tuples:
343 * (name, sortName (key to the sort array))
344 *
345 * @param string $action
346 * The action being performed.
347 * @param enum $output
348 * What should the result set include (web/email/csv).
349 *
350 * @return array
351 * the column headers that need to be displayed
352 */
353 public function &getColumnHeaders($action = NULL, $output = NULL) {
354 if (!isset(self::$_columnHeaders)) {
355 self::$_columnHeaders = array(
356 array('desc' => ts('Contact Type')),
357 array(
358 'name' => ts('Name'),
359 'sort' => 'sort_name',
360 'direction' => CRM_Utils_Sort::DONTCARE,
361 ),
362 array(
363 'name' => ts('Email'),
364 'sort' => 'email',
365 'direction' => CRM_Utils_Sort::DONTCARE,
366 ),
367 array(
368 'name' => ts('Mailing Name'),
369 'sort' => 'mailing_name',
370 'direction' => CRM_Utils_Sort::DONTCARE,
371 ),
372 array(
373 'name' => ts('Mailing Subject'),
374 'sort' => 'mailing_subject',
375 'direction' => CRM_Utils_Sort::DONTCARE,
376 ),
377 array(
378 'name' => ts('Mailing Status'),
379 'sort' => 'mailing_job_status',
380 'direction' => CRM_Utils_Sort::DONTCARE,
381 ),
382 array(
383 'name' => ts('Completed Date'),
384 'sort' => 'mailing_job_end_date',
385 'direction' => CRM_Utils_Sort::DONTCARE,
386 ),
387 array('desc' => ts('Actions')),
388 );
389 }
390 return self::$_columnHeaders;
391 }
392
393 /**
394 * @return mixed
395 */
396 public function alphabetQuery() {
397 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
398 }
399
400 /**
401 * @return string
402 */
403 public function &getQuery() {
404 return $this->_query;
405 }
406
407 /**
408 * Name of export file.
409 *
410 * @param string $output
411 * Type of output.
412 *
413 * @return string
414 * name of the file
415 */
416 public function getExportFileName($output = 'csv') {
417 return ts('CiviCRM Mailing Search');
418 }
419 }