3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 static $_links = NULL;
50 * We use desc to remind us what that column is, name is used in the tpl
54 static $_columnHeaders;
57 * Properties of contact we're interested in displaying
60 static $_properties = array('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
98 protected $_customSearchClass;
103 * @param $customSearchClass
104 * @param array $formValues
105 * Array of form values imported.
106 * @param array $params
107 * Array of parameters for query.
108 * @param null $returnProperties
109 * @param \const|int $action - action of search basic or advanced.
111 * @param bool $includeContactIds
112 * @param bool $searchChildGroups
113 * @param string $searchContext
114 * @param null $contextMenu
116 * @return \CRM_Contact_Selector_Custom
118 public function __construct(
122 $returnProperties = NULL,
123 $action = CRM_Core_Action
::NONE
,
124 $includeContactIds = FALSE,
125 $searchChildGroups = TRUE,
126 $searchContext = 'search',
129 $this->_customSearchClass
= $customSearchClass;
130 $this->_formValues
= $formValues;
131 $this->_includeContactIds
= $includeContactIds;
133 $ext = CRM_Extension_System
::singleton()->getMapper();
135 if (!$ext->isExtensionKey($customSearchClass)) {
136 if ($ext->isExtensionClass($customSearchClass)) {
137 $customSearchFile = $ext->classToPath($customSearchClass);
138 require_once $customSearchFile;
141 require_once str_replace('_', DIRECTORY_SEPARATOR
, $customSearchClass) . '.php';
143 $this->_search
= new $customSearchClass($formValues);
146 $fnName = $ext->keyToPath
;
147 $customSearchFile = $fnName($customSearchClass, 'search');
148 $className = $ext->keyToClass($customSearchClass, 'search');
149 $this->_search
= new $className($formValues);
154 * This method returns the links that are given for each search row.
156 * Currently the links added for each row are
162 public static function &links() {
163 list($key) = func_get_args();
164 $searchContext = "&context=custom";
165 $extraParams = ($key) ?
"&key={$key}" : NULL;
167 if (!(self
::$_links)) {
168 self
::$_links = array(
169 CRM_Core_Action
::VIEW
=> array(
170 'name' => ts('View'),
171 'url' => 'civicrm/contact/view',
172 'qs' => "reset=1&cid=%%id%%{$extraParams}{$searchContext}",
173 'class' => 'no-popup',
174 'title' => ts('View Contact Details'),
176 CRM_Core_Action
::UPDATE
=> array(
177 'name' => ts('Edit'),
178 'url' => 'civicrm/contact/add',
179 'qs' => 'reset=1&action=update&cid=%%id%%',
180 'class' => 'no-popup',
181 'title' => ts('Edit Contact Details'),
185 $config = CRM_Core_Config
::singleton();
186 //CRM-16552: mapAPIKey is not mandatory as google no longer requires an API Key
187 if ($config->mapProvider
&& ($config->mapAPIKey ||
$config->mapProvider
== 'Google')) {
188 self
::$_links[CRM_Core_Action
::MAP
] = array(
190 'url' => 'civicrm/contact/map',
191 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
192 'class' => 'no-popup',
193 'title' => ts('Map Contact'),
197 return self
::$_links;
201 * Getter for array of the parameters required for creating pager.
204 * @param array $params
206 public function getPagerParams($action, &$params) {
207 $params['status'] = ts('Contact %%StatusMessage%%');
208 $params['csvString'] = NULL;
209 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
211 $params['buttonTop'] = 'PagerTopButton';
212 $params['buttonBottom'] = 'PagerBottomButton';
216 * Returns the column headers as an array of tuples.
218 * Keys are name, sortName, key to the sort array.
220 * @param string $action
221 * The action being performed.
222 * @param string $output
223 * What should the result set include (web/email/csv).
226 * the column headers that need to be displayed
228 public function &getColumnHeaders($action = NULL, $output = NULL) {
229 $columns = $this->_search
->columns();
230 if ($output == CRM_Core_Selector_Controller
::EXPORT
) {
231 return array_keys($columns);
235 foreach ($columns as $name => $key) {
240 'direction' => CRM_Utils_Sort
::ASCENDING
,
244 $headers[] = array();
252 * Returns total number of rows for the query.
254 * @param null $action
257 * Total number of rows
259 public function getTotalCount($action) {
260 return $this->_search
->count();
264 * Returns all the rows in the given offset and rowCount.
266 * @param string $action
267 * The action being performed.
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.
274 * @param string $output
275 * What should the result set include (web/email/csv).
278 * the total number of rows for this action
280 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
282 $includeContactIDs = FALSE;
283 if (($output == CRM_Core_Selector_Controller
::EXPORT ||
284 $output == CRM_Core_Selector_Controller
::SCREEN
286 $this->_formValues
['radio_ts'] == 'ts_sel'
288 $includeContactIDs = TRUE;
291 $sql = $this->_search
->all($offset, $rowCount, $sort, $includeContactIDs);
292 // contact query object used for creating $sql
293 $contactQueryObj = NULL;
294 if (method_exists($this->_search
, 'getQueryObj') &&
295 is_a($this->_search
->getQueryObj(), 'CRM_Contact_BAO_Query')
297 $contactQueryObj = $this->_search
->getQueryObj();
300 $dao = CRM_Core_DAO
::executeQuery($sql, CRM_Core_DAO
::$_nullArray);
302 $columns = $this->_search
->columns();
303 $columnNames = array_values($columns);
304 $links = self
::links($this->_key
);
306 $permissions = array(CRM_Core_Permission
::getPermission());
307 if (CRM_Core_Permission
::check('delete contacts')) {
308 $permissions[] = CRM_Core_Permission
::DELETE
;
310 $mask = CRM_Core_Action
::mask($permissions);
313 if (method_exists($this->_customSearchClass
,
319 if (is_a($this->_search
, 'CRM_Contact_Form_Search_Custom_Basic')) {
322 // process the result of the query
324 while ($dao->fetch()) {
328 // if contact query object present
329 // process pseudo constants
330 if ($contactQueryObj) {
331 $contactQueryObj->convertToPseudoNames($dao);
334 // the columns we are interested in
335 foreach ($columnNames as $property) {
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)) {
344 $contactID = isset($dao->contact_id
) ?
$dao->contact_id
: NULL;
346 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $contactID;
347 $row['action'] = CRM_Core_Action
::formLink($links,
349 array('id' => $contactID),
352 'contact.custom.actions',
356 $row['contact_id'] = $contactID;
359 $this->_search
->alterRow($row);
363 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($dao->contact_sub_type ?
$dao->contact_sub_type
: $dao->contact_type
, FALSE, $contactID
370 $this->buildPrevNextCache($sort);
376 * Given the current formValues, gets the query in local language.
379 * which contains an array of strings
381 public function getQILL() {
390 public function getSummary() {
391 return $this->_search
->summary();
395 * Name of export file.
397 * @param string $output
403 public function getExportFileName($output = 'csv') {
404 return ts('CiviCRM Custom Search');
412 public function alphabetQuery() {
417 * Generate contact ID query.
419 * @param array $params
422 * @param null $displayRelationshipType
423 * @param string $queryOperator
427 public function contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
428 // $action, $displayRelationshipType and $queryOperator are unused. I have
429 // no idea why they are there.
431 // I wonder whether there is some helper function for this:
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') {
444 $sql = $this->_search
->contactIDs(0, 0, $sort);
445 return CRM_Core_DAO
::executeQuery($sql);
453 public function addActions(&$rows) {
454 $links = self
::links($this->_key
);
456 $permissions = array(CRM_Core_Permission
::getPermission());
457 if (CRM_Core_Permission
::check('delete contacts')) {
458 $permissions[] = CRM_Core_Permission
::DELETE
;
460 $mask = CRM_Core_Action
::mask($permissions);
462 foreach ($rows as $id => & $row) {
463 $row['action'] = CRM_Core_Action
::formLink($links,
465 array('id' => $row['contact_id']),
468 'contact.custom.actions',
480 public function removeActions(&$rows) {
481 foreach ($rows as $rid => & $rValue) {
482 unset($rValue['action']);