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