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