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