#30660 fixed update widget without pb selected
[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 ),
f00a3ea9 33 'color_title_bg' => array(ts('Widget 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 ),
f00a3ea9 43 'color_widget_bg' => array(ts('Widget background color'),
1720f579
M
44 'text',
45 FALSE,
46 '#96C0E7',
47 ),
f00a3ea9 48 'color_description' => array(ts('Widget description color'),
1720f579
M
49 'text',
50 FALSE,
5881d614 51 '#000000',
1720f579 52 ),
f00a3ea9 53 'color_border' => array(ts('Widget border color'),
1720f579
M
54 'text',
55 FALSE,
56 '#96C0E7',
57 ),
f00a3ea9 58 'color_button' => array(ts('Widget button text color'),
1720f579
M
59 'text',
60 FALSE,
7d8ac5b7 61 '#000000',
1720f579 62 ),
f00a3ea9 63 'color_button_bg' => array(ts('Widget button background color'),
1720f579
M
64 'text',
65 FALSE,
f00a3ea9 66 '#96C0E7',
1720f579 67 ),
f00a3ea9 68 'color_button_bg' => array(ts('Widget 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
J
191 }
192 }
193 else {
194 /** Keep template in civicrm-wci/templates folder*/
851c37a8 195 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
ca0dca6f 196 $elem = $this->getElement('custom_template');
64276c2d 197 $elem->setValue($output);
ca0dca6f 198 }
60488185
M
199 parent::buildQuickForm();
200 }
201
202 function postProcess() {
203 $values = $this->exportValues();
f00a3ea9
J
204
205 $override = 0;
ca0dca6f
J
206 $cust_tmpl = "";
207 $cust_tmpl_col = "";
208 $sql = "";
a946a2b6
J
209 $coma = "";
210 $equals = "";
c181cced 211 $quote = "";
ca0dca6f
J
212 /** If override check is checked state then only save the custom_template to the
213 database. otherwise wci uses default tpl file.
214 */
f00a3ea9
J
215 if(isset($values['override'])){
216 $override = $values['override'];
ca0dca6f 217 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
c181cced
J
218 $cust_tmpl_col = "custom_template";
219 $coma = ",";
220 $equals = " = ";
221 $quote = "'";
f00a3ea9 222 }
12e32264 223
ca0dca6f 224 if (isset($this->_id)) {
ca0dca6f
J
225 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
226 . "', logo_image = '" . $values['logo_image'] . "', image = '"
227 . $values['image'] . "', button_title = '" . $values['button_title']
228 . "', button_link_to = '" . $values['button_link_to']
229 . "', progress_bar_id = '" . $values['progress_bar']
230 . "', description = '" . base64_encode($values['description'])
231 . "', email_signup_group_id = '" . $values['email_signup_group_id']
232 . "', size_variant = '" . $values['size_variant']
233 . "', color_title = '" . $values['color_title']
234 . "', color_title_bg = '" . $values['color_title_bg']
235 . "', color_progress_bar = '" . $values['color_bar']
236 . "', color_widget_bg = '" . $values['color_widget_bg']
237 . "', color_description = '" . $values['color_description']
238 . "', color_border = '" . $values['color_border']
239 . "', color_button = '" . $values['color_button']
240 . "', color_button_bg = '" . $values['color_button_bg']
241 . "', style_rules = '" . $values['style_rules'] . "', override = '"
c181cced 242 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
ca0dca6f
J
243 }
244 else {
245 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
246 button_title, button_link_to, progress_bar_id, description,
247 email_signup_group_id, size_variant, color_title, color_title_bg,
248 color_progress_bar, color_widget_bg, color_description, color_border,
c181cced 249 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
ca0dca6f
J
250 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
251 $values['image'] . "','" . $values['button_title'] . "','" .
252 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
253 base64_encode($values['description']) . "','" .
254 $values['email_signup_group_id'] . "','" .
255 $values['size_variant'] . "','" . $values['color_title'] . "','" .
256 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
257 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
258 $values['color_border'] . "','" . $values['color_button'] . "','" .
259 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
c181cced 260 $override . $quote . $coma . $quote . $cust_tmpl
ca0dca6f 261 . "')";
a946a2b6 262 }
c181cced 263
f00a3ea9
J
264 $errorScope = CRM_Core_TemporaryErrorScope::useException();
265 try {
266 $transaction = new CRM_Core_Transaction();
12e32264 267 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;");
f00a3ea9 268 CRM_Core_DAO::executeQuery($sql);
12e32264 269 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
f00a3ea9 270 $transaction->commit();
052bfa0f
J
271
272 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
b22be3dc
J
273 (isset($this->_id)) ? $widget_id = $this->_id :
274 $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
275 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
052bfa0f 276 } else {
a8c878ab 277 CRM_Utils_System::redirect('widget?reset=1');
052bfa0f 278 }
f00a3ea9
J
279 }
280 catch (Exception $e) {
281 //TODO
282 print_r($e->getMessage());
283 $transaction->rollback();
284 }
052bfa0f 285
60488185
M
286 parent::postProcess();
287 }
f00a3ea9
J
288
289 function getProgressBars() {
290 $options = array(
291 '' => ts('- select -'),
292 );
e364fc74 293 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
f00a3ea9
J
294 foreach ($pbList as $pb) {
295 $options[$pb['id']] = $pb['name'];
296 }
60488185 297
f00a3ea9
J
298 return $options;
299 }
1720f579
M
300 function getContributionPageOptions() {
301 $options = array(
302 '' => ts('- select -'),
303 );
304
94686b36 305 $result = civicrm_api3('contribution_page', 'get');
1720f579
M
306 foreach ($result['values'] as $contribution_page) {
307 $options[$contribution_page['id']] = $contribution_page['title'];
308 }
309
310 return $options;
311 }
312
313 function getGroupOptions() {
60488185
M
314 $options = array(
315 '' => ts('- select -'),
60488185 316 );
1720f579 317
2575ba7d
M
318 $result = civicrm_api3('group', 'get', array(
319 'group_type' => '2'
320 ));
321
1720f579
M
322 foreach ($result['values'] as $group) {
323 $options[$group['id']] = $group['title'];
60488185 324 }
1720f579
M
325
326 return $options;
327 }
328
329 function getSizeOptions() {
330 $options = array(
1720f579
M
331 'thin' => ts('Thin'),
332 'normal' => ts('Normal'),
333 'wide' => ts('Wide'),
334 );
335
60488185
M
336 return $options;
337 }
338
339 /**
340 * Get the fields/elements defined in this form.
341 *
342 * @return array (string)
343 */
344 function getRenderableElementNames() {
345 // The _elements list includes some items which should not be
346 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
347 // items don't have labels. We'll identify renderable by filtering on
348 // the 'label'.
349 $elementNames = array();
350 foreach ($this->_elements as $element) {
351 $label = $element->getLabel();
352 if (!empty($label)) {
353 $elementNames[] = $element->getName();
354 }
355 }
356 return $elementNames;
357 }
358}