From: Coleman Watts Date: Wed, 12 Feb 2014 23:47:30 +0000 (-0800) Subject: CRM-14165 - HR-221 Further abstract options editing to work with civiHR X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8783107356ceb57f51b6fab507861e8a22d34f20;hp=562207255b86c7f932643761d094cf050af09bf3;p=civicrm-core.git CRM-14165 - HR-221 Further abstract options editing to work with civiHR --- diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index aba4f6df3c..5fac6b6f60 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -763,12 +763,12 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $selectAttributes['multiple'] = 'multiple'; } } - // Add popup link for editing options. Normally this is handled by CRM_Core_Form->addSelect + // Add data so popup link. Normally this is handled by CRM_Core_Form->addSelect if (in_array($field->html_type, array('Select', 'Multi-Select')) && !$search && CRM_Core_Permission::check('administer CiviCRM')) { $selectAttributes += array( 'data-api-entity' => 'contact', // FIXME: This works because the getoptions api isn't picky about custom fields, but it's WRONG 'data-api-field' => 'custom_' . $field->id, - 'data-option-group-url' => 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id), + 'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id), ); } diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index d183338798..2bc7a2f9a9 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -942,7 +942,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { list(, $id) = explode('_', $name); $label = isset($props['label']) ? $props['label'] : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id); $gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id); - $props['data-option-group-url'] = array_key_exists('option_url', $props) ? $props['option_url'] : 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $gid); + $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $gid); } // Core field else { @@ -956,7 +956,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } } $label = isset($props['label']) ? $props['label'] : $fieldSpec['title']; - $props['data-option-group-url'] = array_key_exists('option_url', $props) ? $props['option_url'] : $props['data-option-group-url'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec); + $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : $props['data-option-edit-path'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec); } $props['class'] = (isset($props['class']) ? $props['class'] . ' ' : '') . "crm-select2"; $props['data-api-entity'] = $props['entity']; diff --git a/CRM/Core/Form/Renderer.php b/CRM/Core/Form/Renderer.php index e619497dea..5ce90d3085 100644 --- a/CRM/Core/Form/Renderer.php +++ b/CRM/Core/Form/Renderer.php @@ -126,7 +126,7 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { } // Active form elements else { - if ($element->getType() == 'select' && $element->getAttribute('data-option-group-url')) { + if ($element->getType() == 'select' && $element->getAttribute('data-option-edit-path')) { $this->addOptionsEditLink($el, $element); } @@ -227,7 +227,12 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { */ function addOptionsEditLink(&$el, $field) { if (CRM_Core_Permission::check('administer CiviCRM')) { - $el['html'] .= ' '; + // NOTE: $path is used on the client-side to know which option lists need rebuilding, + // that's why we need that bit of data both in the link and in the form element + $path = $field->getAttribute('data-option-edit-path'); + // NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ? + $url = CRM_Utils_System::url($path, 'reset=1'); + $el['html'] .= ' '; } } diff --git a/js/Common.js b/js/Common.js index 6ec6e97e1c..7e253882c3 100644 --- a/js/Common.js +++ b/js/Common.js @@ -269,7 +269,7 @@ CRM.validate = CRM.validate || { var selected = ($.inArray(''+option.key, val) > -1) ? 'selected="selected"' : ''; $elect.append(''); }); - $elect.trigger('change'); + $elect.trigger('crmOptionsUpdated').trigger('change'); }); }; @@ -961,15 +961,19 @@ CRM.validate = CRM.validate || { } }) - .on('click', 'a.crm-edit-optionvalue-link', function() { - var url = $(this).data('option-group-url'); - CRM.loadForm(CRM.url(url, {reset: 1}), {openInline: 'a:not("[href=#], .no-popup")'}) + .on('click', 'a.crm-option-edit-link', function() { + var link = $(this); + CRM.loadForm(this.href, {openInline: 'a:not("[href=#], .no-popup")'}) + // Lots of things can happen once the form opens, this is the only event we can really rely on .on('dialogclose', function() { - var $elects = $('select[data-option-group-url="' + url + '"]'); - CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.data('api-field')}) - .done(function(data) { - CRM.utils.setOptions($elects, data.values); - }); + link.trigger('crmOptionsEdited'); + var $elects = $('select[data-option-edit-path="' + link.data('option-edit-path') + '"]'); + if ($elects.data('api-entity') && $elects.data('api-field')) { + CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.data('api-field')}) + .done(function(data) { + CRM.utils.setOptions($elects, data.values); + }); + } }); return false; })