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