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