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;*/
132 $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id
;
135 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
137 while ($dao->fetch()) {
139 $wid_page[$dao->id
] = array();
140 CRM_Core_DAO
::storeValues($dao, $wid_page[$dao->id
]);
142 $this->setDefaults(array(
143 'title' => $wid_page[$dao->id
]['title']));
144 $this->setDefaults(array(
145 'logo_image' => $wid_page[$dao->id
]['logo_image']));
146 $this->setDefaults(array(
147 'image' => $wid_page[$dao->id
]['image']));
148 $this->setDefaults(array(
149 'button_link_to' => $wid_page[$dao->id
]['button_link_to']));
150 $this->setDefaults(array(
151 'button_title' => $wid_page[$dao->id
]['button_title']));
153 $this->setDefaults(array(
154 'progress_bar' => $dao->progress_bar_id
/*$dao->pbid*/));
155 $description = base64_decode($wid_page[$dao->id
]['description']);
156 $this->setDefaults(array(
157 'description' => $description));
158 $this->setDefaults(array(
159 'email_signup_group_id' => $wid_page[$dao->id
]['email_signup_group_id']));
160 $this->setDefaults(array(
161 'size_variant' => $dao->size_variant
));
162 $this->setDefaults(array(
163 'color_title' => $wid_page[$dao->id
]['color_title']));
164 $this->setDefaults(array(
165 'color_title_bg' => $wid_page[$dao->id
]['color_title_bg']));
166 $this->setDefaults(array(
167 'color_bar' => $wid_page[$dao->id
]['color_progress_bar']));
168 $this->setDefaults(array(
169 'color_widget_bg' => $wid_page[$dao->id
]['color_widget_bg']));
170 $this->setDefaults(array(
171 'color_description' => $wid_page[$dao->id
]['color_description']));
172 $this->setDefaults(array(
173 'color_border' => $wid_page[$dao->id
]['color_border']));
174 $this->setDefaults(array(
175 'color_button' => $wid_page[$dao->id
]['color_button']));
176 $this->setDefaults(array(
177 'color_button_bg' => $wid_page[$dao->id
]['color_button_bg']));
178 $this->setDefaults(array(
179 'style_rules' => $wid_page[$dao->id
]['style_rules']));
180 $this->setDefaults(array(
181 'override' => $wid_page[$dao->id
]['override']));
182 if(true == $wid_page[$dao->id
]['override']) {
183 $cust_templ = base64_decode($wid_page[$dao->id
]['custom_template']);
184 $this->setDefaults(array(
185 'custom_template' => $cust_templ));
187 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
188 $elem = $this->getElement('custom_template');
189 $elem->setValue($output);
194 /** Keep template in civicrm-wci/templates folder*/
195 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
196 $elem = $this->getElement('custom_template');
197 $elem->setValue($output);
199 parent
::buildQuickForm();
202 function postProcess() {
203 $values = $this->exportValues();
212 /** If override check is checked state then only save the custom_template to the
213 database. otherwise wci uses default tpl file.
215 if(isset($values['override'])){
216 $override = $values['override'];
217 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
218 $cust_tmpl_col = "custom_template";
224 if (isset($this->_id
)) {
225 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
226 . "', logo_image = '" . $values['logo_image'] . "', image = '"
227 . $values['image'] . "', button_title = '" . $values['button_title']
228 . "', button_link_to = '" . $values['button_link_to']
229 . "', progress_bar_id = '" . $values['progress_bar']
230 . "', description = '" . base64_encode($values['description'])
231 . "', email_signup_group_id = '" . $values['email_signup_group_id']
232 . "', size_variant = '" . $values['size_variant']
233 . "', color_title = '" . $values['color_title']
234 . "', color_title_bg = '" . $values['color_title_bg']
235 . "', color_progress_bar = '" . $values['color_bar']
236 . "', color_widget_bg = '" . $values['color_widget_bg']
237 . "', color_description = '" . $values['color_description']
238 . "', color_border = '" . $values['color_border']
239 . "', color_button = '" . $values['color_button']
240 . "', color_button_bg = '" . $values['color_button_bg']
241 . "', style_rules = '" . $values['style_rules'] . "', override = '"
242 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id
;
245 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
246 button_title, button_link_to, progress_bar_id, description,
247 email_signup_group_id, size_variant, color_title, color_title_bg,
248 color_progress_bar, color_widget_bg, color_description, color_border,
249 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
250 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
251 $values['image'] . "','" . $values['button_title'] . "','" .
252 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
253 base64_encode($values['description']) . "','" .
254 $values['email_signup_group_id'] . "','" .
255 $values['size_variant'] . "','" . $values['color_title'] . "','" .
256 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
257 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
258 $values['color_border'] . "','" . $values['color_button'] . "','" .
259 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
260 $override . $quote . $coma . $quote . $cust_tmpl
264 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
266 $transaction = new CRM_Core_Transaction();
267 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 0;");
268 CRM_Core_DAO
::executeQuery($sql);
269 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 1;");
270 $transaction->commit();
272 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
273 (isset($this->_id
)) ?
$widget_id = $this->_id
:
274 $widget_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
275 CRM_Utils_System
::redirect('?action=update&reset=1&id=' . $widget_id);
277 CRM_Utils_System
::redirect('widget?reset=1');
280 catch (Exception
$e) {
282 print_r($e->getMessage());
283 $transaction->rollback();
286 parent
::postProcess();
289 function getProgressBars() {
291 '' => ts('- select -'),
293 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
294 foreach ($pbList as $pb) {
295 $options[$pb['id']] = $pb['name'];
300 function getContributionPageOptions() {
302 '' => ts('- select -'),
305 $result = civicrm_api3('contribution_page', 'get');
306 foreach ($result['values'] as $contribution_page) {
307 $options[$contribution_page['id']] = $contribution_page['title'];
313 function getGroupOptions() {
315 '' => ts('- select -'),
318 $result = civicrm_api3('group', 'get', array(
322 foreach ($result['values'] as $group) {
323 $options[$group['id']] = $group['title'];
329 function getSizeOptions() {
331 'thin' => ts('Thin'),
332 'normal' => ts('Normal'),
333 'wide' => ts('Wide'),
340 * Get the fields/elements defined in this form.
342 * @return array (string)
344 function getRenderableElementNames() {
345 // The _elements list includes some items which should not be
346 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
347 // items don't have labels. We'll identify renderable by filtering on
349 $elementNames = array();
350 foreach ($this->_elements
as $element) {
351 $label = $element->getLabel();
352 if (!empty($label)) {
353 $elementNames[] = $element->getName();
356 return $elementNames;