navid error corrected
[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';
0fc9f109 6?>
60488185 7
0fc9f109 8<script type="text/javascript">
ca0dca6f
J
9cj(function ( $ ) {
10 function setState() {
0fc9f109
J
11 if ($('#override').is(':checked') == true) {
12 $('#custom_template').attr("disabled",false);
13 }
14 else {
15 $('#custom_template').attr("disabled",true);
ca0dca6f
J
16 }
17 }
18 $(document).ready(setState)
19 $('#override').click(setState);
20
0fc9f109
J
21});
22</script>
23
24<?php
60488185
M
25/**
26 * Form controller class
27 *
28 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
29 */
30class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
1720f579 31
94686b36
M
32 /**
33 * the widget id saved to the session for an update
34 *
35 * @var int
36 * @access protected
37 */
38 protected $_id;
39
1720f579
M
40 function preProcess() {
41 parent::preProcess();
ca0dca6f
J
42 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
43 FALSE, NULL, 'REQUEST' );
94686b36 44
1720f579
M
45 $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
46 'text',
47 FALSE,
48 '#2786C2',
49 ),
f00a3ea9 50 'color_title_bg' => array(ts('Widget title background color'),
1720f579
M
51 'text',
52 FALSE,
53 '#FFFFFF',
54 ),
f00a3ea9 55 'color_bar' => array(ts('Progress Bar Color'),
1720f579
M
56 'text',
57 FALSE,
58 '#FFFFFF',
59 ),
f00a3ea9 60 'color_widget_bg' => array(ts('Widget background color'),
1720f579
M
61 'text',
62 FALSE,
63 '#96C0E7',
64 ),
f00a3ea9 65 'color_description' => array(ts('Widget description color'),
1720f579
M
66 'text',
67 FALSE,
f00a3ea9 68 '#96C0E7',
1720f579 69 ),
f00a3ea9 70 'color_border' => array(ts('Widget border color'),
1720f579
M
71 'text',
72 FALSE,
73 '#96C0E7',
74 ),
f00a3ea9 75 'color_button' => array(ts('Widget button text color'),
1720f579
M
76 'text',
77 FALSE,
f00a3ea9 78 '#96C0E7',
1720f579 79 ),
f00a3ea9 80 'color_button_bg' => array(ts('Widget button background color'),
1720f579
M
81 'text',
82 FALSE,
f00a3ea9 83 '#96C0E7',
1720f579 84 ),
f00a3ea9 85 'color_button_bg' => array(ts('Widget button background color'),
1720f579
M
86 'text',
87 FALSE,
f00a3ea9 88 '#96C0E7',
1720f579 89 ),
f00a3ea9 90 );
1720f579
M
91 }
92
94686b36
M
93 function setDefaultValues() {
94 $defaults = array();
95
96 $defaults['size_variant'] = 'normal';
97 foreach ($this->_colorFields as $name => $val) {
98 $defaults[$name] = $val[3];
99 }
100
101 return $defaults;
102 }
103
1720f579 104 function buildQuickForm() {
1720f579
M
105 // add form elements
106 $this->add('text', 'title', ts('Title'),true);
107 $this->add('text', 'logo_image', ts('Logo image'));
108 $this->add('text', 'image', ts('Image'));
b18480e2 109 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
1720f579 110 $this->add('text', 'button_title', ts('Contribution button title'));
f00a3ea9 111 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
94686b36 112 $this->addWysiwyg('description', ts('Description'), '');
1720f579
M
113 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
114 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
115 foreach ($this->_colorFields as $name => $val) {
116 $this->add($val[1],
117 $name,
118 $val[0],
119 $name,
120 $val[2]
121 );
122 }
123 $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
124 $this->add('checkbox', 'override', ts('Override default template'));
0fc9f109
J
125 $this->add('textarea', 'custom_template', ts('Custom template'));
126 //$this->addWysiwyg('custom_template', ts('Custom template'), '');
60488185
M
127 $this->addButtons(array(
128 array(
129 'type' => 'submit',
1720f579 130 'name' => ts('Save'),
60488185
M
131 'isDefault' => TRUE,
132 ),
133 ));
134
135 // export form elements
136 $this->assign('elementNames', $this->getRenderableElementNames());
ca0dca6f 137
0fc9f109 138 $smarty = CRM_Core_Smarty::singleton();
ca0dca6f
J
139 if (isset($this->_id)) {
140 /** Updating existing widget*/
141
142 //$query = "SELECT * FROM civicrm_wci_widget where id=" . $this->_id;
143 $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
144where w.id=" . $this->_id;
145 $params = array();
146
147 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_WCI_DAO_Widget');
148
149 while ($dao->fetch()) {
150 $wid_page[$dao->id] = array();
151 CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
152
153 $this->setDefaults(array(
154 'title' => $wid_page[$dao->id]['title']));
155 $this->setDefaults(array(
156 'logo_image' => $wid_page[$dao->id]['logo_image']));
157 $this->setDefaults(array(
158 'image' => $wid_page[$dao->id]['image']));
159 $this->setDefaults(array(
160 'button_link_to' => $wid_page[$dao->id]['button_link_to']));
161 $this->setDefaults(array(
162 'button_title' => $wid_page[$dao->id]['button_title']));
163
164 $this->setDefaults(array(
165 'progress_bar' => $dao->pbid));
166 $description = base64_decode($wid_page[$dao->id]['description']);
167 $this->setDefaults(array(
168 'description' => $description));
169 $this->setDefaults(array(
170 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
171 $this->setDefaults(array(
172 'size_variant' => $wid_page[$dao->id]['size_variant']));
173 $this->setDefaults(array(
174 'color_title' => $wid_page[$dao->id]['color_title']));
175 $this->setDefaults(array(
176 'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
177 $this->setDefaults(array(
178 'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
179 $this->setDefaults(array(
180 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
181 $this->setDefaults(array(
182 'color_description' => $wid_page[$dao->id]['color_description']));
183 $this->setDefaults(array(
184 'color_border' => $wid_page[$dao->id]['color_border']));
185 $this->setDefaults(array(
186 'color_button' => $wid_page[$dao->id]['color_button']));
187 $this->setDefaults(array(
188 'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
189 $this->setDefaults(array(
190 'style_rules' => $wid_page[$dao->id]['style_rules']));
191 $this->setDefaults(array(
192 'override' => $wid_page[$dao->id]['override']));
193 $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);
194
195 $this->setDefaults(array(
196 'custom_template' => $cust_templ));
197 }
198 }
199 else {
200 /** Keep template in civicrm-wci/templates folder*/
201 $output = $smarty->fetch('Widget.tpl');
202 $output = file_get_contents('sites/all/modules/civicrm/extensions/civicrm-wci/templates/Widget.tpl');
203 $elem = $this->getElement('custom_template');
204 $elem->setValue($output);
205 }
0fc9f109 206
60488185
M
207 parent::buildQuickForm();
208 }
209
210 function postProcess() {
211 $values = $this->exportValues();
f00a3ea9
J
212
213 $override = 0;
ca0dca6f
J
214 $cust_tmpl = "";
215 $cust_tmpl_col = "";
216 $sql = "";
217 /** If override check is checked state then only save the custom_template to the
218 database. otherwise wci uses default tpl file.
219 */
f00a3ea9
J
220 if(isset($values['override'])){
221 $override = $values['override'];
ca0dca6f
J
222 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
223 $cust_tmpl_col = ", custom_template";
f00a3ea9 224 }
ca0dca6f
J
225 if (isset($this->_id)) {
226 $cust_tmpl_col = "'" . $cust_tmpl_col . "='";
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 = '"
244 . $override . $cust_tmpl_col . $cust_tmpl . "'";
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,
251 color_button, color_button_bg, style_rules, override" . $cust_tmpl_col ." )
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'] . "','" .
262 $override . "','" . $cust_tmpl
263 . "')";
264 }
f00a3ea9
J
265 $errorScope = CRM_Core_TemporaryErrorScope::useException();
266 try {
267 $transaction = new CRM_Core_Transaction();
268 CRM_Core_DAO::executeQuery($sql);
269
270 $transaction->commit();
271 }
272 catch (Exception $e) {
273 //TODO
274 print_r($e->getMessage());
275 $transaction->rollback();
276 }
277
60488185
M
278 parent::postProcess();
279 }
f00a3ea9
J
280
281 function getProgressBars() {
282 $options = array(
283 '' => ts('- select -'),
284 );
285 $pbList = CRM_WCI_BAO_ProgressBar::getProgressbarList();
286 foreach ($pbList as $pb) {
287 $options[$pb['id']] = $pb['name'];
288 }
60488185 289
f00a3ea9
J
290 return $options;
291 }
1720f579
M
292 function getContributionPageOptions() {
293 $options = array(
294 '' => ts('- select -'),
295 );
296
94686b36 297 $result = civicrm_api3('contribution_page', 'get');
1720f579
M
298 foreach ($result['values'] as $contribution_page) {
299 $options[$contribution_page['id']] = $contribution_page['title'];
300 }
301
302 return $options;
303 }
304
305 function getGroupOptions() {
60488185
M
306 $options = array(
307 '' => ts('- select -'),
60488185 308 );
1720f579
M
309
310 $result = civicrm_api3('group', 'get');
311 foreach ($result['values'] as $group) {
312 $options[$group['id']] = $group['title'];
60488185 313 }
1720f579
M
314
315 return $options;
316 }
317
318 function getSizeOptions() {
319 $options = array(
1720f579
M
320 'thin' => ts('Thin'),
321 'normal' => ts('Normal'),
322 'wide' => ts('Wide'),
323 );
324
60488185
M
325 return $options;
326 }
327
328 /**
329 * Get the fields/elements defined in this form.
330 *
331 * @return array (string)
332 */
333 function getRenderableElementNames() {
334 // The _elements list includes some items which should not be
335 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
336 // items don't have labels. We'll identify renderable by filtering on
337 // the 'label'.
338 $elementNames = array();
339 foreach ($this->_elements as $element) {
340 $label = $element->getLabel();
341 if (!empty($label)) {
342 $elementNames[] = $element->getName();
343 }
344 }
345 return $elementNames;
346 }
347}