Merge pull request #20308 from masetto/master
[civicrm-core.git] / CRM / Core / Form / ShortCode.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Builds a form of shortcodes that can be added to WP posts.
20 *
21 * Use hook_civicrm_preProcess to modify this list.
22 */
23 class CRM_Core_Form_ShortCode extends CRM_Core_Form {
24 /**
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
31 *
32 * @var array
33 * [component => [
34 * label => Option Label
35 * select => key + EntityRef params
36 * ]]
37 * @see CRM_Core_Form::addEntityRef
38 */
39 public $components = [];
40
41 /**
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
46 *
47 * @var array
48 * [key, components, options]
49 */
50 public $options = [];
51
52 /**
53 * Build form data. Can be modified via hook_civicrm_preProcess.
54 */
55 public function preProcess() {
56 $config = CRM_Core_Config::singleton();
57
58 $this->components['user-dashboard'] = [
59 'label' => ts("User Dashboard"),
60 'select' => NULL,
61 ];
62 $this->components['profile'] = [
63 'label' => ts("Profile"),
64 'select' => [
65 'key' => 'gid',
66 'entity' => 'UFGroup',
67 'select' => ['minimumInputLength' => 0],
68 'api' => [
69 'params' => [
70 'id' => $this->profileAccess(),
71 ],
72 ],
73 ],
74 ];
75
76 if (in_array('CiviContribute', $config->enableComponents)) {
77 $this->components['contribution'] = [
78 'label' => ts("Contribution Page"),
79 'select' => [
80 'key' => 'id',
81 'entity' => 'ContributionPage',
82 'select' => ['minimumInputLength' => 0],
83 ],
84 ];
85 $this->components['pcp'] = [
86 'label' => ts("Personal Campaign Page"),
87 'select' => [
88 'key' => 'id',
89 'entity' => 'Pcp',
90 'select' => ['minimumInputLength' => 0],
91 ],
92 ];
93 }
94
95 if (in_array('CiviEvent', $config->enableComponents)) {
96 $this->components['event'] = [
97 'label' => ts("Event Page"),
98 'select' => [
99 'key' => 'id',
100 'entity' => 'Event',
101 'select' => ['minimumInputLength' => 0],
102 ],
103 ];
104 }
105
106 if (in_array('CiviCampaign', $config->enableComponents)) {
107 $this->components['petition'] = [
108 'label' => ts("Petition"),
109 'select' => [
110 'key' => 'id',
111 'entity' => 'Survey',
112 'select' => ['minimumInputLength' => 0],
113 'api' => [
114 'params' => [
115 'activity_type_id' => "Petition",
116 ],
117 ],
118 ],
119 ];
120 }
121
122 $this->options = [
123 [
124 'key' => 'action',
125 'components' => ['contribution'],
126 'options' => [
127 'transact' => ts('Contribution Page'),
128 'setup' => ts('Setup a Personal Campaign Page'),
129 ],
130 ],
131 [
132 'key' => 'action',
133 'components' => ['event'],
134 'options' => [
135 'info' => ts('Event Info Page'),
136 'register' => ts('Event Registration Page'),
137 ],
138 ],
139 [
140 'key' => 'action',
141 'components' => ['pcp'],
142 'options' => [
143 'info' => ts('Info Page'),
144 'transact' => ts('Contribution Page'),
145 ],
146 ],
147 [
148 'key' => 'mode',
149 'components' => ['contribution', 'pcp', 'event'],
150 'options' => [
151 'live' => ts('Live Mode'),
152 'test' => ts('Test Drive'),
153 ],
154 ],
155 [
156 'key' => 'mode',
157 'components' => ['profile'],
158 'options' => [
159 'create' => ts('Create'),
160 'edit' => ts('Edit'),
161 'view' => ts('View'),
162 'search' => ts('Search/Public Directory'),
163 'map' => ts('Map View'),
164 ],
165 ],
166 [
167 'key' => 'hijack',
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.'),
170 'options' => [
171 '0' => ts("Don't override"),
172 '1' => ts('Override page content'),
173 ],
174 ],
175 ];
176 }
177
178 /**
179 * Build form elements based on the above metadata.
180 */
181 public function buildQuickForm() {
182 $components = CRM_Utils_Array::collect('label', $this->components);
183 $data = CRM_Utils_Array::collect('select', $this->components);
184
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 -')]);
187
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);
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
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);
225 $ids = [];
226 while ($dao->fetch()) {
227 $ids[] = $dao->id;
228 }
229 return ['IN' => $ids];
230 }
231
232 // No postProccess fn; this form never gets submitted
233
234 }