Add test for DB error on search add to group CRM-21159
[civicrm-core.git] / CRM / Contact / Selector / Custom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035 32 * $Id: Selector.php 11510 2007-09-18 09:21:34Z lobo $
6a488035
TO
33 */
34
35/**
36 * This class is used to retrieve and display a range of
37 * contacts that match the given criteria (specifically for
38 * results of advanced search options.
6a488035
TO
39 */
40class CRM_Contact_Selector_Custom extends CRM_Contact_Selector {
41
42 /**
43 * This defines two actions- View and Edit.
44 *
45 * @var array
6a488035
TO
46 */
47 static $_links = NULL;
48
49 /**
100fef9d 50 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
51 *
52 * @var array
6a488035
TO
53 */
54 static $_columnHeaders;
55
56 /**
57 * Properties of contact we're interested in displaying
58 * @var array
6a488035
TO
59 */
60 static $_properties = array('contact_id', 'contact_type', 'display_name');
61
62 /**
100fef9d 63 * FormValues is the array returned by exportValues called on
6a488035
TO
64 * the HTML_QuickForm_Controller for that page.
65 *
66 * @var array
6a488035
TO
67 */
68 public $_formValues;
69
70 /**
100fef9d 71 * Params is the array in a value used by the search query creator
6a488035
TO
72 *
73 * @var array
6a488035
TO
74 */
75 public $_params;
76
77 /**
100fef9d 78 * Represent the type of selector
6a488035
TO
79 *
80 * @var int
6a488035
TO
81 */
82 protected $_action;
83
84 protected $_query;
85
86 /**
100fef9d 87 * The public visible fields to be shown to the user
6a488035
TO
88 *
89 * @var array
6a488035
TO
90 */
91 protected $_fields;
92
93 /**
94 * The object that implements the search interface
95 */
96 protected $_search;
97
98 protected $_customSearchClass;
99
100 /**
5c9ff055 101 * Class constructor.
6a488035 102 *
6c8f6e67 103 * @param $customSearchClass
77c5b619
TO
104 * @param array $formValues
105 * Array of form values imported.
106 * @param array $params
107 * Array of parameters for query.
6c8f6e67
EM
108 * @param null $returnProperties
109 * @param \const|int $action - action of search basic or advanced.
6a488035 110 *
6c8f6e67
EM
111 * @param bool $includeContactIds
112 * @param bool $searchChildGroups
113 * @param string $searchContext
114 * @param null $contextMenu
115 *
116 * @return \CRM_Contact_Selector_Custom
6a488035 117 */
db7de9c1 118 public function __construct(
6a488035 119 $customSearchClass,
ce80b209
TO
120 $formValues = NULL,
121 $params = NULL,
122 $returnProperties = NULL,
123 $action = CRM_Core_Action::NONE,
6a488035
TO
124 $includeContactIds = FALSE,
125 $searchChildGroups = TRUE,
ce80b209
TO
126 $searchContext = 'search',
127 $contextMenu = NULL
6a488035
TO
128 ) {
129 $this->_customSearchClass = $customSearchClass;
130 $this->_formValues = $formValues;
131 $this->_includeContactIds = $includeContactIds;
132
133 $ext = CRM_Extension_System::singleton()->getMapper();
134
135 if (!$ext->isExtensionKey($customSearchClass)) {
136 if ($ext->isExtensionClass($customSearchClass)) {
137 $customSearchFile = $ext->classToPath($customSearchClass);
ce80b209 138 require_once $customSearchFile;
6a488035
TO
139 }
140 else {
ce80b209 141 require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php';
6a488035 142 }
481a74f4 143 $this->_search = new $customSearchClass($formValues);
6a488035
TO
144 }
145 else {
c7b8b4e4
DL
146 $fnName = $ext->keyToPath;
147 $customSearchFile = $fnName($customSearchClass, 'search');
148 $className = $ext->keyToClass($customSearchClass, 'search');
149 $this->_search = new $className($formValues);
6a488035
TO
150 }
151 }
6a488035
TO
152
153 /**
154 * This method returns the links that are given for each search row.
6a488035 155 *
db7de9c1 156 * Currently the links added for each row are
6a488035
TO
157 * - View
158 * - Edit
159 *
160 * @return array
6a488035 161 */
00be9182 162 public static function &links() {
7c34ab11 163 list($key) = func_get_args();
119c08a8 164 $searchContext = "&context=custom";
7c34ab11 165 $extraParams = ($key) ? "&key={$key}" : NULL;
166
6a488035
TO
167 if (!(self::$_links)) {
168 self::$_links = array(
169 CRM_Core_Action::VIEW => array(
170 'name' => ts('View'),
171 'url' => 'civicrm/contact/view',
119c08a8 172 'qs' => "reset=1&cid=%%id%%{$extraParams}{$searchContext}",
d3fcd52f 173 'class' => 'no-popup',
6a488035
TO
174 'title' => ts('View Contact Details'),
175 ),
176 CRM_Core_Action::UPDATE => array(
177 'name' => ts('Edit'),
178 'url' => 'civicrm/contact/add',
179 'qs' => 'reset=1&action=update&cid=%%id%%',
d3fcd52f 180 'class' => 'no-popup',
6a488035
TO
181 'title' => ts('Edit Contact Details'),
182 ),
183 );
184
185 $config = CRM_Core_Config::singleton();
030c6f6b
SB
186 //CRM-16552: mapAPIKey is not mandatory as google no longer requires an API Key
187 if ($config->mapProvider && ($config->mapAPIKey || $config->mapProvider == 'Google')) {
6a488035
TO
188 self::$_links[CRM_Core_Action::MAP] = array(
189 'name' => ts('Map'),
190 'url' => 'civicrm/contact/map',
191 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
d3fcd52f 192 'class' => 'no-popup',
6a488035
TO
193 'title' => ts('Map Contact'),
194 );
195 }
196 }
197 return self::$_links;
198 }
6a488035
TO
199
200 /**
100fef9d 201 * Getter for array of the parameters required for creating pager.
6a488035 202 *
da6b46f4 203 * @param $action
c490a46a 204 * @param array $params
6a488035 205 */
00be9182 206 public function getPagerParams($action, &$params) {
353ffa53 207 $params['status'] = ts('Contact %%StatusMessage%%');
6a488035 208 $params['csvString'] = NULL;
353ffa53 209 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
6a488035
TO
210
211 $params['buttonTop'] = 'PagerTopButton';
212 $params['buttonBottom'] = 'PagerBottomButton';
213 }
6a488035
TO
214
215 /**
db7de9c1
EM
216 * Returns the column headers as an array of tuples.
217 *
218 * Keys are name, sortName, key to the sort array.
6a488035 219 *
77c5b619
TO
220 * @param string $action
221 * The action being performed.
3f8d2862 222 * @param string $output
77c5b619 223 * What should the result set include (web/email/csv).
6a488035 224 *
a6c01b45
CW
225 * @return array
226 * the column headers that need to be displayed
6a488035 227 */
00be9182 228 public function &getColumnHeaders($action = NULL, $output = NULL) {
6a488035
TO
229 $columns = $this->_search->columns();
230 if ($output == CRM_Core_Selector_Controller::EXPORT) {
231 return array_keys($columns);
232 }
233 else {
234 $headers = array();
235 foreach ($columns as $name => $key) {
236 if (!empty($name)) {
237 $headers[] = array(
238 'name' => $name,
239 'sort' => $key,
240 'direction' => CRM_Utils_Sort::ASCENDING,
241 );
242 }
243 else {
244 $headers[] = array();
245 }
246 }
247 return $headers;
248 }
249 }
250
251 /**
252 * Returns total number of rows for the query.
253 *
ee0ce2ef 254 * @param null $action
6a488035 255 *
a6c01b45
CW
256 * @return int
257 * Total number of rows
6a488035 258 */
00be9182 259 public function getTotalCount($action) {
6a488035
TO
260 return $this->_search->count();
261 }
262
263 /**
d8689418 264 * Returns all the rows in the given offset and rowCount.
6a488035 265 *
3f8d2862 266 * @param string $action
77c5b619
TO
267 * The action being performed.
268 * @param int $offset
269 * The row number to start from.
270 * @param int $rowCount
271 * The number of rows to return.
272 * @param string $sort
273 * The sql string that describes the sort order.
3f8d2862 274 * @param string $output
77c5b619 275 * What should the result set include (web/email/csv).
6a488035 276 *
a6c01b45
CW
277 * @return int
278 * the total number of rows for this action
6a488035 279 */
00be9182 280 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
281
282 $includeContactIDs = FALSE;
283 if (($output == CRM_Core_Selector_Controller::EXPORT ||
284 $output == CRM_Core_Selector_Controller::SCREEN
285 ) &&
286 $this->_formValues['radio_ts'] == 'ts_sel'
287 ) {
288 $includeContactIDs = TRUE;
289 }
290
291 $sql = $this->_search->all($offset, $rowCount, $sort, $includeContactIDs);
d9ab802d
PJ
292 // contact query object used for creating $sql
293 $contactQueryObj = NULL;
294 if (method_exists($this->_search, 'getQueryObj') &&
353ffa53
TO
295 is_a($this->_search->getQueryObj(), 'CRM_Contact_BAO_Query')
296 ) {
d9ab802d
PJ
297 $contactQueryObj = $this->_search->getQueryObj();
298 }
6a488035 299
8b0533f0 300 $dao = CRM_Core_DAO::executeQuery($sql);
6a488035 301
353ffa53 302 $columns = $this->_search->columns();
6a488035 303 $columnNames = array_values($columns);
353ffa53 304 $links = self::links($this->_key);
6a488035
TO
305
306 $permissions = array(CRM_Core_Permission::getPermission());
307 if (CRM_Core_Permission::check('delete contacts')) {
308 $permissions[] = CRM_Core_Permission::DELETE;
309 }
310 $mask = CRM_Core_Action::mask($permissions);
311
312 $alterRow = FALSE;
313 if (method_exists($this->_customSearchClass,
353ffa53
TO
314 'alterRow'
315 )) {
6a488035
TO
316 $alterRow = TRUE;
317 }
318 $image = FALSE;
319 if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_Basic')) {
320 $image = TRUE;
321 }
322 // process the result of the query
323 $rows = array();
324 while ($dao->fetch()) {
325 $row = array();
326 $empty = TRUE;
327
d9ab802d
PJ
328 // if contact query object present
329 // process pseudo constants
330 if ($contactQueryObj) {
331 $contactQueryObj->convertToPseudoNames($dao);
332 }
333
6a488035
TO
334 // the columns we are interested in
335 foreach ($columnNames as $property) {
37990ecd
JV
336 // Get part of name after last . (if any)
337 $unqualified_property = CRM_Utils_Array::First(array_slice(explode('.', $property), -1));
338 $row[$property] = $dao->$unqualified_property;
339 if (!empty($dao->$unqualified_property)) {
6a488035
TO
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 /**
d8689418 376 * Given the current formValues, gets the query in local language.
6a488035 377 *
a6c01b45
CW
378 * @return array
379 * which contains an array of strings
6a488035
TO
380 */
381 public function getQILL() {
382 return NULL;
383 }
384
86538308 385 /**
5c9ff055
EM
386 * Get summary.
387 *
86538308
EM
388 * @return mixed
389 */
6a488035
TO
390 public function getSummary() {
391 return $this->_search->summary();
392 }
393
394 /**
100fef9d 395 * Name of export file.
6a488035 396 *
77c5b619
TO
397 * @param string $output
398 * Type of output.
6a488035 399 *
a6c01b45
CW
400 * @return string
401 * name of the file
6a488035 402 */
00be9182 403 public function getExportFileName($output = 'csv') {
6a488035
TO
404 return ts('CiviCRM Custom Search');
405 }
406
86538308 407 /**
5c9ff055
EM
408 * Do nothing.
409 *
86538308
EM
410 * @return null
411 */
00be9182 412 public function alphabetQuery() {
6a488035
TO
413 return NULL;
414 }
415
86538308 416 /**
0965e988
EM
417 * Generate contact ID query.
418 *
c490a46a 419 * @param array $params
86538308 420 * @param $action
100fef9d 421 * @param int $sortID
86538308
EM
422 * @param null $displayRelationshipType
423 * @param string $queryOperator
424 *
425 * @return Object
426 */
ae066a2b 427 public function contactIDQuery($params, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
f57a5178
JV
428 // $action, $displayRelationshipType and $queryOperator are unused. I have
429 // no idea why they are there.
430
431 // I wonder whether there is some helper function for this:
432 $matches = array();
433 if (preg_match('/([0-9]*)(_(u|d))?/', $sortID, $matches)) {
434 $columns = array_values($this->_search->columns());
435 $sort = $columns[$matches[1] - 1];
436 if (array_key_exists(3, $matches) && $matches[3] == 'd') {
437 $sort .= " DESC";
438 }
439 }
440 else {
441 $sort = NULL;
442 }
6a488035 443
f57a5178
JV
444 $sql = $this->_search->contactIDs(0, 0, $sort);
445 return CRM_Core_DAO::executeQuery($sql);
6a488035
TO
446 }
447
86538308 448 /**
0965e988
EM
449 * Add actions.
450 *
451 * @param array $rows
86538308 452 */
00be9182 453 public function addActions(&$rows) {
7c34ab11 454 $links = self::links($this->_key);
6a488035
TO
455
456 $permissions = array(CRM_Core_Permission::getPermission());
457 if (CRM_Core_Permission::check('delete contacts')) {
458 $permissions[] = CRM_Core_Permission::DELETE;
459 }
460 $mask = CRM_Core_Action::mask($permissions);
461
462 foreach ($rows as $id => & $row) {
463 $row['action'] = CRM_Core_Action::formLink($links,
464 $mask,
87dab4a4
AH
465 array('id' => $row['contact_id']),
466 ts('more'),
467 FALSE,
468 'contact.custom.actions',
469 'Contact',
470 $row['contact_id']
6a488035
TO
471 );
472 }
473 }
474
86538308 475 /**
0965e988
EM
476 * Remove actions.
477 *
478 * @param array $rows
86538308 479 */
00be9182 480 public function removeActions(&$rows) {
6a488035
TO
481 foreach ($rows as $rid => & $rValue) {
482 unset($rValue['action']);
483 }
484 }
96025800 485
6a488035 486}