From 2229cf4f8e35547b46e4dc8d3e5b3f42d2596645 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 17 Feb 2019 19:56:00 -0500 Subject: [PATCH] EntityRef - standardize on PascalCase for entity name --- CRM/Case/BAO/Case.php | 2 +- CRM/Contact/BAO/Contact.php | 8 ++++---- CRM/Core/Form.php | 8 +++----- CRM/Core/Form/Renderer.php | 8 ++++---- CRM/Core/Resources.php | 4 ++-- js/Common.js | 17 ++++++++++------- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 18549754e0..5bebd48eb6 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -3278,7 +3278,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; ], ]; foreach (CRM_Contact_BAO_Contact::getEntityRefFilters() as $filter) { - $filter += ['entity' => 'contact']; + $filter += ['entity' => 'Contact']; $filter['key'] = 'contact_id.' . $filter['key']; $filters[] = $filter; } diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index b3ebb16e28..a38e8409cb 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -3704,10 +3704,10 @@ LEFT JOIN civicrm_address ON ( civicrm_address.contact_id = civicrm_contact.id ) public static function getEntityRefFilters() { return [ ['key' => 'contact_type', 'value' => ts('Contact Type')], - ['key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'], - ['key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'], - ['key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'], - ['key' => 'country', 'value' => ts('Country'), 'entity' => 'address'], + ['key' => 'group', 'value' => ts('Group'), 'entity' => 'GroupContact'], + ['key' => 'tag', 'value' => ts('Tag'), 'entity' => 'EntityTag'], + ['key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'Address'], + ['key' => 'country', 'value' => ts('Country'), 'entity' => 'Address'], ['key' => 'gender_id', 'value' => ts('Gender'), 'condition' => ['contact_type' => 'Individual']], ['key' => 'is_deceased', 'value' => ts('Deceased'), 'condition' => ['contact_type' => 'Individual']], ['key' => 'contact_id', 'value' => ts('Contact ID'), 'type' => 'text'], diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 5ad43923f9..597e543788 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1936,10 +1936,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * @param array $props * Mix of html and widget properties, including:. * - select - params to give to select2 widget - * - entity - defaults to contact + * - entity - defaults to Contact * - create - can the user create a new entity on-the-fly? * Set to TRUE if entity is contact and you want the default profiles, - * or pass in your own set of links. @see CRM_Core_BAO_UFGroup::getCreateLinks for format + * or pass in your own set of links. @see CRM_Campaign_BAO_Campaign::getEntityRefCreateLinks for format * note that permissions are checked automatically * - api - array of settings for the getlist api wrapper * note that it accepts a 'params' setting which will be passed to the underlying api @@ -1951,11 +1951,9 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * @return HTML_QuickForm_Element */ public function addEntityRef($name, $label = '', $props = array(), $required = FALSE) { - require_once "api/api.php"; - $config = CRM_Core_Config::singleton(); // Default properties $props['api'] = CRM_Utils_Array::value('api', $props, array()); - $props['entity'] = _civicrm_api_get_entity_name_from_camel(CRM_Utils_Array::value('entity', $props, 'contact')); + $props['entity'] = CRM_Utils_String::convertStringToCamel(CRM_Utils_Array::value('entity', $props, 'Contact')); $props['class'] = ltrim(CRM_Utils_Array::value('class', $props, '') . ' crm-form-entityref'); if (array_key_exists('create', $props) && empty($props['create'])) { diff --git a/CRM/Core/Form/Renderer.php b/CRM/Core/Form/Renderer.php index 230cfa142f..764a0098d1 100644 --- a/CRM/Core/Form/Renderer.php +++ b/CRM/Core/Form/Renderer.php @@ -314,20 +314,20 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { * @param HTML_QuickForm_element $field */ public function renderFrozenEntityRef(&$el, $field) { - $entity = strtolower($field->getAttribute('data-api-entity')); + $entity = $field->getAttribute('data-api-entity'); $vals = json_decode($field->getAttribute('data-entity-value'), TRUE); $display = array(); // Custom fields of type contactRef store their data in a slightly different format - if ($field->getAttribute('data-crm-custom') && $entity == 'contact') { + if ($field->getAttribute('data-crm-custom') && $entity == 'Contact') { $vals = array(array('id' => $vals['id'], 'label' => $vals['text'])); } foreach ($vals as $val) { // Format contact as link - if ($entity == 'contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) { + if ($entity == 'Contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) { $url = CRM_Utils_System::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id'])); - $val['label'] = '' . $val['label'] . ''; + $val['label'] = '' . $val['label'] . ''; } $display[] = $val['label']; } diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 2963099d63..bf98e4f6e9 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -830,12 +830,12 @@ class CRM_Core_Resources { if (class_exists($baoName)) { $filters = $baoName::getEntityRefFilters(); if ($filters) { - $data['filters'][_civicrm_api_get_entity_name_from_camel($entity)] = $filters; + $data['filters'][$entity] = $filters; } if (is_callable([$baoName, 'getEntityRefCreateLinks'])) { $createLinks = $baoName::getEntityRefCreateLinks(); if ($createLinks) { - $data['links'][_civicrm_api_get_entity_name_from_camel($entity)] = $createLinks; + $data['links'][$entity] = $createLinks; } } } diff --git a/js/Common.js b/js/Common.js index 687c916845..7561cc2ee8 100644 --- a/js/Common.js +++ b/js/Common.js @@ -466,7 +466,7 @@ if (!CRM.vars) CRM.vars = {}; var entity = $(this).data('api-entity') || ''; $(this) .off('.crmEntity') - .removeClass('crm-form-entityref crm-' + entity.toLowerCase() + '-ref') + .removeClass('crm-form-entityref crm-' + _.kebabCase(entity) + '-ref') .crmSelect2('destroy'); }); } @@ -475,13 +475,17 @@ if (!CRM.vars) CRM.vars = {}; return $(this).each(function() { var $el = $(this).off('.crmEntity'), - entity = options.entity || $el.data('api-entity') || 'contact', + entity = options.entity || $el.data('api-entity') || 'Contact', selectParams = {}; + // Legacy: fix entity name if passed in as snake case + if (entity.charAt(0).toUpperCase() !== entity.charAt(0)) { + entity = _.capitalize(_.camelCase(entity)); + } $el.data('api-entity', entity); $el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select)); $el.data('api-params', $.extend(true, {}, $el.data('api-params') || {}, options.api)); $el.data('create-links', options.create || $el.data('create-links')); - $el.addClass('crm-form-entityref crm-' + entity.toLowerCase() + '-ref'); + $el.addClass('crm-form-entityref crm-' + _.kebabCase(entity) + '-ref'); var settings = { // Use select2 ajax helper instead of CRM.api3 because it provides more value ajax: { @@ -527,7 +531,7 @@ if (!CRM.vars) CRM.vars = {}; } }; // Create new items inline - works for tags - if ($el.data('create-links') && entity.toLowerCase() === 'tag') { + if ($el.data('create-links') && entity === 'Tag') { selectParams.createSearchChoice = function(term, data) { if (!_.findKey(data, {label: term})) { return {id: "0", term: term, label: term + ' (' + ts('new tag') + ')'}; @@ -685,7 +689,7 @@ if (!CRM.vars) CRM.vars = {}; var createLinks = $el.data('create-links'), params = getEntityRefApiParams($el).params, - entity = $el.data('api-entity').toLowerCase(), + entity = $el.data('api-entity'), markup = '