Fix button breakage on viewContribution
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Widget.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contribute_Form_ContributionPage_Widget extends CRM_Contribute_Form_ContributionPage {
18 protected $_colors;
19
20 protected $_widget;
21
22 public function preProcess() {
23 parent::preProcess();
24 $this->setSelectedChild('widget');
25
26 $this->_widget = new CRM_Contribute_DAO_Widget();
27 $this->_widget->contribution_page_id = $this->_id;
28 if (!$this->_widget->find(TRUE)) {
29 $this->_widget = NULL;
30 }
31 else {
32 $this->assign('widget_id', $this->_widget->id);
33
34 // check of home url is set, if set then it flash widget might be in use.
35 $this->assign('showStatus', FALSE);
36 if ($this->_widget->url_homepage) {
37 $this->assign('showStatus', TRUE);
38 }
39 }
40
41 $this->assign('cpageId', $this->_id);
42
43 $this->assign('widgetExternUrl', CRM_Utils_System::externUrl('extern/widget', "cpageId={$this->_id}&widgetId=" . ($this->_widget->id ?? '') . "&format=3"));
44
45 $config = CRM_Core_Config::singleton();
46 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
47 $this->_id,
48 'title'
49 );
50
51 $this->_fields = [
52 'title' => [
53 ts('Title'),
54 'text',
55 FALSE,
56 $title,
57 ],
58 'url_logo' => [
59 ts('URL to Logo Image'),
60 'text',
61 FALSE,
62 NULL,
63 ],
64 'button_title' => [
65 ts('Button Title'),
66 'text',
67 FALSE,
68 ts('Contribute!'),
69 ],
70 ];
71
72 $this->_colorFields = [
73 'color_title' => [
74 ts('Title Text Color'),
75 'color',
76 FALSE,
77 '#2786C2',
78 ],
79 'color_bar' => [
80 ts('Progress Bar Color'),
81 'color',
82 FALSE,
83 '#2786C2',
84 ],
85 'color_main_text' => [
86 ts('Additional Text Color'),
87 'color',
88 FALSE,
89 '#FFFFFF',
90 ],
91 'color_main' => [
92 ts('Background Color'),
93 'color',
94 FALSE,
95 '#96C0E7',
96 ],
97 'color_main_bg' => [
98 ts('Background Color Top Area'),
99 'color',
100 FALSE,
101 '#B7E2FF',
102 ],
103 'color_bg' => [
104 ts('Border Color'),
105 'color',
106 FALSE,
107 '#96C0E7',
108 ],
109 'color_about_link' => [
110 ts('Button Text Color'),
111 'color',
112 FALSE,
113 '#556C82',
114 ],
115 'color_button' => [
116 ts('Button Background Color'),
117 'color',
118 FALSE,
119 '#FFFFFF',
120 ],
121 'color_homepage_link' => [
122 ts('Homepage Link Color'),
123 'color',
124 FALSE,
125 '#FFFFFF',
126 ],
127 ];
128 }
129
130 /**
131 * Set default values for the form.
132 */
133 public function setDefaultValues() {
134 $defaults = [];
135 // check if there is a widget already created
136 if ($this->_widget) {
137 CRM_Core_DAO::storeValues($this->_widget, $defaults);
138 }
139 else {
140 foreach ($this->_fields as $name => $val) {
141 $defaults[$name] = $val[3];
142 }
143 foreach ($this->_colorFields as $name => $val) {
144 $defaults[$name] = $val[3];
145 }
146 $defaults['about'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
147 $this->_id,
148 'intro_text'
149 );
150 }
151
152 $showHide = new CRM_Core_ShowHideBlocks();
153 $showHide->addHide('id-colors');
154 $showHide->addToTemplate();
155 return $defaults;
156 }
157
158 public function buildQuickForm() {
159 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Widget');
160
161 $this->addElement('checkbox',
162 'is_active',
163 ts('Enable Widget?'),
164 NULL,
165 ['onclick' => "widgetBlock(this)"]
166 );
167
168 $this->add('wysiwyg', 'about', ts('About'), $attributes['about']);
169
170 foreach ($this->_fields as $name => $val) {
171 $this->add($val[1],
172 $name,
173 $val[0],
174 $attributes[$name],
175 $val[2]
176 );
177 }
178 foreach ($this->_colorFields as $name => $val) {
179 $this->add($val[1],
180 $name,
181 $val[0],
182 $attributes[$name],
183 $val[2]
184 );
185 }
186
187 $this->assign_by_ref('fields', $this->_fields);
188 $this->assign_by_ref('colorFields', $this->_colorFields);
189
190 $this->_refreshButtonName = $this->getButtonName('refresh');
191 $this->addElement('xbutton',
192 $this->_refreshButtonName,
193 ts('Save and Preview'),
194 ['type' => 'submit', 'class' => 'crm-button crm-form-submit crm-button-type-submit']
195 );
196 parent::buildQuickForm();
197 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Widget', 'formRule'], $this);
198 }
199
200 /**
201 * Validation.
202 *
203 * @param array $params
204 * (ref.) an assoc array of name/value pairs.
205 *
206 * @param $files
207 * @param self $self
208 *
209 * @return bool|array
210 * mixed true or array of errors
211 */
212 public static function formRule($params, $files, $self) {
213 $errors = [];
214 if (!empty($params['is_active'])) {
215 if (empty($params['title'])) {
216 $errors['title'] = ts('Title is a required field.');
217 }
218 if (empty($params['about'])) {
219 $errors['about'] = ts('About is a required field.');
220 }
221
222 foreach ($params as $key => $val) {
223 if (substr($key, 0, 6) == 'color_' && empty($params[$key])) {
224 $errors[$key] = ts('%1 is a required field.', [1 => $self->_colorFields[$key][0]]);
225 }
226 }
227 }
228 return empty($errors) ? TRUE : $errors;
229 }
230
231 public function postProcess() {
232 //to reset quickform elements of next (pcp) page.
233 if ($this->controller->getNextName('Widget') == 'PCP') {
234 $this->controller->resetPage('PCP');
235 }
236
237 // get the submitted form values.
238 $params = $this->controller->exportValues($this->_name);
239
240 if ($this->_widget) {
241 $params['id'] = $this->_widget->id;
242 }
243 $params['contribution_page_id'] = $this->_id;
244 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
245 $params['url_homepage'] = 'null';
246
247 $widget = new CRM_Contribute_DAO_Widget();
248 $widget->copyValues($params);
249 $widget->save();
250
251 $buttonName = $this->controller->getButtonName();
252 if ($buttonName == $this->_refreshButtonName) {
253 return;
254 }
255 parent::endPostProcess();
256 }
257
258 /**
259 * Return a descriptive name for the page, used in wizard header
260 *
261 * @return string
262 */
263 public function getTitle() {
264 return ts('Widget Settings');
265 }
266
267 }