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_btn_newsletter' => array(ts('Newsletter Button text color'),
73 'color_btn_newsletter_bg' => array(ts('Newsletter Button color'),
78 'newsletter_text' => array(ts('Newsletter text'),
81 'Get the monthly newsletter',
83 'color_newsletter_text' => array(ts('Newsletter text color'),
91 function setDefaultValues() {
93 if (!isset($this->_id
)) {
94 $defaults['size_variant'] = 'normal';
95 foreach ($this->_colorFields
as $name => $val) {
96 $defaults[$name] = $val[3];
102 function buildQuickForm() {
104 $this->add('text', 'title', ts('Title'),true)->setSize(45);
105 $this->add('text', 'logo_image', ts('Logo image'))->setSize(45);
106 $this->add('text', 'image', ts('Image'))->setSize(45);
107 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
108 $this->add('text', 'button_title', ts('Contribution button title'))->setSize(45);
109 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
110 $this->addWysiwyg('description', ts('Description'), '');
111 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
112 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
113 // $fieldset = $this->addElement('fieldset')->setLabel('Advanced Settings');
114 $this->add('checkbox', 'hide_title', ts('Hide Title'));
115 $this->add('checkbox', 'hide_border', ts('Hide border'));
116 $this->add('checkbox', 'hide_pbcap', ts('Hide progress bar caption'));
117 foreach ($this->_colorFields
as $name => $val) {
118 $this->addElement($val[1],
125 $this->add('textarea', 'style_rules', ts('Additional Style Rules'), 'cols="70"')->setRows(5);
126 $this->add('checkbox', 'override', ts('Override default template'));
127 $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>'), 'cols="70"')->setRows(10);
129 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
130 $this->addButtons(array(
133 'name' => ts('Save'),
138 'name' => ts('Save & Preview'),
142 // export form elements
143 $this->assign('elementNames', $this->getRenderableElementNames());
145 if (isset($this->_id
)) {
146 /** Updating existing widget*/
148 /*$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
149 where w.id=" . $this->_id;*/
151 $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id
;
154 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
156 while ($dao->fetch()) {
158 $wid_page[$dao->id
] = array();
159 CRM_Core_DAO
::storeValues($dao, $wid_page[$dao->id
]);
161 $this->setDefaults(array(
162 'title' => $wid_page[$dao->id
]['title']));
163 $this->setDefaults(array(
164 'logo_image' => $wid_page[$dao->id
]['logo_image']));
165 $this->setDefaults(array(
166 'image' => $wid_page[$dao->id
]['image']));
167 $this->setDefaults(array(
168 'button_link_to' => $wid_page[$dao->id
]['button_link_to']));
169 $this->setDefaults(array(
170 'button_title' => $wid_page[$dao->id
]['button_title']));
172 $this->setDefaults(array(
173 'progress_bar' => $dao->progress_bar_id
/*$dao->pbid*/));
174 $description = $wid_page[$dao->id
]['description'];
175 $this->setDefaults(array(
176 'description' => $description));
177 $this->setDefaults(array(
178 'email_signup_group_id' => $wid_page[$dao->id
]['email_signup_group_id']));
179 $this->setDefaults(array(
180 'size_variant' => $dao->size_variant
));
181 $this->setDefaults(array(
182 'color_title' => $wid_page[$dao->id
]['color_title']));
183 $this->setDefaults(array(
184 'color_title_bg' => $wid_page[$dao->id
]['color_title_bg']));
185 $this->setDefaults(array(
186 'color_bar' => $wid_page[$dao->id
]['color_progress_bar']));
187 $this->setDefaults(array(
188 'color_widget_bg' => $wid_page[$dao->id
]['color_widget_bg']));
189 $this->setDefaults(array(
190 'color_description' => $wid_page[$dao->id
]['color_description']));
191 $this->setDefaults(array(
192 'color_border' => $wid_page[$dao->id
]['color_border']));
193 $this->setDefaults(array(
194 'color_button' => $wid_page[$dao->id
]['color_button']));
195 $this->setDefaults(array(
196 'color_button_bg' => $wid_page[$dao->id
]['color_button_bg']));
197 $this->setDefaults(array(
198 'style_rules' => $wid_page[$dao->id
]['style_rules']));
199 $this->setDefaults(array(
200 'override' => $wid_page[$dao->id
]['override']));
201 $this->setDefaults(array(
202 'hide_title' => $wid_page[$dao->id
]['hide_title']));
203 $this->setDefaults(array(
204 'hide_border' => $wid_page[$dao->id
]['hide_border']));
205 $this->setDefaults(array(
206 'hide_pbcap' => $wid_page[$dao->id
]['hide_pbcap']));
208 $this->setDefaults(array(
209 'color_btn_newsletter' => $wid_page[$dao->id
]['color_btn_newsletter']));
210 $this->setDefaults(array(
211 'color_btn_newsletter_bg' => $wid_page[$dao->id
]['color_btn_newsletter_bg']));
212 $this->setDefaults(array(
213 'newsletter_text' => $wid_page[$dao->id
]['newsletter_text']));
214 $this->setDefaults(array(
215 'color_newsletter_text' => $wid_page[$dao->id
]['color_newsletter_text']));
217 if(true == $wid_page[$dao->id
]['override']) {
218 $cust_templ = html_entity_decode($wid_page[$dao->id
]['custom_template']);
219 $this->setDefaults(array(
220 'custom_template' => $cust_templ));
222 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
223 $elem = $this->getElement('custom_template');
224 $elem->setValue($output);
227 CRM_Utils_System
::setTitle(ts('Edit Widget'));
230 CRM_Utils_System
::setTitle(ts('Create Widget'));
231 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
232 $elem = $this->getElement('custom_template');
233 $elem->setValue($output);
235 parent
::buildQuickForm();
238 function postProcess() {
239 $values = $this->exportValues();
250 /** If override check is checked state then only save the custom_template to the
251 database. otherwise wci uses default tpl file.
253 if(isset($values['override'])){
254 $override = $values['override'];
255 $cust_tmpl = str_replace("'", "''", $values['custom_template']);
256 $cust_tmpl_col = "custom_template";
261 if(isset($values['hide_title'])){
262 $hide_title = $values['hide_title'];
264 if(isset($values['hide_border'])){
265 $hide_border = $values['hide_border'];
267 if(isset($values['hide_pbcap'])){
268 $hide_pbcap = $values['hide_pbcap'];
270 $title = str_replace("'", "''", $values['title']);
272 if (isset($this->_id
)) {
273 $sql = "UPDATE civicrm_wci_widget SET title = '". $title
274 . "', logo_image = '" . $values['logo_image'] . "', image = '"
275 . $values['image'] . "', button_title = '" . $values['button_title']
276 . "', button_link_to = '" . $values['button_link_to']
277 . "', progress_bar_id = '" . $values['progress_bar']
278 . "', description = '" . str_replace("'", "''", $values['description'])
279 . "', email_signup_group_id = '" . $values['email_signup_group_id']
280 . "', size_variant = '" . $values['size_variant']
281 . "', color_title = '" . $values['color_title']
282 . "', color_title_bg = '" . $values['color_title_bg']
283 . "', color_progress_bar = '" . $values['color_bar']
284 . "', color_widget_bg = '" . $values['color_widget_bg']
285 . "', color_description = '" . $values['color_description']
286 . "', color_border = '" . $values['color_border']
287 . "', color_button = '" . $values['color_button']
288 . "', color_button_bg = '" . $values['color_button_bg']
289 . "', hide_title = '" . $hide_title
290 . "', hide_border = '" . $hide_border
291 . "', hide_pbcap = '" . $hide_pbcap
292 . "', color_btn_newsletter = '" . $values['color_btn_newsletter']
293 . "', color_btn_newsletter_bg = '" . $values['color_btn_newsletter_bg']
294 . "', newsletter_text = '" . $values['newsletter_text']
295 . "', color_newsletter_text = '" . $values['color_newsletter_text']
296 . "', style_rules = '" . str_replace("'", "''", $values['style_rules']) . "', override = '"
297 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id
;
300 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
301 button_title, button_link_to, progress_bar_id, description,
302 email_signup_group_id, size_variant, color_title, color_title_bg,
303 color_progress_bar, color_widget_bg, color_description, color_border,
304 color_button, color_button_bg, hide_title, hide_border, hide_pbcap,
305 color_btn_newsletter, color_btn_newsletter_bg, newsletter_text, color_newsletter_text,
306 style_rules, override" . $coma . $cust_tmpl_col ." )
307 VALUES ('" . $title . "','" . $values['logo_image'] . "','" .
308 $values['image'] . "','" . $values['button_title'] . "','" .
309 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
310 str_replace("'", "''", $values['description']) . "','" .
311 $values['email_signup_group_id'] . "','" .
312 $values['size_variant'] . "','" . $values['color_title'] . "','" .
313 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
314 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
315 $values['color_border'] . "','" . $values['color_button'] . "','" .
316 $values['color_button_bg'] . "','" . $hide_title . "','" .
317 $hide_border . "','" . $hide_pbcap . "','" .
318 $values['color_btn_newsletter'] . "','" . $values['color_btn_newsletter_bg'] . "','" .
319 $values['newsletter_text'] . "','" . $values['color_newsletter_text'] . "','" .
320 str_replace("'", "''", $values['style_rules']) . "','" .
321 $override . $quote . $coma . $quote . $cust_tmpl
325 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
327 $transaction = new CRM_Core_Transaction();
328 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 0;");
329 CRM_Core_DAO
::executeQuery($sql);
330 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 1;");
331 $transaction->commit();
332 CRM_Core_Session
::setStatus(ts('Widget created successfuly'), '', 'success');
333 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
334 (isset($this->_id
)) ?
$widget_id = $this->_id
:
335 $widget_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
336 CRM_Utils_System
::redirect('?action=update&reset=1&id=' . $widget_id);
338 CRM_Utils_System
::redirect('widget?reset=1');
341 catch (Exception
$e) {
342 CRM_Core_Session
::setStatus(ts('Failed to create widget'), '', 'error');
343 $transaction->rollback();
346 parent
::postProcess();
349 function getProgressBars() {
351 '' => ts('- select -'),
353 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
354 foreach ($pbList as $pb) {
355 $options[$pb['id']] = $pb['name'];
360 function getContributionPageOptions() {
362 '' => ts('- select -'),
365 $result = civicrm_api3('contribution_page', 'get');
366 foreach ($result['values'] as $contribution_page) {
367 $options[$contribution_page['id']] = $contribution_page['title'];
373 function getGroupOptions() {
375 '' => ts('- select -'),
378 $result = civicrm_api3('group', 'get', array(
382 foreach ($result['values'] as $group) {
383 $options[$group['id']] = $group['title'];
389 function getSizeOptions() {
391 'thin' => ts('Thin'),
392 'normal' => ts('Normal'),
393 'wide' => ts('Wide'),
400 * Get the fields/elements defined in this form.
402 * @return array (string)
404 function getRenderableElementNames() {
405 // The _elements list includes some items which should not be
406 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
407 // items don't have labels. We'll identify renderable by filtering on
409 $elementNames = array();
410 foreach ($this->_elements
as $element) {
411 $label = $element->getLabel();
412 if (!empty($label)) {
413 $elementNames[] = $element->getName();
416 return $elementNames;