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