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