Merge pull request #18000 from eileenmcnaughton/brn
[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
9df3628e
AM
43 $this->assign('widgetExternUrl', CRM_Utils_System::externUrl('extern/widget', "cpageId={$this->_id}&widgetId={$this->_widget->id}&format=3"));
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');
191 $this->addElement('submit',
192 $this->_refreshButtonName,
193 ts('Save and Preview')
194 );
195 parent::buildQuickForm();
be2fb01f 196 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Widget', 'formRule'], $this);
6a488035
TO
197 }
198
199 /**
fe482240 200 * Validation.
6a488035 201 *
014c4014
TO
202 * @param array $params
203 * (ref.) an assoc array of name/value pairs.
6a488035 204 *
da6b46f4
EM
205 * @param $files
206 * @param $self
207 *
72b3a70c
CW
208 * @return bool|array
209 * mixed true or array of errors
6a488035
TO
210 */
211 public static function formRule($params, $files, $self) {
be2fb01f 212 $errors = [];
a7488080
CW
213 if (!empty($params['is_active'])) {
214 if (empty($params['title'])) {
6a488035
TO
215 $errors['title'] = ts('Title is a required field.');
216 }
a7488080 217 if (empty($params['about'])) {
6a488035
TO
218 $errors['about'] = ts('About is a required field.');
219 }
220
221 foreach ($params as $key => $val) {
8cc574cf 222 if (substr($key, 0, 6) == 'color_' && empty($params[$key])) {
be2fb01f 223 $errors[$key] = ts('%1 is a required field.', [1 => $self->_colorFields[$key][0]]);
6a488035
TO
224 }
225 }
226 }
227 return empty($errors) ? TRUE : $errors;
228 }
229
00be9182 230 public function postProcess() {
6a488035
TO
231 //to reset quickform elements of next (pcp) page.
232 if ($this->controller->getNextName('Widget') == 'PCP') {
233 $this->controller->resetPage('PCP');
234 }
235
236 // get the submitted form values.
237 $params = $this->controller->exportValues($this->_name);
238
239 if ($this->_widget) {
240 $params['id'] = $this->_widget->id;
241 }
242 $params['contribution_page_id'] = $this->_id;
243 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
244 $params['url_homepage'] = 'null';
245
246 $widget = new CRM_Contribute_DAO_Widget();
247 $widget->copyValues($params);
248 $widget->save();
249
250 $buttonName = $this->controller->getButtonName();
251 if ($buttonName == $this->_refreshButtonName) {
252 return;
253 }
254 parent::endPostProcess();
255 }
256
257 /**
258 * Return a descriptive name for the page, used in wizard header
259 *
260 * @return string
6a488035
TO
261 */
262 public function getTitle() {
263 return ts('Widget Settings');
264 }
96025800 265
6a488035 266}