#30845, #30847 bug fixes.
[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,
31 '#2786C2',
32 ),
540f6530 33 'color_title_bg' => array(ts('Title background color'),
1720f579
M
34 'text',
35 FALSE,
36 '#FFFFFF',
37 ),
f00a3ea9 38 'color_bar' => array(ts('Progress Bar Color'),
1720f579
M
39 'text',
40 FALSE,
41 '#FFFFFF',
42 ),
540f6530 43 'color_widget_bg' => array(ts('Background color'),
1720f579
M
44 'text',
45 FALSE,
46 '#96C0E7',
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,
56 '#96C0E7',
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,
f00a3ea9 66 '#96C0E7',
1720f579 67 ),
540f6530 68 'color_button_bg' => array(ts('Button background color'),
1720f579
M
69 'text',
70 FALSE,
f00a3ea9 71 '#96C0E7',
1720f579 72 ),
f00a3ea9 73 );
1720f579
M
74 }
75
94686b36
M
76 function setDefaultValues() {
77 $defaults = array();
246ac1fd
J
78 if (!isset($this->_id)) {
79 $defaults['size_variant'] = 'normal';
80 foreach ($this->_colorFields as $name => $val) {
81 $defaults[$name] = $val[3];
82 }
94686b36 83 }
94686b36
M
84 return $defaults;
85 }
86
1720f579 87 function buildQuickForm() {
1720f579
M
88 // add form elements
89 $this->add('text', 'title', ts('Title'),true);
90 $this->add('text', 'logo_image', ts('Logo image'));
91 $this->add('text', 'image', ts('Image'));
b18480e2 92 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
1720f579 93 $this->add('text', 'button_title', ts('Contribution button title'));
f00a3ea9 94 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
94686b36 95 $this->addWysiwyg('description', ts('Description'), '');
1720f579
M
96 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
97 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
98 foreach ($this->_colorFields as $name => $val) {
99 $this->add($val[1],
100 $name,
101 $val[0],
102 $name,
103 $val[2]
104 );
105 }
106 $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
107 $this->add('checkbox', 'override', ts('Override default template'));
abc1995a
J
108 $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>'));
109
7d8ac5b7 110 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
60488185
M
111 $this->addButtons(array(
112 array(
113 'type' => 'submit',
1720f579 114 'name' => ts('Save'),
60488185
M
115 'isDefault' => TRUE,
116 ),
7d8ac5b7 117 array(
052bfa0f 118 'type' => 'next',
7d8ac5b7
J
119 'name' => ts('Save & Preview'),
120 ),
60488185 121 ));
7d8ac5b7 122
60488185
M
123 // export form elements
124 $this->assign('elementNames', $this->getRenderableElementNames());
ca0dca6f 125
ca0dca6f
J
126 if (isset($this->_id)) {
127 /** Updating existing widget*/
5881d614
J
128
129 /*$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
130where w.id=" . $this->_id;*/
ca0dca6f 131
5881d614 132 $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id;
ca0dca6f
J
133 $params = array();
134
e364fc74 135 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
ca0dca6f
J
136
137 while ($dao->fetch()) {
5881d614 138
ca0dca6f
J
139 $wid_page[$dao->id] = array();
140 CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
141
142 $this->setDefaults(array(
143 'title' => $wid_page[$dao->id]['title']));
144 $this->setDefaults(array(
145 'logo_image' => $wid_page[$dao->id]['logo_image']));
146 $this->setDefaults(array(
147 'image' => $wid_page[$dao->id]['image']));
148 $this->setDefaults(array(
149 'button_link_to' => $wid_page[$dao->id]['button_link_to']));
150 $this->setDefaults(array(
151 'button_title' => $wid_page[$dao->id]['button_title']));
152
153 $this->setDefaults(array(
5881d614 154 'progress_bar' => $dao->progress_bar_id/*$dao->pbid*/));
ca0dca6f
J
155 $description = base64_decode($wid_page[$dao->id]['description']);
156 $this->setDefaults(array(
157 'description' => $description));
158 $this->setDefaults(array(
159 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
160 $this->setDefaults(array(
5881d614 161 'size_variant' => $dao->size_variant));
ca0dca6f
J
162 $this->setDefaults(array(
163 'color_title' => $wid_page[$dao->id]['color_title']));
164 $this->setDefaults(array(
165 'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
166 $this->setDefaults(array(
167 'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
168 $this->setDefaults(array(
169 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
170 $this->setDefaults(array(
171 'color_description' => $wid_page[$dao->id]['color_description']));
172 $this->setDefaults(array(
173 'color_border' => $wid_page[$dao->id]['color_border']));
174 $this->setDefaults(array(
175 'color_button' => $wid_page[$dao->id]['color_button']));
176 $this->setDefaults(array(
177 'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
178 $this->setDefaults(array(
179 'style_rules' => $wid_page[$dao->id]['style_rules']));
180 $this->setDefaults(array(
181 'override' => $wid_page[$dao->id]['override']));
a946a2b6
J
182 if(true == $wid_page[$dao->id]['override']) {
183 $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);
184 $this->setDefaults(array(
ca0dca6f 185 'custom_template' => $cust_templ));
64276c2d
J
186 } else {
187 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
188 $elem = $this->getElement('custom_template');
189 $elem->setValue($output);
a946a2b6 190 }
ca0dca6f 191 }
540f6530 192 CRM_Utils_System::setTitle(ts('Edit Widget'));
ca0dca6f
J
193 }
194 else {
540f6530 195 CRM_Utils_System::setTitle(ts('Create Widget'));
ca0dca6f 196 /** Keep template in civicrm-wci/templates folder*/
851c37a8 197 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
ca0dca6f 198 $elem = $this->getElement('custom_template');
64276c2d 199 $elem->setValue($output);
ca0dca6f 200 }
60488185
M
201 parent::buildQuickForm();
202 }
203
204 function postProcess() {
205 $values = $this->exportValues();
f00a3ea9
J
206
207 $override = 0;
ca0dca6f
J
208 $cust_tmpl = "";
209 $cust_tmpl_col = "";
210 $sql = "";
a946a2b6
J
211 $coma = "";
212 $equals = "";
c181cced 213 $quote = "";
ca0dca6f
J
214 /** If override check is checked state then only save the custom_template to the
215 database. otherwise wci uses default tpl file.
216 */
f00a3ea9
J
217 if(isset($values['override'])){
218 $override = $values['override'];
ca0dca6f 219 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
c181cced
J
220 $cust_tmpl_col = "custom_template";
221 $coma = ",";
222 $equals = " = ";
223 $quote = "'";
f00a3ea9 224 }
12e32264 225
ca0dca6f 226 if (isset($this->_id)) {
ca0dca6f
J
227 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
228 . "', logo_image = '" . $values['logo_image'] . "', image = '"
229 . $values['image'] . "', button_title = '" . $values['button_title']
230 . "', button_link_to = '" . $values['button_link_to']
231 . "', progress_bar_id = '" . $values['progress_bar']
232 . "', description = '" . base64_encode($values['description'])
233 . "', email_signup_group_id = '" . $values['email_signup_group_id']
234 . "', size_variant = '" . $values['size_variant']
235 . "', color_title = '" . $values['color_title']
236 . "', color_title_bg = '" . $values['color_title_bg']
237 . "', color_progress_bar = '" . $values['color_bar']
238 . "', color_widget_bg = '" . $values['color_widget_bg']
239 . "', color_description = '" . $values['color_description']
240 . "', color_border = '" . $values['color_border']
241 . "', color_button = '" . $values['color_button']
242 . "', color_button_bg = '" . $values['color_button_bg']
243 . "', style_rules = '" . $values['style_rules'] . "', override = '"
c181cced 244 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
ca0dca6f
J
245 }
246 else {
247 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
248 button_title, button_link_to, progress_bar_id, description,
249 email_signup_group_id, size_variant, color_title, color_title_bg,
250 color_progress_bar, color_widget_bg, color_description, color_border,
c181cced 251 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
ca0dca6f
J
252 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
253 $values['image'] . "','" . $values['button_title'] . "','" .
254 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
255 base64_encode($values['description']) . "','" .
256 $values['email_signup_group_id'] . "','" .
257 $values['size_variant'] . "','" . $values['color_title'] . "','" .
258 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
259 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
260 $values['color_border'] . "','" . $values['color_button'] . "','" .
261 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
c181cced 262 $override . $quote . $coma . $quote . $cust_tmpl
ca0dca6f 263 . "')";
a946a2b6 264 }
c181cced 265
f00a3ea9
J
266 $errorScope = CRM_Core_TemporaryErrorScope::useException();
267 try {
268 $transaction = new CRM_Core_Transaction();
12e32264 269 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;");
f00a3ea9 270 CRM_Core_DAO::executeQuery($sql);
12e32264 271 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
f00a3ea9 272 $transaction->commit();
052bfa0f
J
273
274 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
b22be3dc
J
275 (isset($this->_id)) ? $widget_id = $this->_id :
276 $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
277 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
052bfa0f 278 } else {
a8c878ab 279 CRM_Utils_System::redirect('widget?reset=1');
052bfa0f 280 }
f00a3ea9
J
281 }
282 catch (Exception $e) {
283 //TODO
284 print_r($e->getMessage());
285 $transaction->rollback();
286 }
052bfa0f 287
60488185
M
288 parent::postProcess();
289 }
f00a3ea9
J
290
291 function getProgressBars() {
292 $options = array(
293 '' => ts('- select -'),
294 );
e364fc74 295 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
f00a3ea9
J
296 foreach ($pbList as $pb) {
297 $options[$pb['id']] = $pb['name'];
298 }
60488185 299
f00a3ea9
J
300 return $options;
301 }
1720f579
M
302 function getContributionPageOptions() {
303 $options = array(
304 '' => ts('- select -'),
305 );
306
94686b36 307 $result = civicrm_api3('contribution_page', 'get');
1720f579
M
308 foreach ($result['values'] as $contribution_page) {
309 $options[$contribution_page['id']] = $contribution_page['title'];
310 }
311
312 return $options;
313 }
314
315 function getGroupOptions() {
60488185
M
316 $options = array(
317 '' => ts('- select -'),
60488185 318 );
1720f579 319
2575ba7d
M
320 $result = civicrm_api3('group', 'get', array(
321 'group_type' => '2'
322 ));
323
1720f579
M
324 foreach ($result['values'] as $group) {
325 $options[$group['id']] = $group['title'];
60488185 326 }
1720f579
M
327
328 return $options;
329 }
330
331 function getSizeOptions() {
332 $options = array(
1720f579
M
333 'thin' => ts('Thin'),
334 'normal' => ts('Normal'),
335 'wide' => ts('Wide'),
336 );
337
60488185
M
338 return $options;
339 }
340
341 /**
342 * Get the fields/elements defined in this form.
343 *
344 * @return array (string)
345 */
346 function getRenderableElementNames() {
347 // The _elements list includes some items which should not be
348 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
349 // items don't have labels. We'll identify renderable by filtering on
350 // the 'label'.
351 $elementNames = array();
352 foreach ($this->_elements as $element) {
353 $label = $element->getLabel();
354 if (!empty($label)) {
355 $elementNames[] = $element->getName();
356 }
357 }
358 return $elementNames;
359 }
360}