Merge pull request #4892 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Contact / Selector / Custom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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: Selector.php 11510 2007-09-18 09:21:34Z lobo $
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_Contact_Selector_Custom extends CRM_Contact_Selector {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
48 * @static
49 */
50 static $_links = NULL;
51
52 /**
100fef9d 53 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
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('contact_id', 'contact_type', 'display_name');
66
67 /**
100fef9d 68 * FormValues is the array returned by exportValues called on
6a488035
TO
69 * the HTML_QuickForm_Controller for that page.
70 *
71 * @var array
6a488035
TO
72 */
73 public $_formValues;
74
75 /**
100fef9d 76 * Params is the array in a value used by the search query creator
6a488035
TO
77 *
78 * @var array
6a488035
TO
79 */
80 public $_params;
81
82 /**
100fef9d 83 * Represent the type of selector
6a488035
TO
84 *
85 * @var int
6a488035
TO
86 */
87 protected $_action;
88
89 protected $_query;
90
91 /**
100fef9d 92 * The public visible fields to be shown to the user
6a488035
TO
93 *
94 * @var array
6a488035
TO
95 */
96 protected $_fields;
97
98 /**
99 * The object that implements the search interface
100 */
101 protected $_search;
102
103 protected $_customSearchClass;
104
105 /**
106 * Class constructor
107 *
6c8f6e67 108 * @param $customSearchClass
77c5b619
TO
109 * @param array $formValues
110 * Array of form values imported.
111 * @param array $params
112 * Array of parameters for query.
6c8f6e67
EM
113 * @param null $returnProperties
114 * @param \const|int $action - action of search basic or advanced.
6a488035 115 *
6c8f6e67
EM
116 * @param bool $includeContactIds
117 * @param bool $searchChildGroups
118 * @param string $searchContext
119 * @param null $contextMenu
120 *
121 * @return \CRM_Contact_Selector_Custom
122 @access public
6a488035
TO
123 */
124 function __construct(
125 $customSearchClass,
ce80b209
TO
126 $formValues = NULL,
127 $params = NULL,
128 $returnProperties = NULL,
129 $action = CRM_Core_Action::NONE,
6a488035
TO
130 $includeContactIds = FALSE,
131 $searchChildGroups = TRUE,
ce80b209
TO
132 $searchContext = 'search',
133 $contextMenu = NULL
6a488035
TO
134 ) {
135 $this->_customSearchClass = $customSearchClass;
136 $this->_formValues = $formValues;
137 $this->_includeContactIds = $includeContactIds;
138
139 $ext = CRM_Extension_System::singleton()->getMapper();
140
141 if (!$ext->isExtensionKey($customSearchClass)) {
142 if ($ext->isExtensionClass($customSearchClass)) {
143 $customSearchFile = $ext->classToPath($customSearchClass);
ce80b209 144 require_once $customSearchFile;
6a488035
TO
145 }
146 else {
ce80b209 147 require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php';
6a488035 148 }
481a74f4 149 $this->_search = new $customSearchClass($formValues);
6a488035
TO
150 }
151 else {
c7b8b4e4
DL
152 $fnName = $ext->keyToPath;
153 $customSearchFile = $fnName($customSearchClass, 'search');
154 $className = $ext->keyToClass($customSearchClass, 'search');
155 $this->_search = new $className($formValues);
6a488035
TO
156 }
157 }
6a488035
TO
158
159 /**
160 * This method returns the links that are given for each search row.
161 * currently the links added for each row are
162 *
163 * - View
164 * - Edit
165 *
166 * @return array
6a488035
TO
167 *
168 */
00be9182 169 public static function &links() {
7c34ab11 170 list($key) = func_get_args();
119c08a8 171 $searchContext = "&context=custom";
7c34ab11 172 $extraParams = ($key) ? "&key={$key}" : NULL;
173
6a488035
TO
174 if (!(self::$_links)) {
175 self::$_links = array(
176 CRM_Core_Action::VIEW => array(
177 'name' => ts('View'),
178 'url' => 'civicrm/contact/view',
119c08a8 179 'qs' => "reset=1&cid=%%id%%{$extraParams}{$searchContext}",
d3fcd52f 180 'class' => 'no-popup',
6a488035
TO
181 'title' => ts('View Contact Details'),
182 ),
183 CRM_Core_Action::UPDATE => array(
184 'name' => ts('Edit'),
185 'url' => 'civicrm/contact/add',
186 'qs' => 'reset=1&action=update&cid=%%id%%',
d3fcd52f 187 'class' => 'no-popup',
6a488035
TO
188 'title' => ts('Edit Contact Details'),
189 ),
190 );
191
192 $config = CRM_Core_Config::singleton();
193 if ($config->mapAPIKey && $config->mapProvider) {
194 self::$_links[CRM_Core_Action::MAP] = array(
195 'name' => ts('Map'),
196 'url' => 'civicrm/contact/map',
197 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
d3fcd52f 198 'class' => 'no-popup',
6a488035
TO
199 'title' => ts('Map Contact'),
200 );
201 }
202 }
203 return self::$_links;
204 }
6a488035
TO
205
206 /**
100fef9d 207 * Getter for array of the parameters required for creating pager.
6a488035 208 *
da6b46f4 209 * @param $action
c490a46a 210 * @param array $params
da6b46f4 211 *
6a488035 212 */
00be9182 213 public function getPagerParams($action, &$params) {
6a488035
TO
214 $params['status'] = ts('Contact %%StatusMessage%%');
215 $params['csvString'] = NULL;
216 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
217
218 $params['buttonTop'] = 'PagerTopButton';
219 $params['buttonBottom'] = 'PagerBottomButton';
220 }
6a488035
TO
221
222 /**
100fef9d 223 * Returns the column headers as an array of tuples:
6a488035
TO
224 * (name, sortName (key to the sort array))
225 *
77c5b619
TO
226 * @param string $action
227 * The action being performed.
228 * @param enum $output
229 * What should the result set include (web/email/csv).
6a488035
TO
230 *
231 * @return array the column headers that need to be displayed
6a488035 232 */
00be9182 233 public function &getColumnHeaders($action = NULL, $output = NULL) {
6a488035
TO
234 $columns = $this->_search->columns();
235 if ($output == CRM_Core_Selector_Controller::EXPORT) {
236 return array_keys($columns);
237 }
238 else {
239 $headers = array();
240 foreach ($columns as $name => $key) {
241 if (!empty($name)) {
242 $headers[] = array(
243 'name' => $name,
244 'sort' => $key,
245 'direction' => CRM_Utils_Sort::ASCENDING,
246 );
247 }
248 else {
249 $headers[] = array();
250 }
251 }
252 return $headers;
253 }
254 }
255
256 /**
257 * Returns total number of rows for the query.
258 *
259 * @param
260 *
261 * @return int Total number of rows
6a488035 262 */
00be9182 263 public function getTotalCount($action) {
6a488035
TO
264 return $this->_search->count();
265 }
266
267 /**
100fef9d 268 * Returns all the rows in the given offset and rowCount
6a488035 269 *
77c5b619
TO
270 * @param enum $action
271 * The action being performed.
272 * @param int $offset
273 * The row number to start from.
274 * @param int $rowCount
275 * The number of rows to return.
276 * @param string $sort
277 * The sql string that describes the sort order.
278 * @param enum $output
279 * What should the result set include (web/email/csv).
6a488035
TO
280 *
281 * @return int the total number of rows for this action
282 */
00be9182 283 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
284
285 $includeContactIDs = FALSE;
286 if (($output == CRM_Core_Selector_Controller::EXPORT ||
287 $output == CRM_Core_Selector_Controller::SCREEN
288 ) &&
289 $this->_formValues['radio_ts'] == 'ts_sel'
290 ) {
291 $includeContactIDs = TRUE;
292 }
293
294 $sql = $this->_search->all($offset, $rowCount, $sort, $includeContactIDs);
d9ab802d
PJ
295 // contact query object used for creating $sql
296 $contactQueryObj = NULL;
297 if (method_exists($this->_search, 'getQueryObj') &&
298 is_a($this->_search->getQueryObj(), 'CRM_Contact_BAO_Query')) {
299 $contactQueryObj = $this->_search->getQueryObj();
300 }
6a488035
TO
301
302 $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
303
304 $columns = $this->_search->columns();
305 $columnNames = array_values($columns);
7c34ab11 306 $links = self::links($this->_key);
6a488035
TO
307
308 $permissions = array(CRM_Core_Permission::getPermission());
309 if (CRM_Core_Permission::check('delete contacts')) {
310 $permissions[] = CRM_Core_Permission::DELETE;
311 }
312 $mask = CRM_Core_Action::mask($permissions);
313
314 $alterRow = FALSE;
315 if (method_exists($this->_customSearchClass,
316 'alterRow'
317 )) {
318 $alterRow = TRUE;
319 }
320 $image = FALSE;
321 if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_Basic')) {
322 $image = TRUE;
323 }
324 // process the result of the query
325 $rows = array();
326 while ($dao->fetch()) {
327 $row = array();
328 $empty = TRUE;
329
d9ab802d
PJ
330 // if contact query object present
331 // process pseudo constants
332 if ($contactQueryObj) {
333 $contactQueryObj->convertToPseudoNames($dao);
334 }
335
6a488035
TO
336 // the columns we are interested in
337 foreach ($columnNames as $property) {
338 $row[$property] = $dao->$property;
339 if (!empty($dao->$property)) {
340 $empty = FALSE;
341 }
342 }
343 if (!$empty) {
344 $contactID = isset($dao->contact_id) ? $dao->contact_id : NULL;
345
346 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $contactID;
347 $row['action'] = CRM_Core_Action::formLink($links,
348 $mask,
87dab4a4
AH
349 array('id' => $contactID),
350 ts('more'),
351 FALSE,
352 'contact.custom.actions',
353 'Contact',
354 $contactID
6a488035
TO
355 );
356 $row['contact_id'] = $contactID;
357
358 if ($alterRow) {
359 $this->_search->alterRow($row);
360 }
361
362 if ($image) {
ce80b209 363 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ? $dao->contact_sub_type : $dao->contact_type, FALSE, $contactID
6a488035
TO
364 );
365 }
366 $rows[] = $row;
367 }
368 }
369
370 $this->buildPrevNextCache($sort);
371
372 return $rows;
373 }
374
375 /**
376 * Given the current formValues, gets the query in local
377 * language
378 *
77c5b619 379 * @param array(
ce80b209 380 reference) $formValues submitted formValues
6a488035
TO
381 *
382 * @return array $qill which contains an array of strings
6a488035
TO
383 */
384 public function getQILL() {
385 return NULL;
386 }
387
86538308
EM
388 /**
389 * @return mixed
390 */
6a488035
TO
391 public function getSummary() {
392 return $this->_search->summary();
393 }
394
395 /**
100fef9d 396 * Name of export file.
6a488035 397 *
77c5b619
TO
398 * @param string $output
399 * Type of output.
6a488035
TO
400 *
401 * @return string name of the file
402 */
00be9182 403 public function getExportFileName($output = 'csv') {
6a488035
TO
404 return ts('CiviCRM Custom Search');
405 }
406
86538308
EM
407 /**
408 * @return null
409 */
00be9182 410 public function alphabetQuery() {
6a488035
TO
411 return NULL;
412 }
413
86538308 414 /**
c490a46a 415 * @param array $params
86538308 416 * @param $action
100fef9d 417 * @param int $sortID
86538308
EM
418 * @param null $displayRelationshipType
419 * @param string $queryOperator
420 *
421 * @return Object
422 */
00be9182 423 public function &contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
6a488035
TO
424 $params = array();
425 $sql = $this->_search->contactIDs($params);
426
427 return CRM_Core_DAO::executeQuery($sql, $params);
428 }
429
86538308
EM
430 /**
431 * @param $rows
432 */
00be9182 433 public function addActions(&$rows) {
7c34ab11 434 $links = self::links($this->_key);
6a488035
TO
435
436 $permissions = array(CRM_Core_Permission::getPermission());
437 if (CRM_Core_Permission::check('delete contacts')) {
438 $permissions[] = CRM_Core_Permission::DELETE;
439 }
440 $mask = CRM_Core_Action::mask($permissions);
441
442 foreach ($rows as $id => & $row) {
443 $row['action'] = CRM_Core_Action::formLink($links,
444 $mask,
87dab4a4
AH
445 array('id' => $row['contact_id']),
446 ts('more'),
447 FALSE,
448 'contact.custom.actions',
449 'Contact',
450 $row['contact_id']
6a488035
TO
451 );
452 }
453 }
454
86538308
EM
455 /**
456 * @param $rows
457 */
00be9182 458 public function removeActions(&$rows) {
6a488035
TO
459 foreach ($rows as $rid => & $rValue) {
460 unset($rValue['action']);
461 }
462 }
463}