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<br><SMALL>Fixed width. Height depends on what contents selected.</SMALL>'), $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'),
141 // export form elements
142 $this->assign('elementNames', $this->getRenderableElementNames());
143 if (isset($this->_id
)) {
144 /** Updating existing widget*/
146 /*$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
147 where w.id=" . $this->_id;*/
149 $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id
;
152 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
154 while ($dao->fetch()) {
155 $wid_page[$dao->id
] = array();
156 CRM_Core_DAO
::storeValues($dao, $wid_page[$dao->id
]);
157 $this->setDefaults(array(
158 'title' => $wid_page[$dao->id
]['title']));
159 $this->setDefaults(array(
160 'logo_image' => $wid_page[$dao->id
]['logo_image']));
161 $this->setDefaults(array(
162 'image' => $wid_page[$dao->id
]['image']));
163 $this->setDefaults(array(
164 'button_link_to' => $wid_page[$dao->id
]['button_link_to']));
165 $this->setDefaults(array(
166 'button_title' => $wid_page[$dao->id
]['button_title']));
167 $this->setDefaults(array(
168 'progress_bar' => $dao->progress_bar_id
/*$dao->pbid*/));
169 $description = $wid_page[$dao->id
]['description'];
170 $this->setDefaults(array(
171 'description' => $description));
172 $this->setDefaults(array(
173 'email_signup_group_id' => $wid_page[$dao->id
]['email_signup_group_id']));
174 $this->setDefaults(array(
175 'size_variant' => $dao->size_variant
));
176 $this->setDefaults(array(
177 'color_title' => $wid_page[$dao->id
]['color_title']));
178 $this->setDefaults(array(
179 'color_title_bg' => $wid_page[$dao->id
]['color_title_bg']));
180 $this->setDefaults(array(
181 'color_bar' => $wid_page[$dao->id
]['color_progress_bar']));
182 $this->setDefaults(array(
183 'color_widget_bg' => $wid_page[$dao->id
]['color_widget_bg']));
184 $this->setDefaults(array(
185 'color_description' => $wid_page[$dao->id
]['color_description']));
186 $this->setDefaults(array(
187 'color_border' => $wid_page[$dao->id
]['color_border']));
188 $this->setDefaults(array(
189 'color_button' => $wid_page[$dao->id
]['color_button']));
190 $this->setDefaults(array(
191 'color_button_bg' => $wid_page[$dao->id
]['color_button_bg']));
192 $this->setDefaults(array(
193 'style_rules' => $wid_page[$dao->id
]['style_rules']));
194 $this->setDefaults(array(
195 'override' => $wid_page[$dao->id
]['override']));
196 $this->setDefaults(array(
197 'hide_title' => $wid_page[$dao->id
]['hide_title']));
198 $this->setDefaults(array(
199 'hide_border' => $wid_page[$dao->id
]['hide_border']));
200 $this->setDefaults(array(
201 'hide_pbcap' => $wid_page[$dao->id
]['hide_pbcap']));
203 $this->setDefaults(array(
204 'color_btn_newsletter' => $wid_page[$dao->id
]['color_btn_newsletter']));
205 $this->setDefaults(array(
206 'color_btn_newsletter_bg' => $wid_page[$dao->id
]['color_btn_newsletter_bg']));
207 $this->setDefaults(array(
208 'newsletter_text' => $wid_page[$dao->id
]['newsletter_text']));
209 $this->setDefaults(array(
210 'color_newsletter_text' => $wid_page[$dao->id
]['color_newsletter_text']));
212 if(true == $wid_page[$dao->id
]['override']) {
213 $cust_templ = html_entity_decode($wid_page[$dao->id
]['custom_template']);
214 $this->setDefaults(array(
215 'custom_template' => $cust_templ));
217 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
218 $elem = $this->getElement('custom_template');
219 $elem->setValue($output);
222 CRM_Utils_System
::setTitle(ts('Edit Widget'));
225 CRM_Utils_System
::setTitle(ts('Create Widget'));
226 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH
);
227 $elem = $this->getElement('custom_template');
228 $elem->setValue($output);
230 parent
::buildQuickForm();
233 function postProcess() {
234 $values = $this->exportValues();
245 /** If override check is checked state then only save the custom_template to the
246 database. otherwise wci uses default tpl file.
248 if(isset($values['override'])){
249 $override = $values['override'];
250 $cust_tmpl = str_replace("'", "''", $values['custom_template']);
251 $cust_tmpl_col = "custom_template";
256 if(isset($values['hide_title'])){
257 $hide_title = $values['hide_title'];
259 if(isset($values['hide_border'])){
260 $hide_border = $values['hide_border'];
262 if(isset($values['hide_pbcap'])){
263 $hide_pbcap = $values['hide_pbcap'];
265 $title = str_replace("'", "''", $values['title']);
267 if (isset($this->_id
)) {
268 $sql = "UPDATE civicrm_wci_widget SET title = '". $title
269 . "', logo_image = '" . $values['logo_image'] . "', image = '"
270 . $values['image'] . "', button_title = '" . $values['button_title']
271 . "', button_link_to = '" . $values['button_link_to']
272 . "', progress_bar_id = '" . $values['progress_bar']
273 . "', description = '" . str_replace("'", "''", $values['description'])
274 . "', email_signup_group_id = '" . $values['email_signup_group_id']
275 . "', size_variant = '" . $values['size_variant']
276 . "', color_title = '" . $values['color_title']
277 . "', color_title_bg = '" . $values['color_title_bg']
278 . "', color_progress_bar = '" . $values['color_bar']
279 . "', color_widget_bg = '" . $values['color_widget_bg']
280 . "', color_description = '" . $values['color_description']
281 . "', color_border = '" . $values['color_border']
282 . "', color_button = '" . $values['color_button']
283 . "', color_button_bg = '" . $values['color_button_bg']
284 . "', hide_title = '" . $hide_title
285 . "', hide_border = '" . $hide_border
286 . "', hide_pbcap = '" . $hide_pbcap
287 . "', color_btn_newsletter = '" . $values['color_btn_newsletter']
288 . "', color_btn_newsletter_bg = '" . $values['color_btn_newsletter_bg']
289 . "', newsletter_text = '" . $values['newsletter_text']
290 . "', color_newsletter_text = '" . $values['color_newsletter_text']
291 . "', style_rules = '" . str_replace("'", "''", $values['style_rules']) . "', override = '"
292 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id
;
295 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
296 button_title, button_link_to, progress_bar_id, description,
297 email_signup_group_id, size_variant, color_title, color_title_bg,
298 color_progress_bar, color_widget_bg, color_description, color_border,
299 color_button, color_button_bg, hide_title, hide_border, hide_pbcap,
300 color_btn_newsletter, color_btn_newsletter_bg, newsletter_text, color_newsletter_text,
301 style_rules, override" . $coma . $cust_tmpl_col ." )
302 VALUES ('" . $title . "','" . $values['logo_image'] . "','" .
303 $values['image'] . "','" . $values['button_title'] . "','" .
304 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
305 str_replace("'", "''", $values['description']) . "','" .
306 $values['email_signup_group_id'] . "','" .
307 $values['size_variant'] . "','" . $values['color_title'] . "','" .
308 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
309 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
310 $values['color_border'] . "','" . $values['color_button'] . "','" .
311 $values['color_button_bg'] . "','" . $hide_title . "','" .
312 $hide_border . "','" . $hide_pbcap . "','" .
313 $values['color_btn_newsletter'] . "','" . $values['color_btn_newsletter_bg'] . "','" .
314 $values['newsletter_text'] . "','" . $values['color_newsletter_text'] . "','" .
315 str_replace("'", "''", $values['style_rules']) . "','" .
316 $override . $quote . $coma . $quote . $cust_tmpl
320 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
322 $transaction = new CRM_Core_Transaction();
323 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 0;");
324 CRM_Core_DAO
::executeQuery($sql);
325 CRM_Core_DAO
::executeQuery("SET foreign_key_checks = 1;");
326 $transaction->commit();
327 CRM_Core_Session
::setStatus(ts('Widget created successfuly'), '', 'success');
328 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
329 (isset($this->_id
)) ?
$widget_id = $this->_id
:
330 $widget_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
331 CRM_Utils_System
::redirect('?action=update&reset=1&id=' . $widget_id);
333 CRM_Utils_System
::redirect('widget?reset=1');
336 catch (Exception
$e) {
337 CRM_Core_Session
::setStatus(ts('Failed to create widget'), '', 'error');
338 $transaction->rollback();
341 parent
::postProcess();
344 function getProgressBars() {
346 '' => ts('- select -'),
348 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
349 foreach ($pbList as $pb) {
350 $options[$pb['id']] = $pb['name'];
355 function getContributionPageOptions() {
357 '' => ts('- select -'),
360 $result = civicrm_api3('contribution_page', 'get');
361 foreach ($result['values'] as $contribution_page) {
362 $options[$contribution_page['id']] = $contribution_page['title'];
368 function getGroupOptions() {
370 '' => ts('- select -'),
373 $result = civicrm_api3('group', 'get', array(
377 foreach ($result['values'] as $group) {
378 $options[$group['id']] = $group['title'];
384 function getSizeOptions() {
386 'thin' => ts('Thin (150px)'),
387 'normal' => ts('Normal (200px)'),
388 'wide' => ts('Wide (250px)'),
395 * Get the fields/elements defined in this form.
397 * @return array (string)
399 function getRenderableElementNames() {
400 // The _elements list includes some items which should not be
401 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
402 // items don't have labels. We'll identify renderable by filtering on
404 $elementNames = array();
405 foreach ($this->_elements
as $element) {
406 $label = $element->getLabel();
407 if (!empty($label)) {
408 $elementNames[] = $element->getName();
411 return $elementNames;