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