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