3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id: Selector.php 11510 2007-09-18 09:21:34Z lobo $
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.
40 class CRM_Contact_Selector_Custom
extends CRM_Contact_Selector
{
43 * This defines two actions- View and Edit.
47 public static $_links = NULL;
50 * We use desc to remind us what that column is, name is used in the tpl
54 public static $_columnHeaders;
57 * Properties of contact we're interested in displaying
60 public static $_properties = ['contact_id', 'contact_type', 'display_name'];
63 * FormValues is the array returned by exportValues called on
64 * the HTML_QuickForm_Controller for that page.
71 * Params is the array in a value used by the search query creator
78 * Represent the type of selector
87 * The public visible fields to be shown to the user
94 * The object that implements the search interface
99 protected $_customSearchClass;
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.
112 * @param bool $includeContactIds
113 * @param bool $searchChildGroups
114 * @param string $searchContext
115 * @param null $contextMenu
117 * @return \CRM_Contact_Selector_Custom
119 public function __construct(
123 $returnProperties = NULL,
124 $action = CRM_Core_Action
::NONE
,
125 $includeContactIds = FALSE,
126 $searchChildGroups = TRUE,
127 $searchContext = 'search',
130 $this->_customSearchClass
= $customSearchClass;
131 $this->_formValues
= $formValues;
132 $this->_includeContactIds
= $includeContactIds;
134 $ext = CRM_Extension_System
::singleton()->getMapper();
136 if (!$ext->isExtensionKey($customSearchClass)) {
137 if ($ext->isExtensionClass($customSearchClass)) {
138 $customSearchFile = $ext->classToPath($customSearchClass);
139 require_once $customSearchFile;
142 require_once str_replace('_', DIRECTORY_SEPARATOR
, $customSearchClass) . '.php';
144 $this->_search
= new $customSearchClass($formValues);
147 $fnName = $ext->keyToPath
;
148 $customSearchFile = $fnName($customSearchClass, 'search');
149 $className = $ext->keyToClass($customSearchClass, 'search');
150 $this->_search
= new $className($formValues);
155 * This method returns the links that are given for each search row.
157 * Currently the links added for each row are
163 public static function &links() {
164 list($key) = func_get_args();
165 $searchContext = "&context=custom";
166 $extraParams = ($key) ?
"&key={$key}" : NULL;
168 if (!(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'),
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'),
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
] = [
191 'url' => 'civicrm/contact/map',
192 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
193 'class' => 'no-popup',
194 'title' => ts('Map Contact'),
198 return self
::$_links;
202 * Getter for array of the parameters required for creating pager.
205 * @param array $params
207 public function getPagerParams($action, &$params) {
208 $params['status'] = ts('Contact %%StatusMessage%%');
209 $params['csvString'] = NULL;
210 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
212 $params['buttonTop'] = 'PagerTopButton';
213 $params['buttonBottom'] = 'PagerBottomButton';
217 * Returns the column headers as an array of tuples.
219 * Keys are name, sortName, key to the sort array.
221 * @param string $action
222 * The action being performed.
223 * @param string $output
224 * What should the result set include (web/email/csv).
227 * the column headers that need to be displayed
229 public function &getColumnHeaders($action = NULL, $output = NULL) {
230 $columns = $this->_search
->columns();
232 if ($output == CRM_Core_Selector_Controller
::EXPORT ||
$output == CRM_Core_Selector_Controller
::SCREEN
) {
233 foreach ($columns as $name => $key) {
234 $headers[$key] = $name;
239 foreach ($columns as $name => $key) {
244 'direction' => CRM_Utils_Sort
::ASCENDING
,
256 * Returns total number of rows for the query.
258 * @param null $action
261 * Total number of rows
263 public function getTotalCount($action) {
264 return $this->_search
->count();
268 * Returns all the rows in the given offset and rowCount.
270 * @param string $action
271 * The action being performed.
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).
282 * the total number of rows for this action
284 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
286 $includeContactIDs = FALSE;
287 if (($output == CRM_Core_Selector_Controller
::EXPORT ||
288 $output == CRM_Core_Selector_Controller
::SCREEN
290 $this->_formValues
['radio_ts'] == 'ts_sel'
292 $includeContactIDs = TRUE;
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')
301 $contactQueryObj = $this->_search
->getQueryObj();
304 $dao = CRM_Core_DAO
::executeQuery($sql);
306 $columns = $this->_search
->columns();
307 $columnNames = array_values($columns);
308 $links = self
::links($this->_key
);
310 $permissions = [CRM_Core_Permission
::getPermission()];
311 if (CRM_Core_Permission
::check('delete contacts')) {
312 $permissions[] = CRM_Core_Permission
::DELETE
;
314 $mask = CRM_Core_Action
::mask($permissions);
317 if (method_exists($this->_customSearchClass
,
323 if (is_a($this->_search
, 'CRM_Contact_Form_Search_Custom_Basic')) {
326 // process the result of the query
328 while ($dao->fetch()) {
332 // if contact query object present
333 // process pseudo constants
334 if ($contactQueryObj) {
335 $contactQueryObj->convertToPseudoNames($dao);
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)) {
348 $contactID = isset($dao->contact_id
) ?
$dao->contact_id
: NULL;
350 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $contactID;
351 $row['action'] = CRM_Core_Action
::formLink($links,
353 ['id' => $contactID],
356 'contact.custom.actions',
360 $row['contact_id'] = $contactID;
363 $this->_search
->alterRow($row);
367 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($dao->contact_sub_type ?
$dao->contact_sub_type
: $dao->contact_type
, FALSE, $contactID
374 $this->buildPrevNextCache($sort);
380 * Given the current formValues, gets the query in local language.
383 * which contains an array of strings
385 public function getQILL() {
394 public function getSummary() {
395 return $this->_search
->summary();
399 * Name of export file.
401 * @param string $output
407 public function getExportFileName($output = 'csv') {
408 return ts('CiviCRM Custom Search');
416 public function alphabetQuery() {
421 * Generate contact ID query.
423 * @param array $params
426 * @param null $displayRelationshipType
427 * @param string $queryOperator
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.
435 // I wonder whether there is some helper function for this:
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') {
448 $sql = $this->_search
->contactIDs(0, 0, $sort);
449 return CRM_Core_DAO
::executeQuery($sql);
457 public function addActions(&$rows) {
458 $links = self
::links($this->_key
);
460 $permissions = [CRM_Core_Permission
::getPermission()];
461 if (CRM_Core_Permission
::check('delete contacts')) {
462 $permissions[] = CRM_Core_Permission
::DELETE
;
464 $mask = CRM_Core_Action
::mask($permissions);
466 foreach ($rows as $id => & $row) {
467 $row['action'] = CRM_Core_Action
::formLink($links,
469 ['id' => $row['contact_id']],
472 'contact.custom.actions',
484 public function removeActions(&$rows) {
485 foreach ($rows as $rid => & $rValue) {
486 unset($rValue['action']);