Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 | 32 | * $Id: Selector.php 11510 2007-09-18 09:21:34Z lobo $ |
6a488035 TO |
33 | */ |
34 | ||
35 | /** | |
36 | * This class is used to retrieve and display a range of | |
37 | * contacts that match the given criteria (specifically for | |
38 | * results of advanced search options. | |
6a488035 TO |
39 | */ |
40 | class CRM_Contact_Selector_Custom extends CRM_Contact_Selector { | |
41 | ||
42 | /** | |
43 | * This defines two actions- View and Edit. | |
44 | * | |
45 | * @var array | |
6a488035 TO |
46 | */ |
47 | static $_links = NULL; | |
48 | ||
49 | /** | |
100fef9d | 50 | * We use desc to remind us what that column is, name is used in the tpl |
6a488035 TO |
51 | * |
52 | * @var array | |
6a488035 TO |
53 | */ |
54 | static $_columnHeaders; | |
55 | ||
56 | /** | |
57 | * Properties of contact we're interested in displaying | |
58 | * @var array | |
6a488035 TO |
59 | */ |
60 | static $_properties = array('contact_id', 'contact_type', 'display_name'); | |
61 | ||
62 | /** | |
100fef9d | 63 | * FormValues is the array returned by exportValues called on |
6a488035 TO |
64 | * the HTML_QuickForm_Controller for that page. |
65 | * | |
66 | * @var array | |
6a488035 TO |
67 | */ |
68 | public $_formValues; | |
69 | ||
70 | /** | |
100fef9d | 71 | * Params is the array in a value used by the search query creator |
6a488035 TO |
72 | * |
73 | * @var array | |
6a488035 TO |
74 | */ |
75 | public $_params; | |
76 | ||
77 | /** | |
100fef9d | 78 | * Represent the type of selector |
6a488035 TO |
79 | * |
80 | * @var int | |
6a488035 TO |
81 | */ |
82 | protected $_action; | |
83 | ||
84 | protected $_query; | |
85 | ||
86 | /** | |
100fef9d | 87 | * The public visible fields to be shown to the user |
6a488035 TO |
88 | * |
89 | * @var array | |
6a488035 TO |
90 | */ |
91 | protected $_fields; | |
92 | ||
93 | /** | |
94 | * The object that implements the search interface | |
95 | */ | |
96 | protected $_search; | |
97 | ||
98 | protected $_customSearchClass; | |
99 | ||
100 | /** | |
5c9ff055 | 101 | * Class constructor. |
6a488035 | 102 | * |
6c8f6e67 | 103 | * @param $customSearchClass |
77c5b619 TO |
104 | * @param array $formValues |
105 | * Array of form values imported. | |
106 | * @param array $params | |
107 | * Array of parameters for query. | |
6c8f6e67 EM |
108 | * @param null $returnProperties |
109 | * @param \const|int $action - action of search basic or advanced. | |
6a488035 | 110 | * |
6c8f6e67 EM |
111 | * @param bool $includeContactIds |
112 | * @param bool $searchChildGroups | |
113 | * @param string $searchContext | |
114 | * @param null $contextMenu | |
115 | * | |
116 | * @return \CRM_Contact_Selector_Custom | |
6a488035 | 117 | */ |
db7de9c1 | 118 | public function __construct( |
6a488035 | 119 | $customSearchClass, |
ce80b209 TO |
120 | $formValues = NULL, |
121 | $params = NULL, | |
122 | $returnProperties = NULL, | |
123 | $action = CRM_Core_Action::NONE, | |
6a488035 TO |
124 | $includeContactIds = FALSE, |
125 | $searchChildGroups = TRUE, | |
ce80b209 TO |
126 | $searchContext = 'search', |
127 | $contextMenu = NULL | |
6a488035 TO |
128 | ) { |
129 | $this->_customSearchClass = $customSearchClass; | |
130 | $this->_formValues = $formValues; | |
131 | $this->_includeContactIds = $includeContactIds; | |
132 | ||
133 | $ext = CRM_Extension_System::singleton()->getMapper(); | |
134 | ||
135 | if (!$ext->isExtensionKey($customSearchClass)) { | |
136 | if ($ext->isExtensionClass($customSearchClass)) { | |
137 | $customSearchFile = $ext->classToPath($customSearchClass); | |
ce80b209 | 138 | require_once $customSearchFile; |
6a488035 TO |
139 | } |
140 | else { | |
ce80b209 | 141 | require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php'; |
6a488035 | 142 | } |
481a74f4 | 143 | $this->_search = new $customSearchClass($formValues); |
6a488035 TO |
144 | } |
145 | else { | |
c7b8b4e4 DL |
146 | $fnName = $ext->keyToPath; |
147 | $customSearchFile = $fnName($customSearchClass, 'search'); | |
148 | $className = $ext->keyToClass($customSearchClass, 'search'); | |
149 | $this->_search = new $className($formValues); | |
6a488035 TO |
150 | } |
151 | } | |
6a488035 TO |
152 | |
153 | /** | |
154 | * This method returns the links that are given for each search row. | |
6a488035 | 155 | * |
db7de9c1 | 156 | * Currently the links added for each row are |
6a488035 TO |
157 | * - View |
158 | * - Edit | |
159 | * | |
160 | * @return array | |
6a488035 | 161 | */ |
00be9182 | 162 | public static function &links() { |
7c34ab11 | 163 | list($key) = func_get_args(); |
119c08a8 | 164 | $searchContext = "&context=custom"; |
7c34ab11 | 165 | $extraParams = ($key) ? "&key={$key}" : NULL; |
166 | ||
6a488035 TO |
167 | if (!(self::$_links)) { |
168 | self::$_links = array( | |
169 | CRM_Core_Action::VIEW => array( | |
170 | 'name' => ts('View'), | |
171 | 'url' => 'civicrm/contact/view', | |
119c08a8 | 172 | 'qs' => "reset=1&cid=%%id%%{$extraParams}{$searchContext}", |
d3fcd52f | 173 | 'class' => 'no-popup', |
6a488035 TO |
174 | 'title' => ts('View Contact Details'), |
175 | ), | |
176 | CRM_Core_Action::UPDATE => array( | |
177 | 'name' => ts('Edit'), | |
178 | 'url' => 'civicrm/contact/add', | |
179 | 'qs' => 'reset=1&action=update&cid=%%id%%', | |
d3fcd52f | 180 | 'class' => 'no-popup', |
6a488035 TO |
181 | 'title' => ts('Edit Contact Details'), |
182 | ), | |
183 | ); | |
184 | ||
185 | $config = CRM_Core_Config::singleton(); | |
186 | if ($config->mapAPIKey && $config->mapProvider) { | |
187 | self::$_links[CRM_Core_Action::MAP] = array( | |
188 | 'name' => ts('Map'), | |
189 | 'url' => 'civicrm/contact/map', | |
190 | 'qs' => 'reset=1&cid=%%id%%&searchType=custom', | |
d3fcd52f | 191 | 'class' => 'no-popup', |
6a488035 TO |
192 | 'title' => ts('Map Contact'), |
193 | ); | |
194 | } | |
195 | } | |
196 | return self::$_links; | |
197 | } | |
6a488035 TO |
198 | |
199 | /** | |
100fef9d | 200 | * Getter for array of the parameters required for creating pager. |
6a488035 | 201 | * |
da6b46f4 | 202 | * @param $action |
c490a46a | 203 | * @param array $params |
6a488035 | 204 | */ |
00be9182 | 205 | public function getPagerParams($action, &$params) { |
353ffa53 | 206 | $params['status'] = ts('Contact %%StatusMessage%%'); |
6a488035 | 207 | $params['csvString'] = NULL; |
353ffa53 | 208 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; |
6a488035 TO |
209 | |
210 | $params['buttonTop'] = 'PagerTopButton'; | |
211 | $params['buttonBottom'] = 'PagerBottomButton'; | |
212 | } | |
6a488035 TO |
213 | |
214 | /** | |
db7de9c1 EM |
215 | * Returns the column headers as an array of tuples. |
216 | * | |
217 | * Keys are name, sortName, key to the sort array. | |
6a488035 | 218 | * |
77c5b619 TO |
219 | * @param string $action |
220 | * The action being performed. | |
3f8d2862 | 221 | * @param string $output |
77c5b619 | 222 | * What should the result set include (web/email/csv). |
6a488035 | 223 | * |
a6c01b45 CW |
224 | * @return array |
225 | * the column headers that need to be displayed | |
6a488035 | 226 | */ |
00be9182 | 227 | public function &getColumnHeaders($action = NULL, $output = NULL) { |
6a488035 TO |
228 | $columns = $this->_search->columns(); |
229 | if ($output == CRM_Core_Selector_Controller::EXPORT) { | |
230 | return array_keys($columns); | |
231 | } | |
232 | else { | |
233 | $headers = array(); | |
234 | foreach ($columns as $name => $key) { | |
235 | if (!empty($name)) { | |
236 | $headers[] = array( | |
237 | 'name' => $name, | |
238 | 'sort' => $key, | |
239 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
240 | ); | |
241 | } | |
242 | else { | |
243 | $headers[] = array(); | |
244 | } | |
245 | } | |
246 | return $headers; | |
247 | } | |
248 | } | |
249 | ||
250 | /** | |
251 | * Returns total number of rows for the query. | |
252 | * | |
ee0ce2ef | 253 | * @param null $action |
6a488035 | 254 | * |
a6c01b45 CW |
255 | * @return int |
256 | * Total number of rows | |
6a488035 | 257 | */ |
00be9182 | 258 | public function getTotalCount($action) { |
6a488035 TO |
259 | return $this->_search->count(); |
260 | } | |
261 | ||
262 | /** | |
d8689418 | 263 | * Returns all the rows in the given offset and rowCount. |
6a488035 | 264 | * |
3f8d2862 | 265 | * @param string $action |
77c5b619 TO |
266 | * The action being performed. |
267 | * @param int $offset | |
268 | * The row number to start from. | |
269 | * @param int $rowCount | |
270 | * The number of rows to return. | |
271 | * @param string $sort | |
272 | * The sql string that describes the sort order. | |
3f8d2862 | 273 | * @param string $output |
77c5b619 | 274 | * What should the result set include (web/email/csv). |
6a488035 | 275 | * |
a6c01b45 CW |
276 | * @return int |
277 | * the total number of rows for this action | |
6a488035 | 278 | */ |
00be9182 | 279 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { |
6a488035 TO |
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') && | |
353ffa53 TO |
294 | is_a($this->_search->getQueryObj(), 'CRM_Contact_BAO_Query') |
295 | ) { | |
d9ab802d PJ |
296 | $contactQueryObj = $this->_search->getQueryObj(); |
297 | } | |
6a488035 TO |
298 | |
299 | $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); | |
300 | ||
353ffa53 | 301 | $columns = $this->_search->columns(); |
6a488035 | 302 | $columnNames = array_values($columns); |
353ffa53 | 303 | $links = self::links($this->_key); |
6a488035 TO |
304 | |
305 | $permissions = array(CRM_Core_Permission::getPermission()); | |
306 | if (CRM_Core_Permission::check('delete contacts')) { | |
307 | $permissions[] = CRM_Core_Permission::DELETE; | |
308 | } | |
309 | $mask = CRM_Core_Action::mask($permissions); | |
310 | ||
311 | $alterRow = FALSE; | |
312 | if (method_exists($this->_customSearchClass, | |
353ffa53 TO |
313 | 'alterRow' |
314 | )) { | |
6a488035 TO |
315 | $alterRow = TRUE; |
316 | } | |
317 | $image = FALSE; | |
318 | if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_Basic')) { | |
319 | $image = TRUE; | |
320 | } | |
321 | // process the result of the query | |
322 | $rows = array(); | |
323 | while ($dao->fetch()) { | |
324 | $row = array(); | |
325 | $empty = TRUE; | |
326 | ||
d9ab802d PJ |
327 | // if contact query object present |
328 | // process pseudo constants | |
329 | if ($contactQueryObj) { | |
330 | $contactQueryObj->convertToPseudoNames($dao); | |
331 | } | |
332 | ||
6a488035 TO |
333 | // the columns we are interested in |
334 | foreach ($columnNames as $property) { | |
335 | $row[$property] = $dao->$property; | |
336 | if (!empty($dao->$property)) { | |
337 | $empty = FALSE; | |
338 | } | |
339 | } | |
340 | if (!$empty) { | |
341 | $contactID = isset($dao->contact_id) ? $dao->contact_id : NULL; | |
342 | ||
343 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $contactID; | |
344 | $row['action'] = CRM_Core_Action::formLink($links, | |
345 | $mask, | |
87dab4a4 AH |
346 | array('id' => $contactID), |
347 | ts('more'), | |
348 | FALSE, | |
349 | 'contact.custom.actions', | |
350 | 'Contact', | |
351 | $contactID | |
6a488035 TO |
352 | ); |
353 | $row['contact_id'] = $contactID; | |
354 | ||
355 | if ($alterRow) { | |
356 | $this->_search->alterRow($row); | |
357 | } | |
358 | ||
359 | if ($image) { | |
ce80b209 | 360 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ? $dao->contact_sub_type : $dao->contact_type, FALSE, $contactID |
6a488035 TO |
361 | ); |
362 | } | |
363 | $rows[] = $row; | |
364 | } | |
365 | } | |
366 | ||
367 | $this->buildPrevNextCache($sort); | |
368 | ||
369 | return $rows; | |
370 | } | |
371 | ||
372 | /** | |
d8689418 | 373 | * Given the current formValues, gets the query in local language. |
6a488035 | 374 | * |
a6c01b45 CW |
375 | * @return array |
376 | * which contains an array of strings | |
6a488035 TO |
377 | */ |
378 | public function getQILL() { | |
379 | return NULL; | |
380 | } | |
381 | ||
86538308 | 382 | /** |
5c9ff055 EM |
383 | * Get summary. |
384 | * | |
86538308 EM |
385 | * @return mixed |
386 | */ | |
6a488035 TO |
387 | public function getSummary() { |
388 | return $this->_search->summary(); | |
389 | } | |
390 | ||
391 | /** | |
100fef9d | 392 | * Name of export file. |
6a488035 | 393 | * |
77c5b619 TO |
394 | * @param string $output |
395 | * Type of output. | |
6a488035 | 396 | * |
a6c01b45 CW |
397 | * @return string |
398 | * name of the file | |
6a488035 | 399 | */ |
00be9182 | 400 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
401 | return ts('CiviCRM Custom Search'); |
402 | } | |
403 | ||
86538308 | 404 | /** |
5c9ff055 EM |
405 | * Do nothing. |
406 | * | |
86538308 EM |
407 | * @return null |
408 | */ | |
00be9182 | 409 | public function alphabetQuery() { |
6a488035 TO |
410 | return NULL; |
411 | } | |
412 | ||
86538308 | 413 | /** |
0965e988 EM |
414 | * Generate contact ID query. |
415 | * | |
c490a46a | 416 | * @param array $params |
86538308 | 417 | * @param $action |
100fef9d | 418 | * @param int $sortID |
86538308 EM |
419 | * @param null $displayRelationshipType |
420 | * @param string $queryOperator | |
421 | * | |
422 | * @return Object | |
423 | */ | |
0965e988 | 424 | public function contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') { |
6a488035 TO |
425 | $params = array(); |
426 | $sql = $this->_search->contactIDs($params); | |
427 | ||
428 | return CRM_Core_DAO::executeQuery($sql, $params); | |
429 | } | |
430 | ||
86538308 | 431 | /** |
0965e988 EM |
432 | * Add actions. |
433 | * | |
434 | * @param array $rows | |
86538308 | 435 | */ |
00be9182 | 436 | public function addActions(&$rows) { |
7c34ab11 | 437 | $links = self::links($this->_key); |
6a488035 TO |
438 | |
439 | $permissions = array(CRM_Core_Permission::getPermission()); | |
440 | if (CRM_Core_Permission::check('delete contacts')) { | |
441 | $permissions[] = CRM_Core_Permission::DELETE; | |
442 | } | |
443 | $mask = CRM_Core_Action::mask($permissions); | |
444 | ||
445 | foreach ($rows as $id => & $row) { | |
446 | $row['action'] = CRM_Core_Action::formLink($links, | |
447 | $mask, | |
87dab4a4 AH |
448 | array('id' => $row['contact_id']), |
449 | ts('more'), | |
450 | FALSE, | |
451 | 'contact.custom.actions', | |
452 | 'Contact', | |
453 | $row['contact_id'] | |
6a488035 TO |
454 | ); |
455 | } | |
456 | } | |
457 | ||
86538308 | 458 | /** |
0965e988 EM |
459 | * Remove actions. |
460 | * | |
461 | * @param array $rows | |
86538308 | 462 | */ |
00be9182 | 463 | public function removeActions(&$rows) { |
6a488035 TO |
464 | foreach ($rows as $rid => & $rValue) { |
465 | unset($rValue['action']); | |
466 | } | |
467 | } | |
96025800 | 468 | |
6a488035 | 469 | } |