3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * Builds a form of shortcodes that can be added to WP posts.
37 * Use hook_civicrm_preProcess to modify this list.
39 class CRM_Core_Form_ShortCode
extends CRM_Core_Form
{
41 * List of entities supported by shortcodes, and their form properties.
43 * Keys should be the "component" string for the shortcode
44 * Values should be an array with label and select.
45 * Select can be NULL if there is no entity to select.
46 * Otherwise it contains the shortcode key for this entity id (usually 'id') plus an array of params for the EntityRef field
47 * @see CRM_Core_Form::addEntityRef
51 * label => Option Label
52 * select => key + EntityRef params
55 public $components = array();
58 * List of radio option groups to display on the form
60 * Control the conditional logic of showing/hiding each group via the "components" array.
61 * Or set 'components' => TRUE if it applies to all
64 * [key, components, options]
66 public $options = array();
70 * Build form data. Can be modified via hook_civicrm_preProcess.
72 public function preProcess() {
73 $config = CRM_Core_Config
::singleton();
75 $this->components
['user-dashboard'] = array(
76 'label' => ts("User Dashboard"),
79 $this->components
['profile'] = array(
80 'label' => ts("Profile"),
83 'entity' => 'UFGroup',
84 'select' => array('minimumInputLength' => 0),
87 'id' => $this->profileAccess(),
93 if (in_array('CiviContribute', $config->enableComponents
)) {
94 $this->components
['contribution'] = array(
95 'label' => ts("Contribution Page"),
98 'entity' => 'ContributionPage',
99 'select' => array('minimumInputLength' => 0),
104 if (in_array('CiviEvent', $config->enableComponents
)) {
105 $this->components
['event'] = array(
106 'label' => ts("Event Page"),
110 'select' => array('minimumInputLength' => 0),
115 if (in_array('CiviCampaign', $config->enableComponents
)) {
116 $this->components
['petition'] = array(
117 'label' => ts("Petition"),
120 'entity' => 'Survey',
121 'select' => array('minimumInputLength' => 0),
124 'activity_type_id' => "Petition",
131 $this->options
= array(
134 'components' => array('event'),
136 'info' => ts('Event Info Page'),
137 'register' => ts('Event Registration Page'),
142 'components' => array('contribution', 'event'),
144 'live' => ts('Live Mode'),
145 'test' => ts('Test Drive'),
150 'components' => array('profile'),
152 'create' => ts('Create'),
153 'edit' => ts('Edit'),
154 'view' => ts('View'),
155 'search' => ts('Search/Public Directory'),
160 'components' => TRUE,
161 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
163 '0' => ts("Don't override"),
164 '1' => ts('Override page content'),
171 * Build form elements based on the above metadata.
173 public function buildQuickForm() {
174 CRM_Core_Resources
::singleton()
175 ->addScriptFile('civicrm', 'js/crm.insert-shortcode.js');
177 $components = CRM_Utils_Array
::collect('label', $this->components
);
178 $data = CRM_Utils_Array
::collect('select', $this->components
);
180 $this->add('select', 'component', NULL, $components, FALSE, array('class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)));
181 $this->add('text', 'entity', NULL, array('placeholder' => ts('- select -')));
183 $options = $defaults = array();
184 foreach ($this->options
as $num => $field) {
185 $this->addRadio("option_$num", CRM_Utils_Array
::value('label', $field), $field['options'], array('allowClear' => FALSE, 'data-key' => $field['key']));
186 if ($field['components'] === TRUE) {
187 $field['components'] = array_keys($this->components
);
189 $options["option_$num"] = $field;
191 // Select 1st option as default
192 $keys = array_keys($field['options']);
193 $defaults["option_$num"] = $keys[0];
196 $this->assign('options', $options);
197 $this->assign('selects', array_keys(array_filter($data)));
198 $this->setDefaults($defaults);
202 * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins.
204 * I'm not proud of this, but here's a workaround to pre-filter the api params
208 private function profileAccess() {
211 FROM civicrm_uf_group g, civicrm_uf_join j
212 WHERE g.is_active = 1
214 AND ( group_type LIKE '%Individual%'
215 OR group_type LIKE '%Contact%' )
216 AND g.id = j.uf_group_id
217 AND j.module = 'Profile'
219 $dao = CRM_Core_DAO
::executeQuery($sql);
221 while ($dao->fetch()) {
224 return array('IN' => $ids);
227 // No postProccess fn; this form never gets submitted