Merge pull request #21578 from civicrm/5.42
[civicrm-core.git] / CRM / Core / Form / ShortCode.php
CommitLineData
15f842bd
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
15f842bd 5 | |
bc77d7c0
TO
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 |
15f842bd
CW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
15f842bd
CW
16 */
17
18/**
8eedd10a 19 * Builds a form of shortcodes that can be added to WP posts.
20 *
21 * Use hook_civicrm_preProcess to modify this list.
15f842bd
CW
22 */
23class CRM_Core_Form_ShortCode extends CRM_Core_Form {
24 /**
b233e1b4
CW
25 * List of entities supported by shortcodes, and their form properties.
26 *
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
15f842bd
CW
31 *
32 * @var array
b233e1b4
CW
33 * [component => [
34 * label => Option Label
35 * select => key + EntityRef params
36 * ]]
518fa0ee 37 * @see CRM_Core_Form::addEntityRef
15f842bd 38 */
be2fb01f 39 public $components = [];
15f842bd
CW
40
41 /**
b233e1b4
CW
42 * List of radio option groups to display on the form
43 *
44 * Control the conditional logic of showing/hiding each group via the "components" array.
45 * Or set 'components' => TRUE if it applies to all
15f842bd
CW
46 *
47 * @var array
b233e1b4 48 * [key, components, options]
15f842bd 49 */
be2fb01f 50 public $options = [];
15f842bd 51
15f842bd 52 /**
8eedd10a 53 * Build form data. Can be modified via hook_civicrm_preProcess.
15f842bd
CW
54 */
55 public function preProcess() {
56 $config = CRM_Core_Config::singleton();
57
be2fb01f 58 $this->components['user-dashboard'] = [
15f842bd
CW
59 'label' => ts("User Dashboard"),
60 'select' => NULL,
be2fb01f
CW
61 ];
62 $this->components['profile'] = [
15f842bd 63 'label' => ts("Profile"),
be2fb01f 64 'select' => [
15f842bd 65 'key' => 'gid',
b233e1b4 66 'entity' => 'UFGroup',
be2fb01f
CW
67 'select' => ['minimumInputLength' => 0],
68 'api' => [
69 'params' => [
b233e1b4 70 'id' => $this->profileAccess(),
be2fb01f
CW
71 ],
72 ],
73 ],
74 ];
15f842bd
CW
75
76 if (in_array('CiviContribute', $config->enableComponents)) {
be2fb01f 77 $this->components['contribution'] = [
15f842bd 78 'label' => ts("Contribution Page"),
be2fb01f 79 'select' => [
15f842bd
CW
80 'key' => 'id',
81 'entity' => 'ContributionPage',
be2fb01f
CW
82 'select' => ['minimumInputLength' => 0],
83 ],
84 ];
65c1b421
MW
85 $this->components['pcp'] = [
86 'label' => ts("Personal Campaign Page"),
87 'select' => [
88 'key' => 'id',
89 'entity' => 'Pcp',
90 'select' => ['minimumInputLength' => 0],
91 ],
92 ];
15f842bd
CW
93 }
94
95 if (in_array('CiviEvent', $config->enableComponents)) {
be2fb01f 96 $this->components['event'] = [
15f842bd 97 'label' => ts("Event Page"),
be2fb01f 98 'select' => [
15f842bd
CW
99 'key' => 'id',
100 'entity' => 'Event',
be2fb01f
CW
101 'select' => ['minimumInputLength' => 0],
102 ],
103 ];
15f842bd
CW
104 }
105
106 if (in_array('CiviCampaign', $config->enableComponents)) {
be2fb01f 107 $this->components['petition'] = [
15f842bd 108 'label' => ts("Petition"),
be2fb01f 109 'select' => [
15f842bd
CW
110 'key' => 'id',
111 'entity' => 'Survey',
be2fb01f
CW
112 'select' => ['minimumInputLength' => 0],
113 'api' => [
114 'params' => [
b233e1b4 115 'activity_type_id' => "Petition",
be2fb01f
CW
116 ],
117 ],
118 ],
119 ];
15f842bd
CW
120 }
121
be2fb01f 122 $this->options = [
cb0aafc7
KK
123 [
124 'key' => 'action',
125 'components' => ['contribution'],
126 'options' => [
127 'transact' => ts('Contribution Page'),
128 'setup' => ts('Setup a Personal Campaign Page'),
129 ],
130 ],
be2fb01f 131 [
15f842bd 132 'key' => 'action',
be2fb01f
CW
133 'components' => ['event'],
134 'options' => [
15f842bd
CW
135 'info' => ts('Event Info Page'),
136 'register' => ts('Event Registration Page'),
be2fb01f
CW
137 ],
138 ],
a3589dd7
MW
139 [
140 'key' => 'action',
141 'components' => ['pcp'],
142 'options' => [
143 'info' => ts('Info Page'),
144 'transact' => ts('Contribution Page'),
145 ],
146 ],
be2fb01f 147 [
15f842bd 148 'key' => 'mode',
65c1b421 149 'components' => ['contribution', 'pcp', 'event'],
be2fb01f 150 'options' => [
15f842bd
CW
151 'live' => ts('Live Mode'),
152 'test' => ts('Test Drive'),
be2fb01f
CW
153 ],
154 ],
155 [
15f842bd 156 'key' => 'mode',
be2fb01f
CW
157 'components' => ['profile'],
158 'options' => [
15f842bd
CW
159 'create' => ts('Create'),
160 'edit' => ts('Edit'),
161 'view' => ts('View'),
162 'search' => ts('Search/Public Directory'),
bd70f6db 163 'map' => ts('Map View'),
be2fb01f
CW
164 ],
165 ],
166 [
15f842bd 167 'key' => 'hijack',
b233e1b4 168 'components' => TRUE,
15f842bd 169 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
be2fb01f 170 'options' => [
15f842bd
CW
171 '0' => ts("Don't override"),
172 '1' => ts('Override page content'),
be2fb01f
CW
173 ],
174 ],
175 ];
15f842bd
CW
176 }
177
178 /**
8eedd10a 179 * Build form elements based on the above metadata.
15f842bd
CW
180 */
181 public function buildQuickForm() {
15f842bd
CW
182 $components = CRM_Utils_Array::collect('label', $this->components);
183 $data = CRM_Utils_Array::collect('select', $this->components);
184
be2fb01f
CW
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 -')]);
15f842bd 187
be2fb01f 188 $options = $defaults = [];
15f842bd 189 foreach ($this->options as $num => $field) {
be2fb01f 190 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]);
b233e1b4 191 if ($field['components'] === TRUE) {
15f842bd
CW
192 $field['components'] = array_keys($this->components);
193 }
194 $options["option_$num"] = $field;
195
196 // Select 1st option as default
197 $keys = array_keys($field['options']);
198 $defaults["option_$num"] = $keys[0];
199 }
200
201 $this->assign('options', $options);
202 $this->assign('selects', array_keys(array_filter($data)));
203 $this->setDefaults($defaults);
204 }
205
b233e1b4
CW
206 /**
207 * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins.
208 *
209 * I'm not proud of this, but here's a workaround to pre-filter the api params
210 *
211 * @return array
212 */
213 private function profileAccess() {
214 $sql = "
215 SELECT g.id
216 FROM civicrm_uf_group g, civicrm_uf_join j
217 WHERE g.is_active = 1
218 AND j.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'
223 ";
224 $dao = CRM_Core_DAO::executeQuery($sql);
be2fb01f 225 $ids = [];
b233e1b4
CW
226 while ($dao->fetch()) {
227 $ids[] = $dao->id;
228 }
be2fb01f 229 return ['IN' => $ids];
b233e1b4
CW
230 }
231
15f842bd
CW
232 // No postProccess fn; this form never gets submitted
233
234}