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('Widget title background color'),
38 'color_bar' => array(ts('Progress Bar Color'),
43 'color_widget_bg' => array(ts('Widget background color'),
48 'color_description' => array(ts('Widget description color'),
53 'color_border' => array(ts('Widget border color'),
58 'color_button' => array(ts('Widget button text color'),
63 'color_button_bg' => array(ts('Widget button background color'),
68 'color_button_bg' => array(ts('Widget button background color'),
76 function setDefaultValues() {
78 if (!isset($this->_id
)) {
79 $defaults['size_variant'] = 'normal';
80 foreach ($this->_colorFields
as $name => $val) {
81 $defaults[$name] = $val[3];
87 function buildQuickForm() {
89 $this->add('text', 'title', ts('Title'),true);
90 $this->add('text', 'logo_image', ts('Logo image'));
91 $this->add('text', 'image', ts('Image'));
92 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
93 $this->add('text', 'button_title', ts('Contribution button title'));
94 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
95 $this->addWysiwyg('description', ts('Description'), '');
96 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
97 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
98 foreach ($this->_colorFields
as $name => $val) {
106 $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
107 $this->add('checkbox', 'override', ts('Override default template'));
108 $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>'));
110 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
111 $this->addButtons(array(
114 'name' => ts('Save'),
119 'name' => ts('Save & Preview'),
123 // export form elements
124 $this->assign('elementNames', $this->getRenderableElementNames());
126 if (isset($this->_id
)) {
127 /** Updating existing widget*/
129 $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
130 where w.id=" . $this->_id
;
133 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
135 while ($dao->fetch()) {
136 $wid_page[$dao->id
] = array();
137 CRM_Core_DAO
::storeValues($dao, $wid_page[$dao->id
]);
139 $this->setDefaults(array(
140 'title' => $wid_page[$dao->id
]['title']));
141 $this->setDefaults(array(
142 'logo_image' => $wid_page[$dao->id
]['logo_image']));
143 $this->setDefaults(array(
144 'image' => $wid_page[$dao->id
]['image']));
145 $this->setDefaults(array(
146 'button_link_to' => $wid_page[$dao->id
]['button_link_to']));
147 $this->setDefaults(array(
148 'button_title' => $wid_page[$dao->id
]['button_title']));
150 $this->setDefaults(array(
151 'progress_bar' => $dao->pbid
));
152 $description = base64_decode($wid_page[$dao->id
]['description']);
153 $this->setDefaults(array(
154 'description' => $description));
155 $this->setDefaults(array(
156 'email_signup_group_id' => $wid_page[$dao->id
]['email_signup_group_id']));
157 $this->setDefaults(array(
158 'size_variant' => $wid_page[$dao->id
]['size_variant']));
159 $this->setDefaults(array(
160 'color_title' => $wid_page[$dao->id
]['color_title']));
161 $this->setDefaults(array(
162 'color_title_bg' => $wid_page[$dao->id
]['color_title_bg']));
163 $this->setDefaults(array(
164 'color_bar' => $wid_page[$dao->id
]['color_progress_bar']));
165 $this->setDefaults(array(
166 'color_widget_bg' => $wid_page[$dao->id
]['color_widget_bg']));
167 $this->setDefaults(array(
168 'color_description' => $wid_page[$dao->id
]['color_description']));
169 $this->setDefaults(array(
170 'color_border' => $wid_page[$dao->id
]['color_border']));
171 $this->setDefaults(array(
172 'color_button' => $wid_page[$dao->id
]['color_button']));
173 $this->setDefaults(array(
174 'color_button_bg' => $wid_page[$dao->id
]['color_button_bg']));
175 $this->setDefaults(array(
176 'style_rules' => $wid_page[$dao->id
]['style_rules']));
177 $this->setDefaults(array(
178 'override' => $wid_page[$dao->id
]['override']));
179 if(true == $wid_page[$dao->id
]['override']) {
180 $cust_templ = base64_decode($wid_page[$dao->id
]['custom_template']);
181 $this->setDefaults(array(
182 'custom_template' => $cust_templ));
184 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
185 $elem = $this->getElement('custom_template');
186 $elem->setValue($output);
191 /** Keep template in civicrm-wci/templates folder*/
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);
196 parent
::buildQuickForm();
199 function postProcess() {
200 $values = $this->exportValues();
209 /** If override check is checked state then only save the custom_template to the
210 database. otherwise wci uses default tpl file.
212 if(isset($values['override'])){
213 $override = $values['override'];
214 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
215 $cust_tmpl_col = "custom_template";
221 if (isset($this->_id
)) {
222 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
223 . "', logo_image = '" . $values['logo_image'] . "', image = '"
224 . $values['image'] . "', button_title = '" . $values['button_title']
225 . "', button_link_to = '" . $values['button_link_to']
226 . "', progress_bar_id = '" . $values['progress_bar']
227 . "', description = '" . base64_encode($values['description'])
228 . "', email_signup_group_id = '" . $values['email_signup_group_id']
229 . "', size_variant = '" . $values['size_variant']
230 . "', color_title = '" . $values['color_title']
231 . "', color_title_bg = '" . $values['color_title_bg']
232 . "', color_progress_bar = '" . $values['color_bar']
233 . "', color_widget_bg = '" . $values['color_widget_bg']
234 . "', color_description = '" . $values['color_description']
235 . "', color_border = '" . $values['color_border']
236 . "', color_button = '" . $values['color_button']
237 . "', color_button_bg = '" . $values['color_button_bg']
238 . "', style_rules = '" . $values['style_rules'] . "', override = '"
239 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id
;
242 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
243 button_title, button_link_to, progress_bar_id, description,
244 email_signup_group_id, size_variant, color_title, color_title_bg,
245 color_progress_bar, color_widget_bg, color_description, color_border,
246 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
247 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
248 $values['image'] . "','" . $values['button_title'] . "','" .
249 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
250 base64_encode($values['description']) . "','" .
251 $values['email_signup_group_id'] . "','" .
252 $values['size_variant'] . "','" . $values['color_title'] . "','" .
253 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
254 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
255 $values['color_border'] . "','" . $values['color_button'] . "','" .
256 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
257 $override . $quote . $coma . $quote . $cust_tmpl
261 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
263 $transaction = new CRM_Core_Transaction();
264 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 0;");
265 CRM_Core_DAO
::executeQuery($sql);
266 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 1;");
267 $transaction->commit();
269 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
270 (isset($this->_id
)) ?
$widget_id = $this->_id
:
271 $widget_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
272 CRM_Utils_System
::redirect('?action=update&reset=1&id=' . $widget_id);
274 CRM_Utils_System
::redirect('widget?reset=1');
277 catch (Exception
$e) {
279 print_r($e->getMessage());
280 $transaction->rollback();
283 parent
::postProcess();
286 function getProgressBars() {
288 '' => ts('- select -'),
290 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
291 foreach ($pbList as $pb) {
292 $options[$pb['id']] = $pb['name'];
297 function getContributionPageOptions() {
299 '' => ts('- select -'),
302 $result = civicrm_api3('contribution_page', 'get');
303 foreach ($result['values'] as $contribution_page) {
304 $options[$contribution_page['id']] = $contribution_page['title'];
310 function getGroupOptions() {
312 '' => ts('- select -'),
315 $result = civicrm_api3('group', 'get', array(
319 foreach ($result['values'] as $group) {
320 $options[$group['id']] = $group['title'];
326 function getSizeOptions() {
328 'thin' => ts('Thin'),
329 'normal' => ts('Normal'),
330 'wide' => ts('Wide'),
337 * Get the fields/elements defined in this form.
339 * @return array (string)
341 function getRenderableElementNames() {
342 // The _elements list includes some items which should not be
343 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
344 // items don't have labels. We'll identify renderable by filtering on
346 $elementNames = array();
347 foreach ($this->_elements
as $element) {
348 $label = $element->getLabel();
349 if (!empty($label)) {
350 $elementNames[] = $element->getName();
353 return $elementNames;