Merge pull request #17390 from mattwire/api3activitydatetimedefault
[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' => ['event'],
126 'options' => [
127 'info' => ts('Event Info Page'),
128 'register' => ts('Event Registration Page'),
129 ],
130 ],
131 [
132 'key' => 'mode',
133 'components' => ['contribution', 'pcp', 'event'],
134 'options' => [
135 'live' => ts('Live Mode'),
136 'test' => ts('Test Drive'),
137 ],
138 ],
139 [
140 'key' => 'mode',
141 'components' => ['profile'],
142 'options' => [
143 'create' => ts('Create'),
144 'edit' => ts('Edit'),
145 'view' => ts('View'),
146 'search' => ts('Search/Public Directory'),
147 ],
148 ],
149 [
150 'key' => 'hijack',
151 'components' => TRUE,
152 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
153 'options' => [
154 '0' => ts("Don't override"),
155 '1' => ts('Override page content'),
156 ],
157 ],
158 ];
159 }
160
161 /**
162 * Build form elements based on the above metadata.
163 */
164 public function buildQuickForm() {
165 $components = CRM_Utils_Array::collect('label', $this->components);
166 $data = CRM_Utils_Array::collect('select', $this->components);
167
168 $this->add('select', 'component', NULL, $components, FALSE, ['class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)]);
169 $this->add('text', 'entity', NULL, ['placeholder' => ts('- select -')]);
170
171 $options = $defaults = [];
172 foreach ($this->options as $num => $field) {
173 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]);
174 if ($field['components'] === TRUE) {
175 $field['components'] = array_keys($this->components);
176 }
177 $options["option_$num"] = $field;
178
179 // Select 1st option as default
180 $keys = array_keys($field['options']);
181 $defaults["option_$num"] = $keys[0];
182 }
183
184 $this->assign('options', $options);
185 $this->assign('selects', array_keys(array_filter($data)));
186 $this->setDefaults($defaults);
187 }
188
189 /**
190 * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins.
191 *
192 * I'm not proud of this, but here's a workaround to pre-filter the api params
193 *
194 * @return array
195 */
196 private function profileAccess() {
197 $sql = "
198 SELECT g.id
199 FROM civicrm_uf_group g, civicrm_uf_join j
200 WHERE g.is_active = 1
201 AND j.is_active = 1
202 AND ( group_type LIKE '%Individual%'
203 OR group_type LIKE '%Contact%' )
204 AND g.id = j.uf_group_id
205 AND j.module = 'Profile'
206 ";
207 $dao = CRM_Core_DAO::executeQuery($sql);
208 $ids = [];
209 while ($dao->fetch()) {
210 $ids[] = $dao->id;
211 }
212 return ['IN' => $ids];
213 }
214
215 // No postProccess fn; this form never gets submitted
216
217 }