3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class is used to retrieve and display a range of contacts that match the given criteria.
21 class CRM_Campaign_Selector_Search
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
24 * This defines two actions- View and Edit.
28 public static $_links = NULL;
31 * We use desc to remind us what that column is, name is used in the tpl
35 public static $_columnHeaders;
38 * Properties of contact we're interested in displaying
41 public static $_properties = [
56 'survey_activity_target_id',
57 'survey_activity_target_contact_id',
61 * Are we restricting ourselves to a single contact
65 protected $_single = FALSE;
68 * Are we restricting ourselves to a single contact
72 protected $_limit = NULL;
75 * What context are we being invoked from
79 protected $_context = NULL;
82 * QueryParams is the array returned by exportValues called on
83 * the HTML_QuickForm_Controller for that page.
90 * Represent the type of selector.
97 * The additional clause that we restrict the search with.
101 protected $_surveyClause = NULL;
113 * @param array $queryParams
114 * Array of parameters for query.
115 * @param \const|int $action - action of search basic or advanced.
116 * @param string $surveyClause
117 * If the caller wants to further restrict the search.
118 * @param bool $single
119 * Are we dealing only with one contact?.
121 * How many voters do we want returned.
123 * @param string $context
125 * @return \CRM_Campaign_Selector_Search
127 public function __construct(
129 $action = CRM_Core_Action
::NONE
,
130 $surveyClause = NULL,
135 // submitted form values
136 $this->_queryParams
= &$queryParams;
138 $this->_single
= $single;
139 $this->_limit
= $limit;
140 $this->_context
= $context;
142 $this->_campaignClause
= $surveyClause;
143 $this->_campaignFromClause
= CRM_Utils_Array
::value('fromClause', $surveyClause);
144 $this->_campaignWhereClause
= CRM_Utils_Array
::value('whereClause', $surveyClause);
147 $this->_action
= $action;
149 $this->_query
= new CRM_Contact_BAO_Query($this->_queryParams
,
150 NULL, NULL, FALSE, FALSE,
151 CRM_Contact_BAO_Query
::MODE_CAMPAIGN
,
157 * This method returns the links that are given for each search row.
158 * currently the links added for each row are
165 public static function &links() {
166 return self
::$_links = [];
170 * Getter for array of the parameters required for creating pager.
173 * @param array $params
175 public function getPagerParams($action, &$params) {
176 $params['csvString'] = NULL;
177 $params['status'] = ts('Respondents') . ' %%StatusMessage%%';
178 $params['rowCount'] = ($this->_limit
) ?
$this->_limit
: CRM_Utils_Pager
::ROWCOUNT
;
179 $params['buttonTop'] = 'PagerTopButton';
180 $params['buttonBottom'] = 'PagerBottomButton';
184 * Returns total number of rows for the query.
186 * @param string $action
189 * Total number of rows
191 public function getTotalCount($action) {
192 return $this->_query
->searchQuery(0, 0, NULL,
195 $this->_campaignWhereClause
,
197 $this->_campaignFromClause
202 * Returns all the rows in the given offset and rowCount.
204 * @param string $action
205 * The action being performed.
207 * The row number to start from.
208 * @param int $rowCount
209 * The number of rows to return.
210 * @param string $sort
211 * The sql string that describes the sort order.
212 * @param string $output
213 * What should the result set include (web/email/csv).
216 * the total number of rows for this action
218 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
219 $result = $this->_query
->searchQuery($offset, $rowCount, $sort,
222 FALSE, $this->_campaignWhereClause
,
224 $this->_campaignFromClause
227 // process the result of the query
230 while ($result->fetch()) {
231 $this->_query
->convertToPseudoNames($result);
233 // the columns we are interested in
234 foreach (self
::$_properties as $property) {
235 if (property_exists($result, $property)) {
236 $row[$property] = $result->$property;
239 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contact_id
;
240 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_type
, FALSE, $result->contact_id
);
244 $this->buildPrevNextCache($sort);
252 public function buildPrevNextCache($sort) {
253 //for prev/next pagination
254 $crmPID = CRM_Utils_Request
::retrieve('crmPID', 'Integer');
257 $cacheKey = "civicrm search {$this->_key}";
258 Civi
::service('prevnext')->deleteItem(NULL, $cacheKey, 'civicrm_contact');
260 $sql = $this->_query
->getSearchSQLParts(0, 0, $sort,
263 $this->_campaignWhereClause
,
265 $this->_campaignFromClause
269 SELECT %1, contact_a.id, contact_a.display_name
274 Civi
::service('prevnext')->fillWithSql($cacheKey, $selectSQL, [1 => [$cacheKey, 'String']]);
276 catch (CRM_Core_Exception
$e) {
277 // Heavy handed, no? Seems like this merits an explanation.
281 if (Civi
::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql
) {
282 // SQL-backed prevnext cache uses an extra record for pruning the cache.
283 // Also ensure that caches stay alive for 2 days a per previous code.
284 Civi
::cache('prevNextCache')->set($cacheKey, $cacheKey, 60 * 60 * 24 * CRM_Core_PrevNextCache_Sql
::cacheDays
);
291 * which contains an array of strings
293 public function getQILL() {
294 return $this->_query
->qill();
298 * Returns the column headers as an array of tuples:
299 * (name, sortName (key to the sort array))
301 * @param string $action
302 * The action being performed.
303 * @param string $output
304 * What should the result set include (web/email/csv).
307 * the column headers that need to be displayed
309 public function &getColumnHeaders($action = NULL, $output = NULL) {
310 self
::$_columnHeaders = [];
312 if (!$this->_single
) {
315 'name' => ts('Contact Name'),
316 'sort' => 'sort_name',
317 'direction' => CRM_Utils_Sort
::ASCENDING
,
320 'name' => ts('Street Number'),
321 'sort' => 'street_number',
324 'name' => ts('Street Name'),
325 'sort' => 'street_name',
327 ['name' => ts('Street Address')],
329 'name' => ts('City'),
333 'name' => ts('Postal Code'),
334 'sort' => 'postal_code',
337 'name' => ts('State'),
338 'sort' => 'state_province_name',
340 ['name' => ts('Country')],
341 ['name' => ts('Email')],
342 ['name' => ts('Phone')],
344 self
::$_columnHeaders = array_merge($contactDetails, self
::$_columnHeaders);
347 return self
::$_columnHeaders;
353 public function &getQuery() {
354 return $this->_query
;
358 * Name of export file.
360 * @param string $output
366 public function getExportFileName($output = 'csv') {
367 return ts('CiviCRM Respondent Search');