Merge pull request #21473 from mattwire/contributionrecurtplupdate
[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 $this->components['user-dashboard'] = [
57 'label' => ts("User Dashboard"),
58 'select' => NULL,
59 ];
60 $this->components['profile'] = [
61 'label' => ts("Profile"),
62 'select' => [
63 'key' => 'gid',
64 'entity' => 'UFGroup',
65 'select' => ['minimumInputLength' => 0],
66 'api' => [
67 'params' => [
68 'id' => $this->profileAccess(),
69 ],
70 ],
71 ],
72 ];
73
74 if (CRM_Core_Component::isEnabled('CiviContribute')) {
75 $this->components['contribution'] = [
76 'label' => ts("Contribution Page"),
77 'select' => [
78 'key' => 'id',
79 'entity' => 'ContributionPage',
80 'select' => ['minimumInputLength' => 0],
81 ],
82 ];
83 $this->components['pcp'] = [
84 'label' => ts("Personal Campaign Page"),
85 'select' => [
86 'key' => 'id',
87 'entity' => 'Pcp',
88 'select' => ['minimumInputLength' => 0],
89 ],
90 ];
91 }
92
93 if (CRM_Core_Component::isEnabled('CiviEvent')) {
94 $this->components['event'] = [
95 'label' => ts("Event Page"),
96 'select' => [
97 'key' => 'id',
98 'entity' => 'Event',
99 'select' => ['minimumInputLength' => 0],
100 ],
101 ];
102 }
103
104 if (CRM_Core_Component::isEnabled('CiviCampaign')) {
105 $this->components['petition'] = [
106 'label' => ts("Petition"),
107 'select' => [
108 'key' => 'id',
109 'entity' => 'Survey',
110 'select' => ['minimumInputLength' => 0],
111 'api' => [
112 'params' => [
113 'activity_type_id' => "Petition",
114 ],
115 ],
116 ],
117 ];
118 }
119
120 $this->options = [
121 [
122 'key' => 'action',
123 'components' => ['contribution'],
124 'options' => [
125 'transact' => ts('Contribution Page'),
126 'setup' => ts('Setup a Personal Campaign Page'),
127 ],
128 ],
129 [
130 'key' => 'action',
131 'components' => ['event'],
132 'options' => [
133 'info' => ts('Event Info Page'),
134 'register' => ts('Event Registration Page'),
135 ],
136 ],
137 [
138 'key' => 'action',
139 'components' => ['pcp'],
140 'options' => [
141 'info' => ts('Info Page'),
142 'transact' => ts('Contribution Page'),
143 ],
144 ],
145 [
146 'key' => 'mode',
147 'components' => ['contribution', 'pcp', 'event'],
148 'options' => [
149 'live' => ts('Live Mode'),
150 'test' => ts('Test Drive'),
151 ],
152 ],
153 [
154 'key' => 'mode',
155 'components' => ['profile'],
156 'options' => [
157 'create' => ts('Create'),
158 'edit' => ts('Edit'),
159 'view' => ts('View'),
160 'search' => ts('Search/Public Directory'),
161 'map' => ts('Map View'),
162 ],
163 ],
164 [
165 'key' => 'hijack',
166 'components' => TRUE,
167 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
168 'options' => [
169 '0' => ts("Don't override"),
170 '1' => ts('Override page content'),
171 ],
172 ],
173 ];
174 }
175
176 /**
177 * Build form elements based on the above metadata.
178 */
179 public function buildQuickForm() {
180 $components = CRM_Utils_Array::collect('label', $this->components);
181 $data = CRM_Utils_Array::collect('select', $this->components);
182
183 $this->add('select', 'component', NULL, $components, FALSE, ['class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)]);
184 $this->add('text', 'entity', NULL, ['placeholder' => ts('- select -')]);
185
186 $options = $defaults = [];
187 foreach ($this->options as $num => $field) {
188 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]);
189 if ($field['components'] === TRUE) {
190 $field['components'] = array_keys($this->components);
191 }
192 $options["option_$num"] = $field;
193
194 // Select 1st option as default
195 $keys = array_keys($field['options']);
196 $defaults["option_$num"] = $keys[0];
197 }
198
199 $this->assign('options', $options);
200 $this->assign('selects', array_keys(array_filter($data)));
201 $this->setDefaults($defaults);
202 }
203
204 /**
205 * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins.
206 *
207 * I'm not proud of this, but here's a workaround to pre-filter the api params
208 *
209 * @return array
210 */
211 private function profileAccess() {
212 $sql = "
213 SELECT g.id
214 FROM civicrm_uf_group g, civicrm_uf_join j
215 WHERE g.is_active = 1
216 AND j.is_active = 1
217 AND ( group_type LIKE '%Individual%'
218 OR group_type LIKE '%Contact%' )
219 AND g.id = j.uf_group_id
220 AND j.module = 'Profile'
221 ";
222 $dao = CRM_Core_DAO::executeQuery($sql);
223 $ids = [];
224 while ($dao->fetch()) {
225 $ids[] = $dao->id;
226 }
227 return ['IN' => $ids];
228 }
229
230 // No postProccess fn; this form never gets submitted
231
232 }