moved .js files to js folder
[com.zyxware.civiwci.git] / CRM / Wci / Form / CreateWidget.php
1 <?php
2
3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
6
7 /**
8 * Form controller class
9 *
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
11 */
12 class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
13
14 /**
15 * the widget id saved to the session for an update
16 *
17 * @var int
18 * @access protected
19 */
20 protected $_id;
21
22 function preProcess() {
23 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'js/createwidget.js');
24 parent::preProcess();
25 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
26 FALSE, NULL, 'REQUEST' );
27
28 $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
29 'text',
30 FALSE,
31 '#BF0F0F',
32 ),
33 'color_title_bg' => array(ts('Title background color'),
34 'text',
35 FALSE,
36 '#FFFFFF',
37 ),
38 'color_bar' => array(ts('Progress Bar Color'),
39 'text',
40 FALSE,
41 '#BF0F0F',
42 ),
43 'color_bar_bg' => array(ts('Progress Bar Background Color'),
44 'text',
45 FALSE,
46 '#FFFFFF',
47 ),
48 'color_widget_bg' => array(ts('Background color'),
49 'text',
50 FALSE,
51 '#FFFFFF',
52 ),
53 'color_description' => array(ts('Description color'),
54 'text',
55 FALSE,
56 '#000000',
57 ),
58 'color_border' => array(ts('Border color'),
59 'text',
60 FALSE,
61 '#BF0F0F',
62 ),
63 'color_button' => array(ts('Button text color'),
64 'text',
65 FALSE,
66 '#FFFFFF',
67 ),
68 'color_button_bg' => array(ts('Button background color'),
69 'text',
70 FALSE,
71 '#BF0F0F',
72 ),
73 'color_btn_newsletter' => array(ts('Newsletter Button text color'),
74 'text',
75 FALSE,
76 '#FFFFFF',
77 ),
78 'color_btn_newsletter_bg' => array(ts('Newsletter Button color'),
79 'text',
80 FALSE,
81 '#BF0F0F',
82 ),
83 'newsletter_text' => array(ts('Newsletter text'),
84 'text',
85 FALSE,
86 'Get the monthly newsletter',
87 ),
88 'color_newsletter_text' => array(ts('Newsletter text color'),
89 'text',
90 FALSE,
91 '#BF0F0F',
92 ),
93 );
94 }
95
96 function setDefaultValues() {
97 $defaults = array();
98 if (!isset($this->_id)) {
99 $defaults['size_variant'] = 'normal';
100 $defaults['show_pb_perc'] = 1;
101 foreach ($this->_colorFields as $name => $val) {
102 $defaults[$name] = $val[3];
103 }
104 }
105 return $defaults;
106 }
107
108 function buildQuickForm() {
109 // add form elements
110 $this->add('text', 'title', ts('Title'),true, true)->setSize(45);
111 $this->add('text', 'logo_image', ts('Logo image'))->setSize(45);
112 $this->add('text', 'image', ts('Image'))->setSize(45);
113 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
114 $this->add('text', 'button_title', ts('Contribution button title'))->setSize(45);
115 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
116
117 $pbtype = array();
118 $pbtype[1] = "Percentage";
119 $pbtype[0] = "Amount";
120 $this->addRadio('show_pb_perc', ts('Progressbar caption type'), $pbtype,
121 NULL, "&nbsp;");
122
123 $this->addWysiwyg('description', ts('Description'), '');
124 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
125 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
126 // $fieldset = $this->addElement('fieldset')->setLabel('Advanced Settings');
127 $this->add('checkbox', 'hide_title', ts('Hide Title'));
128 $this->add('checkbox', 'hide_border', ts('Hide border'));
129 $this->add('checkbox', 'hide_pbcap', ts('Hide progress bar caption'));
130 foreach ($this->_colorFields as $name => $val) {
131 $this->addElement($val[1],
132 $name,
133 $val[0],
134 $name,
135 $val[2]
136 );
137 }
138 $this->add('textarea', 'style_rules', ts('Additional Style Rules'), 'style=width:94%')->setRows(5);
139 $this->add('checkbox', 'override', ts('Override default template'));
140 $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>'), 'style=width:94%')->setRows(10);
141
142 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
143 $this->addButtons(array(
144 array(
145 'type' => 'submit',
146 'name' => ts('Save'),
147 'isDefault' => TRUE,
148 ),
149 array(
150 'type' => 'next',
151 'name' => ts('Save & Preview'),
152 ),
153 ));
154 // export form elements
155 $this->assign('elementNames', $this->getRenderableElementNames());
156 if (isset($this->_id)) {
157 /** Updating existing widget*/
158
159 /*$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
160 where w.id=" . $this->_id;*/
161
162 $query = "SELECT * FROM civicrm_wci_widget WHERE id=%1";
163 $params = array(1 => array($this->_id, 'Integer'));
164
165 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
166
167 while ($dao->fetch()) {
168 $wid_page[$dao->id] = array();
169 CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
170 $this->setDefaults(array(
171 'title' => $wid_page[$dao->id]['title']));
172 $this->setDefaults(array(
173 'logo_image' => $wid_page[$dao->id]['logo_image']));
174 $this->setDefaults(array(
175 'image' => $wid_page[$dao->id]['image']));
176 $this->setDefaults(array(
177 'button_link_to' => $wid_page[$dao->id]['button_link_to']));
178 $this->setDefaults(array(
179 'button_title' => $wid_page[$dao->id]['button_title']));
180 $this->setDefaults(array(
181 'progress_bar' => $dao->progress_bar_id/*$dao->pbid*/));
182 $description = $wid_page[$dao->id]['description'];
183 $this->setDefaults(array(
184 'description' => $description));
185 $this->setDefaults(array(
186 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
187 $this->setDefaults(array(
188 'size_variant' => $dao->size_variant));
189 $this->setDefaults(array(
190 'color_title' => $wid_page[$dao->id]['color_title']));
191 $this->setDefaults(array(
192 'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
193 $this->setDefaults(array(
194 'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
195 $this->setDefaults(array(
196 'color_bar_bg' => $wid_page[$dao->id]['color_progress_bar_bg']));
197 $this->setDefaults(array(
198 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
199 $this->setDefaults(array(
200 'color_description' => $wid_page[$dao->id]['color_description']));
201 $this->setDefaults(array(
202 'color_border' => $wid_page[$dao->id]['color_border']));
203 $this->setDefaults(array(
204 'color_button' => $wid_page[$dao->id]['color_button']));
205 $this->setDefaults(array(
206 'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
207 $this->setDefaults(array(
208 'style_rules' => $wid_page[$dao->id]['style_rules']));
209 $this->setDefaults(array(
210 'override' => $wid_page[$dao->id]['override']));
211 $this->setDefaults(array(
212 'hide_title' => $wid_page[$dao->id]['hide_title']));
213 $this->setDefaults(array(
214 'hide_border' => $wid_page[$dao->id]['hide_border']));
215 $this->setDefaults(array(
216 'hide_pbcap' => $wid_page[$dao->id]['hide_pbcap']));
217 $this->setDefaults(array(
218 'show_pb_perc' => $wid_page[$dao->id]['show_pb_perc']));
219
220 $this->setDefaults(array(
221 'color_btn_newsletter' => $wid_page[$dao->id]['color_btn_newsletter']));
222 $this->setDefaults(array(
223 'color_btn_newsletter_bg' => $wid_page[$dao->id]['color_btn_newsletter_bg']));
224 $this->setDefaults(array(
225 'newsletter_text' => $wid_page[$dao->id]['newsletter_text']));
226 $this->setDefaults(array(
227 'color_newsletter_text' => $wid_page[$dao->id]['color_newsletter_text']));
228
229 if(true == $wid_page[$dao->id]['override']) {
230 $cust_templ = html_entity_decode($wid_page[$dao->id]['custom_template']);
231 $this->setDefaults(array(
232 'custom_template' => $cust_templ));
233 } else {
234 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
235 $elem = $this->getElement('custom_template');
236 $elem->setValue($output);
237 }
238 }
239 CRM_Utils_System::setTitle(ts('Edit Widget'));
240 }
241 else {
242 CRM_Utils_System::setTitle(ts('Create Widget'));
243 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
244 $elem = $this->getElement('custom_template');
245 $elem->setValue($output);
246 }
247 parent::buildQuickForm();
248 }
249
250 function postProcess() {
251 $values = $this->exportValues();
252 $override = 0;
253 $hide_title = 0;
254 $hide_border = 0;
255 $hide_pbcap = 0;
256 $cust_tmpl = "";
257 $cust_tmpl_col = "";
258 $sql = "";
259 $coma = "";
260 $equals = "";
261 $quote = "";
262 /** If override check is checked state then only save the custom_template to the
263 database. otherwise wci uses default tpl file.
264 */
265 if(isset($values['override'])){
266 $override = $values['override'];
267 $cust_tmpl = str_replace("'", "''", $values['custom_template']);
268 $cust_tmpl_col = "custom_template";
269 $coma = ",";
270 $equals = " = ";
271 $quote = "'";
272 }
273 if(isset($values['hide_title'])){
274 $hide_title = $values['hide_title'];
275 }
276 if(isset($values['hide_border'])){
277 $hide_border = $values['hide_border'];
278 }
279 if(isset($values['hide_pbcap'])){
280 $hide_pbcap = $values['hide_pbcap'];
281 }
282
283 $title = str_replace("'", "''", $values['title']);
284 $params = array(1 => array($title, 'String'),
285 2 => array($values['logo_image'], 'String'),
286 3 => array($values['image'], 'String'),
287 4 => array($values['button_title'], 'String'),
288 5 => array($values['button_link_to'], 'String'),
289 6 => array($values['progress_bar'], 'Integer'),
290 7 => array(str_replace("'", "''", $values['description']), 'String'),
291 8 => array($values['email_signup_group_id'], 'String'),
292 9 => array($values['size_variant'], 'String'),
293 10 => array($values['color_title'], 'String'),
294 11 => array($values['color_title_bg'], 'String'),
295 12 => array($values['color_bar'], 'String'),
296 13 => array($values['color_bar_bg'], 'String'),
297 14 => array($values['color_widget_bg'], 'String'),
298 15 => array($values['color_description'], 'String'),
299 16 => array($values['color_border'], 'String'),
300 17 => array($values['color_button'], 'String'),
301 18 => array($values['color_button_bg'], 'String'),
302 19 => array($hide_title, 'Integer'),
303 20 => array($hide_border, 'Integer'),
304 21 => array($hide_pbcap, 'Integer'),
305 22 => array($values['color_btn_newsletter'], 'String'),
306 23 => array($values['color_btn_newsletter_bg'], 'String'),
307 24 => array($values['newsletter_text'], 'String'),
308 25 => array($values['color_newsletter_text'], 'String'),
309 26 => array($values['style_rules'], 'String'),
310 27 => array($override, 'Integer'),
311 28 => array($values['custom_template'], 'String'),
312 29 => array($values['show_pb_perc'], 'Integer'),);
313
314 if (isset($this->_id)) {
315 $sql = "UPDATE civicrm_wci_widget SET title = %1, logo_image =%2,
316 image = %3, button_title =%4, button_link_to =%5,
317 progress_bar_id = %6, description = %7, email_signup_group_id = %8,
318 size_variant = %9, color_title = %10, color_title_bg = %11,
319 color_progress_bar = %12, color_progress_bar_bg = %13,
320 color_widget_bg=%14, color_description=%15, color_border = %16,
321 color_button = %17, color_button_bg = %18, hide_title = %19,
322 hide_border = %20, hide_pbcap = %21, color_btn_newsletter = %22,
323 color_btn_newsletter_bg = %23, newsletter_text = %24,
324 color_newsletter_text = %25, style_rules = %26, override = %27,
325 custom_template = %28, show_pb_perc = %29 where id = %30";
326
327 $params += array(30 => array($this->_id, 'Integer'),);
328 }
329 else {
330 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
331 button_title, button_link_to, progress_bar_id, description,
332 email_signup_group_id, size_variant, color_title, color_title_bg,
333 color_progress_bar, color_progress_bar_bg, color_widget_bg, color_description, color_border,
334 color_button, color_button_bg, hide_title, hide_border, hide_pbcap,
335 color_btn_newsletter, color_btn_newsletter_bg, newsletter_text,
336 color_newsletter_text, style_rules, override, custom_template, show_pb_perc)
337 VALUES (%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13,
338 %14, %15, %16, %17, %18, %19, %20, %21, %22, %23, %24, %25, %26, %27, %28, %29)";
339 }
340
341 $errorScope = CRM_Core_TemporaryErrorScope::useException();
342 try {
343 $transaction = new CRM_Core_Transaction();
344 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;");
345 CRM_Core_DAO::executeQuery($sql, $params);
346 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
347 $transaction->commit();
348 if (isset($this->_id)) {
349 $widget_id = $this->_id;
350 CRM_Wci_BAO_WidgetCache::deleteWidgetCache($widget_id);
351 }
352 else {
353 $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
354 }
355 CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success');
356
357 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
358 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
359 } else {
360 CRM_Utils_System::redirect('widget?reset=1');
361 }
362 }
363 catch (Exception $e) {
364 CRM_Core_Session::setStatus(ts('Failed to create widget. ').
365 $e->getMessage(), '', 'error');
366 $transaction->rollback();
367 }
368
369 parent::postProcess();
370 }
371
372 function getProgressBars() {
373 $options = array(
374 0 => ts('- select -'),
375 );
376 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
377 foreach ($pbList as $pb) {
378 $options[$pb['id']] = $pb['name'];
379 }
380
381 return $options;
382 }
383
384 function getGroupOptions() {
385 $options = array(
386 0 => ts('- select -'),
387 );
388
389 $result = civicrm_api3('group', 'get', array(
390 'group_type' => '2' // group type of newsletter(2)
391 ));
392 if (0 != $result['count']) {
393 foreach ($result['values'] as $group) {
394 $options[$group['id']] = $group['title'];
395 }
396 }
397 return $options;
398 }
399
400 function getSizeOptions() {
401 $options = array(
402 'thin' => ts('Thin (150px)'),
403 'normal' => ts('Normal (200px)'),
404 'wide' => ts('Wide (250px)'),
405 );
406
407 return $options;
408 }
409
410 /**
411 * Get the fields/elements defined in this form.
412 *
413 * @return array (string)
414 */
415 function getRenderableElementNames() {
416 // The _elements list includes some items which should not be
417 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
418 // items don't have labels. We'll identify renderable by filtering on
419 // the 'label'.
420 $elementNames = array();
421 foreach ($this->_elements as $element) {
422 $label = $element->getLabel();
423 if (!empty($label)) {
424 $elementNames[] = $element->getName();
425 }
426 }
427 return $elementNames;
428 }
429 }