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