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