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'),
68 'color_button_bg' => array(ts('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);
192 CRM_Utils_System
::setTitle(ts('Edit Widget'));
195 CRM_Utils_System
::setTitle(ts('Create Widget'));
196 /** Keep template in civicrm-wci/templates folder*/
197 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
198 $elem = $this->getElement('custom_template');
199 $elem->setValue($output);
201 parent
::buildQuickForm();
204 function postProcess() {
205 $values = $this->exportValues();
214 /** If override check is checked state then only save the custom_template to the
215 database. otherwise wci uses default tpl file.
217 if(isset($values['override'])){
218 $override = $values['override'];
219 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
220 $cust_tmpl_col = "custom_template";
226 if (isset($this->_id
)) {
227 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
228 . "', logo_image = '" . $values['logo_image'] . "', image = '"
229 . $values['image'] . "', button_title = '" . $values['button_title']
230 . "', button_link_to = '" . $values['button_link_to']
231 . "', progress_bar_id = '" . $values['progress_bar']
232 . "', description = '" . base64_encode($values['description'])
233 . "', email_signup_group_id = '" . $values['email_signup_group_id']
234 . "', size_variant = '" . $values['size_variant']
235 . "', color_title = '" . $values['color_title']
236 . "', color_title_bg = '" . $values['color_title_bg']
237 . "', color_progress_bar = '" . $values['color_bar']
238 . "', color_widget_bg = '" . $values['color_widget_bg']
239 . "', color_description = '" . $values['color_description']
240 . "', color_border = '" . $values['color_border']
241 . "', color_button = '" . $values['color_button']
242 . "', color_button_bg = '" . $values['color_button_bg']
243 . "', style_rules = '" . $values['style_rules'] . "', override = '"
244 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id
;
247 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
248 button_title, button_link_to, progress_bar_id, description,
249 email_signup_group_id, size_variant, color_title, color_title_bg,
250 color_progress_bar, color_widget_bg, color_description, color_border,
251 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
252 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
253 $values['image'] . "','" . $values['button_title'] . "','" .
254 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
255 base64_encode($values['description']) . "','" .
256 $values['email_signup_group_id'] . "','" .
257 $values['size_variant'] . "','" . $values['color_title'] . "','" .
258 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
259 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
260 $values['color_border'] . "','" . $values['color_button'] . "','" .
261 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
262 $override . $quote . $coma . $quote . $cust_tmpl
266 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
268 $transaction = new CRM_Core_Transaction();
269 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 0;");
270 CRM_Core_DAO
::executeQuery($sql);
271 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 1;");
272 $transaction->commit();
274 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
275 (isset($this->_id
)) ?
$widget_id = $this->_id
:
276 $widget_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
277 CRM_Utils_System
::redirect('?action=update&reset=1&id=' . $widget_id);
279 CRM_Utils_System
::redirect('widget?reset=1');
282 catch (Exception
$e) {
284 print_r($e->getMessage());
285 $transaction->rollback();
288 parent
::postProcess();
291 function getProgressBars() {
293 '' => ts('- select -'),
295 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
296 foreach ($pbList as $pb) {
297 $options[$pb['id']] = $pb['name'];
302 function getContributionPageOptions() {
304 '' => ts('- select -'),
307 $result = civicrm_api3('contribution_page', 'get');
308 foreach ($result['values'] as $contribution_page) {
309 $options[$contribution_page['id']] = $contribution_page['title'];
315 function getGroupOptions() {
317 '' => ts('- select -'),
320 $result = civicrm_api3('group', 'get', array(
324 foreach ($result['values'] as $group) {
325 $options[$group['id']] = $group['title'];
331 function getSizeOptions() {
333 'thin' => ts('Thin'),
334 'normal' => ts('Normal'),
335 'wide' => ts('Wide'),
342 * Get the fields/elements defined in this form.
344 * @return array (string)
346 function getRenderableElementNames() {
347 // The _elements list includes some items which should not be
348 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
349 // items don't have labels. We'll identify renderable by filtering on
351 $elementNames = array();
352 foreach ($this->_elements
as $element) {
353 $label = $element->getLabel();
354 if (!empty($label)) {
355 $elementNames[] = $element->getName();
358 return $elementNames;