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