From 475e9f440a10dc2bd1d724784cab4306b60687d7 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 4 Feb 2014 14:26:30 -0800 Subject: [PATCH] CRM-14165 - Add client-side options editor and add to activity form --- CRM/Activity/Form/Activity.php | 23 ++++------- CRM/Case/Form/Activity.php | 2 + CRM/Core/Form.php | 16 ++++--- CRM/Core/Form/Renderer.php | 4 +- js/Common.js | 71 ++++++++++++++++++++++++-------- xml/schema/Activity/Activity.xml | 4 +- 6 files changed, 77 insertions(+), 43 deletions(-) diff --git a/CRM/Activity/Form/Activity.php b/CRM/Activity/Form/Activity.php index f4b28b9df9..23af98507e 100644 --- a/CRM/Activity/Form/Activity.php +++ b/CRM/Activity/Form/Activity.php @@ -159,17 +159,11 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { ), 'status_id' => array( 'type' => 'select', - 'label' => ts('Status'), - 'attributes' => - CRM_Core_PseudoConstant::activityStatus(), - 'required' => TRUE + 'required' => TRUE, ), 'priority_id' => array( 'type' => 'select', - 'label' => ts('Priority'), - 'attributes' => - CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'), - 'required' => TRUE + 'required' => TRUE, ), 'source_contact_id' => array( 'type' => 'text', @@ -694,18 +688,15 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { foreach ($this->_fields as $field => $values) { if (!empty($this->_fields[$field])) { - $attribute = NULL; - if (!empty($values['attributes'])) { - $attribute = $values['attributes']; - } + $attribute = CRM_Utils_Array::value('attributes', $values); + $required = !empty($values['required']); - $required = FALSE; - if (!empty($values['required'])) { - $required = TRUE; - } if ($values['type'] == 'wysiwyg') { $this->addWysiwyg($field, $values['label'], $attribute, $required); } + elseif ($values['type'] == 'select' && empty($attribute)) { + $this->addSelect('CRM_Activity_BAO_Activity', $field, array(), $required); + } elseif ($field != 'source_contact_id') { $this->add($values['type'], $field, $values['label'], $attribute, $required); } diff --git a/CRM/Case/Form/Activity.php b/CRM/Case/Form/Activity.php index eb0d6fcc64..a6f2f9f04d 100644 --- a/CRM/Case/Form/Activity.php +++ b/CRM/Case/Form/Activity.php @@ -290,6 +290,8 @@ class CRM_Case_Form_Activity extends CRM_Activity_Form_Activity { $this->assign('urlPath', 'civicrm/case/activity'); $encounterMediums = CRM_Case_PseudoConstant::encounterMedium(); + // Fixme: what's the justification for this? It seems like it is just re-adding an option in case it is the default and disabled. + // Is that really a big problem? if ($this->_activityTypeFile == 'OpenCase') { $this->_encounterMedium = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId, 'medium_id' diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 8b20d5c652..b6b573f7a3 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -904,6 +904,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { /** * Adds a select based on field metadata * TODO: This could be even more generic and widget type (select in this case) could also be read from metadata + * Perhaps a method like $form->bind($name) which would look up all metadata for named field * @param CRM_Core_DAO $baoName - string representing bao object * @param $name * @param array $props @@ -922,25 +923,28 @@ class CRM_Core_Form extends HTML_QuickForm_Page { if (!empty($props['placeholder']) && empty($props['multiple'])) { $options = array('' => '') + $options; } + $bao = new $baoName; // Handle custom field if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) { list(, $id) = explode('_', $name); $label = isset($props['label']) ? $props['label'] : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id); - $props['data-option-group'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id); + $gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id); + $props['data-option-group-url'] = 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $gid); } // Core field else { - $bao = new $baoName; $meta = $bao->getFieldSpec($name); - $bao->free(); - // Todo: localize - $label = CRM_Utils_Array::value('label', $props, $meta['title']); + $label = isset($props['label']) ? $props['label'] : ts($meta['title']); if (!empty($meta['pseudoconstant']['optionGroupName'])) { - $props['data-option-group'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $meta['pseudoconstant']['optionGroupName'], 'id', 'name'); + $props['data-option-group-url'] = 'civicrm/admin/options/' . $meta['pseudoconstant']['optionGroupName']; } } + require_once 'api/api.php'; + $props['data-api-entity'] = _civicrm_api_get_entity_name_from_dao($bao); + $bao->free(); $props['class'] = isset($props['class']) ? $props['class'] . ' ' : ''; $props['class'] .= "crm-select2"; + CRM_Utils_Array::remove($props, 'label'); return $this->add('select', $name, $label, $options, $required, $props); } diff --git a/CRM/Core/Form/Renderer.php b/CRM/Core/Form/Renderer.php index bc15901d88..0783e4a046 100644 --- a/CRM/Core/Form/Renderer.php +++ b/CRM/Core/Form/Renderer.php @@ -117,7 +117,7 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { } } - if ($element->getType() == 'select' && $element->getAttribute('data-option-group')) { + if ($element->getType() == 'select' && $element->getAttribute('data-option-group-url')) { $this->addOptionsEditLink($el, $element); } @@ -216,7 +216,7 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { */ function addOptionsEditLink(&$el, $field) { if (CRM_Core_Permission::check('administer CiviCRM')) { - $el['html'] .= '   '; + $el['html'] .= '   '; } } } diff --git a/js/Common.js b/js/Common.js index 37f299e93d..0fc1c0fc73 100644 --- a/js/Common.js +++ b/js/Common.js @@ -247,6 +247,7 @@ function showHideRow(index) { return false; } +CRM.utils = CRM.utils || {}; CRM.strings = CRM.strings || {}; CRM.validate = CRM.validate || { params: {}, @@ -266,6 +267,28 @@ CRM.validate = CRM.validate || { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; + /** + * Populate a select list, overwriting the existing options except for the placeholder. + * @param $el jquery collection - 1 or more select elements + * @param options array in format returned by api.getoptions + */ + CRM.utils.setOptions = function($el, options) { + $el.each(function() { + var + $elect = $(this), + val = $elect.val() || []; + if (typeof(val) !== 'array') { + val = [val]; + } + $elect.find('option[value!=""]').remove(); + $.each(options, function(key, option) { + var selected = ($.inArray(''+option.key, val) > -1) ? 'selected="selected"' : ''; + $elect.append(''); + }); + $elect.trigger('change'); + }); + }; + // Initialize widgets $(document).on('crmLoad', function(e) { $('table.row-highlight', e.target) @@ -907,26 +930,40 @@ CRM.validate = CRM.validate || { } // bind the event for image popup - $('body').on('click', 'a.crm-image-popup', function() { - var o = $('
'); + $('body') + .on('click', 'a.crm-image-popup', function() { + var o = $('
'); - CRM.confirm('', - { - title: ts('Preview'), - message: o - }, - ts('Done') - ); - return false; - }); + CRM.confirm('', + { + title: ts('Preview'), + message: o + }, + ts('Done') + ); + return false; + }) + .on('click', function (event) { + $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); + if ($(event.target).is('.btn-slide')) { + $(event.target).addClass('btn-slide-active').find('.panel').show(); + } + }) + + .on('click', 'a.crm-edit-optionvalue-link', function() { + var url = $(this).data('option-group-url'); + CRM.loadForm(CRM.url(url, {reset: 1})) + .on('dialogclose', function() { + var $elects = $('select[data-option-group-url="' + url + '"]'); + CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.attr('name')}) + .done(function(data) { + CRM.utils.setOptions($elects, data.values); + }); + }); + return false; + }); $().crmtooltip(); - $('body').on('click', function (event) { - $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); - if ($(event.target).is('.btn-slide')) { - $(event.target).addClass('btn-slide-active').find('.panel').show(); - } - }); }); $.fn.crmAccordions = function (speed) { diff --git a/xml/schema/Activity/Activity.xml b/xml/schema/Activity/Activity.xml index 4f7c55879a..063b36313a 100644 --- a/xml/schema/Activity/Activity.xml +++ b/xml/schema/Activity/Activity.xml @@ -60,7 +60,7 @@ activity_type_id - Activity Type ID + Activity Type true int unsigned true @@ -248,7 +248,7 @@ status_id activity_status_id - Activity Status Id + Activity Status true false /(activity.)?status(.label$)?/i -- 2.25.1