Merge pull request #15790 from civicrm/5.20
[civicrm-core.git] / CRM / Core / Form / ShortCode.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2020
32 */
33
34 /**
35 * Builds a form of shortcodes that can be added to WP posts.
36 *
37 * Use hook_civicrm_preProcess to modify this list.
38 */
39 class CRM_Core_Form_ShortCode extends CRM_Core_Form {
40 /**
41 * List of entities supported by shortcodes, and their form properties.
42 *
43 * Keys should be the "component" string for the shortcode
44 * Values should be an array with label and select.
45 * Select can be NULL if there is no entity to select.
46 * Otherwise it contains the shortcode key for this entity id (usually 'id') plus an array of params for the EntityRef field
47 *
48 * @var array
49 * [component => [
50 * label => Option Label
51 * select => key + EntityRef params
52 * ]]
53 * @see CRM_Core_Form::addEntityRef
54 */
55 public $components = [];
56
57 /**
58 * List of radio option groups to display on the form
59 *
60 * Control the conditional logic of showing/hiding each group via the "components" array.
61 * Or set 'components' => TRUE if it applies to all
62 *
63 * @var array
64 * [key, components, options]
65 */
66 public $options = [];
67
68 /**
69 * Build form data. Can be modified via hook_civicrm_preProcess.
70 */
71 public function preProcess() {
72 $config = CRM_Core_Config::singleton();
73
74 $this->components['user-dashboard'] = [
75 'label' => ts("User Dashboard"),
76 'select' => NULL,
77 ];
78 $this->components['profile'] = [
79 'label' => ts("Profile"),
80 'select' => [
81 'key' => 'gid',
82 'entity' => 'UFGroup',
83 'select' => ['minimumInputLength' => 0],
84 'api' => [
85 'params' => [
86 'id' => $this->profileAccess(),
87 ],
88 ],
89 ],
90 ];
91
92 if (in_array('CiviContribute', $config->enableComponents)) {
93 $this->components['contribution'] = [
94 'label' => ts("Contribution Page"),
95 'select' => [
96 'key' => 'id',
97 'entity' => 'ContributionPage',
98 'select' => ['minimumInputLength' => 0],
99 ],
100 ];
101 }
102
103 if (in_array('CiviEvent', $config->enableComponents)) {
104 $this->components['event'] = [
105 'label' => ts("Event Page"),
106 'select' => [
107 'key' => 'id',
108 'entity' => 'Event',
109 'select' => ['minimumInputLength' => 0],
110 ],
111 ];
112 }
113
114 if (in_array('CiviCampaign', $config->enableComponents)) {
115 $this->components['petition'] = [
116 'label' => ts("Petition"),
117 'select' => [
118 'key' => 'id',
119 'entity' => 'Survey',
120 'select' => ['minimumInputLength' => 0],
121 'api' => [
122 'params' => [
123 'activity_type_id' => "Petition",
124 ],
125 ],
126 ],
127 ];
128 }
129
130 $this->options = [
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' => 'mode',
141 'components' => ['contribution', 'event'],
142 'options' => [
143 'live' => ts('Live Mode'),
144 'test' => ts('Test Drive'),
145 ],
146 ],
147 [
148 'key' => 'mode',
149 'components' => ['profile'],
150 'options' => [
151 'create' => ts('Create'),
152 'edit' => ts('Edit'),
153 'view' => ts('View'),
154 'search' => ts('Search/Public Directory'),
155 ],
156 ],
157 [
158 'key' => 'hijack',
159 'components' => TRUE,
160 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
161 'options' => [
162 '0' => ts("Don't override"),
163 '1' => ts('Override page content'),
164 ],
165 ],
166 ];
167 }
168
169 /**
170 * Build form elements based on the above metadata.
171 */
172 public function buildQuickForm() {
173 $components = CRM_Utils_Array::collect('label', $this->components);
174 $data = CRM_Utils_Array::collect('select', $this->components);
175
176 $this->add('select', 'component', NULL, $components, FALSE, ['class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)]);
177 $this->add('text', 'entity', NULL, ['placeholder' => ts('- select -')]);
178
179 $options = $defaults = [];
180 foreach ($this->options as $num => $field) {
181 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]);
182 if ($field['components'] === TRUE) {
183 $field['components'] = array_keys($this->components);
184 }
185 $options["option_$num"] = $field;
186
187 // Select 1st option as default
188 $keys = array_keys($field['options']);
189 $defaults["option_$num"] = $keys[0];
190 }
191
192 $this->assign('options', $options);
193 $this->assign('selects', array_keys(array_filter($data)));
194 $this->setDefaults($defaults);
195 }
196
197 /**
198 * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins.
199 *
200 * I'm not proud of this, but here's a workaround to pre-filter the api params
201 *
202 * @return array
203 */
204 private function profileAccess() {
205 $sql = "
206 SELECT g.id
207 FROM civicrm_uf_group g, civicrm_uf_join j
208 WHERE g.is_active = 1
209 AND j.is_active = 1
210 AND ( group_type LIKE '%Individual%'
211 OR group_type LIKE '%Contact%' )
212 AND g.id = j.uf_group_id
213 AND j.module = 'Profile'
214 ";
215 $dao = CRM_Core_DAO::executeQuery($sql);
216 $ids = [];
217 while ($dao->fetch()) {
218 $ids[] = $dao->id;
219 }
220 return ['IN' => $ids];
221 }
222
223 // No postProccess fn; this form never gets submitted
224
225 }