3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Builds a form of shortcodes that can be added to WP posts.
21 * Use hook_civicrm_preProcess to modify this list.
23 class CRM_Core_Form_ShortCode
extends CRM_Core_Form
{
25 * List of entities supported by shortcodes, and their form properties.
27 * Keys should be the "component" string for the shortcode
28 * Values should be an array with label and select.
29 * Select can be NULL if there is no entity to select.
30 * Otherwise it contains the shortcode key for this entity id (usually 'id') plus an array of params for the EntityRef field
34 * label => Option Label
35 * select => key + EntityRef params
37 * @see CRM_Core_Form::addEntityRef
39 public $components = [];
42 * List of radio option groups to display on the form
44 * Control the conditional logic of showing/hiding each group via the "components" array.
45 * Or set 'components' => TRUE if it applies to all
48 * [key, components, options]
53 * Build form data. Can be modified via hook_civicrm_preProcess.
55 public function preProcess() {
56 $config = CRM_Core_Config
::singleton();
58 $this->components
['user-dashboard'] = [
59 'label' => ts("User Dashboard"),
62 $this->components
['profile'] = [
63 'label' => ts("Profile"),
66 'entity' => 'UFGroup',
67 'select' => ['minimumInputLength' => 0],
70 'id' => $this->profileAccess(),
76 if (in_array('CiviContribute', $config->enableComponents
)) {
77 $this->components
['contribution'] = [
78 'label' => ts("Contribution Page"),
81 'entity' => 'ContributionPage',
82 'select' => ['minimumInputLength' => 0],
85 $this->components
['pcp'] = [
86 'label' => ts("Personal Campaign Page"),
90 'select' => ['minimumInputLength' => 0],
95 if (in_array('CiviEvent', $config->enableComponents
)) {
96 $this->components
['event'] = [
97 'label' => ts("Event Page"),
101 'select' => ['minimumInputLength' => 0],
106 if (in_array('CiviCampaign', $config->enableComponents
)) {
107 $this->components
['petition'] = [
108 'label' => ts("Petition"),
111 'entity' => 'Survey',
112 'select' => ['minimumInputLength' => 0],
115 'activity_type_id' => "Petition",
125 'components' => ['contribution'],
127 'transact' => ts('Contribution Page'),
128 'setup' => ts('Setup a Personal Campaign Page'),
133 'components' => ['event'],
135 'info' => ts('Event Info Page'),
136 'register' => ts('Event Registration Page'),
141 'components' => ['pcp'],
143 'info' => ts('Info Page'),
144 'transact' => ts('Contribution Page'),
149 'components' => ['contribution', 'pcp', 'event'],
151 'live' => ts('Live Mode'),
152 'test' => ts('Test Drive'),
157 'components' => ['profile'],
159 'create' => ts('Create'),
160 'edit' => ts('Edit'),
161 'view' => ts('View'),
162 'search' => ts('Search/Public Directory'),
163 'map' => ts('Map View'),
168 'components' => TRUE,
169 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
171 '0' => ts("Don't override"),
172 '1' => ts('Override page content'),
179 * Build form elements based on the above metadata.
181 public function buildQuickForm() {
182 $components = CRM_Utils_Array
::collect('label', $this->components
);
183 $data = CRM_Utils_Array
::collect('select', $this->components
);
185 $this->add('select', 'component', NULL, $components, FALSE, ['class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)]);
186 $this->add('text', 'entity', NULL, ['placeholder' => ts('- select -')]);
188 $options = $defaults = [];
189 foreach ($this->options
as $num => $field) {
190 $this->addRadio("option_$num", CRM_Utils_Array
::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]);
191 if ($field['components'] === TRUE) {
192 $field['components'] = array_keys($this->components
);
194 $options["option_$num"] = $field;
196 // Select 1st option as default
197 $keys = array_keys($field['options']);
198 $defaults["option_$num"] = $keys[0];
201 $this->assign('options', $options);
202 $this->assign('selects', array_keys(array_filter($data)));
203 $this->setDefaults($defaults);
207 * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins.
209 * I'm not proud of this, but here's a workaround to pre-filter the api params
213 private function profileAccess() {
216 FROM civicrm_uf_group g, civicrm_uf_join j
217 WHERE g.is_active = 1
219 AND ( group_type LIKE '%Individual%'
220 OR group_type LIKE '%Contact%' )
221 AND g.id = j.uf_group_id
222 AND j.module = 'Profile'
224 $dao = CRM_Core_DAO
::executeQuery($sql);
226 while ($dao->fetch()) {
229 return ['IN' => $ids];
232 // No postProccess fn; this form never gets submitted