3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * This implements the profile page for all contacts. It uses a selector
21 * object to do the actual dispay. The fields displayd are controlled by
24 class CRM_Profile_Page_Listings
extends CRM_Core_Page
{
27 * All the fields that are listings related.
34 * The custom fields for this domain.
38 protected $_customFields;
41 * The input params from the request.
48 * The group id that we are editing.
55 * State whether to display search form or not.
62 * Should we display a map.
69 * Store profile ids if multiple profile ids are passed using comma separated.
70 * Currently lets implement this functionality only for dialog mode
73 protected $_profileIds = [];
76 * Extracts the parameters from the request and constructs information for
77 * the selector object to do a query
80 public function preProcess() {
82 $this->_search
= TRUE;
84 $search = CRM_Utils_Request
::retrieve('search', 'Boolean', $this, FALSE, 0, 'GET');
85 if (isset($search) && $search == 0) {
86 $this->_search
= FALSE;
89 $this->_gid
= $this->get('gid');
90 $this->_profileIds
= $this->get('profileIds');
92 $gids = explode(',', CRM_Utils_Request
::retrieve('gid', 'String', CRM_Core_DAO
::$_nullObject, FALSE, 0, 'GET'));
94 if ((count($gids) > 1) && !$this->_profileIds
&& empty($this->_profileIds
)) {
96 foreach ($gids as $pfId) {
97 $this->_profileIds
[] = CRM_Utils_Type
::escape($pfId, 'Positive');
101 // check if we are rendering mixed profiles
102 if (CRM_Core_BAO_UFGroup
::checkForMixProfiles($this->_profileIds
)) {
103 CRM_Core_Error
::statusBounce(ts('You cannot combine profiles of multiple types.'));
106 $this->_gid
= $this->_profileIds
[0];
107 $this->set('profileIds', $this->_profileIds
);
108 $this->set('gid', $this->_gid
);
112 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this, FALSE, 0, 'GET');
115 if (empty($this->_profileIds
)) {
119 $gids = $this->_profileIds
;
122 $this->_fields
= CRM_Core_BAO_UFGroup
::getListingFields(CRM_Core_Action
::UPDATE
,
123 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
124 FALSE, $gids, FALSE, 'Profile',
125 CRM_Core_Permission
::SEARCH
128 $this->_customFields
= CRM_Core_BAO_CustomField
::getFieldsForImport(NULL, FALSE, FALSE, FALSE, TRUE, TRUE);
134 'preferred_communication_method',
143 foreach ($this->_fields
as $name => $field) {
144 if ((substr($name, 0, 6) == 'custom') && !empty($field['is_search_range'])) {
145 $from = CRM_Utils_Request
::retrieve($name . '_from', 'String', $this);
146 $to = CRM_Utils_Request
::retrieve($name . '_to', 'String', $this);
149 $value[$name . '_from'] = $from;
150 $value[$name . '_to'] = $to;
153 $value[$name . '_from'] = $from;
156 $value[$name . '_to'] = $to;
159 elseif ((substr($name, 0, 7) == 'custom_') &&
160 (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
161 substr($name, 7), 'html_type'
164 $value = trim(CRM_Utils_Request
::retrieve($name, 'String',
165 $this, FALSE, NULL, 'REQUEST'
167 if (!empty($value) &&
168 !((substr($value, 0, 1) == '%') &&
169 (substr($value, -1, 1) == '%')
172 $value = '%' . $value . '%';
175 elseif (CRM_Utils_Array
::value('html_type', $field) == 'Multi-Select State/Province'
176 || CRM_Utils_Array
::value('html_type', $field) == 'Multi-Select Country'
178 $value = CRM_Utils_Request
::retrieve($name, 'String', $this, FALSE, NULL, 'REQUEST');
179 if (!is_array($value)) {
180 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($value, 1, -1));
183 elseif ($name == 'contact_sub_type') {
184 $v = CRM_Utils_Request
::retrieve($name, 'String', $this, FALSE, NULL, 'REQUEST');
185 if ($v && !is_array($v)) {
186 $v = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($v, CRM_Core_DAO
::VALUE_SEPARATOR
));
189 foreach ($v as $item) {
195 $value = CRM_Utils_Request
::retrieve($name, 'String',
196 $this, FALSE, NULL, 'REQUEST'
200 if (($name == 'group' ||
$name == 'tag') && !empty($value) && !is_array($value)) {
201 $v = explode(',', $value);
203 foreach ($v as $item) {
208 $customField = $this->_customFields
[$name] ??
NULL;
210 if (!empty($_POST) && empty($_POST[$name])) {
212 // reset checkbox/radio because a form does not send null checkbox values
213 if (in_array($customField['html_type'],
214 ['Multi-Select', 'CheckBox', 'Multi-Select State/Province', 'Multi-Select Country', 'Radio', 'Select']
216 // only reset on a POST submission if we don't see any value
218 $this->set($name, $value);
221 elseif (in_array($name, $resetArray)) {
223 $this->set($name, $value);
227 if (isset($value) && $value != NULL) {
228 if (!is_array($value)) {
229 $value = trim($value);
231 $operator = CRM_Utils_Request
::retrieve($name . '_operator', 'String', $this);
233 $this->_params
[$name . '_operator'] = $operator;
235 if ((substr($name, 0, 6) == 'custom') && !empty($field['is_search_range'])) {
236 $this->_params +
= $value;
239 $this->_params
[$name] = $this->_fields
[$name]['value'] = $value;
244 // set the prox params
245 // need to ensure proximity searching is enabled
255 foreach ($proximityVars as $var) {
256 $value = CRM_Utils_Request
::retrieve("prox_{$var}",
258 $this, FALSE, NULL, 'REQUEST'
261 $this->_params
["prox_{$var}"] = $value;
265 // set the params in session
266 $session = CRM_Core_Session
::singleton();
267 $session->set('profileParams', $this->_params
);
271 * Run this page (figure out the action needed and perform it).
274 public function run() {
277 $this->assign('recentlyViewed', FALSE);
278 // override later (if possible):
279 $this->assign('ufGroupName', 'unknown');
282 $ufgroupDAO = new CRM_Core_DAO_UFGroup();
283 $ufgroupDAO->id
= $this->_gid
;
284 if (!$ufgroupDAO->find(TRUE)) {
285 CRM_Core_Error
::statusBounce('Unable to find matching UF Group');
290 // set the title of the page
291 if ($ufgroupDAO->title
) {
292 CRM_Utils_System
::setTitle($ufgroupDAO->title
);
294 if ($ufgroupDAO->name
) {
295 $this->assign('ufGroupName', $ufgroupDAO->name
);
299 $this->assign('isReset', TRUE);
301 $formController = new CRM_Core_Controller_Simple('CRM_Profile_Form_Search',
302 ts('Search Profile'),
305 $formController->setEmbedded(TRUE);
306 $formController->set('gid', $this->_gid
);
307 $formController->process();
309 $searchError = FALSE;
310 // check if there is a POST
311 if (!empty($_POST)) {
312 if ($formController->validate() !== TRUE) {
317 // also get the search tpl name
318 $this->assign('searchTPL', $formController->getHookedTemplateFileName());
320 $this->assign('search', $this->_search
);
322 // search if search returned a form error?
323 if ((empty($_GET['reset']) ||
!empty($_GET['force'])) &&
326 $this->assign('isReset', FALSE);
328 $gidString = $this->_gid
;
329 if (empty($this->_profileIds
)) {
333 $gids = $this->_profileIds
;
334 $gidString = implode(',', $this->_profileIds
);
341 $map = $ufgroupDAO->is_map
;
342 $linkToUF = $ufgroupDAO->is_uf_link
;
343 $editLink = $ufgroupDAO->is_edit_link
;
347 $this->assign('mapURL',
348 CRM_Utils_System
::url('civicrm/profile/map',
349 "map=1&gid={$gidString}&reset=1"
353 if (!empty($this->_params
['group'])) {
354 foreach ($this->_params
['group'] as $key => $val) {
356 unset($this->_params
['group'][$key]);
361 // the selector will override this if the user does have
362 // edit permissions as determined by the mask, CRM-4341
363 // do not allow edit for anon users in joomla frontend, CRM-4668
364 $config = CRM_Core_Config
::singleton();
365 if (!CRM_Core_Permission
::check('access CiviCRM') ||
366 $config->userFrameworkFrontend
== 1
371 $selector = new CRM_Profile_Selector_Listings($this->_params
, $this->_customFields
, $gids,
372 $map, $editLink, $linkToUF
375 $controller = new CRM_Core_Selector_Controller($selector,
376 $this->get(CRM_Utils_Pager
::PAGE_ID
),
377 $this->get(CRM_Utils_Sort
::SORT_ID
),
378 CRM_Core_Action
::VIEW
,
380 CRM_Core_Selector_Controller
::TEMPLATE
382 $controller->setEmbedded(TRUE);
386 //CRM-6862 -run form controller after
387 //selector, since it erase $_POST
388 $formController->run();
390 return parent
::run();
394 * Get the list of contacts for a profile.
400 public static function getProfileContact($gid) {
401 $session = CRM_Core_Session
::singleton();
402 $params = $session->get('profileParams');
405 $ufGroupParam = ['id' => $gid];
406 CRM_Core_BAO_UFGroup
::retrieve($ufGroupParam, $details);
408 // make sure this group can be mapped
409 if (!$details['is_map']) {
410 CRM_Core_Error
::statusBounce(ts('This profile does not have the map feature turned on.'));
413 $groupId = $details['limit_listings_group_id'] ??
NULL;
415 // add group id to params if a uf group belong to a any group
417 if (!empty($params['group'])) {
418 $params['group'][$groupId] = 1;
421 $params['group'] = [$groupId => 1];
425 $fields = CRM_Core_BAO_UFGroup
::getListingFields(
426 CRM_Core_Action
::VIEW
,
427 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
432 $returnProperties = CRM_Contact_BAO_Contact
::makeHierReturnProperties($fields);
433 $returnProperties['contact_type'] = 1;
434 $returnProperties['sort_name'] = 1;
436 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params, 1);
437 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, $fields);
439 $additionalWhereClause = 'contact_a.is_deleted = 0';
441 $ids = $query->searchQuery(0, 0, NULL,
444 $additionalWhereClause
447 $contactIds = explode(',', $ids);
453 * @param string $suffix
455 * @return null|string
457 public function checkTemplateFileExists($suffix = '') {
459 $templateFile = "CRM/Profile/Page/{$this->_gid}/Listings.{$suffix}tpl";
460 $template = CRM_Core_Page
::getTemplate();
461 if ($template->template_exists($templateFile)) {
462 return $templateFile;
465 // lets see if we have customized by name
466 $ufGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
468 $templateFile = "CRM/Profile/Page/{$ufGroupName}/Listings.{$suffix}tpl";
469 if ($template->template_exists($templateFile)) {
470 return $templateFile;
478 * Use the form name to create the tpl file name.
482 public function getTemplateFileName() {
483 $fileName = $this->checkTemplateFileExists();
484 return $fileName ?
$fileName : parent
::getTemplateFileName();
488 * Default extra tpl file basically just replaces .tpl with .extra.tpl
489 * i.e. we dont override
493 public function overrideExtraTemplateFileName() {
494 $fileName = $this->checkTemplateFileExists('extra.');
495 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();