Merge pull request #15699 from mattwire/participant_cleanup_completeOrderPBRef
[civicrm-core.git] / CRM / Core / Form / ShortCode.php
CommitLineData
15f842bd
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
15f842bd 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
15f842bd
CW
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
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
15f842bd
CW
32 */
33
34/**
8eedd10a 35 * Builds a form of shortcodes that can be added to WP posts.
36 *
37 * Use hook_civicrm_preProcess to modify this list.
15f842bd
CW
38 */
39class CRM_Core_Form_ShortCode extends CRM_Core_Form {
40 /**
b233e1b4
CW
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
15f842bd
CW
47 *
48 * @var array
b233e1b4
CW
49 * [component => [
50 * label => Option Label
51 * select => key + EntityRef params
52 * ]]
518fa0ee 53 * @see CRM_Core_Form::addEntityRef
15f842bd 54 */
be2fb01f 55 public $components = [];
15f842bd
CW
56
57 /**
b233e1b4
CW
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
15f842bd
CW
62 *
63 * @var array
b233e1b4 64 * [key, components, options]
15f842bd 65 */
be2fb01f 66 public $options = [];
15f842bd 67
15f842bd 68 /**
8eedd10a 69 * Build form data. Can be modified via hook_civicrm_preProcess.
15f842bd
CW
70 */
71 public function preProcess() {
72 $config = CRM_Core_Config::singleton();
73
be2fb01f 74 $this->components['user-dashboard'] = [
15f842bd
CW
75 'label' => ts("User Dashboard"),
76 'select' => NULL,
be2fb01f
CW
77 ];
78 $this->components['profile'] = [
15f842bd 79 'label' => ts("Profile"),
be2fb01f 80 'select' => [
15f842bd 81 'key' => 'gid',
b233e1b4 82 'entity' => 'UFGroup',
be2fb01f
CW
83 'select' => ['minimumInputLength' => 0],
84 'api' => [
85 'params' => [
b233e1b4 86 'id' => $this->profileAccess(),
be2fb01f
CW
87 ],
88 ],
89 ],
90 ];
15f842bd
CW
91
92 if (in_array('CiviContribute', $config->enableComponents)) {
be2fb01f 93 $this->components['contribution'] = [
15f842bd 94 'label' => ts("Contribution Page"),
be2fb01f 95 'select' => [
15f842bd
CW
96 'key' => 'id',
97 'entity' => 'ContributionPage',
be2fb01f
CW
98 'select' => ['minimumInputLength' => 0],
99 ],
100 ];
15f842bd
CW
101 }
102
103 if (in_array('CiviEvent', $config->enableComponents)) {
be2fb01f 104 $this->components['event'] = [
15f842bd 105 'label' => ts("Event Page"),
be2fb01f 106 'select' => [
15f842bd
CW
107 'key' => 'id',
108 'entity' => 'Event',
be2fb01f
CW
109 'select' => ['minimumInputLength' => 0],
110 ],
111 ];
15f842bd
CW
112 }
113
114 if (in_array('CiviCampaign', $config->enableComponents)) {
be2fb01f 115 $this->components['petition'] = [
15f842bd 116 'label' => ts("Petition"),
be2fb01f 117 'select' => [
15f842bd
CW
118 'key' => 'id',
119 'entity' => 'Survey',
be2fb01f
CW
120 'select' => ['minimumInputLength' => 0],
121 'api' => [
122 'params' => [
b233e1b4 123 'activity_type_id' => "Petition",
be2fb01f
CW
124 ],
125 ],
126 ],
127 ];
15f842bd
CW
128 }
129
be2fb01f
CW
130 $this->options = [
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 ],
139 [
15f842bd 140 'key' => 'mode',
be2fb01f
CW
141 'components' => ['contribution', 'event'],
142 'options' => [
15f842bd
CW
143 'live' => ts('Live Mode'),
144 'test' => ts('Test Drive'),
be2fb01f
CW
145 ],
146 ],
147 [
15f842bd 148 'key' => 'mode',
be2fb01f
CW
149 'components' => ['profile'],
150 'options' => [
15f842bd
CW
151 'create' => ts('Create'),
152 'edit' => ts('Edit'),
153 'view' => ts('View'),
154 'search' => ts('Search/Public Directory'),
be2fb01f
CW
155 ],
156 ],
157 [
15f842bd 158 'key' => 'hijack',
b233e1b4 159 'components' => TRUE,
15f842bd 160 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
be2fb01f 161 'options' => [
15f842bd
CW
162 '0' => ts("Don't override"),
163 '1' => ts('Override page content'),
be2fb01f
CW
164 ],
165 ],
166 ];
15f842bd
CW
167 }
168
169 /**
8eedd10a 170 * Build form elements based on the above metadata.
15f842bd
CW
171 */
172 public function buildQuickForm() {
15f842bd
CW
173 $components = CRM_Utils_Array::collect('label', $this->components);
174 $data = CRM_Utils_Array::collect('select', $this->components);
175
be2fb01f
CW
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 -')]);
15f842bd 178
be2fb01f 179 $options = $defaults = [];
15f842bd 180 foreach ($this->options as $num => $field) {
be2fb01f 181 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], ['allowClear' => FALSE, 'data-key' => $field['key']]);
b233e1b4 182 if ($field['components'] === TRUE) {
15f842bd
CW
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
b233e1b4
CW
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);
be2fb01f 216 $ids = [];
b233e1b4
CW
217 while ($dao->fetch()) {
218 $ids[] = $dao->id;
219 }
be2fb01f 220 return ['IN' => $ids];
b233e1b4
CW
221 }
222
15f842bd
CW
223 // No postProccess fn; this form never gets submitted
224
225}