Commit | Line | Data |
---|---|---|
15f842bd CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
15f842bd | 5 | | | |
bc77d7c0 TO |
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 | | |
15f842bd CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
15f842bd CW |
16 | */ |
17 | ||
18 | /** | |
8eedd10a | 19 | * Builds a form of shortcodes that can be added to WP posts. |
20 | * | |
21 | * Use hook_civicrm_preProcess to modify this list. | |
15f842bd CW |
22 | */ |
23 | class CRM_Core_Form_ShortCode extends CRM_Core_Form { | |
24 | /** | |
b233e1b4 CW |
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 | |
15f842bd CW |
31 | * |
32 | * @var array | |
b233e1b4 CW |
33 | * [component => [ |
34 | * label => Option Label | |
35 | * select => key + EntityRef params | |
36 | * ]] | |
518fa0ee | 37 | * @see CRM_Core_Form::addEntityRef |
15f842bd | 38 | */ |
be2fb01f | 39 | public $components = []; |
15f842bd CW |
40 | |
41 | /** | |
b233e1b4 CW |
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 | |
15f842bd CW |
46 | * |
47 | * @var array | |
b233e1b4 | 48 | * [key, components, options] |
15f842bd | 49 | */ |
be2fb01f | 50 | public $options = []; |
15f842bd | 51 | |
15f842bd | 52 | /** |
8eedd10a | 53 | * Build form data. Can be modified via hook_civicrm_preProcess. |
15f842bd CW |
54 | */ |
55 | public function preProcess() { | |
56 | $config = CRM_Core_Config::singleton(); | |
57 | ||
be2fb01f | 58 | $this->components['user-dashboard'] = [ |
15f842bd CW |
59 | 'label' => ts("User Dashboard"), |
60 | 'select' => NULL, | |
be2fb01f CW |
61 | ]; |
62 | $this->components['profile'] = [ | |
15f842bd | 63 | 'label' => ts("Profile"), |
be2fb01f | 64 | 'select' => [ |
15f842bd | 65 | 'key' => 'gid', |
b233e1b4 | 66 | 'entity' => 'UFGroup', |
be2fb01f CW |
67 | 'select' => ['minimumInputLength' => 0], |
68 | 'api' => [ | |
69 | 'params' => [ | |
b233e1b4 | 70 | 'id' => $this->profileAccess(), |
be2fb01f CW |
71 | ], |
72 | ], | |
73 | ], | |
74 | ]; | |
15f842bd CW |
75 | |
76 | if (in_array('CiviContribute', $config->enableComponents)) { | |
be2fb01f | 77 | $this->components['contribution'] = [ |
15f842bd | 78 | 'label' => ts("Contribution Page"), |
be2fb01f | 79 | 'select' => [ |
15f842bd CW |
80 | 'key' => 'id', |
81 | 'entity' => 'ContributionPage', | |
be2fb01f CW |
82 | 'select' => ['minimumInputLength' => 0], |
83 | ], | |
84 | ]; | |
65c1b421 MW |
85 | $this->components['pcp'] = [ |
86 | 'label' => ts("Personal Campaign Page"), | |
87 | 'select' => [ | |
88 | 'key' => 'id', | |
89 | 'entity' => 'Pcp', | |
90 | 'select' => ['minimumInputLength' => 0], | |
91 | ], | |
92 | ]; | |
15f842bd CW |
93 | } |
94 | ||
95 | if (in_array('CiviEvent', $config->enableComponents)) { | |
be2fb01f | 96 | $this->components['event'] = [ |
15f842bd | 97 | 'label' => ts("Event Page"), |
be2fb01f | 98 | 'select' => [ |
15f842bd CW |
99 | 'key' => 'id', |
100 | 'entity' => 'Event', | |
be2fb01f CW |
101 | 'select' => ['minimumInputLength' => 0], |
102 | ], | |
103 | ]; | |
15f842bd CW |
104 | } |
105 | ||
106 | if (in_array('CiviCampaign', $config->enableComponents)) { | |
be2fb01f | 107 | $this->components['petition'] = [ |
15f842bd | 108 | 'label' => ts("Petition"), |
be2fb01f | 109 | 'select' => [ |
15f842bd CW |
110 | 'key' => 'id', |
111 | 'entity' => 'Survey', | |
be2fb01f CW |
112 | 'select' => ['minimumInputLength' => 0], |
113 | 'api' => [ | |
114 | 'params' => [ | |
b233e1b4 | 115 | 'activity_type_id' => "Petition", |
be2fb01f CW |
116 | ], |
117 | ], | |
118 | ], | |
119 | ]; | |
15f842bd CW |
120 | } |
121 | ||
be2fb01f | 122 | $this->options = [ |
cb0aafc7 KK |
123 | [ |
124 | 'key' => 'action', | |
125 | 'components' => ['contribution'], | |
126 | 'options' => [ | |
127 | 'transact' => ts('Contribution Page'), | |
128 | 'setup' => ts('Setup a Personal Campaign Page'), | |
129 | ], | |
130 | ], | |
be2fb01f | 131 | [ |
15f842bd | 132 | 'key' => 'action', |
be2fb01f CW |
133 | 'components' => ['event'], |
134 | 'options' => [ | |
15f842bd CW |
135 | 'info' => ts('Event Info Page'), |
136 | 'register' => ts('Event Registration Page'), | |
be2fb01f CW |
137 | ], |
138 | ], | |
a3589dd7 MW |
139 | [ |
140 | 'key' => 'action', | |
141 | 'components' => ['pcp'], | |
142 | 'options' => [ | |
143 | 'info' => ts('Info Page'), | |
144 | 'transact' => ts('Contribution Page'), | |
145 | ], | |
146 | ], | |
be2fb01f | 147 | [ |
15f842bd | 148 | 'key' => 'mode', |
65c1b421 | 149 | 'components' => ['contribution', 'pcp', 'event'], |
be2fb01f | 150 | 'options' => [ |
15f842bd CW |
151 | 'live' => ts('Live Mode'), |
152 | 'test' => ts('Test Drive'), | |
be2fb01f CW |
153 | ], |
154 | ], | |
155 | [ | |
15f842bd | 156 | 'key' => 'mode', |
be2fb01f CW |
157 | 'components' => ['profile'], |
158 | 'options' => [ | |
15f842bd CW |
159 | 'create' => ts('Create'), |
160 | 'edit' => ts('Edit'), | |
161 | 'view' => ts('View'), | |
162 | 'search' => ts('Search/Public Directory'), | |
be2fb01f CW |
163 | ], |
164 | ], | |
165 | [ | |
15f842bd | 166 | 'key' => 'hijack', |
b233e1b4 | 167 | 'components' => TRUE, |
15f842bd | 168 | 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'), |
be2fb01f | 169 | 'options' => [ |
15f842bd CW |
170 | '0' => ts("Don't override"), |
171 | '1' => ts('Override page content'), | |
be2fb01f CW |
172 | ], |
173 | ], | |
174 | ]; | |
15f842bd CW |
175 | } |
176 | ||
177 | /** | |
8eedd10a | 178 | * Build form elements based on the above metadata. |
15f842bd CW |
179 | */ |
180 | public function buildQuickForm() { | |
15f842bd CW |
181 | $components = CRM_Utils_Array::collect('label', $this->components); |
182 | $data = CRM_Utils_Array::collect('select', $this->components); | |
183 | ||
be2fb01f CW |
184 | $this->add('select', 'component', NULL, $components, FALSE, ['class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)]); |
185 | $this->add('text', 'entity', NULL, ['placeholder' => ts('- select -')]); | |
15f842bd | 186 | |
be2fb01f | 187 | $options = $defaults = []; |
15f842bd | 188 | foreach ($this->options as $num => $field) { |
be2fb01f | 189 | $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]); |
b233e1b4 | 190 | if ($field['components'] === TRUE) { |
15f842bd CW |
191 | $field['components'] = array_keys($this->components); |
192 | } | |
193 | $options["option_$num"] = $field; | |
194 | ||
195 | // Select 1st option as default | |
196 | $keys = array_keys($field['options']); | |
197 | $defaults["option_$num"] = $keys[0]; | |
198 | } | |
199 | ||
200 | $this->assign('options', $options); | |
201 | $this->assign('selects', array_keys(array_filter($data))); | |
202 | $this->setDefaults($defaults); | |
203 | } | |
204 | ||
b233e1b4 CW |
205 | /** |
206 | * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins. | |
207 | * | |
208 | * I'm not proud of this, but here's a workaround to pre-filter the api params | |
209 | * | |
210 | * @return array | |
211 | */ | |
212 | private function profileAccess() { | |
213 | $sql = " | |
214 | SELECT g.id | |
215 | FROM civicrm_uf_group g, civicrm_uf_join j | |
216 | WHERE g.is_active = 1 | |
217 | AND j.is_active = 1 | |
218 | AND ( group_type LIKE '%Individual%' | |
219 | OR group_type LIKE '%Contact%' ) | |
220 | AND g.id = j.uf_group_id | |
221 | AND j.module = 'Profile' | |
222 | "; | |
223 | $dao = CRM_Core_DAO::executeQuery($sql); | |
be2fb01f | 224 | $ids = []; |
b233e1b4 CW |
225 | while ($dao->fetch()) { |
226 | $ids[] = $dao->id; | |
227 | } | |
be2fb01f | 228 | return ['IN' => $ids]; |
b233e1b4 CW |
229 | } |
230 | ||
15f842bd CW |
231 | // No postProccess fn; this form never gets submitted |
232 | ||
233 | } |