bulk comment fix
[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() {
174 if (!(self::$_links)) {
175 self::$_links = array(
176 CRM_Core_Action::VIEW => array(
177 'name' => ts('View'),
178 'url' => 'civicrm/contact/view',
179 'qs' => 'reset=1&cid=%%id%%',
180 'title' => ts('View Contact Details'),
181 ),
182 CRM_Core_Action::UPDATE => array(
183 'name' => ts('Edit'),
184 'url' => 'civicrm/contact/add',
185 'qs' => 'reset=1&action=update&cid=%%id%%',
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 'title' => ts('Map Contact'),
197 );
198 }
199 }
200 return self::$_links;
201 }
202 //end of function
203
204 /**
205 * getter for array of the parameters required for creating pager.
206 *
da6b46f4
EM
207 * @param $action
208 * @param $params
209 *
210 * @internal param $
6a488035
TO
211 * @access public
212 */
213 function getPagerParams($action, &$params) {
214 $params['status'] = ts('Contact %%StatusMessage%%');
215 $params['csvString'] = NULL;
216 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
217
218 $params['buttonTop'] = 'PagerTopButton';
219 $params['buttonBottom'] = 'PagerBottomButton';
220 }
221 //end of function
222
223 /**
224 * returns the column headers as an array of tuples:
225 * (name, sortName (key to the sort array))
226 *
227 * @param string $action the action being performed
228 * @param enum $output what should the result set include (web/email/csv)
229 *
230 * @return array the column headers that need to be displayed
231 * @access public
232 */
233 function &getColumnHeaders($action = NULL, $output = NULL) {
234 $columns = $this->_search->columns();
235 if ($output == CRM_Core_Selector_Controller::EXPORT) {
236 return array_keys($columns);
237 }
238 else {
239 $headers = array();
240 foreach ($columns as $name => $key) {
241 if (!empty($name)) {
242 $headers[] = array(
243 'name' => $name,
244 'sort' => $key,
245 'direction' => CRM_Utils_Sort::ASCENDING,
246 );
247 }
248 else {
249 $headers[] = array();
250 }
251 }
252 return $headers;
253 }
254 }
255
256 /**
257 * Returns total number of rows for the query.
258 *
259 * @param
260 *
261 * @return int Total number of rows
262 * @access public
263 */
264 function getTotalCount($action) {
265 return $this->_search->count();
266 }
267
268 /**
269 * returns all the rows in the given offset and rowCount
270 *
271 * @param enum $action the action being performed
272 * @param int $offset the row number to start from
273 * @param int $rowCount the number of rows to return
274 * @param string $sort the sql string that describes the sort order
275 * @param enum $output what should the result set include (web/email/csv)
276 *
277 * @return int the total number of rows for this action
278 */
279 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
280
281 $includeContactIDs = FALSE;
282 if (($output == CRM_Core_Selector_Controller::EXPORT ||
283 $output == CRM_Core_Selector_Controller::SCREEN
284 ) &&
285 $this->_formValues['radio_ts'] == 'ts_sel'
286 ) {
287 $includeContactIDs = TRUE;
288 }
289
290 $sql = $this->_search->all($offset, $rowCount, $sort, $includeContactIDs);
d9ab802d
PJ
291 // contact query object used for creating $sql
292 $contactQueryObj = NULL;
293 if (method_exists($this->_search, 'getQueryObj') &&
294 is_a($this->_search->getQueryObj(), 'CRM_Contact_BAO_Query')) {
295 $contactQueryObj = $this->_search->getQueryObj();
296 }
6a488035
TO
297
298 $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
299
300 $columns = $this->_search->columns();
301 $columnNames = array_values($columns);
302 $links = self::links();
303
304 $permissions = array(CRM_Core_Permission::getPermission());
305 if (CRM_Core_Permission::check('delete contacts')) {
306 $permissions[] = CRM_Core_Permission::DELETE;
307 }
308 $mask = CRM_Core_Action::mask($permissions);
309
310 $alterRow = FALSE;
311 if (method_exists($this->_customSearchClass,
312 'alterRow'
313 )) {
314 $alterRow = TRUE;
315 }
316 $image = FALSE;
317 if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_Basic')) {
318 $image = TRUE;
319 }
320 // process the result of the query
321 $rows = array();
322 while ($dao->fetch()) {
323 $row = array();
324 $empty = TRUE;
325
d9ab802d
PJ
326 // if contact query object present
327 // process pseudo constants
328 if ($contactQueryObj) {
329 $contactQueryObj->convertToPseudoNames($dao);
330 }
331
6a488035
TO
332 // the columns we are interested in
333 foreach ($columnNames as $property) {
334 $row[$property] = $dao->$property;
335 if (!empty($dao->$property)) {
336 $empty = FALSE;
337 }
338 }
339 if (!$empty) {
340 $contactID = isset($dao->contact_id) ? $dao->contact_id : NULL;
341
342 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $contactID;
343 $row['action'] = CRM_Core_Action::formLink($links,
344 $mask,
87dab4a4
AH
345 array('id' => $contactID),
346 ts('more'),
347 FALSE,
348 'contact.custom.actions',
349 'Contact',
350 $contactID
6a488035
TO
351 );
352 $row['contact_id'] = $contactID;
353
354 if ($alterRow) {
355 $this->_search->alterRow($row);
356 }
357
358 if ($image) {
359 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ?
360 $dao->contact_sub_type : $dao->contact_type, FALSE, $contactID
361 );
362 }
363 $rows[] = $row;
364 }
365 }
366
367 $this->buildPrevNextCache($sort);
368
369 return $rows;
370 }
371
372 /**
373 * Given the current formValues, gets the query in local
374 * language
375 *
376 * @param array(
377 reference) $formValues submitted formValues
378 *
379 * @return array $qill which contains an array of strings
380 * @access public
381 */
382 public function getQILL() {
383 return NULL;
384 }
385
386 public function getSummary() {
387 return $this->_search->summary();
388 }
389
390 /**
391 * name of export file.
392 *
393 * @param string $output type of output
394 *
395 * @return string name of the file
396 */
397 function getExportFileName($output = 'csv') {
398 return ts('CiviCRM Custom Search');
399 }
400
401 function alphabetQuery() {
402 return NULL;
403 }
404
405 function &contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
406 $params = array();
407 $sql = $this->_search->contactIDs($params);
408
409 return CRM_Core_DAO::executeQuery($sql, $params);
410 }
411
412 function addActions(&$rows) {
413 $links = self::links();
414
415 $permissions = array(CRM_Core_Permission::getPermission());
416 if (CRM_Core_Permission::check('delete contacts')) {
417 $permissions[] = CRM_Core_Permission::DELETE;
418 }
419 $mask = CRM_Core_Action::mask($permissions);
420
421 foreach ($rows as $id => & $row) {
422 $row['action'] = CRM_Core_Action::formLink($links,
423 $mask,
87dab4a4
AH
424 array('id' => $row['contact_id']),
425 ts('more'),
426 FALSE,
427 'contact.custom.actions',
428 'Contact',
429 $row['contact_id']
6a488035
TO
430 );
431 }
432 }
433
434 function removeActions(&$rows) {
435 foreach ($rows as $rid => & $rValue) {
436 unset($rValue['action']);
437 }
438 }
439}
440