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