From 15f842bdd85a0ada060cad92ec61e66e4750fff1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 10 Jun 2015 07:54:07 -0400 Subject: [PATCH] CRM-16650 - Rewrite shortcode form ---------------------------------------- * CRM-16650: Allow hooks to modify available shortcodes https://issues.civicrm.org/jira/browse/CRM-16650 --- CRM/Core/Form/ShortCode.php | 184 ++++++++++++++++++++++++++ CRM/Core/xml/Menu/Misc.xml | 6 + js/crm.insert-shortcode.js | 52 ++++++++ templates/CRM/Core/Form/ShortCode.tpl | 53 ++++++++ 4 files changed, 295 insertions(+) create mode 100755 CRM/Core/Form/ShortCode.php create mode 100755 js/crm.insert-shortcode.js create mode 100755 templates/CRM/Core/Form/ShortCode.tpl diff --git a/CRM/Core/Form/ShortCode.php b/CRM/Core/Form/ShortCode.php new file mode 100755 index 0000000000..2f501072f2 --- /dev/null +++ b/CRM/Core/Form/ShortCode.php @@ -0,0 +1,184 @@ +components['user-dashboard'] = array( + 'label' => ts("User Dashboard"), + 'select' => NULL, + ); + $this->components['profile'] = array( + 'label' => ts("Profile"), + 'select' => array( + 'key' => 'gid', + 'entity' => 'Profile', + 'select' => array('minimumInputLength' => 0), + ), + ); + + if (in_array('CiviContribute', $config->enableComponents)) { + $this->components['contribution'] = array( + 'label' => ts("Contribution Page"), + 'select' => array( + 'key' => 'id', + 'entity' => 'ContributionPage', + 'select' => array('minimumInputLength' => 0), + ), + ); + } + + if (in_array('CiviEvent', $config->enableComponents)) { + $this->components['event'] = array( + 'label' => ts("Event Page"), + 'select' => array( + 'key' => 'id', + 'entity' => 'Event', + 'select' => array('minimumInputLength' => 0), + ), + ); + } + + if (in_array('CiviCampaign', $config->enableComponents)) { + $this->components['petition'] = array( + 'label' => ts("Petition"), + 'select' => array( + 'key' => 'id', + 'entity' => 'Survey', + 'select' => array('minimumInputLength' => 0), + ), + ); + } + + $this->options = array( + array( + 'key' => 'action', + 'components' => array('event'), + 'options' => array( + 'info' => ts('Event Info Page'), + 'register' => ts('Event Registration Page'), + ), + ), + array( + 'key' => 'mode', + 'components' => array('contribution', 'event'), + 'options' => array( + 'live' => ts('Live Mode'), + 'test' => ts('Test Drive'), + ), + ), + array( + 'key' => 'mode', + 'components' => array('profile'), + 'options' => array( + 'create' => ts('Create'), + 'edit' => ts('Edit'), + 'view' => ts('View'), + 'search' => ts('Search/Public Directory'), + ), + ), + array( + 'key' => 'hijack', + 'components' => '*', + 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'), + 'options' => array( + '0' => ts("Don't override"), + '1' => ts('Override page content'), + ), + ), + ); + } + + /** + * Build form elements based on the above metadata + * + * @return void + */ + public function buildQuickForm() { + CRM_Core_Resources::singleton() + ->addScriptFile('civicrm', 'js/crm.insert-shortcode.js'); + + $components = CRM_Utils_Array::collect('label', $this->components); + $data = CRM_Utils_Array::collect('select', $this->components); + + $this->add('select', 'component', NULL, $components, FALSE, array('class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data))); + $this->add('text', 'entity', NULL, array('placeholder' => ts('- select -'))); + + $options = $defaults = array(); + foreach ($this->options as $num => $field) { + $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], array('allowClear' => FALSE, 'data-key' => $field['key'])); + if ($field['components'] === '*') { + $field['components'] = array_keys($this->components); + } + $options["option_$num"] = $field; + + // Select 1st option as default + $keys = array_keys($field['options']); + $defaults["option_$num"] = $keys[0]; + } + + $this->assign('options', $options); + $this->assign('selects', array_keys(array_filter($data))); + $this->setDefaults($defaults); + } + + // No postProccess fn; this form never gets submitted + +} diff --git a/CRM/Core/xml/Menu/Misc.xml b/CRM/Core/xml/Menu/Misc.xml index f6703f958f..aec5f169d7 100644 --- a/CRM/Core/xml/Menu/Misc.xml +++ b/CRM/Core/xml/Menu/Misc.xml @@ -201,4 +201,10 @@ CRM_Core_Resources::outputLocalizationJS 1 + + civicrm/shortcode + CRM_Core_Form_ShortCode + access CiviCRM + Insert CiviCRM Content + diff --git a/js/crm.insert-shortcode.js b/js/crm.insert-shortcode.js new file mode 100755 index 0000000000..778d312480 --- /dev/null +++ b/js/crm.insert-shortcode.js @@ -0,0 +1,52 @@ +// https://civicrm.org/licensing + +CRM.$(function($) { + var $form = $('form.CRM_Core_Form_ShortCode'); + + function changeComponent() { + var component = $(this).val(), + entities = $(this).data('entities'); + + $('.shortcode-param[data-components]', $form).each(function() { + $(this).toggle($.inArray(component, $(this).data('components')) > -1); + + if (entities[component]) { + $('input[name=entity]') + .val('') + .data('key', entities[component].key) + .data('select-params', null) + .data('api-params', null) + .crmEntityRef(entities[component]); + } + }); + } + + function close() { + $form.closest('.ui-dialog-content').dialog('close'); + } + + function insert() { + var code = '[civicrm'; + $('.shortcode-param:visible', $form).each(function() { + var $el = $('input:checked, select, input.crm-form-entityref', this); + code += ' ' + $el.data('key') + '="' + $el.val() + '"'; + }); + window.send_to_editor(code + ']'); + close(); + } + + $('select[name=component]', $form).each(changeComponent).change(changeComponent); + + $form.closest('.ui-dialog-content').dialog('option', 'buttons', [ + { + text: ts("Insert"), + icons: {primary: "ui-icon-check"}, + click: insert + }, + { + text: ts("Cancel"), + icons: {primary: "ui-icon-close"}, + click: close + } + ]); +}); \ No newline at end of file diff --git a/templates/CRM/Core/Form/ShortCode.tpl b/templates/CRM/Core/Form/ShortCode.tpl new file mode 100755 index 0000000000..1b835cf831 --- /dev/null +++ b/templates/CRM/Core/Form/ShortCode.tpl @@ -0,0 +1,53 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 4.6 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2015 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} +
+ {ts}Can't find your form? Make sure it is active.{/ts} +
+ +
+ {$form.component.html}   + {$form.entity.html} +
+ +{foreach from=$options key='item' item='option'} +
+ {if $form.$item.label} +

{$form.$item.label}

+ {/if} + {$form.$item.html} +
+ +{/foreach} + +{* Hack to prevent WP toolbars from popping up above the dialog *} +{literal}{/literal} -- 2.25.1