Commit | Line | Data |
---|---|---|
60488185 M |
1 | <?php |
2 | ||
3 | require_once 'CRM/Core/Form.php'; | |
b18480e2 | 4 | require_once 'wci-helper-functions.php'; |
f00a3ea9 | 5 | require_once 'CRM/Wci/BAO/ProgressBar.php'; |
60488185 M |
6 | |
7 | /** | |
8 | * Form controller class | |
9 | * | |
10 | * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference | |
11 | */ | |
12 | class CRM_Wci_Form_CreateWidget extends CRM_Core_Form { | |
1720f579 | 13 | |
94686b36 M |
14 | /** |
15 | * the widget id saved to the session for an update | |
16 | * | |
17 | * @var int | |
18 | * @access protected | |
19 | */ | |
20 | protected $_id; | |
21 | ||
1720f579 | 22 | function preProcess() { |
0514c86a | 23 | CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'createwidget.js'); |
1720f579 | 24 | parent::preProcess(); |
ca0dca6f J |
25 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, |
26 | FALSE, NULL, 'REQUEST' ); | |
94686b36 | 27 | |
1720f579 M |
28 | $this->_colorFields = array('color_title' => array(ts('Title Text Color'), |
29 | 'text', | |
30 | FALSE, | |
68f8aff6 | 31 | '#BF0F0F', |
1720f579 | 32 | ), |
540f6530 | 33 | 'color_title_bg' => array(ts('Title background color'), |
1720f579 M |
34 | 'text', |
35 | FALSE, | |
68f8aff6 | 36 | '#FFFFFF', |
1720f579 | 37 | ), |
f00a3ea9 | 38 | 'color_bar' => array(ts('Progress Bar Color'), |
1720f579 M |
39 | 'text', |
40 | FALSE, | |
4d4d53e3 | 41 | '#BF0F0F', |
1720f579 | 42 | ), |
540f6530 | 43 | 'color_widget_bg' => array(ts('Background color'), |
1720f579 M |
44 | 'text', |
45 | FALSE, | |
4d4d53e3 | 46 | '#FFFFFF', |
1720f579 | 47 | ), |
540f6530 | 48 | 'color_description' => array(ts('Description color'), |
1720f579 M |
49 | 'text', |
50 | FALSE, | |
5881d614 | 51 | '#000000', |
1720f579 | 52 | ), |
540f6530 | 53 | 'color_border' => array(ts('Border color'), |
1720f579 M |
54 | 'text', |
55 | FALSE, | |
4d4d53e3 | 56 | '#BF0F0F', |
1720f579 | 57 | ), |
540f6530 | 58 | 'color_button' => array(ts('Button text color'), |
1720f579 M |
59 | 'text', |
60 | FALSE, | |
7d8ac5b7 | 61 | '#000000', |
1720f579 | 62 | ), |
540f6530 | 63 | 'color_button_bg' => array(ts('Button background color'), |
1720f579 M |
64 | 'text', |
65 | FALSE, | |
4d4d53e3 | 66 | '#BF0F0F', |
1720f579 | 67 | ), |
f00a3ea9 | 68 | ); |
1720f579 M |
69 | } |
70 | ||
94686b36 M |
71 | function setDefaultValues() { |
72 | $defaults = array(); | |
246ac1fd J |
73 | if (!isset($this->_id)) { |
74 | $defaults['size_variant'] = 'normal'; | |
75 | foreach ($this->_colorFields as $name => $val) { | |
76 | $defaults[$name] = $val[3]; | |
77 | } | |
94686b36 | 78 | } |
94686b36 M |
79 | return $defaults; |
80 | } | |
81 | ||
1720f579 | 82 | function buildQuickForm() { |
1720f579 | 83 | // add form elements |
4d4d53e3 M |
84 | $this->add('text', 'title', ts('Title'),true)->setSize(45); |
85 | $this->add('text', 'logo_image', ts('Logo image'))->setSize(45); | |
86 | $this->add('text', 'image', ts('Image'))->setSize(45); | |
b18480e2 | 87 | $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions()); |
4d4d53e3 | 88 | $this->add('text', 'button_title', ts('Contribution button title'))->setSize(45); |
f00a3ea9 | 89 | $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars()); |
94686b36 | 90 | $this->addWysiwyg('description', ts('Description'), ''); |
1720f579 M |
91 | $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions()); |
92 | $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions()); | |
4d4d53e3 | 93 | // $fieldset = $this->addElement('fieldset')->setLabel('Advanced Settings'); |
6d608c2b J |
94 | $this->add('checkbox', 'hide_title', ts('Hide Title')); |
95 | $this->add('checkbox', 'hide_border', ts('Hide border')); | |
96 | $this->add('checkbox', 'hide_pbcap', ts('Hide progress bar caption')); | |
1720f579 | 97 | foreach ($this->_colorFields as $name => $val) { |
4d4d53e3 | 98 | $this->addElement($val[1], |
1720f579 M |
99 | $name, |
100 | $val[0], | |
101 | $name, | |
102 | $val[2] | |
103 | ); | |
104 | } | |
2082fcdf | 105 | $this->add('textarea', 'style_rules', ts('Additional Style Rules'))->setRows(5); |
1720f579 | 106 | $this->add('checkbox', 'override', ts('Override default template')); |
2082fcdf | 107 | $this->add('textarea', 'custom_template', ts('Custom template:<br><SMALL><font color="red">Please customize the smarty v2 template only if you know what you are doing</font></SMALL>'))->setRows(10); |
abc1995a | 108 | |
7d8ac5b7 | 109 | $this->addElement('submit','preview','name="Save and Preview" id="preview"'); |
60488185 M |
110 | $this->addButtons(array( |
111 | array( | |
112 | 'type' => 'submit', | |
1720f579 | 113 | 'name' => ts('Save'), |
60488185 M |
114 | 'isDefault' => TRUE, |
115 | ), | |
7d8ac5b7 | 116 | array( |
052bfa0f | 117 | 'type' => 'next', |
7d8ac5b7 J |
118 | 'name' => ts('Save & Preview'), |
119 | ), | |
60488185 | 120 | )); |
7d8ac5b7 | 121 | |
60488185 M |
122 | // export form elements |
123 | $this->assign('elementNames', $this->getRenderableElementNames()); | |
ca0dca6f | 124 | |
ca0dca6f J |
125 | if (isset($this->_id)) { |
126 | /** Updating existing widget*/ | |
5881d614 J |
127 | |
128 | /*$query = "SELECT pb.id as pbid, w.* FROM civicrm_wci_widget w INNER JOIN civicrm_wci_progress_bar pb on pb.id = w.progress_bar_id | |
129 | where w.id=" . $this->_id;*/ | |
ca0dca6f | 130 | |
5881d614 | 131 | $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id; |
ca0dca6f J |
132 | $params = array(); |
133 | ||
e364fc74 | 134 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget'); |
ca0dca6f J |
135 | |
136 | while ($dao->fetch()) { | |
5881d614 | 137 | |
ca0dca6f J |
138 | $wid_page[$dao->id] = array(); |
139 | CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]); | |
140 | ||
141 | $this->setDefaults(array( | |
cb59e7c2 | 142 | 'title' => $wid_page[$dao->id]['title'])); |
ca0dca6f J |
143 | $this->setDefaults(array( |
144 | 'logo_image' => $wid_page[$dao->id]['logo_image'])); | |
145 | $this->setDefaults(array( | |
146 | 'image' => $wid_page[$dao->id]['image'])); | |
147 | $this->setDefaults(array( | |
148 | 'button_link_to' => $wid_page[$dao->id]['button_link_to'])); | |
149 | $this->setDefaults(array( | |
150 | 'button_title' => $wid_page[$dao->id]['button_title'])); | |
151 | ||
152 | $this->setDefaults(array( | |
5881d614 | 153 | 'progress_bar' => $dao->progress_bar_id/*$dao->pbid*/)); |
ca0dca6f J |
154 | $description = base64_decode($wid_page[$dao->id]['description']); |
155 | $this->setDefaults(array( | |
156 | 'description' => $description)); | |
157 | $this->setDefaults(array( | |
158 | 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id'])); | |
159 | $this->setDefaults(array( | |
5881d614 | 160 | 'size_variant' => $dao->size_variant)); |
ca0dca6f J |
161 | $this->setDefaults(array( |
162 | 'color_title' => $wid_page[$dao->id]['color_title'])); | |
163 | $this->setDefaults(array( | |
164 | 'color_title_bg' => $wid_page[$dao->id]['color_title_bg'])); | |
165 | $this->setDefaults(array( | |
166 | 'color_bar' => $wid_page[$dao->id]['color_progress_bar'])); | |
167 | $this->setDefaults(array( | |
168 | 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg'])); | |
169 | $this->setDefaults(array( | |
170 | 'color_description' => $wid_page[$dao->id]['color_description'])); | |
171 | $this->setDefaults(array( | |
172 | 'color_border' => $wid_page[$dao->id]['color_border'])); | |
173 | $this->setDefaults(array( | |
174 | 'color_button' => $wid_page[$dao->id]['color_button'])); | |
175 | $this->setDefaults(array( | |
176 | 'color_button_bg' => $wid_page[$dao->id]['color_button_bg'])); | |
177 | $this->setDefaults(array( | |
7b653fab | 178 | 'style_rules' => base64_decode($wid_page[$dao->id]['style_rules']))); |
ca0dca6f J |
179 | $this->setDefaults(array( |
180 | 'override' => $wid_page[$dao->id]['override'])); | |
6d608c2b J |
181 | $this->setDefaults(array( |
182 | 'hide_title' => $wid_page[$dao->id]['hide_title'])); | |
183 | $this->setDefaults(array( | |
184 | 'hide_border' => $wid_page[$dao->id]['hide_border'])); | |
185 | $this->setDefaults(array( | |
186 | 'hide_pbcap' => $wid_page[$dao->id]['hide_pbcap'])); | |
a946a2b6 J |
187 | if(true == $wid_page[$dao->id]['override']) { |
188 | $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']); | |
189 | $this->setDefaults(array( | |
ca0dca6f | 190 | 'custom_template' => $cust_templ)); |
64276c2d J |
191 | } else { |
192 | $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH); | |
193 | $elem = $this->getElement('custom_template'); | |
194 | $elem->setValue($output); | |
a946a2b6 | 195 | } |
ca0dca6f | 196 | } |
540f6530 | 197 | CRM_Utils_System::setTitle(ts('Edit Widget')); |
ca0dca6f J |
198 | } |
199 | else { | |
540f6530 | 200 | CRM_Utils_System::setTitle(ts('Create Widget')); |
ca0dca6f | 201 | /** Keep template in civicrm-wci/templates folder*/ |
851c37a8 | 202 | $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH); |
ca0dca6f | 203 | $elem = $this->getElement('custom_template'); |
64276c2d | 204 | $elem->setValue($output); |
ca0dca6f | 205 | } |
60488185 M |
206 | parent::buildQuickForm(); |
207 | } | |
208 | ||
209 | function postProcess() { | |
210 | $values = $this->exportValues(); | |
f00a3ea9 | 211 | $override = 0; |
6d608c2b J |
212 | $hide_title = 0; |
213 | $hide_border = 0; | |
214 | $hide_pbcap = 0; | |
ca0dca6f J |
215 | $cust_tmpl = ""; |
216 | $cust_tmpl_col = ""; | |
217 | $sql = ""; | |
a946a2b6 J |
218 | $coma = ""; |
219 | $equals = ""; | |
c181cced | 220 | $quote = ""; |
ca0dca6f J |
221 | /** If override check is checked state then only save the custom_template to the |
222 | database. otherwise wci uses default tpl file. | |
223 | */ | |
f00a3ea9 J |
224 | if(isset($values['override'])){ |
225 | $override = $values['override']; | |
ca0dca6f | 226 | $cust_tmpl = base64_encode(html_entity_decode($values['custom_template'])); |
c181cced J |
227 | $cust_tmpl_col = "custom_template"; |
228 | $coma = ","; | |
229 | $equals = " = "; | |
230 | $quote = "'"; | |
f00a3ea9 | 231 | } |
6d608c2b J |
232 | if(isset($values['hide_title'])){ |
233 | $hide_title = $values['hide_title']; | |
234 | } | |
235 | if(isset($values['hide_border'])){ | |
236 | $hide_border = $values['hide_border']; | |
237 | } | |
238 | if(isset($values['hide_pbcap'])){ | |
239 | $hide_pbcap = $values['hide_pbcap']; | |
240 | } | |
9b8544d7 | 241 | $title = str_replace("'", "''", $values['title']); |
6d608c2b | 242 | |
ca0dca6f | 243 | if (isset($this->_id)) { |
9b8544d7 | 244 | $sql = "UPDATE civicrm_wci_widget SET title = '". $title |
ca0dca6f J |
245 | . "', logo_image = '" . $values['logo_image'] . "', image = '" |
246 | . $values['image'] . "', button_title = '" . $values['button_title'] | |
247 | . "', button_link_to = '" . $values['button_link_to'] | |
248 | . "', progress_bar_id = '" . $values['progress_bar'] | |
249 | . "', description = '" . base64_encode($values['description']) | |
250 | . "', email_signup_group_id = '" . $values['email_signup_group_id'] | |
251 | . "', size_variant = '" . $values['size_variant'] | |
252 | . "', color_title = '" . $values['color_title'] | |
253 | . "', color_title_bg = '" . $values['color_title_bg'] | |
254 | . "', color_progress_bar = '" . $values['color_bar'] | |
255 | . "', color_widget_bg = '" . $values['color_widget_bg'] | |
256 | . "', color_description = '" . $values['color_description'] | |
257 | . "', color_border = '" . $values['color_border'] | |
258 | . "', color_button = '" . $values['color_button'] | |
6d608c2b J |
259 | . "', color_button_bg = '" . $values['color_button_bg'] |
260 | . "', hide_title = '" . $hide_title | |
261 | . "', hide_border = '" . $hide_border | |
262 | . "', hide_pbcap = '" . $hide_pbcap | |
7b653fab | 263 | . "', style_rules = '" . base64_encode($values['style_rules']) . "', override = '" |
c181cced | 264 | . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ; |
ca0dca6f J |
265 | } |
266 | else { | |
267 | $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image, | |
268 | button_title, button_link_to, progress_bar_id, description, | |
269 | email_signup_group_id, size_variant, color_title, color_title_bg, | |
270 | color_progress_bar, color_widget_bg, color_description, color_border, | |
6d608c2b | 271 | color_button, color_button_bg, hide_title, hide_border, hide_pbcap, style_rules, override" . $coma . $cust_tmpl_col ." ) |
9b8544d7 | 272 | VALUES ('" . $title . "','" . $values['logo_image'] . "','" . |
ca0dca6f J |
273 | $values['image'] . "','" . $values['button_title'] . "','" . |
274 | $values['button_link_to'] . "','" . $values['progress_bar'] . "','" . | |
275 | base64_encode($values['description']) . "','" . | |
276 | $values['email_signup_group_id'] . "','" . | |
277 | $values['size_variant'] . "','" . $values['color_title'] . "','" . | |
278 | $values['color_title_bg'] . "','" . $values['color_bar'] . "','" . | |
279 | $values['color_widget_bg'] . "','" . $values['color_description'] . "','" . | |
280 | $values['color_border'] . "','" . $values['color_button'] . "','" . | |
9b8544d7 J |
281 | $values['color_button_bg'] . "','" . $hide_title . "','" . |
282 | $hide_border . "','" . $hide_pbcap . "','" | |
6d608c2b | 283 | . base64_encode($values['style_rules']) . "','" . |
c181cced | 284 | $override . $quote . $coma . $quote . $cust_tmpl |
ca0dca6f | 285 | . "')"; |
a946a2b6 | 286 | } |
c181cced | 287 | |
f00a3ea9 J |
288 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); |
289 | try { | |
290 | $transaction = new CRM_Core_Transaction(); | |
12e32264 | 291 | CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;"); |
f00a3ea9 | 292 | CRM_Core_DAO::executeQuery($sql); |
12e32264 | 293 | CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;"); |
f00a3ea9 | 294 | $transaction->commit(); |
8966f337 | 295 | CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success'); |
052bfa0f | 296 | if(isset($_REQUEST['_qf_CreateWidget_next'])) { |
b22be3dc J |
297 | (isset($this->_id)) ? $widget_id = $this->_id : |
298 | $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'); | |
299 | CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id); | |
052bfa0f | 300 | } else { |
a8c878ab | 301 | CRM_Utils_System::redirect('widget?reset=1'); |
052bfa0f | 302 | } |
f00a3ea9 J |
303 | } |
304 | catch (Exception $e) { | |
8966f337 | 305 | CRM_Core_Session::setStatus(ts('Failed to create widget'), '', 'error'); |
f00a3ea9 J |
306 | $transaction->rollback(); |
307 | } | |
052bfa0f | 308 | |
60488185 M |
309 | parent::postProcess(); |
310 | } | |
f00a3ea9 J |
311 | |
312 | function getProgressBars() { | |
313 | $options = array( | |
314 | '' => ts('- select -'), | |
315 | ); | |
e364fc74 | 316 | $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList(); |
f00a3ea9 J |
317 | foreach ($pbList as $pb) { |
318 | $options[$pb['id']] = $pb['name']; | |
319 | } | |
60488185 | 320 | |
f00a3ea9 J |
321 | return $options; |
322 | } | |
1720f579 M |
323 | function getContributionPageOptions() { |
324 | $options = array( | |
325 | '' => ts('- select -'), | |
326 | ); | |
327 | ||
94686b36 | 328 | $result = civicrm_api3('contribution_page', 'get'); |
1720f579 M |
329 | foreach ($result['values'] as $contribution_page) { |
330 | $options[$contribution_page['id']] = $contribution_page['title']; | |
331 | } | |
332 | ||
333 | return $options; | |
334 | } | |
335 | ||
336 | function getGroupOptions() { | |
60488185 M |
337 | $options = array( |
338 | '' => ts('- select -'), | |
60488185 | 339 | ); |
1720f579 | 340 | |
2575ba7d M |
341 | $result = civicrm_api3('group', 'get', array( |
342 | 'group_type' => '2' | |
343 | )); | |
344 | ||
1720f579 M |
345 | foreach ($result['values'] as $group) { |
346 | $options[$group['id']] = $group['title']; | |
60488185 | 347 | } |
1720f579 M |
348 | |
349 | return $options; | |
350 | } | |
351 | ||
352 | function getSizeOptions() { | |
353 | $options = array( | |
1720f579 M |
354 | 'thin' => ts('Thin'), |
355 | 'normal' => ts('Normal'), | |
356 | 'wide' => ts('Wide'), | |
357 | ); | |
358 | ||
60488185 M |
359 | return $options; |
360 | } | |
361 | ||
362 | /** | |
363 | * Get the fields/elements defined in this form. | |
364 | * | |
365 | * @return array (string) | |
366 | */ | |
367 | function getRenderableElementNames() { | |
368 | // The _elements list includes some items which should not be | |
369 | // auto-rendered in the loop -- such as "qfKey" and "buttons". These | |
370 | // items don't have labels. We'll identify renderable by filtering on | |
371 | // the 'label'. | |
372 | $elementNames = array(); | |
373 | foreach ($this->_elements as $element) { | |
374 | $label = $element->getLabel(); | |
375 | if (!empty($label)) { | |
376 | $elementNames[] = $element->getName(); | |
377 | } | |
378 | } | |
379 | return $elementNames; | |
380 | } | |
381 | } |