Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize format for entityRef create links #13628

Merged
merged 1 commit into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Campaign/BAO/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
[
Expand Down
52 changes: 45 additions & 7 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW - I've taken to using the newer format where you stick ['values'] on the end & then don't need to unpack from values

'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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Edit/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
51 changes: 0 additions & 51 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 0 additions & 5 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
28 changes: 17 additions & 11 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions js/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '">' +
Expand Down