3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
8 * Form controller class
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
12 class CRM_Wci_Form_CreateWidget
extends CRM_Core_Form
{
15 * the widget id saved to the session for an update
22 function preProcess() {
23 CRM_Core_Resources
::singleton()->addScriptFile('org.civicrm.wci', 'createwidget.js');
25 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this,
26 FALSE, NULL, 'REQUEST' );
28 $this->_colorFields
= array('color_title' => array(ts('Title Text Color'),
33 'color_title_bg' => array(ts('Title background color'),
38 'color_bar' => array(ts('Progress Bar Color'),
43 'color_bar_bg' => array(ts('Progress Bar Background Color'),
48 'color_widget_bg' => array(ts('Background color'),
53 'color_description' => array(ts('Description color'),
58 'color_border' => array(ts('Border color'),
63 'color_button' => array(ts('Button text color'),
68 'color_button_bg' => array(ts('Button background color'),
73 'color_btn_newsletter' => array(ts('Newsletter Button text color'),
78 'color_btn_newsletter_bg' => array(ts('Newsletter Button color'),
83 'newsletter_text' => array(ts('Newsletter text'),
86 'Get the monthly newsletter',
88 'color_newsletter_text' => array(ts('Newsletter text color'),
96 function setDefaultValues() {
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];
108 function buildQuickForm() {
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());
118 $pbtype[1] = "Percentage";
119 $pbtype[0] = "Amount";
120 $this->addRadio('show_pb_perc', ts('Progressbar caption type'), $pbtype,
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],
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);
142 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
143 $this->addButtons(array(
146 'name' => ts('Save'),
151 'name' => ts('Save & Preview'),
154 // export form elements
155 $this->assign('elementNames', $this->getRenderableElementNames());
156 if (isset($this->_id
)) {
157 /** Updating existing widget*/
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;*/
162 $query = "SELECT * FROM civicrm_wci_widget WHERE id=%1";
163 $params = array(1 => array($this->_id
, 'Integer'));
165 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
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']));
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']));
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));
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);
239 CRM_Utils_System
::setTitle(ts('Edit Widget'));
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);
247 parent
::buildQuickForm();
250 function postProcess() {
251 $values = $this->exportValues();
262 /** If override check is checked state then only save the custom_template to the
263 database. otherwise wci uses default tpl file.
265 if(isset($values['override'])){
266 $override = $values['override'];
267 $cust_tmpl = str_replace("'", "''", $values['custom_template']);
268 $cust_tmpl_col = "custom_template";
273 if(isset($values['hide_title'])){
274 $hide_title = $values['hide_title'];
276 if(isset($values['hide_border'])){
277 $hide_border = $values['hide_border'];
279 if(isset($values['hide_pbcap'])){
280 $hide_pbcap = $values['hide_pbcap'];
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'),);
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";
327 $params +
= array(30 => array($this->_id
, 'Integer'),);
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)";
341 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
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 CRM_Core_Session
::setStatus(ts('Widget created successfuly'), '', 'success');
349 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
350 (isset($this->_id
)) ?
$widget_id = $this->_id
:
351 $widget_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
352 CRM_Utils_System
::redirect('?action=update&reset=1&id=' . $widget_id);
354 CRM_Utils_System
::redirect('widget?reset=1');
357 catch (Exception
$e) {
358 CRM_Core_Session
::setStatus(ts('Failed to create widget. ').
359 $e->getMessage(), '', 'error');
360 $transaction->rollback();
363 parent
::postProcess();
366 function getProgressBars() {
368 0 => ts('- select -'),
370 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
371 foreach ($pbList as $pb) {
372 $options[$pb['id']] = $pb['name'];
378 function getGroupOptions() {
380 0 => ts('- select -'),
383 $result = civicrm_api3('group', 'get', array(
387 foreach ($result['values'] as $group) {
388 $options[$group['id']] = $group['title'];
394 function getSizeOptions() {
396 'thin' => ts('Thin (150px)'),
397 'normal' => ts('Normal (200px)'),
398 'wide' => ts('Wide (250px)'),
405 * Get the fields/elements defined in this form.
407 * @return array (string)
409 function getRenderableElementNames() {
410 // The _elements list includes some items which should not be
411 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
412 // items don't have labels. We'll identify renderable by filtering on
414 $elementNames = array();
415 foreach ($this->_elements
as $element) {
416 $label = $element->getLabel();
417 if (!empty($label)) {
418 $elementNames[] = $element->getName();
421 return $elementNames;