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