CRM-16650 - Rewrite shortcode form
authorColeman Watts <coleman@civicrm.org>
Wed, 10 Jun 2015 11:54:07 +0000 (07:54 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 10 Jun 2015 14:10:46 +0000 (10:10 -0400)
----------------------------------------
* CRM-16650: Allow hooks to modify available shortcodes
  https://issues.civicrm.org/jira/browse/CRM-16650

CRM/Core/Form/ShortCode.php [new file with mode: 0755]
CRM/Core/xml/Menu/Misc.xml
js/crm.insert-shortcode.js [new file with mode: 0755]
templates/CRM/Core/Form/ShortCode.tpl [new file with mode: 0755]

diff --git a/CRM/Core/Form/ShortCode.php b/CRM/Core/Form/ShortCode.php
new file mode 100755 (executable)
index 0000000..2f50107
--- /dev/null
@@ -0,0 +1,184 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | 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        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2015
+ * $Id$
+ *
+ */
+
+/**
+ * Builds a form of shortcodes that can be added to WP posts
+ * Use hook_civicrm_preProcess to modify this list
+ */
+class CRM_Core_Form_ShortCode extends CRM_Core_Form {
+  /**
+   * List of entities supported by shortcodes, and their form properties
+   *
+   * @var array
+   */
+  public $components = array();
+
+  /**
+   * List of options to display on the form
+   *
+   * @var array
+   */
+  public $options = array();
+
+
+  /**
+   * Build form data. Overridable via hook_civicrm_preProcess
+   *
+   * @return void
+   */
+  public function preProcess() {
+    $config = CRM_Core_Config::singleton();
+
+    $this->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
+
+}
index f6703f958f58c10c889a73614591b78a7157ce1f..aec5f169d76761ffd267d98a9280c32f1117bbd2 100644 (file)
     <page_callback>CRM_Core_Resources::outputLocalizationJS</page_callback>
     <access_callback>1</access_callback>
   </item>
+  <item>
+    <path>civicrm/shortcode</path>
+    <page_callback>CRM_Core_Form_ShortCode</page_callback>
+    <access_arguments>access CiviCRM</access_arguments>
+    <title>Insert CiviCRM Content</title>
+  </item>
 </menu>
diff --git a/js/crm.insert-shortcode.js b/js/crm.insert-shortcode.js
new file mode 100755 (executable)
index 0000000..778d312
--- /dev/null
@@ -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 (executable)
index 0000000..1b835cf
--- /dev/null
@@ -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        |
+ +--------------------------------------------------------------------+
+*}
+<div class="help">
+  {ts}Can't find your form? Make sure it is active.{/ts}
+</div>
+
+<div class="crm-field-wrapper">
+  <span class="shortcode-param">{$form.component.html}</span>&nbsp;&nbsp;
+  <span class="shortcode-param" data-components='{$selects|@json_encode}'>{$form.entity.html}</span>
+</div>
+
+{foreach from=$options key='item' item='option'}
+  <div class="crm-field-wrapper shortcode-param" data-components='{$option.components|@json_encode}'>
+    {if $form.$item.label}
+      <p>{$form.$item.label}</p>
+    {/if}
+    {$form.$item.html}
+  </div>
+
+{/foreach}
+
+{* Hack to prevent WP toolbars from popping up above the dialog *}
+{literal}<style type="text/css">
+  #wpadminbar,
+  #adminmenuwrap,
+  .wp-editor-expand #wp-content-editor-tools,
+  .wp-editor-expand div.mce-toolbar-grp {
+    z-index: 100 !important;
+  }
+</style>{/literal}