Fix button breakage on viewContribution
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Widget.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contribute_Form_ContributionPage_Widget extends CRM_Contribute_Form_ContributionPage {
18 protected $_colors;
19
430ae6dd
TO
20 protected $_widget;
21
00be9182 22 public function preProcess() {
6a488035 23 parent::preProcess();
7885e669 24 $this->setSelectedChild('widget');
6a488035
TO
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
3699ac58 43 $this->assign('widgetExternUrl', CRM_Utils_System::externUrl('extern/widget', "cpageId={$this->_id}&widgetId=" . ($this->_widget->id ?? '') . "&format=3"));
9df3628e 44
6a488035
TO
45 $config = CRM_Core_Config::singleton();
46 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
47 $this->_id,
48 'title'
49 );
50
be2fb01f
CW
51 $this->_fields = [
52 'title' => [
353ffa53 53 ts('Title'),
6a488035
TO
54 'text',
55 FALSE,
56 $title,
be2fb01f
CW
57 ],
58 'url_logo' => [
353ffa53 59 ts('URL to Logo Image'),
6a488035
TO
60 'text',
61 FALSE,
62 NULL,
be2fb01f
CW
63 ],
64 'button_title' => [
353ffa53 65 ts('Button Title'),
6a488035
TO
66 'text',
67 FALSE,
68 ts('Contribute!'),
be2fb01f
CW
69 ],
70 ];
6a488035 71
be2fb01f
CW
72 $this->_colorFields = [
73 'color_title' => [
353ffa53 74 ts('Title Text Color'),
6320652c 75 'color',
6a488035
TO
76 FALSE,
77 '#2786C2',
be2fb01f
CW
78 ],
79 'color_bar' => [
353ffa53 80 ts('Progress Bar Color'),
6320652c 81 'color',
6a488035 82 FALSE,
e950cdd3 83 '#2786C2',
be2fb01f
CW
84 ],
85 'color_main_text' => [
353ffa53 86 ts('Additional Text Color'),
6320652c 87 'color',
6a488035
TO
88 FALSE,
89 '#FFFFFF',
be2fb01f
CW
90 ],
91 'color_main' => [
353ffa53 92 ts('Background Color'),
6320652c 93 'color',
6a488035
TO
94 FALSE,
95 '#96C0E7',
be2fb01f
CW
96 ],
97 'color_main_bg' => [
353ffa53 98 ts('Background Color Top Area'),
6320652c 99 'color',
6a488035
TO
100 FALSE,
101 '#B7E2FF',
be2fb01f
CW
102 ],
103 'color_bg' => [
353ffa53 104 ts('Border Color'),
6320652c 105 'color',
6a488035
TO
106 FALSE,
107 '#96C0E7',
be2fb01f
CW
108 ],
109 'color_about_link' => [
e950cdd3 110 ts('Button Text Color'),
6320652c 111 'color',
6a488035
TO
112 FALSE,
113 '#556C82',
be2fb01f
CW
114 ],
115 'color_button' => [
353ffa53 116 ts('Button Background Color'),
6320652c 117 'color',
6a488035
TO
118 FALSE,
119 '#FFFFFF',
be2fb01f
CW
120 ],
121 'color_homepage_link' => [
353ffa53 122 ts('Homepage Link Color'),
6320652c 123 'color',
6a488035
TO
124 FALSE,
125 '#FFFFFF',
be2fb01f
CW
126 ],
127 ];
6a488035
TO
128 }
129
186c9c17 130 /**
95cdcc0f 131 * Set default values for the form.
186c9c17 132 */
00be9182 133 public function setDefaultValues() {
be2fb01f 134 $defaults = [];
6a488035
TO
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
00be9182 158 public function buildQuickForm() {
6a488035
TO
159 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Widget');
160
161 $this->addElement('checkbox',
162 'is_active',
163 ts('Enable Widget?'),
164 NULL,
be2fb01f 165 ['onclick' => "widgetBlock(this)"]
6a488035
TO
166 );
167
5d51a2f9 168 $this->add('wysiwyg', 'about', ts('About'), $attributes['about']);
6a488035
TO
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');
cbd83dde 191 $this->addElement('xbutton',
6a488035 192 $this->_refreshButtonName,
cbd83dde 193 ts('Save and Preview'),
5cbbd191 194 ['type' => 'submit', 'class' => 'crm-button crm-form-submit crm-button-type-submit']
6a488035
TO
195 );
196 parent::buildQuickForm();
be2fb01f 197 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Widget', 'formRule'], $this);
6a488035
TO
198 }
199
200 /**
fe482240 201 * Validation.
6a488035 202 *
014c4014
TO
203 * @param array $params
204 * (ref.) an assoc array of name/value pairs.
6a488035 205 *
da6b46f4 206 * @param $files
e8cf95b4 207 * @param self $self
da6b46f4 208 *
72b3a70c
CW
209 * @return bool|array
210 * mixed true or array of errors
6a488035
TO
211 */
212 public static function formRule($params, $files, $self) {
be2fb01f 213 $errors = [];
a7488080
CW
214 if (!empty($params['is_active'])) {
215 if (empty($params['title'])) {
6a488035
TO
216 $errors['title'] = ts('Title is a required field.');
217 }
a7488080 218 if (empty($params['about'])) {
6a488035
TO
219 $errors['about'] = ts('About is a required field.');
220 }
221
222 foreach ($params as $key => $val) {
8cc574cf 223 if (substr($key, 0, 6) == 'color_' && empty($params[$key])) {
be2fb01f 224 $errors[$key] = ts('%1 is a required field.', [1 => $self->_colorFields[$key][0]]);
6a488035
TO
225 }
226 }
227 }
228 return empty($errors) ? TRUE : $errors;
229 }
230
00be9182 231 public function postProcess() {
6a488035
TO
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
6a488035
TO
262 */
263 public function getTitle() {
264 return ts('Widget Settings');
265 }
96025800 266
6a488035 267}