Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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_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 | * | |
142 | * @param array $queryParams array of parameters for query | |
143 | * @param 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 | |
147 | * | |
148 | * @return CRM_Contact_Selector | |
149 | * @access public | |
150 | */ function __construct(&$queryParams, | |
151 | $action = CRM_Core_Action::NONE, | |
152 | $surveyClause = NULL, | |
153 | $single = FALSE, | |
154 | $limit = NULL, | |
155 | $context = 'search' | |
156 | ) { | |
157 | // submitted form values | |
158 | $this->_queryParams = &$queryParams; | |
159 | ||
160 | $this->_single = $single; | |
161 | $this->_limit = $limit; | |
162 | $this->_context = $context; | |
163 | ||
164 | $this->_campaignClause = $surveyClause; | |
165 | $this->_campaignFromClause = CRM_Utils_Array::value('fromClause', $surveyClause); | |
166 | $this->_campaignWhereClause = CRM_Utils_Array::value('whereClause', $surveyClause); | |
167 | ||
168 | // type of selector | |
169 | $this->_action = $action; | |
170 | ||
171 | $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, | |
172 | NULL, NULL, FALSE, FALSE, | |
173 | CRM_Contact_BAO_Query::MODE_CAMPAIGN, | |
174 | TRUE | |
175 | ); | |
176 | } | |
177 | //end of constructor | |
178 | ||
179 | /** | |
180 | * This method returns the links that are given for each search row. | |
181 | * currently the links added for each row are | |
182 | * | |
183 | * - View | |
184 | * - Edit | |
185 | * | |
186 | * @return array | |
187 | * @access public | |
188 | * | |
189 | */ | |
190 | static | |
191 | function &links() { | |
192 | return self::$_links = array(); | |
193 | } | |
194 | ||
195 | /** | |
196 | * getter for array of the parameters required for creating pager. | |
197 | * | |
198 | * @param | |
199 | * @access public | |
200 | */ | |
201 | function getPagerParams($action, &$params) { | |
202 | $params['csvString'] = NULL; | |
203 | $params['status'] = ts('Respondents') . ' %%StatusMessage%%'; | |
204 | $params['rowCount'] = ($this->_limit) ? $this->_limit : CRM_Utils_Pager::ROWCOUNT; | |
205 | $params['buttonTop'] = 'PagerTopButton'; | |
206 | $params['buttonBottom'] = 'PagerBottomButton'; | |
207 | } | |
208 | ||
209 | /** | |
210 | * Returns total number of rows for the query. | |
211 | * | |
212 | * @param | |
213 | * | |
214 | * @return int Total number of rows | |
215 | * @access public | |
216 | */ | |
217 | function getTotalCount($action) { | |
218 | return $this->_query->searchQuery(0, 0, NULL, | |
219 | TRUE, FALSE, | |
220 | FALSE, FALSE, FALSE, | |
221 | $this->_campaignWhereClause, | |
222 | NULL, | |
223 | $this->_campaignFromClause | |
224 | ); | |
225 | } | |
226 | ||
227 | /** | |
228 | * returns all the rows in the given offset and rowCount | |
229 | * | |
230 | * @param enum $action the action being performed | |
231 | * @param int $offset the row number to start from | |
232 | * @param int $rowCount the number of rows to return | |
233 | * @param string $sort the sql string that describes the sort order | |
234 | * @param enum $output what should the result set include (web/email/csv) | |
235 | * | |
236 | * @return int the total number of rows for this action | |
237 | */ | |
238 | function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { | |
239 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, | |
240 | FALSE, FALSE, | |
241 | FALSE, FALSE, | |
242 | FALSE, $this->_campaignWhereClause, | |
243 | NULL, | |
244 | $this->_campaignFromClause | |
245 | ); | |
246 | ||
247 | ||
248 | // process the result of the query | |
249 | $rows = array(); | |
250 | ||
251 | While ($result->fetch()) { | |
d9ab802d | 252 | $this->_query->convertToPseudoNames($result); |
6a488035 TO |
253 | $row = array(); |
254 | // the columns we are interested in | |
255 | foreach (self::$_properties as $property) { | |
256 | if (property_exists($result, $property)) { | |
257 | $row[$property] = $result->$property; | |
258 | } | |
259 | } | |
260 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id; | |
261 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_type, FALSE, $result->contact_id); | |
262 | ||
263 | $rows[] = $row; | |
264 | } | |
265 | $this->buildPrevNextCache($sort); | |
266 | ||
267 | return $rows; | |
268 | } | |
269 | ||
270 | function buildPrevNextCache($sort) { | |
271 | //for prev/next pagination | |
272 | $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject); | |
273 | ||
274 | if (!$crmPID) { | |
275 | $cacheKey = "civicrm search {$this->_key}"; | |
276 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact'); | |
91da6cd5 | 277 | |
6a488035 TO |
278 | $sql = $this->_query->searchQuery(0, 0, $sort, |
279 | FALSE, FALSE, | |
280 | FALSE, FALSE, | |
281 | TRUE, $this->_campaignWhereClause, | |
282 | NULL, | |
283 | $this->_campaignFromClause | |
284 | ); | |
285 | list($select, $from) = explode(' FROM ', $sql); | |
286 | $insertSQL = " | |
287 | INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) | |
91da6cd5 | 288 | SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name |
6a488035 TO |
289 | FROM {$from} |
290 | "; | |
291 | CRM_Core_Error::ignoreException(); | |
292 | $result = CRM_Core_DAO::executeQuery($insertSQL); | |
293 | CRM_Core_Error::setCallback(); | |
294 | ||
295 | if (is_a($result, 'DB_Error')) { | |
296 | return; | |
297 | } | |
298 | // also record an entry in the cache key table, so we can delete it periodically | |
299 | CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey); | |
300 | } | |
301 | } | |
302 | ||
303 | /** | |
304 | * | |
305 | * @return array $qill which contains an array of strings | |
306 | * @access public | |
307 | **/ | |
308 | public function getQILL() { | |
309 | return $this->_query->qill(); | |
310 | } | |
311 | ||
312 | /** | |
313 | * returns the column headers as an array of tuples: | |
314 | * (name, sortName (key to the sort array)) | |
315 | * | |
316 | * @param string $action the action being performed | |
317 | * @param enum $output what should the result set include (web/email/csv) | |
318 | * | |
319 | * @return array the column headers that need to be displayed | |
320 | * @access public | |
321 | */ | |
322 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
323 | self::$_columnHeaders = array(); | |
324 | ||
325 | if (!$this->_single) { | |
326 | $contactDetails = array( | |
327 | array('name' => ts('Contact Name'), | |
328 | 'sort' => 'sort_name', | |
329 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
330 | ), | |
331 | array('name' => ts('Street Number'), | |
332 | 'sort' => 'street_number', | |
333 | ), | |
334 | array('name' => ts('Street Name'), | |
335 | 'sort' => 'street_name', | |
336 | ), | |
337 | array('name' => ts('Street Address')), | |
338 | array('name' => ts('City'), | |
339 | 'sort' => 'city', | |
340 | ), | |
341 | array('name' => ts('Postal Code'), | |
342 | 'sort' => 'postal_code', | |
343 | ), | |
344 | array('name' => ts('State'), | |
345 | 'sort' => 'state_province_name', | |
346 | ), | |
347 | array('name' => ts('Country')), | |
348 | array('name' => ts('Email')), | |
349 | array('name' => ts('Phone')), | |
350 | ); | |
351 | self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders); | |
352 | } | |
353 | ||
354 | return self::$_columnHeaders; | |
355 | } | |
356 | ||
357 | function &getQuery() { | |
358 | return $this->_query; | |
359 | } | |
360 | ||
361 | /** | |
362 | * name of export file. | |
363 | * | |
364 | * @param string $output type of output | |
365 | * | |
366 | * @return string name of the file | |
367 | */ | |
368 | function getExportFileName($output = 'csv') { | |
369 | return ts('CiviCRM Respondent Search'); | |
370 | } | |
371 | } | |
372 | //end of class | |
373 |