Merge pull request #14404 from seamuslee001/test_run_phpunit6
[civicrm-core.git] / CRM / Contact / Selector / Custom.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 * $Id: Selector.php 11510 2007-09-18 09:21:34Z lobo $
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.
39 */
40 class CRM_Contact_Selector_Custom extends CRM_Contact_Selector {
41
42 /**
43 * This defines two actions- View and Edit.
44 *
45 * @var array
46 */
47 public static $_links = NULL;
48
49 /**
50 * We use desc to remind us what that column is, name is used in the tpl
51 *
52 * @var array
53 */
54 public static $_columnHeaders;
55
56 /**
57 * Properties of contact we're interested in displaying
58 * @var array
59 */
60 public static $_properties = ['contact_id', 'contact_type', 'display_name'];
61
62 /**
63 * FormValues is the array returned by exportValues called on
64 * the HTML_QuickForm_Controller for that page.
65 *
66 * @var array
67 */
68 public $_formValues;
69
70 /**
71 * Params is the array in a value used by the search query creator
72 *
73 * @var array
74 */
75 public $_params;
76
77 /**
78 * Represent the type of selector
79 *
80 * @var int
81 */
82 protected $_action;
83
84 protected $_query;
85
86 /**
87 * The public visible fields to be shown to the user
88 *
89 * @var array
90 */
91 protected $_fields;
92
93 /**
94 * The object that implements the search interface
95 * @var object
96 */
97 protected $_search;
98
99 protected $_customSearchClass;
100
101 /**
102 * Class constructor.
103 *
104 * @param $customSearchClass
105 * @param array $formValues
106 * Array of form values imported.
107 * @param array $params
108 * Array of parameters for query.
109 * @param null $returnProperties
110 * @param \const|int $action - action of search basic or advanced.
111 *
112 * @param bool $includeContactIds
113 * @param bool $searchChildGroups
114 * @param string $searchContext
115 * @param null $contextMenu
116 *
117 * @return \CRM_Contact_Selector_Custom
118 */
119 public function __construct(
120 $customSearchClass,
121 $formValues = NULL,
122 $params = NULL,
123 $returnProperties = NULL,
124 $action = CRM_Core_Action::NONE,
125 $includeContactIds = FALSE,
126 $searchChildGroups = TRUE,
127 $searchContext = 'search',
128 $contextMenu = NULL
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);
139 require_once $customSearchFile;
140 }
141 else {
142 require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php';
143 }
144 $this->_search = new $customSearchClass($formValues);
145 }
146 else {
147 $fnName = $ext->keyToPath;
148 $customSearchFile = $fnName($customSearchClass, 'search');
149 $className = $ext->keyToClass($customSearchClass, 'search');
150 $this->_search = new $className($formValues);
151 }
152 }
153
154 /**
155 * This method returns the links that are given for each search row.
156 *
157 * Currently the links added for each row are
158 * - View
159 * - Edit
160 *
161 * @return array
162 */
163 public static function &links() {
164 list($key) = func_get_args();
165 $searchContext = "&context=custom";
166 $extraParams = ($key) ? "&key={$key}" : NULL;
167
168 if (!(self::$_links)) {
169 self::$_links = [
170 CRM_Core_Action::VIEW => [
171 'name' => ts('View'),
172 'url' => 'civicrm/contact/view',
173 'qs' => "reset=1&cid=%%id%%{$extraParams}{$searchContext}",
174 'class' => 'no-popup',
175 'title' => ts('View Contact Details'),
176 ],
177 CRM_Core_Action::UPDATE => [
178 'name' => ts('Edit'),
179 'url' => 'civicrm/contact/add',
180 'qs' => 'reset=1&action=update&cid=%%id%%',
181 'class' => 'no-popup',
182 'title' => ts('Edit Contact Details'),
183 ],
184 ];
185
186 $config = CRM_Core_Config::singleton();
187 //CRM-16552: mapAPIKey is not mandatory as google no longer requires an API Key
188 if ($config->mapProvider && ($config->mapAPIKey || $config->mapProvider == 'Google')) {
189 self::$_links[CRM_Core_Action::MAP] = [
190 'name' => ts('Map'),
191 'url' => 'civicrm/contact/map',
192 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
193 'class' => 'no-popup',
194 'title' => ts('Map Contact'),
195 ];
196 }
197 }
198 return self::$_links;
199 }
200
201 /**
202 * Getter for array of the parameters required for creating pager.
203 *
204 * @param $action
205 * @param array $params
206 */
207 public function getPagerParams($action, &$params) {
208 $params['status'] = ts('Contact %%StatusMessage%%');
209 $params['csvString'] = NULL;
210 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
211
212 $params['buttonTop'] = 'PagerTopButton';
213 $params['buttonBottom'] = 'PagerBottomButton';
214 }
215
216 /**
217 * Returns the column headers as an array of tuples.
218 *
219 * Keys are name, sortName, key to the sort array.
220 *
221 * @param string $action
222 * The action being performed.
223 * @param string $output
224 * What should the result set include (web/email/csv).
225 *
226 * @return array
227 * the column headers that need to be displayed
228 */
229 public function &getColumnHeaders($action = NULL, $output = NULL) {
230 $columns = $this->_search->columns();
231 $headers = [];
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;
237 }
238 else {
239 foreach ($columns as $name => $key) {
240 if (!empty($name)) {
241 $headers[] = [
242 'name' => $name,
243 'sort' => $key,
244 'direction' => CRM_Utils_Sort::ASCENDING,
245 ];
246 }
247 else {
248 $headers[] = [];
249 }
250 }
251 return $headers;
252 }
253 }
254
255 /**
256 * Returns total number of rows for the query.
257 *
258 * @param null $action
259 *
260 * @return int
261 * Total number of rows
262 */
263 public function getTotalCount($action) {
264 return $this->_search->count();
265 }
266
267 /**
268 * Returns all the rows in the given offset and rowCount.
269 *
270 * @param string $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 string $output
279 * What should the result set include (web/email/csv).
280 *
281 * @return int
282 * the total number of rows for this action
283 */
284 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
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);
296 // contact query object used for creating $sql
297 $contactQueryObj = NULL;
298 if (method_exists($this->_search, 'getQueryObj') &&
299 is_a($this->_search->getQueryObj(), 'CRM_Contact_BAO_Query')
300 ) {
301 $contactQueryObj = $this->_search->getQueryObj();
302 }
303
304 $dao = CRM_Core_DAO::executeQuery($sql);
305
306 $columns = $this->_search->columns();
307 $columnNames = array_values($columns);
308 $links = self::links($this->_key);
309
310 $permissions = [CRM_Core_Permission::getPermission()];
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,
318 'alterRow'
319 )) {
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
327 $rows = [];
328 while ($dao->fetch()) {
329 $row = [];
330 $empty = TRUE;
331
332 // if contact query object present
333 // process pseudo constants
334 if ($contactQueryObj) {
335 $contactQueryObj->convertToPseudoNames($dao);
336 }
337
338 // the columns we are interested in
339 foreach ($columnNames as $property) {
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)) {
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,
353 ['id' => $contactID],
354 ts('more'),
355 FALSE,
356 'contact.custom.actions',
357 'Contact',
358 $contactID
359 );
360 $row['contact_id'] = $contactID;
361
362 if ($alterRow) {
363 $this->_search->alterRow($row);
364 }
365
366 if ($image) {
367 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ? $dao->contact_sub_type : $dao->contact_type, FALSE, $contactID
368 );
369 }
370 $rows[] = $row;
371 }
372 }
373
374 $this->buildPrevNextCache($sort);
375
376 return $rows;
377 }
378
379 /**
380 * Given the current formValues, gets the query in local language.
381 *
382 * @return array
383 * which contains an array of strings
384 */
385 public function getQILL() {
386 return NULL;
387 }
388
389 /**
390 * Get summary.
391 *
392 * @return mixed
393 */
394 public function getSummary() {
395 return $this->_search->summary();
396 }
397
398 /**
399 * Name of export file.
400 *
401 * @param string $output
402 * Type of output.
403 *
404 * @return string
405 * name of the file
406 */
407 public function getExportFileName($output = 'csv') {
408 return ts('CiviCRM Custom Search');
409 }
410
411 /**
412 * Do nothing.
413 *
414 * @return null
415 */
416 public function alphabetQuery() {
417 return NULL;
418 }
419
420 /**
421 * Generate contact ID query.
422 *
423 * @param array $params
424 * @param $action
425 * @param int $sortID
426 * @param null $displayRelationshipType
427 * @param string $queryOperator
428 *
429 * @return Object
430 */
431 public function contactIDQuery($params, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
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:
436 $matches = [];
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 }
447
448 $sql = $this->_search->contactIDs(0, 0, $sort);
449 return CRM_Core_DAO::executeQuery($sql);
450 }
451
452 /**
453 * Add actions.
454 *
455 * @param array $rows
456 */
457 public function addActions(&$rows) {
458 $links = self::links($this->_key);
459
460 $permissions = [CRM_Core_Permission::getPermission()];
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,
469 ['id' => $row['contact_id']],
470 ts('more'),
471 FALSE,
472 'contact.custom.actions',
473 'Contact',
474 $row['contact_id']
475 );
476 }
477 }
478
479 /**
480 * Remove actions.
481 *
482 * @param array $rows
483 */
484 public function removeActions(&$rows) {
485 foreach ($rows as $rid => & $rValue) {
486 unset($rValue['action']);
487 }
488 }
489
490 }