CRM-14165 - HR-221 Further abstract options editing to work with civiHR
authorColeman Watts <coleman@civicrm.org>
Wed, 12 Feb 2014 23:47:30 +0000 (15:47 -0800)
committerColeman Watts <coleman@civicrm.org>
Wed, 12 Feb 2014 23:47:30 +0000 (15:47 -0800)
CRM/Core/BAO/CustomField.php
CRM/Core/Form.php
CRM/Core/Form/Renderer.php
js/Common.js

index aba4f6df3c42c7b5b267d7092e00f28dafecfbac..5fac6b6f60ceb9ced2a228ee3033c6be9b40fdb8 100644 (file)
@@ -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),
       );
     }
 
index d1833387985c945a38c2a79923273ecdea8f0db7..2bc7a2f9a98056aa2b069d26377c9bc1672bd139 100644 (file)
@@ -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'];
index e619497deaf2228de78e4b203b524ad20c0799e3..5ce90d308525493343c2a12a1e21bde5646f154f 100644 (file)
@@ -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'] .= ' <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>';
     }
   }
 
index 6ec6e97e1c408b61d47a83e3109df6bc3b41ea31..7e253882c347ea0275afd8dc7d734666494737d1 100644 (file)
@@ -269,7 +269,7 @@ CRM.validate = CRM.validate || {
         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');
     });
   };
 
@@ -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;
       })