copyright and version fixes
[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 *
112 * @param array $formValues array of form values imported
113 * @param array $params array of parameters for query
114 * @param int $action - action of search basic or advanced.
115 *
116 * @return CRM_Contact_Selector
117 * @access public
118 */
119 function __construct(
120 $customSearchClass,
121 $formValues = NULL,
122 $params = NULL,
123 $returnProperties = NULL,
124 $action = CRM_Core_Action::NONE,
125 $includeContactIds = FALSE,
126 $searchChildGroups = TRUE,
127 $searchContext = 'search',
128 $contextMenu = NULL
129 ) {
130 $this->_customSearchClass = $customSearchClass;
131 $this->_formValues = $formValues;
132 $this->_includeContactIds = $includeContactIds;
133
134 $ext = CRM_Extension_System::singleton()->getMapper();
135
136 if (!$ext->isExtensionKey($customSearchClass)) {
137 if ($ext->isExtensionClass($customSearchClass)) {
138 $customSearchFile = $ext->classToPath($customSearchClass);
139 require_once ($customSearchFile);
140 }
141 else {
142 require_once (str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php');
143 }
4d5c2eb5 144 $this->_search = new $customSearchClass( $formValues );
6a488035
TO
145 }
146 else {
c7b8b4e4
DL
147 $fnName = $ext->keyToPath;
148 $customSearchFile = $fnName($customSearchClass, 'search');
149 $className = $ext->keyToClass($customSearchClass, 'search');
150 $this->_search = new $className($formValues);
6a488035
TO
151 }
152 }
153 //end of constructor
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 * @access public
164 *
165 */
166 static function &links() {
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%%',
173 'title' => ts('View Contact Details'),
174 ),
175 CRM_Core_Action::UPDATE => array(
176 'name' => ts('Edit'),
177 'url' => 'civicrm/contact/add',
178 'qs' => 'reset=1&action=update&cid=%%id%%',
179 'title' => ts('Edit Contact Details'),
180 ),
181 );
182
183 $config = CRM_Core_Config::singleton();
184 if ($config->mapAPIKey && $config->mapProvider) {
185 self::$_links[CRM_Core_Action::MAP] = array(
186 'name' => ts('Map'),
187 'url' => 'civicrm/contact/map',
188 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
189 'title' => ts('Map Contact'),
190 );
191 }
192 }
193 return self::$_links;
194 }
195 //end of function
196
197 /**
198 * getter for array of the parameters required for creating pager.
199 *
200 * @param
201 * @access public
202 */
203 function getPagerParams($action, &$params) {
204 $params['status'] = ts('Contact %%StatusMessage%%');
205 $params['csvString'] = NULL;
206 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
207
208 $params['buttonTop'] = 'PagerTopButton';
209 $params['buttonBottom'] = 'PagerBottomButton';
210 }
211 //end of function
212
213 /**
214 * returns the column headers as an array of tuples:
215 * (name, sortName (key to the sort array))
216 *
217 * @param string $action the action being performed
218 * @param enum $output what should the result set include (web/email/csv)
219 *
220 * @return array the column headers that need to be displayed
221 * @access public
222 */
223 function &getColumnHeaders($action = NULL, $output = NULL) {
224 $columns = $this->_search->columns();
225 if ($output == CRM_Core_Selector_Controller::EXPORT) {
226 return array_keys($columns);
227 }
228 else {
229 $headers = array();
230 foreach ($columns as $name => $key) {
231 if (!empty($name)) {
232 $headers[] = array(
233 'name' => $name,
234 'sort' => $key,
235 'direction' => CRM_Utils_Sort::ASCENDING,
236 );
237 }
238 else {
239 $headers[] = array();
240 }
241 }
242 return $headers;
243 }
244 }
245
246 /**
247 * Returns total number of rows for the query.
248 *
249 * @param
250 *
251 * @return int Total number of rows
252 * @access public
253 */
254 function getTotalCount($action) {
255 return $this->_search->count();
256 }
257
258 /**
259 * returns all the rows in the given offset and rowCount
260 *
261 * @param enum $action the action being performed
262 * @param int $offset the row number to start from
263 * @param int $rowCount the number of rows to return
264 * @param string $sort the sql string that describes the sort order
265 * @param enum $output what should the result set include (web/email/csv)
266 *
267 * @return int the total number of rows for this action
268 */
269 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
270
271 $includeContactIDs = FALSE;
272 if (($output == CRM_Core_Selector_Controller::EXPORT ||
273 $output == CRM_Core_Selector_Controller::SCREEN
274 ) &&
275 $this->_formValues['radio_ts'] == 'ts_sel'
276 ) {
277 $includeContactIDs = TRUE;
278 }
279
280 $sql = $this->_search->all($offset, $rowCount, $sort, $includeContactIDs);
d9ab802d
PJ
281 // contact query object used for creating $sql
282 $contactQueryObj = NULL;
283 if (method_exists($this->_search, 'getQueryObj') &&
284 is_a($this->_search->getQueryObj(), 'CRM_Contact_BAO_Query')) {
285 $contactQueryObj = $this->_search->getQueryObj();
286 }
6a488035
TO
287
288 $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
289
290 $columns = $this->_search->columns();
291 $columnNames = array_values($columns);
292 $links = self::links();
293
294 $permissions = array(CRM_Core_Permission::getPermission());
295 if (CRM_Core_Permission::check('delete contacts')) {
296 $permissions[] = CRM_Core_Permission::DELETE;
297 }
298 $mask = CRM_Core_Action::mask($permissions);
299
300 $alterRow = FALSE;
301 if (method_exists($this->_customSearchClass,
302 'alterRow'
303 )) {
304 $alterRow = TRUE;
305 }
306 $image = FALSE;
307 if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_Basic')) {
308 $image = TRUE;
309 }
310 // process the result of the query
311 $rows = array();
312 while ($dao->fetch()) {
313 $row = array();
314 $empty = TRUE;
315
d9ab802d
PJ
316 // if contact query object present
317 // process pseudo constants
318 if ($contactQueryObj) {
319 $contactQueryObj->convertToPseudoNames($dao);
320 }
321
6a488035
TO
322 // the columns we are interested in
323 foreach ($columnNames as $property) {
324 $row[$property] = $dao->$property;
325 if (!empty($dao->$property)) {
326 $empty = FALSE;
327 }
328 }
329 if (!$empty) {
330 $contactID = isset($dao->contact_id) ? $dao->contact_id : NULL;
331
332 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $contactID;
333 $row['action'] = CRM_Core_Action::formLink($links,
334 $mask,
87dab4a4
AH
335 array('id' => $contactID),
336 ts('more'),
337 FALSE,
338 'contact.custom.actions',
339 'Contact',
340 $contactID
6a488035
TO
341 );
342 $row['contact_id'] = $contactID;
343
344 if ($alterRow) {
345 $this->_search->alterRow($row);
346 }
347
348 if ($image) {
349 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ?
350 $dao->contact_sub_type : $dao->contact_type, FALSE, $contactID
351 );
352 }
353 $rows[] = $row;
354 }
355 }
356
357 $this->buildPrevNextCache($sort);
358
359 return $rows;
360 }
361
362 /**
363 * Given the current formValues, gets the query in local
364 * language
365 *
366 * @param array(
367 reference) $formValues submitted formValues
368 *
369 * @return array $qill which contains an array of strings
370 * @access public
371 */
372 public function getQILL() {
373 return NULL;
374 }
375
376 public function getSummary() {
377 return $this->_search->summary();
378 }
379
380 /**
381 * name of export file.
382 *
383 * @param string $output type of output
384 *
385 * @return string name of the file
386 */
387 function getExportFileName($output = 'csv') {
388 return ts('CiviCRM Custom Search');
389 }
390
391 function alphabetQuery() {
392 return NULL;
393 }
394
395 function &contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
396 $params = array();
397 $sql = $this->_search->contactIDs($params);
398
399 return CRM_Core_DAO::executeQuery($sql, $params);
400 }
401
402 function addActions(&$rows) {
403 $links = self::links();
404
405 $permissions = array(CRM_Core_Permission::getPermission());
406 if (CRM_Core_Permission::check('delete contacts')) {
407 $permissions[] = CRM_Core_Permission::DELETE;
408 }
409 $mask = CRM_Core_Action::mask($permissions);
410
411 foreach ($rows as $id => & $row) {
412 $row['action'] = CRM_Core_Action::formLink($links,
413 $mask,
87dab4a4
AH
414 array('id' => $row['contact_id']),
415 ts('more'),
416 FALSE,
417 'contact.custom.actions',
418 'Contact',
419 $row['contact_id']
6a488035
TO
420 );
421 }
422 }
423
424 function removeActions(&$rows) {
425 foreach ($rows as $rid => & $rValue) {
426 unset($rValue['action']);
427 }
428 }
429}
430