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