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