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