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_widget_bg' => array(ts('Background color'),
48 'color_description' => array(ts('Description color'),
53 'color_border' => array(ts('Border color'),
58 'color_button' => array(ts('Button text color'),
63 'color_button_bg' => array(ts('Button background color'),
71 function setDefaultValues() {
73 if (!isset($this->_id
)) {
74 $defaults['size_variant'] = 'normal';
75 foreach ($this->_colorFields
as $name => $val) {
76 $defaults[$name] = $val[3];
82 function buildQuickForm() {
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);
87 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
88 $this->add('text', 'button_title', ts('Contribution button title'))->setSize(45);
89 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
90 $this->addWysiwyg('description', ts('Description'), '');
91 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
92 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
93 // $fieldset = $this->addElement('fieldset')->setLabel('Advanced Settings');
94 $this->add('checkbox', 'hide_title', ts('Hide Title'));
95 $this->add('checkbox', 'hide_border', ts('Hide border'));
96 $this->add('checkbox', 'hide_pbcap', ts('Hide progress bar caption'));
97 foreach ($this->_colorFields
as $name => $val) {
98 $this->addElement($val[1],
105 $this->add('textarea', 'style_rules', ts('Additional Style Rules'))->setRows(5);
106 $this->add('checkbox', 'override', ts('Override default template'));
107 $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);
109 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
110 $this->addButtons(array(
113 'name' => ts('Save'),
118 'name' => ts('Save & Preview'),
122 // export form elements
123 $this->assign('elementNames', $this->getRenderableElementNames());
125 if (isset($this->_id
)) {
126 /** Updating existing widget*/
128 /*$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
129 where w.id=" . $this->_id;*/
131 $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id
;
134 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
136 while ($dao->fetch()) {
138 $wid_page[$dao->id
] = array();
139 CRM_Core_DAO
::storeValues($dao, $wid_page[$dao->id
]);
141 $this->setDefaults(array(
142 'title' => $wid_page[$dao->id
]['title']));
143 $this->setDefaults(array(
144 'logo_image' => $wid_page[$dao->id
]['logo_image']));
145 $this->setDefaults(array(
146 'image' => $wid_page[$dao->id
]['image']));
147 $this->setDefaults(array(
148 'button_link_to' => $wid_page[$dao->id
]['button_link_to']));
149 $this->setDefaults(array(
150 'button_title' => $wid_page[$dao->id
]['button_title']));
152 $this->setDefaults(array(
153 'progress_bar' => $dao->progress_bar_id
/*$dao->pbid*/));
154 $description = base64_decode($wid_page[$dao->id
]['description']);
155 $this->setDefaults(array(
156 'description' => $description));
157 $this->setDefaults(array(
158 'email_signup_group_id' => $wid_page[$dao->id
]['email_signup_group_id']));
159 $this->setDefaults(array(
160 'size_variant' => $dao->size_variant
));
161 $this->setDefaults(array(
162 'color_title' => $wid_page[$dao->id
]['color_title']));
163 $this->setDefaults(array(
164 'color_title_bg' => $wid_page[$dao->id
]['color_title_bg']));
165 $this->setDefaults(array(
166 'color_bar' => $wid_page[$dao->id
]['color_progress_bar']));
167 $this->setDefaults(array(
168 'color_widget_bg' => $wid_page[$dao->id
]['color_widget_bg']));
169 $this->setDefaults(array(
170 'color_description' => $wid_page[$dao->id
]['color_description']));
171 $this->setDefaults(array(
172 'color_border' => $wid_page[$dao->id
]['color_border']));
173 $this->setDefaults(array(
174 'color_button' => $wid_page[$dao->id
]['color_button']));
175 $this->setDefaults(array(
176 'color_button_bg' => $wid_page[$dao->id
]['color_button_bg']));
177 $this->setDefaults(array(
178 'style_rules' => base64_decode($wid_page[$dao->id
]['style_rules'])));
179 $this->setDefaults(array(
180 'override' => $wid_page[$dao->id
]['override']));
181 $this->setDefaults(array(
182 'hide_title' => $wid_page[$dao->id
]['hide_title']));
183 $this->setDefaults(array(
184 'hide_border' => $wid_page[$dao->id
]['hide_border']));
185 $this->setDefaults(array(
186 'hide_pbcap' => $wid_page[$dao->id
]['hide_pbcap']));
187 if(true == $wid_page[$dao->id
]['override']) {
188 $cust_templ = base64_decode($wid_page[$dao->id
]['custom_template']);
189 $this->setDefaults(array(
190 'custom_template' => $cust_templ));
192 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
193 $elem = $this->getElement('custom_template');
194 $elem->setValue($output);
197 CRM_Utils_System
::setTitle(ts('Edit Widget'));
200 CRM_Utils_System
::setTitle(ts('Create Widget'));
201 /** Keep template in civicrm-wci/templates folder*/
202 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
203 $elem = $this->getElement('custom_template');
204 $elem->setValue($output);
206 parent
::buildQuickForm();
209 function postProcess() {
210 $values = $this->exportValues();
221 /** If override check is checked state then only save the custom_template to the
222 database. otherwise wci uses default tpl file.
224 if(isset($values['override'])){
225 $override = $values['override'];
226 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
227 $cust_tmpl_col = "custom_template";
232 if(isset($values['hide_title'])){
233 $hide_title = $values['hide_title'];
235 if(isset($values['hide_border'])){
236 $hide_border = $values['hide_border'];
238 if(isset($values['hide_pbcap'])){
239 $hide_pbcap = $values['hide_pbcap'];
242 if (isset($this->_id
)) {
243 $sql = "UPDATE civicrm_wci_widget SET title = '". base64_encode($values['title'])
244 . "', logo_image = '" . $values['logo_image'] . "', image = '"
245 . $values['image'] . "', button_title = '" . $values['button_title']
246 . "', button_link_to = '" . $values['button_link_to']
247 . "', progress_bar_id = '" . $values['progress_bar']
248 . "', description = '" . base64_encode($values['description'])
249 . "', email_signup_group_id = '" . $values['email_signup_group_id']
250 . "', size_variant = '" . $values['size_variant']
251 . "', color_title = '" . $values['color_title']
252 . "', color_title_bg = '" . $values['color_title_bg']
253 . "', color_progress_bar = '" . $values['color_bar']
254 . "', color_widget_bg = '" . $values['color_widget_bg']
255 . "', color_description = '" . $values['color_description']
256 . "', color_border = '" . $values['color_border']
257 . "', color_button = '" . $values['color_button']
258 . "', color_button_bg = '" . $values['color_button_bg']
259 . "', hide_title = '" . $hide_title
260 . "', hide_border = '" . $hide_border
261 . "', hide_pbcap = '" . $hide_pbcap
262 . "', style_rules = '" . base64_encode($values['style_rules']) . "', override = '"
263 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id
;
266 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
267 button_title, button_link_to, progress_bar_id, description,
268 email_signup_group_id, size_variant, color_title, color_title_bg,
269 color_progress_bar, color_widget_bg, color_description, color_border,
270 color_button, color_button_bg, hide_title, hide_border, hide_pbcap, style_rules, override" . $coma . $cust_tmpl_col ." )
271 VALUES ('" . base64_encode($values['title']) . "','" . $values['logo_image'] . "','" .
272 $values['image'] . "','" . $values['button_title'] . "','" .
273 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
274 base64_encode($values['description']) . "','" .
275 $values['email_signup_group_id'] . "','" .
276 $values['size_variant'] . "','" . $values['color_title'] . "','" .
277 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
278 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
279 $values['color_border'] . "','" . $values['color_button'] . "','" .
280 $values['color_button_bg'] . "','" . $values['hide_title'] . "','" .
281 $values['hide_border'] . "','" . $values['hide_pbcap'] . "','"
282 . base64_encode($values['style_rules']) . "','" .
283 $override . $quote . $coma . $quote . $cust_tmpl
287 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
289 $transaction = new CRM_Core_Transaction();
290 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 0;");
291 CRM_Core_DAO
::executeQuery($sql);
292 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 1;");
293 $transaction->commit();
295 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
296 (isset($this->_id
)) ?
$widget_id = $this->_id
:
297 $widget_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
298 CRM_Utils_System
::redirect('?action=update&reset=1&id=' . $widget_id);
300 CRM_Utils_System
::redirect('widget?reset=1');
303 catch (Exception
$e) {
305 print_r($e->getMessage());
306 $transaction->rollback();
309 parent
::postProcess();
312 function getProgressBars() {
314 '' => ts('- select -'),
316 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
317 foreach ($pbList as $pb) {
318 $options[$pb['id']] = $pb['name'];
323 function getContributionPageOptions() {
325 '' => ts('- select -'),
328 $result = civicrm_api3('contribution_page', 'get');
329 foreach ($result['values'] as $contribution_page) {
330 $options[$contribution_page['id']] = $contribution_page['title'];
336 function getGroupOptions() {
338 '' => ts('- select -'),
341 $result = civicrm_api3('group', 'get', array(
345 foreach ($result['values'] as $group) {
346 $options[$group['id']] = $group['title'];
352 function getSizeOptions() {
354 'thin' => ts('Thin'),
355 'normal' => ts('Normal'),
356 'wide' => ts('Wide'),
363 * Get the fields/elements defined in this form.
365 * @return array (string)
367 function getRenderableElementNames() {
368 // The _elements list includes some items which should not be
369 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
370 // items don't have labels. We'll identify renderable by filtering on
372 $elementNames = array();
373 foreach ($this->_elements
as $element) {
374 $label = $element->getLabel();
375 if (!empty($label)) {
376 $elementNames[] = $element->getName();
379 return $elementNames;