$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),
);
}
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 {
}
}
$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'];
}
// 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);
}
*/
function addOptionsEditLink(&$el, $field) {
if (CRM_Core_Permission::check('administer CiviCRM')) {
- $el['html'] .= ' <a href="#" class="crm-edit-optionvalue-link crm-hover-button" title="' . ts('Edit Options') . '" data-option-group-url="' . $field->getAttribute('data-option-group-url') . '"><span class="icon edit-icon"></span></a>';
+ // 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'] .= ' <a href="' . $url . '" class="crm-option-edit-link crm-hover-button" target="_blank" title="' . ts('Edit Options') . '" data-option-edit-path="' . $path . '"><span class="icon edit-icon"></span></a>';
}
}
var selected = ($.inArray(''+option.key, val) > -1) ? 'selected="selected"' : '';
$elect.append('<option value="' + option.key + '"' + selected + '>' + option.value + '</option>');
});
- $elect.trigger('change');
+ $elect.trigger('crmOptionsUpdated').trigger('change');
});
};
}
})
- .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;
})