diff --git a/CRM/Campaign/BAO/Campaign.php b/CRM/Campaign/BAO/Campaign.php index 31135918847..9f6416dff19 100644 --- a/CRM/Campaign/BAO/Campaign.php +++ b/CRM/Campaign/BAO/Campaign.php @@ -708,7 +708,7 @@ public static function getEntityRefFilters() { * * @return array|bool */ - public static function entityRefCreateLinks() { + public static function getEntityRefCreateLinks() { if (CRM_Core_Permission::check([['administer CiviCampaign', 'manage campaign']])) { return [ [ diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index b72f1b409a7..b3ebb16e281 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -3649,15 +3649,53 @@ public static function isFieldHasLocationType($fieldTitle) { } /** - * Checks permission to create new contacts from entityRef widget + * @param array $appendProfiles + * Name of profile(s) to append to each link. * - * Note: other components must return an array of links from this function, - * but Contacts are given special treatment - the links are in javascript already. - * - * @return bool + * @return array */ - public static function entityRefCreateLinks() { - return CRM_Core_Permission::check([['profile create', 'profile listings and forms']]); + public static function getEntityRefCreateLinks($appendProfiles = []) { + // You'd think that "create contacts" would be the permission to check, + // But new contact popups are profile forms and those use their own permissions. + if (!CRM_Core_Permission::check([['profile create', 'profile listings and forms']])) { + return FALSE; + } + $profiles = []; + foreach (CRM_Contact_BAO_ContactType::basicTypes() as $contactType) { + $profiles[] = 'new_' . strtolower($contactType); + } + $retrieved = civicrm_api3('uf_group', 'get', [ + 'name' => ['IN' => array_merge($profiles, (array) $appendProfiles)], + 'is_active' => 1, + ]); + $links = $append = []; + if (!empty($retrieved['values'])) { + $icons = [ + 'individual' => 'fa-user', + 'organization' => 'fa-building', + 'household' => 'fa-home', + ]; + foreach ($retrieved['values'] as $id => $profile) { + if (in_array($profile['name'], $profiles)) { + $links[] = array( + 'label' => $profile['title'], + 'url' => CRM_Utils_System::url('civicrm/profile/create', "reset=1&context=dialog&gid=$id", + NULL, NULL, FALSE, FALSE, TRUE), + 'type' => ucfirst(str_replace('new_', '', $profile['name'])), + 'icon' => CRM_Utils_Array::value(str_replace('new_', '', $profile['name']), $icons), + ); + } + else { + $append[] = $id; + } + } + foreach ($append as $id) { + foreach ($links as &$link) { + $link['url'] .= ",$id"; + } + } + } + return $links; } /** diff --git a/CRM/Contact/Form/Edit/Address.php b/CRM/Contact/Form/Edit/Address.php index 1f80087afc5..9dad5533f51 100644 --- a/CRM/Contact/Form/Edit/Address.php +++ b/CRM/Contact/Form/Edit/Address.php @@ -165,7 +165,7 @@ public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharin $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address')); // Override the default profile links to add address form - $profileLinks = CRM_Contact_BAO_Contact::entityRefCreateLinks() ? CRM_Core_BAO_UFGroup::getCreateLinks('', 'shared_address') : FALSE; + $profileLinks = CRM_Contact_BAO_Contact::getEntityRefCreateLinks('shared_address'); $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks)); } } diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 952e70aedfe..c5910c6b1b6 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -3320,57 +3320,6 @@ public static function setComponentDefaults(&$fields, $componentId, $component, } } - /** - * @param array|string $profiles - name of profile(s) to create links for - * @param array $appendProfiles - * Name of profile(s) to append to each link. - * - * @return array - */ - public static function getCreateLinks($profiles = '', $appendProfiles = array()) { - if (!CRM_Contact_BAO_Contact::entityRefCreateLinks()) { - return []; - } - // Default to contact profiles - if (!$profiles) { - $profiles = array('new_individual', 'new_organization', 'new_household'); - } - $profiles = (array) $profiles; - $toGet = array_merge($profiles, (array) $appendProfiles); - $retrieved = civicrm_api3('uf_group', 'get', array( - 'name' => array('IN' => $toGet), - 'is_active' => 1, - )); - $links = $append = array(); - if (!empty($retrieved['values'])) { - $icons = [ - 'individual' => 'fa-user', - 'organization' => 'fa-building', - 'household' => 'fa-home', - ]; - foreach ($retrieved['values'] as $id => $profile) { - if (in_array($profile['name'], $profiles)) { - $links[] = array( - 'label' => $profile['title'], - 'url' => CRM_Utils_System::url('civicrm/profile/create', "reset=1&context=dialog&gid=$id", - NULL, NULL, FALSE, FALSE, TRUE), - 'type' => ucfirst(str_replace('new_', '', $profile['name'])), - 'icon' => CRM_Utils_Array::value(str_replace('new_', '', $profile['name']), $icons), - ); - } - else { - $append[] = $id; - } - } - foreach ($append as $id) { - foreach ($links as &$link) { - $link['url'] .= ",$id"; - } - } - } - return $links; - } - /** * Retrieve groups of profiles. * diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index ac378d88f7c..5ad43923f9e 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1958,11 +1958,6 @@ public function addEntityRef($name, $label = '', $props = array(), $required = F $props['entity'] = _civicrm_api_get_entity_name_from_camel(CRM_Utils_Array::value('entity', $props, 'contact')); $props['class'] = ltrim(CRM_Utils_Array::value('class', $props, '') . ' crm-form-entityref'); - if (isset($props['create']) && $props['create'] === TRUE) { - require_once "api/v3/utils.php"; - $baoClass = _civicrm_api3_get_BAO($props['entity']); - $props['create'] = $baoClass && is_callable([$baoClass, 'entityRefCreateLinks']) ? $baoClass::entityRefCreateLinks() : FALSE; - } if (array_key_exists('create', $props) && empty($props['create'])) { unset($props['create']); } diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 17786d3f0d5..2963099d63a 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -681,10 +681,7 @@ public static function outputLocalizationJS() { 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)), 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')), 'otherSearch' => json_encode(ts('Enter search term...')), - 'entityRef' => array( - 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(), - 'filters' => self::getEntityRefFilters(), - ), + 'entityRef' => self::getEntityRefMetadata(), 'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled, 'allowAlertAutodismissal' => (bool) Civi::settings()->get('allow_alert_autodismissal'), 'resourceCacheCode' => self::singleton()->getCacheCode(), @@ -807,8 +804,11 @@ public static function isAjaxMode() { * * @return array */ - public static function getEntityRefFilters() { - $filters = array(); + public static function getEntityRefMetadata() { + $data = [ + 'filters' => [], + 'links' => [], + ]; $config = CRM_Core_Config::singleton(); $disabledComponents = []; @@ -828,16 +828,22 @@ public static function getEntityRefFilters() { } $baoName = str_replace('_DAO_', '_BAO_', $daoName); if (class_exists($baoName)) { - $entityFilters = $baoName::getEntityRefFilters(); - if ($entityFilters) { - $filters[_civicrm_api_get_entity_name_from_camel($entity)] = $entityFilters; + $filters = $baoName::getEntityRefFilters(); + if ($filters) { + $data['filters'][_civicrm_api_get_entity_name_from_camel($entity)] = $filters; + } + if (is_callable([$baoName, 'getEntityRefCreateLinks'])) { + $createLinks = $baoName::getEntityRefCreateLinks(); + if ($createLinks) { + $data['links'][_civicrm_api_get_entity_name_from_camel($entity)] = $createLinks; + } } } } - CRM_Utils_Hook::entityRefFilters($filters); + CRM_Utils_Hook::entityRefFilters($data['filters']); - return $filters; + return $data; } /** diff --git a/js/Common.js b/js/Common.js index 01bc7572baf..687c9168454 100644 --- a/js/Common.js +++ b/js/Common.js @@ -685,12 +685,13 @@ if (!CRM.vars) CRM.vars = {}; var createLinks = $el.data('create-links'), params = getEntityRefApiParams($el).params, + entity = $el.data('api-entity').toLowerCase(), markup = '