Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
32 | * $Id$ | |
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 extends CRM_Core_Selector_Base implements CRM_Core_Selector_API { | |
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( | |
66 | 'contact_id', 'contact_type', 'contact_sub_type', | |
67 | 'sort_name', 'street_address', | |
68 | 'city', 'state_province', 'postal_code', 'country', | |
69 | 'geo_code_1', 'geo_code_2', 'is_deceased', | |
70 | 'email', 'on_hold', 'phone', 'status', | |
71 | 'do_not_email', 'do_not_phone', 'do_not_mail', | |
72 | ); | |
73 | ||
74 | /** | |
75 | * formValues is the array returned by exportValues called on | |
76 | * the HTML_QuickForm_Controller for that page. | |
77 | * | |
78 | * @var array | |
79 | * @access protected | |
80 | */ | |
81 | public $_formValues; | |
82 | ||
83 | /** | |
84 | * The contextMenu | |
85 | * | |
86 | * @var array | |
87 | * @access protected | |
88 | */ | |
89 | protected $_contextMenu; | |
90 | ||
91 | /** | |
92 | * params is the array in a value used by the search query creator | |
93 | * | |
94 | * @var array | |
95 | * @access protected | |
96 | */ | |
97 | public $_params; | |
98 | ||
99 | /** | |
100 | * The return properties used for search | |
101 | * | |
102 | * @var array | |
103 | * @access protected | |
104 | */ | |
105 | protected $_returnProperties; | |
106 | ||
107 | /** | |
108 | * represent the type of selector | |
109 | * | |
110 | * @var int | |
111 | * @access protected | |
112 | */ | |
113 | protected $_action; | |
114 | ||
115 | protected $_searchContext; | |
116 | ||
117 | protected $_query; | |
118 | ||
119 | /** | |
120 | * group id | |
121 | * | |
122 | * @var int | |
123 | */ | |
124 | protected $_ufGroupID; | |
125 | ||
126 | /** | |
127 | * the public visible fields to be shown to the user | |
128 | * | |
129 | * @var array | |
130 | * @access protected | |
131 | */ | |
132 | protected $_fields; | |
133 | ||
134 | /** | |
135 | * Class constructor | |
136 | * | |
137 | * @param array $formValues array of form values imported | |
138 | * @param array $params array of parameters for query | |
139 | * @param int $action - action of search basic or advanced. | |
140 | * | |
141 | * @return CRM_Contact_Selector | |
142 | * @access public | |
143 | */ | |
144 | function __construct( | |
145 | $customSearchClass, | |
146 | $formValues = NULL, | |
147 | $params = NULL, | |
148 | $returnProperties = NULL, | |
149 | $action = CRM_Core_Action::NONE, | |
150 | $includeContactIds = FALSE, | |
151 | $searchDescendentGroups = TRUE, | |
152 | $searchContext = 'search', | |
153 | $contextMenu = NULL | |
154 | ) { | |
155 | //don't build query constructor, if form is not submitted | |
156 | $force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject); | |
157 | if (empty($formValues) && !$force) { | |
158 | return; | |
159 | } | |
160 | ||
161 | // submitted form values | |
162 | $this->_formValues = &$formValues; | |
163 | $this->_params = &$params; | |
164 | $this->_returnProperties = &$returnProperties; | |
165 | $this->_contextMenu = &$contextMenu; | |
166 | $this->_context = $searchContext; | |
167 | ||
168 | // type of selector | |
169 | $this->_action = $action; | |
170 | ||
171 | $this->_searchContext = $searchContext; | |
172 | ||
173 | $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $this->_formValues); | |
174 | ||
175 | if ($this->_ufGroupID) { | |
176 | $this->_fields = CRM_Core_BAO_UFGroup::getListingFields(CRM_Core_Action::VIEW, | |
177 | CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | | |
178 | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY, | |
179 | FALSE, $this->_ufGroupID | |
180 | ); | |
181 | self::$_columnHeaders = NULL; | |
182 | ||
183 | $this->_customFields = CRM_Core_BAO_CustomField::getFieldsForImport('Individual'); | |
184 | ||
185 | $this->_returnProperties = CRM_Contact_BAO_Contact::makeHierReturnProperties($this->_fields); | |
186 | $this->_returnProperties['contact_type'] = 1; | |
187 | $this->_returnProperties['contact_sub_type'] = 1; | |
188 | $this->_returnProperties['sort_name'] = 1; | |
189 | } | |
190 | ||
191 | $displayRelationshipType = CRM_Utils_Array::value('display_relationship_type', $this->_formValues); | |
192 | $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND'); | |
193 | ||
194 | // rectify params to what proximity search expects if there is a value for prox_distance | |
195 | // CRM-7021 | |
196 | if (!empty($this->_params)) { | |
197 | CRM_Contact_BAO_ProximityQuery::fixInputParams($this->_params); | |
198 | } | |
199 | ||
200 | $this->_query = new CRM_Contact_BAO_Query( | |
201 | $this->_params, | |
202 | $this->_returnProperties, | |
203 | NULL, | |
204 | $includeContactIds, | |
205 | FALSE, | |
206 | CRM_Contact_BAO_Query::MODE_CONTACTS, | |
207 | FALSE, | |
208 | $searchDescendentGroups, | |
209 | FALSE, | |
210 | $displayRelationshipType, | |
211 | $operator | |
212 | ); | |
213 | ||
214 | $this->_options = &$this->_query->_options; | |
215 | } | |
216 | //end of constructor | |
217 | ||
218 | /** | |
219 | * This method returns the links that are given for each search row. | |
220 | * currently the links added for each row are | |
221 | * | |
222 | * - View | |
223 | * - Edit | |
224 | * | |
225 | * @return array | |
226 | * @access public | |
227 | * | |
228 | */ | |
229 | static function &links() { | |
230 | list($context, $contextMenu, $key) = func_get_args(); | |
231 | $extraParams = ($key) ? "&key={$key}" : NULL; | |
232 | $searchContext = ($context) ? "&context=$context" : NULL; | |
233 | ||
234 | if (!(self::$_links)) { | |
235 | self::$_links = array( | |
236 | CRM_Core_Action::VIEW => array( | |
237 | 'name' => ts('View'), | |
238 | 'url' => 'civicrm/contact/view', | |
239 | 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}", | |
240 | 'title' => ts('View Contact Details'), | |
241 | 'ref' => 'view-contact', | |
242 | ), | |
243 | CRM_Core_Action::UPDATE => array( | |
244 | 'name' => ts('Edit'), | |
245 | 'url' => 'civicrm/contact/add', | |
246 | 'qs' => "reset=1&action=update&cid=%%id%%{$searchContext}{$extraParams}", | |
247 | 'title' => ts('Edit Contact Details'), | |
248 | 'ref' => 'edit-contact', | |
249 | ), | |
250 | ); | |
251 | ||
252 | $config = CRM_Core_Config::singleton(); | |
253 | if ($config->mapAPIKey && $config->mapProvider) { | |
254 | self::$_links[CRM_Core_Action::MAP] = array( | |
255 | 'name' => ts('Map'), | |
256 | 'url' => 'civicrm/contact/map', | |
257 | 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}", | |
258 | 'title' => ts('Map Contact'), | |
259 | ); | |
260 | } | |
261 | ||
262 | // Adding Context Menu Links in more action | |
263 | if ($contextMenu) { | |
264 | $counter = 7000; | |
265 | foreach ($contextMenu as $key => $value) { | |
266 | $contextVal = '&context=' . $value['key']; | |
267 | if ($value['key'] == 'delete') { | |
268 | $contextVal = $searchContext; | |
269 | } | |
270 | ||
271 | $url = "civicrm/contact/view/{$value['key']}"; | |
272 | $qs = "reset=1&action=add&cid=%%id%%{$contextVal}{$extraParams}"; | |
273 | if ($value['key'] == 'activity') { | |
274 | $qs = "action=browse&selectedChild=activity&reset=1&cid=%%id%%{$extraParams}"; | |
275 | } | |
276 | elseif ($value['key'] == 'email') { | |
277 | $url = "civicrm/contact/view/activity"; | |
278 | $qs = "atype=3&action=add&reset=1&cid=%%id%%{$extraParams}"; | |
279 | } | |
280 | ||
281 | self::$_links[$counter++] = array( | |
282 | 'name' => $value['title'], | |
283 | 'url' => $url, | |
284 | 'qs' => $qs, | |
285 | 'title' => $value['title'], | |
286 | 'ref' => $value['ref'], | |
287 | ); | |
288 | } | |
289 | } | |
290 | } | |
291 | return self::$_links; | |
292 | } | |
293 | //end of function | |
294 | ||
295 | /** | |
296 | * getter for array of the parameters required for creating pager. | |
297 | * | |
298 | * @param | |
299 | * @access public | |
300 | */ | |
301 | function getPagerParams($action, &$params) { | |
302 | $params['status'] = ts('Contact %%StatusMessage%%'); | |
303 | $params['csvString'] = NULL; | |
304 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
305 | ||
306 | $params['buttonTop'] = 'PagerTopButton'; | |
307 | $params['buttonBottom'] = 'PagerBottomButton'; | |
308 | } | |
309 | //end of function | |
310 | function &getColHeads($action = NULL, $output = NULL) { | |
311 | ||
312 | $colHeads = self::_getColumnHeaders(); | |
313 | ||
314 | $colHeads[] = array('desc' => ts('Actions'), 'name' => ts('Action')); | |
315 | return $colHeads; | |
316 | } | |
317 | ||
318 | /** | |
319 | * returns the column headers as an array of tuples: | |
320 | * (name, sortName (key to the sort array)) | |
321 | * | |
322 | * @param string $action the action being performed | |
323 | * @param enum $output what should the result set include (web/email/csv) | |
324 | * | |
325 | * @return array the column headers that need to be displayed | |
326 | * @access public | |
327 | */ | |
328 | function &getColumnHeaders($action = NULL, $output = NULL) { | |
329 | $headers = NULL; | |
330 | if ($output == CRM_Core_Selector_Controller::EXPORT) { | |
331 | $csvHeaders = array(ts('Contact Id'), ts('Contact Type')); | |
332 | foreach ($this->getColHeads($action, $output) as $column) { | |
333 | if (array_key_exists('name', $column)) { | |
334 | $csvHeaders[] = $column['name']; | |
335 | } | |
336 | } | |
337 | $headers = $csvHeaders; | |
338 | } | |
339 | elseif ($output == CRM_Core_Selector_Controller::SCREEN) { | |
340 | $csvHeaders = array(ts('Name')); | |
341 | foreach ($this->getColHeads($action, $output) as $key => $column) { | |
342 | if (array_key_exists('name', $column) && | |
343 | $column['name'] && | |
344 | $column['name'] != ts('Name') | |
345 | ) { | |
346 | $csvHeaders[$key] = $column['name']; | |
347 | } | |
348 | } | |
349 | $headers = $csvHeaders; | |
350 | } | |
351 | elseif ($this->_ufGroupID) { | |
352 | // we dont use the cached value of column headers | |
353 | // since it potentially changed because of the profile selected | |
354 | static $skipFields = array('group', 'tag'); | |
355 | $direction = CRM_Utils_Sort::ASCENDING; | |
356 | $empty = TRUE; | |
357 | if (!self::$_columnHeaders) { | |
358 | self::$_columnHeaders = array(array('name' => ''), | |
359 | array( | |
360 | 'name' => ts('Name'), | |
361 | 'sort' => 'sort_name', | |
362 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
363 | ), | |
364 | ); | |
365 | ||
366 | $locationTypes = CRM_Core_PseudoConstant::locationType(); | |
367 | ||
368 | foreach ($this->_fields as $name => $field) { | |
369 | if (CRM_Utils_Array::value('in_selector', $field) && | |
370 | !in_array($name, $skipFields) | |
371 | ) { | |
372 | if (strpos($name, '-') !== FALSE) { | |
373 | list($fieldName, $lType, $type) = CRM_Utils_System::explode('-', $name, 3); | |
374 | ||
375 | if ($lType == 'Primary') { | |
376 | $locationTypeName = 1; | |
377 | } | |
378 | else { | |
379 | $locationTypeName = $locationTypes[$lType]; | |
380 | } | |
381 | ||
382 | if (in_array($fieldName, array( | |
383 | 'phone', 'im', 'email'))) { | |
384 | if ($type) { | |
385 | $name = "`$locationTypeName-$fieldName-$type`"; | |
386 | } | |
387 | else { | |
388 | $name = "`$locationTypeName-$fieldName`"; | |
389 | } | |
390 | } | |
391 | else { | |
392 | $name = "`$locationTypeName-$fieldName`"; | |
393 | } | |
394 | } | |
395 | //to handle sort key for Internal contactId.CRM-2289 | |
396 | if ($name == 'id') { | |
397 | $name = 'contact_id'; | |
398 | } | |
399 | ||
400 | self::$_columnHeaders[] = array( | |
401 | 'name' => $field['title'], | |
402 | 'sort' => $name, | |
403 | 'direction' => $direction, | |
404 | ); | |
405 | $direction = CRM_Utils_Sort::DONTCARE; | |
406 | $empty = FALSE; | |
407 | } | |
408 | } | |
409 | ||
410 | // if we dont have any valid columns, dont add the implicit ones | |
411 | // this allows the template to check on emptiness of column headers | |
412 | if ($empty) { | |
413 | self::$_columnHeaders = array(); | |
414 | } | |
415 | else { | |
416 | self::$_columnHeaders[] = array('desc' => ts('Actions'), 'name' => ts('Action')); | |
417 | } | |
418 | } | |
419 | $headers = self::$_columnHeaders; | |
420 | } | |
421 | elseif (!empty($this->_returnProperties)) { | |
422 | ||
423 | self::$_columnHeaders = array(array('name' => ''), | |
424 | array( | |
425 | 'name' => ts('Name'), | |
426 | 'sort' => 'sort_name', | |
427 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
428 | ), | |
429 | ); | |
430 | $properties = self::makeProperties($this->_returnProperties); | |
431 | ||
432 | foreach ($properties as $prop) { | |
433 | if ($prop == 'contact_type' || $prop == 'contact_sub_type' || $prop == 'sort_name') { | |
434 | continue; | |
435 | } | |
436 | ||
437 | if (strpos($prop, '-')) { | |
438 | list($loc, $fld, $phoneType) = CRM_Utils_System::explode('-', $prop, 3); | |
439 | $title = $this->_query->_fields[$fld]['title']; | |
440 | if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) { | |
441 | $title .= "-{$phoneType}"; | |
442 | } | |
443 | $title .= " ($loc)"; | |
444 | } | |
445 | elseif (isset($this->_query->_fields[$prop]) && isset($this->_query->_fields[$prop]['title'])) { | |
446 | $title = $this->_query->_fields[$prop]['title']; | |
447 | } else { | |
448 | $title = ''; | |
449 | } | |
450 | ||
451 | self::$_columnHeaders[] = array('name' => $title, 'sort' => $prop); | |
452 | } | |
453 | self::$_columnHeaders[] = array('name' => ts('Actions')); | |
454 | $headers = self::$_columnHeaders; | |
455 | } | |
456 | else { | |
457 | $headers = $this->getColHeads($action, $output); | |
458 | } | |
459 | ||
460 | return $headers; | |
461 | } | |
462 | ||
463 | /** | |
464 | * Returns total number of rows for the query. | |
465 | * | |
466 | * @param | |
467 | * | |
468 | * @return int Total number of rows | |
469 | * @access public | |
470 | */ | |
471 | function getTotalCount($action) { | |
472 | return $this->_query->searchQuery(0, 0, NULL, TRUE); | |
473 | } | |
474 | ||
475 | /** | |
476 | * returns all the rows in the given offset and rowCount | |
477 | * | |
478 | * @param enum $action the action being performed | |
479 | * @param int $offset the row number to start from | |
480 | * @param int $rowCount the number of rows to return | |
481 | * @param string $sort the sql string that describes the sort order | |
482 | * @param enum $output what should the result set include (web/email/csv) | |
483 | * | |
484 | * @return int the total number of rows for this action | |
485 | */ | |
486 | function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { | |
487 | $config = CRM_Core_Config::singleton(); | |
488 | ||
489 | if (($output == CRM_Core_Selector_Controller::EXPORT || | |
490 | $output == CRM_Core_Selector_Controller::SCREEN | |
491 | ) && | |
492 | $this->_formValues['radio_ts'] == 'ts_sel' | |
493 | ) { | |
494 | $includeContactIds = TRUE; | |
495 | } | |
496 | else { | |
497 | $includeContactIds = FALSE; | |
498 | } | |
499 | ||
500 | // note the formvalues were given by CRM_Contact_Form_Search to us | |
501 | // and contain the search criteria (parameters) | |
502 | // note that the default action is basic | |
503 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds); | |
504 | ||
505 | // process the result of the query | |
506 | $rows = array(); | |
507 | $permissions = array(CRM_Core_Permission::getPermission()); | |
508 | if (CRM_Core_Permission::check('delete contacts')) { | |
509 | $permissions[] = CRM_Core_Permission::DELETE; | |
510 | } | |
511 | $mask = CRM_Core_Action::mask($permissions); | |
512 | ||
513 | // mask value to hide map link if there are not lat/long | |
514 | $mapMask = $mask & 4095; | |
515 | ||
516 | if ($this->_searchContext == 'smog') { | |
517 | $gc = CRM_Core_SelectValues::groupContactStatus(); | |
518 | } | |
519 | ||
520 | if ($this->_ufGroupID) { | |
521 | $locationTypes = CRM_Core_PseudoConstant::locationType(); | |
522 | ||
523 | $names = array(); | |
524 | static $skipFields = array('group', 'tag'); | |
525 | foreach ($this->_fields as $key => $field) { | |
526 | if (CRM_Utils_Array::value('in_selector', $field) && | |
527 | !in_array($key, $skipFields) | |
528 | ) { | |
529 | if (strpos($key, '-') !== FALSE) { | |
530 | list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $key, 3); | |
531 | ||
532 | if ($id == 'Primary') { | |
533 | $locationTypeName = 1; | |
534 | } | |
535 | else { | |
536 | $locationTypeName = CRM_Utils_Array::value($id, $locationTypes); | |
537 | if (!$locationTypeName) { | |
538 | continue; | |
539 | } | |
540 | } | |
541 | ||
542 | $locationTypeName = str_replace(' ', '_', $locationTypeName); | |
543 | if (in_array($fieldName, array( | |
544 | 'phone', 'im', 'email'))) { | |
545 | if ($type) { | |
546 | $names[] = "{$locationTypeName}-{$fieldName}-{$type}"; | |
547 | } | |
548 | else { | |
549 | $names[] = "{$locationTypeName}-{$fieldName}"; | |
550 | } | |
551 | } | |
552 | else { | |
553 | $names[] = "{$locationTypeName}-{$fieldName}"; | |
554 | } | |
555 | } | |
556 | else { | |
557 | $names[] = $field['name']; | |
558 | } | |
559 | } | |
560 | } | |
561 | ||
562 | $names[] = "status"; | |
563 | } | |
564 | elseif (!empty($this->_returnProperties)) { | |
565 | $names = self::makeProperties($this->_returnProperties); | |
566 | } | |
567 | else { | |
568 | $names = self::$_properties; | |
569 | } | |
570 | ||
571 | $multipleSelectFields = array('preferred_communication_method' => 1); | |
572 | ||
573 | $links = self::links($this->_context, $this->_contextMenu, $this->_key); | |
574 | ||
575 | //check explicitly added contact to a Smart Group. | |
576 | $groupID = CRM_Utils_Array::key('1', $this->_formValues['group']); | |
577 | ||
578 | // for CRM-3157 purposes | |
579 | if (in_array('country', $names)) { | |
580 | $countries = CRM_Core_PseudoConstant::country(); | |
581 | } | |
582 | ||
583 | if (in_array('state_province', $names)) { | |
584 | $provinces = CRM_Core_PseudoConstant::stateProvince(); | |
585 | } | |
586 | ||
587 | if (in_array('world_region', $names)) { | |
588 | $regions = CRM_Core_PseudoConstant::worldRegion(); | |
589 | } | |
590 | ||
591 | $seenIDs = array(); | |
592 | while ($result->fetch()) { | |
593 | $row = array(); | |
594 | ||
595 | // the columns we are interested in | |
596 | foreach ($names as $property) { | |
597 | if ($property == 'status') { | |
598 | continue; | |
599 | } | |
600 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($property)) { | |
601 | $row[$property] = CRM_Core_BAO_CustomField::getDisplayValue($result->$property, | |
602 | $cfID, | |
603 | $this->_options, | |
604 | $result->contact_id | |
605 | ); | |
606 | } | |
607 | elseif ($multipleSelectFields && | |
608 | array_key_exists($property, $multipleSelectFields) | |
609 | ) { | |
610 | //fix to display student checkboxes | |
611 | $key = $property; | |
612 | $paramsNew = array($key => $result->$property); | |
613 | ||
614 | if ($key == 'test_tutoring') { | |
615 | $name = array($key => array('newName' => $key, 'groupName' => 'test')); | |
616 | // for readers group | |
617 | } | |
618 | elseif (substr($key, 0, 4) == 'cmr_') { | |
619 | $name = array($key => array('newName' => $key, 'groupName' => substr($key, 0, -3))); | |
620 | } | |
621 | else { | |
622 | $name = array($key => array('newName' => $key, 'groupName' => $key)); | |
623 | } | |
624 | CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE); | |
625 | $row[$key] = $paramsNew[$key]; | |
626 | } | |
627 | elseif (strpos($property, '-im')) { | |
628 | $row[$property] = $result->$property; | |
629 | if (!empty($result->$property)) { | |
630 | $imProviders = CRM_Core_PseudoConstant::IMProvider(); | |
631 | $providerId = $property . "-provider_id"; | |
632 | $providerName = $imProviders[$result->$providerId]; | |
633 | $row[$property] = $result->$property . " ({$providerName})"; | |
634 | } | |
635 | } | |
636 | elseif (in_array($property, array( | |
637 | 'addressee', 'email_greeting', 'postal_greeting'))) { | |
638 | $greeting = $property . '_display'; | |
639 | $row[$property] = $result->$greeting; | |
640 | } | |
641 | elseif ($property == 'country') { | |
642 | $row[$property] = CRM_Utils_Array::value($result->country_id, $countries); | |
643 | } | |
644 | elseif ($property == 'state_province') { | |
645 | $row[$property] = CRM_Utils_Array::value($result->state_province_id, $provinces); | |
646 | } | |
647 | elseif ($property == 'world_region') { | |
648 | $row[$property] = CRM_Utils_Array::value($result->worldregion_id, $regions); | |
649 | } | |
650 | elseif (strpos($property, '-url') !== FALSE) { | |
651 | $websiteUrl = ''; | |
652 | $websiteKey = 'website-1'; | |
653 | $propertyArray = explode('-', $property); | |
654 | $websiteFld = $websiteKey . '-' . array_pop($propertyArray); | |
655 | if (!empty($result->$websiteFld)) { | |
656 | $websiteTypes = CRM_Core_PseudoConstant::websiteType(); | |
657 | $websiteType = $websiteTypes[$result->{"$websiteKey-website_type_id"}]; | |
658 | $websiteValue = $result->$websiteFld; | |
659 | $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue} ({$websiteType})</a>"; | |
660 | } | |
661 | $row[$property] = $websiteUrl; | |
662 | } | |
663 | else { | |
664 | $row[$property] = isset($result->$property) ? $result->$property : NULL; | |
665 | } | |
666 | } | |
667 | ||
668 | if (!empty($result->postal_code_suffix)) { | |
669 | $row['postal_code'] .= "-" . $result->postal_code_suffix; | |
670 | } | |
671 | ||
672 | if ($output != CRM_Core_Selector_Controller::EXPORT && | |
673 | $this->_searchContext == 'smog' | |
674 | ) { | |
675 | if (empty($result->status) && | |
676 | $groupID | |
677 | ) { | |
678 | $contactID = $result->contact_id; | |
679 | if ($contactID) { | |
680 | $gcParams = array( | |
681 | 'contact_id' => $contactID, | |
682 | 'group_id' => $groupID, | |
683 | ); | |
684 | ||
685 | $gcDefaults = array(); | |
686 | CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults); | |
687 | ||
688 | if (empty($gcDefaults)) { | |
689 | $row['status'] = ts('Smart'); | |
690 | } | |
691 | else { | |
692 | $row['status'] = $gc[$gcDefaults['status']]; | |
693 | } | |
694 | } | |
695 | else { | |
696 | $row['status'] = NULL; | |
697 | } | |
698 | } | |
699 | else { | |
700 | $row['status'] = $gc[$result->status]; | |
701 | } | |
702 | } | |
703 | ||
704 | if ($output != CRM_Core_Selector_Controller::EXPORT) { | |
705 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id; | |
706 | ||
707 | if (CRM_Utils_Array::value('deleted_contacts', $this->_formValues) | |
708 | && CRM_Core_Permission::check('access deleted contacts') | |
709 | ) { | |
710 | $links = array( | |
711 | array( | |
712 | 'name' => ts('View'), | |
713 | 'url' => 'civicrm/contact/view', | |
714 | 'qs' => 'reset=1&cid=%%id%%', | |
715 | 'title' => ts('View Contact Details'), | |
716 | ), | |
717 | array( | |
718 | 'name' => ts('Restore'), | |
719 | 'url' => 'civicrm/contact/view/delete', | |
720 | 'qs' => 'reset=1&cid=%%id%%&restore=1', | |
721 | 'title' => ts('Restore Contact'), | |
722 | ), | |
723 | ); | |
724 | if (CRM_Core_Permission::check('delete contacts')) { | |
725 | $links[] = array( | |
726 | 'name' => ts('Delete Permanently'), | |
727 | 'url' => 'civicrm/contact/view/delete', | |
728 | 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1', | |
729 | 'title' => ts('Permanently Delete Contact'), | |
730 | ); | |
731 | } | |
732 | $row['action'] = CRM_Core_Action::formLink($links, NULL, array('id' => $result->contact_id)); | |
733 | } | |
734 | elseif ((is_numeric(CRM_Utils_Array::value('geo_code_1', $row))) || | |
735 | ($config->mapGeoCoding && | |
736 | CRM_Utils_Array::value('city', $row) && | |
737 | CRM_Utils_Array::value('state_province', $row) | |
738 | ) | |
739 | ) { | |
740 | $row['action'] = CRM_Core_Action::formLink($links, $mask, array('id' => $result->contact_id)); | |
741 | } | |
742 | else { | |
743 | $row['action'] = CRM_Core_Action::formLink($links, $mapMask, array('id' => $result->contact_id)); | |
744 | } | |
745 | ||
746 | // allow components to add more actions | |
747 | CRM_Core_Component::searchAction($row, $result->contact_id); | |
748 | ||
749 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? | |
750 | $result->contact_sub_type : $result->contact_type, | |
751 | FALSE, | |
752 | $result->contact_id | |
753 | ); | |
754 | ||
2bbddafc | 755 | $row['contact_type_orig'] = $result->contact_sub_type ? $result->contact_sub_type : $result->contact_type; |
6a488035 TO |
756 | $row['contact_sub_type'] = $result->contact_sub_type ? |
757 | CRM_Contact_BAO_ContactType::contactTypePairs(FALSE, $result->contact_sub_type, ', ') : $result->contact_sub_type; | |
758 | $row['contact_id'] = $result->contact_id; | |
759 | $row['sort_name'] = $result->sort_name; | |
760 | if (array_key_exists('id', $row)) { | |
761 | $row['id'] = $result->contact_id; | |
762 | } | |
763 | } | |
764 | ||
765 | // Dedupe contacts | |
766 | if (in_array($row['contact_id'], $seenIDs) === FALSE) { | |
767 | $seenIDs[] = $row['contact_id']; | |
768 | $rows[] = $row; | |
769 | } | |
770 | } | |
771 | ||
772 | $this->buildPrevNextCache($sort); | |
773 | ||
774 | return $rows; | |
775 | } | |
776 | ||
777 | function buildPrevNextCache($sort) { | |
778 | $cacheKey = CRM_Utils_Array::value('qfKey', $this->_formValues); | |
779 | ||
780 | //for prev/next pagination | |
781 | $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject); | |
782 | ||
783 | //for alphabetic pagination selection save | |
784 | $sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', CRM_Core_DAO::$_nullObject); | |
785 | ||
786 | //for text field pagination selection save | |
787 | $countRow = CRM_Core_BAO_PrevNextCache::getCount("%civicrm search {$cacheKey}%", NULL, "entity_table = 'civicrm_contact'", "LIKE"); | |
788 | ||
789 | if (!$crmPID && $countRow == 0 && !$sortByCharacter) { | |
790 | $this->fillupPrevNextCache($sort); | |
791 | } | |
792 | elseif ($sortByCharacter) { | |
793 | $cacheKeyCharacter = "civicrm search {$cacheKey}_alphabet"; | |
794 | if ($sortByCharacter == 'all') { | |
795 | //delete the alphabet key corresponding records in prevnext_cache | |
796 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKeyCharacter, 'civicrm_contact'); | |
6aee9e36 | 797 | $cacheKeyCharacter = NULL; |
6a488035 | 798 | } |
6aee9e36 | 799 | $this->fillupPrevNextCache($sort, $cacheKeyCharacter); |
6a488035 TO |
800 | } |
801 | } | |
802 | ||
803 | function addActions(&$rows) { | |
804 | $config = CRM_Core_Config::singleton(); | |
805 | ||
806 | $permissions = array(CRM_Core_Permission::getPermission()); | |
807 | if (CRM_Core_Permission::check('delete contacts')) { | |
808 | $permissions[] = CRM_Core_Permission::DELETE; | |
809 | } | |
810 | $mask = CRM_Core_Action::mask($permissions); | |
811 | // mask value to hide map link if there are not lat/long | |
812 | $mapMask = $mask & 4095; | |
813 | ||
814 | // mask value to hide map link if there are not lat/long | |
815 | $mapMask = $mask & 4095; | |
816 | ||
817 | $links = self::links($this->_context, $this->_contextMenu, $this->_key); | |
818 | ||
819 | ||
820 | foreach ($rows as $id => & $row) { | |
821 | if (CRM_Utils_Array::value('deleted_contacts', $this->_formValues) | |
822 | && CRM_Core_Permission::check('access deleted contacts') | |
823 | ) { | |
824 | $links = array( | |
825 | array( | |
826 | 'name' => ts('View'), | |
827 | 'url' => 'civicrm/contact/view', | |
828 | 'qs' => 'reset=1&cid=%%id%%', | |
829 | 'title' => ts('View Contact Details'), | |
830 | ), | |
831 | array( | |
832 | 'name' => ts('Restore'), | |
833 | 'url' => 'civicrm/contact/view/delete', | |
834 | 'qs' => 'reset=1&cid=%%id%%&restore=1', | |
835 | 'title' => ts('Restore Contact'), | |
836 | ), | |
837 | ); | |
838 | if (CRM_Core_Permission::check('delete contacts')) { | |
839 | $links[] = array( | |
840 | 'name' => ts('Delete Permanently'), | |
841 | 'url' => 'civicrm/contact/view/delete', | |
842 | 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1', | |
843 | 'title' => ts('Permanently Delete Contact'), | |
844 | ); | |
845 | } | |
846 | $row['action'] = CRM_Core_Action::formLink($links, NULL, array('id' => $row['contact_id'])); | |
847 | } | |
848 | elseif ((is_numeric(CRM_Utils_Array::value('geo_code_1', $row))) || | |
849 | ($config->mapGeoCoding && | |
850 | CRM_Utils_Array::value('city', $row) && | |
851 | CRM_Utils_Array::value('state_province', $row) | |
852 | ) | |
853 | ) { | |
854 | $row['action'] = CRM_Core_Action::formLink($links, $mask, array('id' => $row['contact_id'])); | |
855 | } | |
856 | else { | |
857 | $row['action'] = CRM_Core_Action::formLink($links, $mapMask, array('id' => $row['contact_id'])); | |
858 | } | |
859 | ||
860 | // allow components to add more actions | |
861 | CRM_Core_Component::searchAction($row, $row['contact_id']); | |
862 | ||
2bbddafc CW |
863 | if (!empty($row['contact_type_orig'])) { |
864 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($row['contact_type_orig'], | |
6a488035 TO |
865 | FALSE, $row['contact_id']); |
866 | } | |
867 | } | |
868 | } | |
869 | ||
870 | function removeActions(&$rows) { | |
871 | foreach ($rows as $rid => & $rValue) { | |
872 | unset($rValue['contact_type']); | |
873 | unset($rValue['action']); | |
874 | } | |
875 | } | |
876 | ||
877 | function fillupPrevNextCache($sort, $cacheKey = NULL) { | |
878 | if (!$cacheKey) { | |
879 | $cacheKey = "civicrm search {$this->_key}"; | |
880 | } | |
881 | ||
882 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact'); | |
883 | ||
884 | // lets fill up the prev next cache here, so view can scroll thru | |
885 | if (is_a($this, 'CRM_Contact_Selector_Custom')) { | |
886 | $sql = $this->_search->contactIDs(0, 0, $sort, TRUE); | |
887 | $replaceSQL = "SELECT contact_a.id as contact_id"; | |
888 | } | |
889 | else { | |
890 | $sql = $this->_query->searchQuery( | |
891 | 0, 0, $sort, | |
892 | FALSE, FALSE, | |
893 | FALSE, TRUE, TRUE, NULL | |
894 | ); | |
895 | $replaceSQL = "SELECT contact_a.id as id"; | |
896 | } | |
897 | ||
898 | // CRM-9096 | |
899 | // due to limitations in our search query writer, the above query does not work | |
900 | // in cases where the query is being sorted on a non-contact table | |
901 | // this results in a fatal error :( | |
902 | // see below for the gross hack of trapping the error and not filling | |
903 | // the prev next cache in this situation | |
904 | // the other alternative of running the FULL query will just be incredibly inefficient | |
905 | // and slow things down way too much on large data sets / complex queries | |
906 | ||
907 | $insertSQL = " | |
908 | INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) | |
909 | SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name | |
910 | "; | |
911 | ||
912 | $sql = str_replace($replaceSQL, $insertSQL, $sql); | |
913 | ||
914 | ||
915 | CRM_Core_Error::ignoreException(); | |
916 | $result = CRM_Core_DAO::executeQuery($sql); | |
917 | CRM_Core_Error::setCallback(); | |
918 | ||
919 | if (is_a($result, 'DB_Error')) { | |
920 | // oops the above query failed, so lets just ignore it | |
921 | // and return | |
922 | // we print a sorry cant figure it out on view page | |
923 | return; | |
924 | } | |
925 | ||
926 | // also record an entry in the cache key table, so we can delete it periodically | |
927 | CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey); | |
928 | } | |
929 | ||
930 | /** | |
931 | * Given the current formValues, gets the query in local | |
932 | * language | |
933 | * | |
934 | * @param array( | |
935 | reference) $formValues submitted formValues | |
936 | * | |
937 | * @return array $qill which contains an array of strings | |
938 | * @access public | |
939 | */ | |
940 | ||
941 | // the current internationalisation is bad, but should more or less work | |
942 | // for most of "European" languages | |
943 | public function getQILL() { | |
944 | return $this->_query->qill(); | |
945 | } | |
946 | ||
947 | /** | |
948 | * name of export file. | |
949 | * | |
950 | * @param string $output type of output | |
951 | * | |
952 | * @return string name of the file | |
953 | */ | |
954 | function getExportFileName($output = 'csv') { | |
955 | return ts('CiviCRM Contact Search'); | |
956 | } | |
957 | ||
958 | /** | |
959 | * get colunmn headers for search selector | |
960 | * | |
961 | * | |
962 | * @return array $_columnHeaders | |
963 | * @access private | |
964 | */ | |
965 | private static function &_getColumnHeaders() { | |
966 | if (!isset(self::$_columnHeaders)) { | |
967 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
968 | 'address_options', TRUE, NULL, TRUE | |
969 | ); | |
970 | ||
971 | self::$_columnHeaders = array('contact_type' => array('desc' => ts('Contact Type')), | |
972 | 'sort_name' => array( | |
973 | 'name' => ts('Name'), | |
974 | 'sort' => 'sort_name', | |
975 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
976 | ), | |
977 | ); | |
978 | ||
979 | $defaultAddress = array('street_address' => array('name' => ts('Address')), | |
980 | 'city' => array('name' => ts('City'), | |
981 | 'sort' => 'city', | |
982 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
983 | ), | |
984 | 'state_province' => array('name' => ts('State'), | |
985 | 'sort' => 'state_province', | |
986 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
987 | ), | |
988 | 'postal_code' => array('name' => ts('Postal'), | |
989 | 'sort' => 'postal_code', | |
990 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
991 | ), | |
992 | 'country' => array('name' => ts('Country'), | |
993 | 'sort' => 'country', | |
994 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
995 | ), | |
996 | ); | |
997 | ||
998 | foreach ($defaultAddress as $columnName => $column) { | |
999 | if (CRM_Utils_Array::value($columnName, $addressOptions)) { | |
1000 | self::$_columnHeaders[$columnName] = $column; | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | self::$_columnHeaders['email'] = array('name' => ts('Email'), | |
1005 | 'sort' => 'email', | |
1006 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
1007 | ); | |
1008 | ||
1009 | self::$_columnHeaders['phone'] = array('name' => ts('Phone')); | |
1010 | } | |
1011 | return self::$_columnHeaders; | |
1012 | } | |
1013 | ||
1014 | function &getQuery() { | |
1015 | return $this->_query; | |
1016 | } | |
1017 | ||
1018 | function alphabetQuery() { | |
1019 | return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE); | |
1020 | } | |
1021 | ||
1022 | function contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') { | |
1023 | $sortOrder = &$this->getSortOrder($this->_action); | |
1024 | $sort = new CRM_Utils_Sort($sortOrder, $sortID); | |
1025 | ||
1026 | // rectify params to what proximity search expects if there is a value for prox_distance | |
1027 | // CRM-7021 CRM-7905 | |
1028 | if (!empty($params)) { | |
1029 | CRM_Contact_BAO_ProximityQuery::fixInputParams($params); | |
1030 | } | |
1031 | ||
1032 | if (!$displayRelationshipType) { | |
1033 | $query = new CRM_Contact_BAO_Query($params, | |
1034 | $this->_returnProperties, | |
1035 | NULL, FALSE, FALSE, 1, | |
1036 | FALSE, TRUE, TRUE, NULL, | |
1037 | $queryOperator | |
1038 | ); | |
1039 | } | |
1040 | else { | |
1041 | $query = new CRM_Contact_BAO_Query($params, $this->_returnProperties, | |
1042 | NULL, FALSE, FALSE, 1, | |
1043 | FALSE, TRUE, TRUE, $displayRelationshipType, | |
1044 | $queryOperator | |
1045 | ); | |
1046 | } | |
1047 | $value = $query->searchQuery(0, 0, $sort, | |
1048 | FALSE, FALSE, FALSE, | |
1049 | FALSE, FALSE | |
1050 | ); | |
1051 | return $value; | |
1052 | } | |
1053 | ||
1054 | function &makeProperties(&$returnProperties) { | |
1055 | $properties = array(); | |
1056 | foreach ($returnProperties as $name => $value) { | |
1057 | if ($name != 'location') { | |
1058 | $properties[] = $name; | |
1059 | } | |
1060 | else { | |
1061 | // extract all the location stuff | |
1062 | foreach ($value as $n => $v) { | |
1063 | foreach ($v as $n1 => $v1) { | |
1064 | if (!strpos('_id', $n1) && $n1 != 'location_type') { | |
1065 | $properties[] = "{$n}-{$n1}"; | |
1066 | } | |
1067 | } | |
1068 | } | |
1069 | } | |
1070 | } | |
1071 | return $properties; | |
1072 | } | |
1073 | } | |
1074 | //end of class | |
1075 |