Commit e695ee7c authored by colemanw's avatar colemanw

Standardize format for entityRef create links

parent 063b55b8
......@@ -708,7 +708,7 @@ INNER JOIN civicrm_group grp ON ( grp.id = campgrp.entity_id )
*
* @return array|bool
*/
public static function entityRefCreateLinks() {
public static function getEntityRefCreateLinks() {
if (CRM_Core_Permission::check([['administer CiviCampaign', 'manage campaign']])) {
return [
[
......
......@@ -3649,15 +3649,53 @@ LEFT JOIN civicrm_address ON ( civicrm_address.contact_id = civicrm_contact.id )
}
/**
* 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;
}
/**
......
......@@ -165,7 +165,7 @@ class CRM_Contact_Form_Edit_Address {
$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));
}
}
......
......@@ -3320,57 +3320,6 @@ AND ( entity_id IS NULL OR entity_id <= 0 )
}
}
/**
* @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.
*
......
......@@ -1958,11 +1958,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
$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']);
}
......
......@@ -681,10 +681,7 @@ class CRM_Core_Resources {
'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 @@ class CRM_Core_Resources {
*
* @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 @@ class CRM_Core_Resources {
}
$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;
}
/**
......
......@@ -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 = '<div class="crm-entityref-links">';
if (!createLinks || (createLinks === true && $el.data('api-entity').toLowerCase() !== 'contact')) {
if (!createLinks || (createLinks === true && !CRM.config.entityRef.links[entity])) {
return '';
}
if (createLinks === true) {
createLinks = params.contact_type ? _.where(CRM.config.entityRef.contactCreate, {type: params.contact_type}) : CRM.config.entityRef.contactCreate;
createLinks = params.contact_type ? _.where(CRM.config.entityRef.links[entity], {type: params.contact_type}) : CRM.config.entityRef.links[entity];
}
_.each(createLinks, function(link) {
markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">' +
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment