Merge pull request #17390 from mattwire/api3activitydatetimedefault
[civicrm-core.git] / CRM / Core / Form / ShortCode.php
CommitLineData
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 */
23class 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
CW
122 $this->options = [
123 [
15f842bd 124 'key' => 'action',
be2fb01f
CW
125 'components' => ['event'],
126 'options' => [
15f842bd
CW
127 'info' => ts('Event Info Page'),
128 'register' => ts('Event Registration Page'),
be2fb01f
CW
129 ],
130 ],
131 [
15f842bd 132 'key' => 'mode',
65c1b421 133 'components' => ['contribution', 'pcp', 'event'],
be2fb01f 134 'options' => [
15f842bd
CW
135 'live' => ts('Live Mode'),
136 'test' => ts('Test Drive'),
be2fb01f
CW
137 ],
138 ],
139 [
15f842bd 140 'key' => 'mode',
be2fb01f
CW
141 'components' => ['profile'],
142 'options' => [
15f842bd
CW
143 'create' => ts('Create'),
144 'edit' => ts('Edit'),
145 'view' => ts('View'),
146 'search' => ts('Search/Public Directory'),
be2fb01f
CW
147 ],
148 ],
149 [
15f842bd 150 'key' => 'hijack',
b233e1b4 151 'components' => TRUE,
15f842bd 152 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
be2fb01f 153 'options' => [
15f842bd
CW
154 '0' => ts("Don't override"),
155 '1' => ts('Override page content'),
be2fb01f
CW
156 ],
157 ],
158 ];
15f842bd
CW
159 }
160
161 /**
8eedd10a 162 * Build form elements based on the above metadata.
15f842bd
CW
163 */
164 public function buildQuickForm() {
15f842bd
CW
165 $components = CRM_Utils_Array::collect('label', $this->components);
166 $data = CRM_Utils_Array::collect('select', $this->components);
167
be2fb01f
CW
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 -')]);
15f842bd 170
be2fb01f 171 $options = $defaults = [];
15f842bd 172 foreach ($this->options as $num => $field) {
be2fb01f 173 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]);
b233e1b4 174 if ($field['components'] === TRUE) {
15f842bd
CW
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
b233e1b4
CW
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);
be2fb01f 208 $ids = [];
b233e1b4
CW
209 while ($dao->fetch()) {
210 $ids[] = $dao->id;
211 }
be2fb01f 212 return ['IN' => $ids];
b233e1b4
CW
213 }
214
15f842bd
CW
215 // No postProccess fn; this form never gets submitted
216
217}