CRM-16650 - Rewrite shortcode form
[civicrm-core.git] / CRM / Core / Form / ShortCode.php
CommitLineData
15f842bd
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36/**
37 * Builds a form of shortcodes that can be added to WP posts
38 * Use hook_civicrm_preProcess to modify this list
39 */
40class CRM_Core_Form_ShortCode extends CRM_Core_Form {
41 /**
42 * List of entities supported by shortcodes, and their form properties
43 *
44 * @var array
45 */
46 public $components = array();
47
48 /**
49 * List of options to display on the form
50 *
51 * @var array
52 */
53 public $options = array();
54
55
56 /**
57 * Build form data. Overridable via hook_civicrm_preProcess
58 *
59 * @return void
60 */
61 public function preProcess() {
62 $config = CRM_Core_Config::singleton();
63
64 $this->components['user-dashboard'] = array(
65 'label' => ts("User Dashboard"),
66 'select' => NULL,
67 );
68 $this->components['profile'] = array(
69 'label' => ts("Profile"),
70 'select' => array(
71 'key' => 'gid',
72 'entity' => 'Profile',
73 'select' => array('minimumInputLength' => 0),
74 ),
75 );
76
77 if (in_array('CiviContribute', $config->enableComponents)) {
78 $this->components['contribution'] = array(
79 'label' => ts("Contribution Page"),
80 'select' => array(
81 'key' => 'id',
82 'entity' => 'ContributionPage',
83 'select' => array('minimumInputLength' => 0),
84 ),
85 );
86 }
87
88 if (in_array('CiviEvent', $config->enableComponents)) {
89 $this->components['event'] = array(
90 'label' => ts("Event Page"),
91 'select' => array(
92 'key' => 'id',
93 'entity' => 'Event',
94 'select' => array('minimumInputLength' => 0),
95 ),
96 );
97 }
98
99 if (in_array('CiviCampaign', $config->enableComponents)) {
100 $this->components['petition'] = array(
101 'label' => ts("Petition"),
102 'select' => array(
103 'key' => 'id',
104 'entity' => 'Survey',
105 'select' => array('minimumInputLength' => 0),
106 ),
107 );
108 }
109
110 $this->options = array(
111 array(
112 'key' => 'action',
113 'components' => array('event'),
114 'options' => array(
115 'info' => ts('Event Info Page'),
116 'register' => ts('Event Registration Page'),
117 ),
118 ),
119 array(
120 'key' => 'mode',
121 'components' => array('contribution', 'event'),
122 'options' => array(
123 'live' => ts('Live Mode'),
124 'test' => ts('Test Drive'),
125 ),
126 ),
127 array(
128 'key' => 'mode',
129 'components' => array('profile'),
130 'options' => array(
131 'create' => ts('Create'),
132 'edit' => ts('Edit'),
133 'view' => ts('View'),
134 'search' => ts('Search/Public Directory'),
135 ),
136 ),
137 array(
138 'key' => 'hijack',
139 'components' => '*',
140 'label' => ts('If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.'),
141 'options' => array(
142 '0' => ts("Don't override"),
143 '1' => ts('Override page content'),
144 ),
145 ),
146 );
147 }
148
149 /**
150 * Build form elements based on the above metadata
151 *
152 * @return void
153 */
154 public function buildQuickForm() {
155 CRM_Core_Resources::singleton()
156 ->addScriptFile('civicrm', 'js/crm.insert-shortcode.js');
157
158 $components = CRM_Utils_Array::collect('label', $this->components);
159 $data = CRM_Utils_Array::collect('select', $this->components);
160
161 $this->add('select', 'component', NULL, $components, FALSE, array('class' => 'crm-select2', 'data-key' => 'component', 'data-entities' => json_encode($data)));
162 $this->add('text', 'entity', NULL, array('placeholder' => ts('- select -')));
163
164 $options = $defaults = array();
165 foreach ($this->options as $num => $field) {
166 $this->addRadio("option_$num", CRM_Utils_Array::value('label', $field), $field['options'], array('allowClear' => FALSE, 'data-key' => $field['key']));
167 if ($field['components'] === '*') {
168 $field['components'] = array_keys($this->components);
169 }
170 $options["option_$num"] = $field;
171
172 // Select 1st option as default
173 $keys = array_keys($field['options']);
174 $defaults["option_$num"] = $keys[0];
175 }
176
177 $this->assign('options', $options);
178 $this->assign('selects', array_keys(array_filter($data)));
179 $this->setDefaults($defaults);
180 }
181
182 // No postProccess fn; this form never gets submitted
183
184}