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