Merge pull request #13291 from eileenmcnaughton/process_default_active
[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 = array();
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 = array();
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'] = array(
76 'label' => ts("User Dashboard"),
77 'select' => NULL,
78 );
79 $this->components['profile'] = array(
80 'label' => ts("Profile"),
81 'select' => array(
82 'key' => 'gid',
83 'entity' => 'UFGroup',
84 'select' => array('minimumInputLength' => 0),
85 'api' => array(
86 'params' => array(
87 'id' => $this->profileAccess(),
88 ),
89 ),
90 ),
91 );
92
93 if (in_array('CiviContribute', $config->enableComponents)) {
94 $this->components['contribution'] = array(
95 'label' => ts("Contribution Page"),
96 'select' => array(
97 'key' => 'id',
98 'entity' => 'ContributionPage',
99 'select' => array('minimumInputLength' => 0),
100 ),
101 );
102 }
103
104 if (in_array('CiviEvent', $config->enableComponents)) {
105 $this->components['event'] = array(
106 'label' => ts("Event Page"),
107 'select' => array(
108 'key' => 'id',
109 'entity' => 'Event',
110 'select' => array('minimumInputLength' => 0),
111 ),
112 );
113 }
114
115 if (in_array('CiviCampaign', $config->enableComponents)) {
116 $this->components['petition'] = array(
117 'label' => ts("Petition"),
118 'select' => array(
119 'key' => 'id',
120 'entity' => 'Survey',
121 'select' => array('minimumInputLength' => 0),
122 'api' => array(
123 'params' => array(
124 'activity_type_id' => "Petition",
125 ),
126 ),
127 ),
128 );
129 }
130
131 $this->options = array(
132 array(
133 'key' => 'action',
134 'components' => array('event'),
135 'options' => array(
136 'info' => ts('Event Info Page'),
137 'register' => ts('Event Registration Page'),
138 ),
139 ),
140 array(
141 'key' => 'mode',
142 'components' => array('contribution', 'event'),
143 'options' => array(
144 'live' => ts('Live Mode'),
145 'test' => ts('Test Drive'),
146 ),
147 ),
148 array(
149 'key' => 'mode',
150 'components' => array('profile'),
151 'options' => array(
152 'create' => ts('Create'),
153 'edit' => ts('Edit'),
154 'view' => ts('View'),
155 'search' => ts('Search/Public Directory'),
156 ),
157 ),
158 array(
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' => array(
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 CRM_Core_Resources::singleton()
175 ->addScriptFile('civicrm', 'js/crm.insert-shortcode.js');
176
177 $components = CRM_Utils_Array::collect('label', $this->components);
178 $data = CRM_Utils_Array::collect('select', $this->components);
179
180 $this->add('select', 'component', NULL, $components, FALSE, array('class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)));
181 $this->add('text', 'entity', NULL, array('placeholder' => ts('- select -')));
182
183 $options = $defaults = array();
184 foreach ($this->options as $num => $field) {
185 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], array('allowClear' => FALSE, 'data-key' => $field['key']));
186 if ($field['components'] === TRUE) {
187 $field['components'] = array_keys($this->components);
188 }
189 $options["option_$num"] = $field;
190
191 // Select 1st option as default
192 $keys = array_keys($field['options']);
193 $defaults["option_$num"] = $keys[0];
194 }
195
196 $this->assign('options', $options);
197 $this->assign('selects', array_keys(array_filter($data)));
198 $this->setDefaults($defaults);
199 }
200
201 /**
202 * The CiviCRM api (and therefore EntityRef) does not support OR logic, ACLs or joins.
203 *
204 * I'm not proud of this, but here's a workaround to pre-filter the api params
205 *
206 * @return array
207 */
208 private function profileAccess() {
209 $sql = "
210 SELECT g.id
211 FROM civicrm_uf_group g, civicrm_uf_join j
212 WHERE g.is_active = 1
213 AND j.is_active = 1
214 AND ( group_type LIKE '%Individual%'
215 OR group_type LIKE '%Contact%' )
216 AND g.id = j.uf_group_id
217 AND j.module = 'Profile'
218 ";
219 $dao = CRM_Core_DAO::executeQuery($sql);
220 $ids = array();
221 while ($dao->fetch()) {
222 $ids[] = $dao->id;
223 }
224 return array('IN' => $ids);
225 }
226
227 // No postProccess fn; this form never gets submitted
228
229 }