CRM/Case add automatically generated comments
[civicrm-core.git] / CRM / Campaign / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
6a488035
TO
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_Campaign_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 'sort_name',
68 'street_unit',
69 'street_name',
70 'street_number',
71 'street_address',
72 'city',
73 'postal_code',
74 'state_province',
75 'country',
76 'email',
77 'phone',
78 'campaign_id',
79 'survey_activity_id',
80 'survey_activity_target_id',
81 'survey_activity_target_contact_id',
82 );
83
84 /**
85 * are we restricting ourselves to a single contact
86 *
87 * @access protected
88 * @var boolean
89 */
90 protected $_single = FALSE;
91
92 /**
93 * are we restricting ourselves to a single contact
94 *
95 * @access protected
96 * @var boolean
97 */
98 protected $_limit = NULL;
99
100 /**
101 * what context are we being invoked from
102 *
103 * @access protected
104 * @var string
105 */
106 protected $_context = NULL;
107
108 /**
109 * queryParams is the array returned by exportValues called on
110 * the HTML_QuickForm_Controller for that page.
111 *
112 * @var array
113 * @access protected
114 */
115 public $_queryParams;
116
117 /**
118 * represent the type of selector
119 *
120 * @var int
121 * @access protected
122 */
123 protected $_action;
124
125 /**
126 * The additional clause that we restrict the search with
127 *
128 * @var string
129 */
130 protected $_surveyClause = NULL;
131
132 /**
133 * The query object
134 *
135 * @var string
136 */
137 protected $_query;
138
139 /**
140 * Class constructor
141 *
dd244018
EM
142 * @param array $queryParams array of parameters for query
143 * @param \const|int $action - action of search basic or advanced.
144 * @param string $surveyClause if the caller wants to further restrict the search.
145 * @param boolean $single are we dealing only with one contact?
146 * @param int $limit how many voters do we want returned
6a488035 147 *
dd244018
EM
148 * @param string $context
149 *
150 * @return \CRM_Campaign_Selector_Search
151 @access public
152 */
153 function __construct(&$queryParams,
6a488035
TO
154 $action = CRM_Core_Action::NONE,
155 $surveyClause = NULL,
156 $single = FALSE,
157 $limit = NULL,
158 $context = 'search'
159 ) {
160 // submitted form values
161 $this->_queryParams = &$queryParams;
162
163 $this->_single = $single;
164 $this->_limit = $limit;
165 $this->_context = $context;
166
167 $this->_campaignClause = $surveyClause;
168 $this->_campaignFromClause = CRM_Utils_Array::value('fromClause', $surveyClause);
169 $this->_campaignWhereClause = CRM_Utils_Array::value('whereClause', $surveyClause);
170
171 // type of selector
172 $this->_action = $action;
173
174 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
175 NULL, NULL, FALSE, FALSE,
176 CRM_Contact_BAO_Query::MODE_CAMPAIGN,
177 TRUE
178 );
179 }
180 //end of constructor
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
190 * @access public
191 *
192 */
193 static
194 function &links() {
195 return self::$_links = array();
196 }
197
198 /**
199 * getter for array of the parameters required for creating pager.
200 *
da6b46f4
EM
201 * @param $action
202 * @param $params
203 *
204 * @internal param $
6a488035
TO
205 * @access public
206 */
207 function getPagerParams($action, &$params) {
208 $params['csvString'] = NULL;
209 $params['status'] = ts('Respondents') . ' %%StatusMessage%%';
210 $params['rowCount'] = ($this->_limit) ? $this->_limit : CRM_Utils_Pager::ROWCOUNT;
211 $params['buttonTop'] = 'PagerTopButton';
212 $params['buttonBottom'] = 'PagerBottomButton';
213 }
214
215 /**
216 * Returns total number of rows for the query.
217 *
218 * @param
219 *
220 * @return int Total number of rows
221 * @access public
222 */
223 function getTotalCount($action) {
224 return $this->_query->searchQuery(0, 0, NULL,
225 TRUE, FALSE,
226 FALSE, FALSE, FALSE,
227 $this->_campaignWhereClause,
228 NULL,
229 $this->_campaignFromClause
230 );
231 }
232
233 /**
234 * returns all the rows in the given offset and rowCount
235 *
236 * @param enum $action the action being performed
237 * @param int $offset the row number to start from
238 * @param int $rowCount the number of rows to return
239 * @param string $sort the sql string that describes the sort order
240 * @param enum $output what should the result set include (web/email/csv)
241 *
242 * @return int the total number of rows for this action
243 */
244 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
245 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
246 FALSE, FALSE,
247 FALSE, FALSE,
248 FALSE, $this->_campaignWhereClause,
249 NULL,
250 $this->_campaignFromClause
251 );
252
253
254 // process the result of the query
255 $rows = array();
256
257 While ($result->fetch()) {
d9ab802d 258 $this->_query->convertToPseudoNames($result);
6a488035
TO
259 $row = array();
260 // the columns we are interested in
261 foreach (self::$_properties as $property) {
262 if (property_exists($result, $property)) {
263 $row[$property] = $result->$property;
264 }
265 }
266 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id;
267 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_type, FALSE, $result->contact_id);
268
269 $rows[] = $row;
270 }
271 $this->buildPrevNextCache($sort);
272
273 return $rows;
274 }
275
276 function buildPrevNextCache($sort) {
277 //for prev/next pagination
278 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
279
280 if (!$crmPID) {
281 $cacheKey = "civicrm search {$this->_key}";
282 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact');
91da6cd5 283
6a488035
TO
284 $sql = $this->_query->searchQuery(0, 0, $sort,
285 FALSE, FALSE,
286 FALSE, FALSE,
287 TRUE, $this->_campaignWhereClause,
288 NULL,
289 $this->_campaignFromClause
290 );
291 list($select, $from) = explode(' FROM ', $sql);
292 $insertSQL = "
293INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
91da6cd5 294SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
6a488035
TO
295FROM {$from}
296";
6a4257d4 297 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
6a488035 298 $result = CRM_Core_DAO::executeQuery($insertSQL);
6a4257d4 299 unset($errorScope);
6a488035
TO
300
301 if (is_a($result, 'DB_Error')) {
302 return;
303 }
304 // also record an entry in the cache key table, so we can delete it periodically
305 CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
306 }
307 }
308
309 /**
310 *
311 * @return array $qill which contains an array of strings
312 * @access public
313 **/
314 public function getQILL() {
315 return $this->_query->qill();
316 }
317
318 /**
319 * returns the column headers as an array of tuples:
320 * (name, sortName (key to the sort array))
321 *
322 * @param string $action the action being performed
323 * @param enum $output what should the result set include (web/email/csv)
324 *
325 * @return array the column headers that need to be displayed
326 * @access public
327 */
328 public function &getColumnHeaders($action = NULL, $output = NULL) {
329 self::$_columnHeaders = array();
330
331 if (!$this->_single) {
332 $contactDetails = array(
333 array('name' => ts('Contact Name'),
334 'sort' => 'sort_name',
335 'direction' => CRM_Utils_Sort::ASCENDING,
336 ),
337 array('name' => ts('Street Number'),
338 'sort' => 'street_number',
339 ),
340 array('name' => ts('Street Name'),
341 'sort' => 'street_name',
342 ),
343 array('name' => ts('Street Address')),
344 array('name' => ts('City'),
345 'sort' => 'city',
346 ),
347 array('name' => ts('Postal Code'),
348 'sort' => 'postal_code',
349 ),
350 array('name' => ts('State'),
351 'sort' => 'state_province_name',
352 ),
353 array('name' => ts('Country')),
354 array('name' => ts('Email')),
355 array('name' => ts('Phone')),
356 );
357 self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders);
358 }
359
360 return self::$_columnHeaders;
361 }
362
363 function &getQuery() {
364 return $this->_query;
365 }
366
367 /**
368 * name of export file.
369 *
370 * @param string $output type of output
371 *
372 * @return string name of the file
373 */
374 function getExportFileName($output = 'csv') {
375 return ts('CiviCRM Respondent Search');
376 }
377}
378//end of class
379