| 1 | <?php |
| 2 | |
| 3 | require_once 'CRM/Core/Form.php'; |
| 4 | require_once 'wci-helper-functions.php'; |
| 5 | require_once 'CRM/Wci/BAO/ProgressBar.php'; |
| 6 | |
| 7 | /** |
| 8 | * Form controller class |
| 9 | * |
| 10 | * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference |
| 11 | */ |
| 12 | class CRM_Wci_Form_CreateWidget extends CRM_Core_Form { |
| 13 | |
| 14 | /** |
| 15 | * the widget id saved to the session for an update |
| 16 | * |
| 17 | * @var int |
| 18 | * @access protected |
| 19 | */ |
| 20 | protected $_id; |
| 21 | |
| 22 | function preProcess() { |
| 23 | CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'createwidget.js'); |
| 24 | parent::preProcess(); |
| 25 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, |
| 26 | FALSE, NULL, 'REQUEST' ); |
| 27 | |
| 28 | $this->_colorFields = array('color_title' => array(ts('Title Text Color'), |
| 29 | 'text', |
| 30 | FALSE, |
| 31 | '#BF0F0F', |
| 32 | ), |
| 33 | 'color_title_bg' => array(ts('Title background color'), |
| 34 | 'text', |
| 35 | FALSE, |
| 36 | '#FFFFFF', |
| 37 | ), |
| 38 | 'color_bar' => array(ts('Progress Bar Color'), |
| 39 | 'text', |
| 40 | FALSE, |
| 41 | '#BF0F0F', |
| 42 | ), |
| 43 | 'color_widget_bg' => array(ts('Background color'), |
| 44 | 'text', |
| 45 | FALSE, |
| 46 | '#FFFFFF', |
| 47 | ), |
| 48 | 'color_description' => array(ts('Description color'), |
| 49 | 'text', |
| 50 | FALSE, |
| 51 | '#000000', |
| 52 | ), |
| 53 | 'color_border' => array(ts('Border color'), |
| 54 | 'text', |
| 55 | FALSE, |
| 56 | '#BF0F0F', |
| 57 | ), |
| 58 | 'color_button' => array(ts('Button text color'), |
| 59 | 'text', |
| 60 | FALSE, |
| 61 | '#000000', |
| 62 | ), |
| 63 | 'color_button_bg' => array(ts('Button background color'), |
| 64 | 'text', |
| 65 | FALSE, |
| 66 | '#BF0F0F', |
| 67 | ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | function setDefaultValues() { |
| 72 | $defaults = array(); |
| 73 | if (!isset($this->_id)) { |
| 74 | $defaults['size_variant'] = 'normal'; |
| 75 | foreach ($this->_colorFields as $name => $val) { |
| 76 | $defaults[$name] = $val[3]; |
| 77 | } |
| 78 | } |
| 79 | return $defaults; |
| 80 | } |
| 81 | |
| 82 | function buildQuickForm() { |
| 83 | // add form elements |
| 84 | $this->add('text', 'title', ts('Title'),true)->setSize(45); |
| 85 | $this->add('text', 'logo_image', ts('Logo image'))->setSize(45); |
| 86 | $this->add('text', 'image', ts('Image'))->setSize(45); |
| 87 | $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions()); |
| 88 | $this->add('text', 'button_title', ts('Contribution button title'))->setSize(45); |
| 89 | $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars()); |
| 90 | $this->addWysiwyg('description', ts('Description'), ''); |
| 91 | $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions()); |
| 92 | $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions()); |
| 93 | // $fieldset = $this->addElement('fieldset')->setLabel('Advanced Settings'); |
| 94 | $this->add('checkbox', 'hide_title', ts('Hide Title')); |
| 95 | $this->add('checkbox', 'hide_border', ts('Hide border')); |
| 96 | $this->add('checkbox', 'hide_pbcap', ts('Hide progress bar caption')); |
| 97 | foreach ($this->_colorFields as $name => $val) { |
| 98 | $this->addElement($val[1], |
| 99 | $name, |
| 100 | $val[0], |
| 101 | $name, |
| 102 | $val[2] |
| 103 | ); |
| 104 | } |
| 105 | $this->add('textarea', 'style_rules', ts('Additional Style Rules'))->setRows(5); |
| 106 | $this->add('checkbox', 'override', ts('Override default template')); |
| 107 | $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>'))->setRows(10); |
| 108 | |
| 109 | $this->addElement('submit','preview','name="Save and Preview" id="preview"'); |
| 110 | $this->addButtons(array( |
| 111 | array( |
| 112 | 'type' => 'submit', |
| 113 | 'name' => ts('Save'), |
| 114 | 'isDefault' => TRUE, |
| 115 | ), |
| 116 | array( |
| 117 | 'type' => 'next', |
| 118 | 'name' => ts('Save & Preview'), |
| 119 | ), |
| 120 | )); |
| 121 | |
| 122 | // export form elements |
| 123 | $this->assign('elementNames', $this->getRenderableElementNames()); |
| 124 | |
| 125 | if (isset($this->_id)) { |
| 126 | /** Updating existing widget*/ |
| 127 | |
| 128 | /*$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 |
| 129 | where w.id=" . $this->_id;*/ |
| 130 | |
| 131 | $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id; |
| 132 | $params = array(); |
| 133 | |
| 134 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget'); |
| 135 | |
| 136 | while ($dao->fetch()) { |
| 137 | |
| 138 | $wid_page[$dao->id] = array(); |
| 139 | CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]); |
| 140 | |
| 141 | $this->setDefaults(array( |
| 142 | 'title' => $wid_page[$dao->id]['title'])); |
| 143 | $this->setDefaults(array( |
| 144 | 'logo_image' => $wid_page[$dao->id]['logo_image'])); |
| 145 | $this->setDefaults(array( |
| 146 | 'image' => $wid_page[$dao->id]['image'])); |
| 147 | $this->setDefaults(array( |
| 148 | 'button_link_to' => $wid_page[$dao->id]['button_link_to'])); |
| 149 | $this->setDefaults(array( |
| 150 | 'button_title' => $wid_page[$dao->id]['button_title'])); |
| 151 | |
| 152 | $this->setDefaults(array( |
| 153 | 'progress_bar' => $dao->progress_bar_id/*$dao->pbid*/)); |
| 154 | $description = base64_decode($wid_page[$dao->id]['description']); |
| 155 | $this->setDefaults(array( |
| 156 | 'description' => $description)); |
| 157 | $this->setDefaults(array( |
| 158 | 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id'])); |
| 159 | $this->setDefaults(array( |
| 160 | 'size_variant' => $dao->size_variant)); |
| 161 | $this->setDefaults(array( |
| 162 | 'color_title' => $wid_page[$dao->id]['color_title'])); |
| 163 | $this->setDefaults(array( |
| 164 | 'color_title_bg' => $wid_page[$dao->id]['color_title_bg'])); |
| 165 | $this->setDefaults(array( |
| 166 | 'color_bar' => $wid_page[$dao->id]['color_progress_bar'])); |
| 167 | $this->setDefaults(array( |
| 168 | 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg'])); |
| 169 | $this->setDefaults(array( |
| 170 | 'color_description' => $wid_page[$dao->id]['color_description'])); |
| 171 | $this->setDefaults(array( |
| 172 | 'color_border' => $wid_page[$dao->id]['color_border'])); |
| 173 | $this->setDefaults(array( |
| 174 | 'color_button' => $wid_page[$dao->id]['color_button'])); |
| 175 | $this->setDefaults(array( |
| 176 | 'color_button_bg' => $wid_page[$dao->id]['color_button_bg'])); |
| 177 | $this->setDefaults(array( |
| 178 | 'style_rules' => base64_decode($wid_page[$dao->id]['style_rules']))); |
| 179 | $this->setDefaults(array( |
| 180 | 'override' => $wid_page[$dao->id]['override'])); |
| 181 | $this->setDefaults(array( |
| 182 | 'hide_title' => $wid_page[$dao->id]['hide_title'])); |
| 183 | $this->setDefaults(array( |
| 184 | 'hide_border' => $wid_page[$dao->id]['hide_border'])); |
| 185 | $this->setDefaults(array( |
| 186 | 'hide_pbcap' => $wid_page[$dao->id]['hide_pbcap'])); |
| 187 | if(true == $wid_page[$dao->id]['override']) { |
| 188 | $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']); |
| 189 | $this->setDefaults(array( |
| 190 | 'custom_template' => $cust_templ)); |
| 191 | } else { |
| 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); |
| 195 | } |
| 196 | } |
| 197 | CRM_Utils_System::setTitle(ts('Edit Widget')); |
| 198 | } |
| 199 | else { |
| 200 | CRM_Utils_System::setTitle(ts('Create Widget')); |
| 201 | /** Keep template in civicrm-wci/templates folder*/ |
| 202 | $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH); |
| 203 | $elem = $this->getElement('custom_template'); |
| 204 | $elem->setValue($output); |
| 205 | } |
| 206 | parent::buildQuickForm(); |
| 207 | } |
| 208 | |
| 209 | function postProcess() { |
| 210 | $values = $this->exportValues(); |
| 211 | $override = 0; |
| 212 | $hide_title = 0; |
| 213 | $hide_border = 0; |
| 214 | $hide_pbcap = 0; |
| 215 | $cust_tmpl = ""; |
| 216 | $cust_tmpl_col = ""; |
| 217 | $sql = ""; |
| 218 | $coma = ""; |
| 219 | $equals = ""; |
| 220 | $quote = ""; |
| 221 | /** If override check is checked state then only save the custom_template to the |
| 222 | database. otherwise wci uses default tpl file. |
| 223 | */ |
| 224 | if(isset($values['override'])){ |
| 225 | $override = $values['override']; |
| 226 | $cust_tmpl = base64_encode(html_entity_decode($values['custom_template'])); |
| 227 | $cust_tmpl_col = "custom_template"; |
| 228 | $coma = ","; |
| 229 | $equals = " = "; |
| 230 | $quote = "'"; |
| 231 | } |
| 232 | if(isset($values['hide_title'])){ |
| 233 | $hide_title = $values['hide_title']; |
| 234 | } |
| 235 | if(isset($values['hide_border'])){ |
| 236 | $hide_border = $values['hide_border']; |
| 237 | } |
| 238 | if(isset($values['hide_pbcap'])){ |
| 239 | $hide_pbcap = $values['hide_pbcap']; |
| 240 | } |
| 241 | $title = str_replace("'", "''", $values['title']); |
| 242 | |
| 243 | if (isset($this->_id)) { |
| 244 | $sql = "UPDATE civicrm_wci_widget SET title = '". $title |
| 245 | . "', logo_image = '" . $values['logo_image'] . "', image = '" |
| 246 | . $values['image'] . "', button_title = '" . $values['button_title'] |
| 247 | . "', button_link_to = '" . $values['button_link_to'] |
| 248 | . "', progress_bar_id = '" . $values['progress_bar'] |
| 249 | . "', description = '" . base64_encode($values['description']) |
| 250 | . "', email_signup_group_id = '" . $values['email_signup_group_id'] |
| 251 | . "', size_variant = '" . $values['size_variant'] |
| 252 | . "', color_title = '" . $values['color_title'] |
| 253 | . "', color_title_bg = '" . $values['color_title_bg'] |
| 254 | . "', color_progress_bar = '" . $values['color_bar'] |
| 255 | . "', color_widget_bg = '" . $values['color_widget_bg'] |
| 256 | . "', color_description = '" . $values['color_description'] |
| 257 | . "', color_border = '" . $values['color_border'] |
| 258 | . "', color_button = '" . $values['color_button'] |
| 259 | . "', color_button_bg = '" . $values['color_button_bg'] |
| 260 | . "', hide_title = '" . $hide_title |
| 261 | . "', hide_border = '" . $hide_border |
| 262 | . "', hide_pbcap = '" . $hide_pbcap |
| 263 | . "', style_rules = '" . base64_encode($values['style_rules']) . "', override = '" |
| 264 | . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ; |
| 265 | } |
| 266 | else { |
| 267 | $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image, |
| 268 | button_title, button_link_to, progress_bar_id, description, |
| 269 | email_signup_group_id, size_variant, color_title, color_title_bg, |
| 270 | color_progress_bar, color_widget_bg, color_description, color_border, |
| 271 | color_button, color_button_bg, hide_title, hide_border, hide_pbcap, style_rules, override" . $coma . $cust_tmpl_col ." ) |
| 272 | VALUES ('" . $title . "','" . $values['logo_image'] . "','" . |
| 273 | $values['image'] . "','" . $values['button_title'] . "','" . |
| 274 | $values['button_link_to'] . "','" . $values['progress_bar'] . "','" . |
| 275 | base64_encode($values['description']) . "','" . |
| 276 | $values['email_signup_group_id'] . "','" . |
| 277 | $values['size_variant'] . "','" . $values['color_title'] . "','" . |
| 278 | $values['color_title_bg'] . "','" . $values['color_bar'] . "','" . |
| 279 | $values['color_widget_bg'] . "','" . $values['color_description'] . "','" . |
| 280 | $values['color_border'] . "','" . $values['color_button'] . "','" . |
| 281 | $values['color_button_bg'] . "','" . $hide_title . "','" . |
| 282 | $hide_border . "','" . $hide_pbcap . "','" |
| 283 | . base64_encode($values['style_rules']) . "','" . |
| 284 | $override . $quote . $coma . $quote . $cust_tmpl |
| 285 | . "')"; |
| 286 | } |
| 287 | |
| 288 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); |
| 289 | try { |
| 290 | $transaction = new CRM_Core_Transaction(); |
| 291 | CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;"); |
| 292 | CRM_Core_DAO::executeQuery($sql); |
| 293 | CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;"); |
| 294 | $transaction->commit(); |
| 295 | CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success'); |
| 296 | if(isset($_REQUEST['_qf_CreateWidget_next'])) { |
| 297 | (isset($this->_id)) ? $widget_id = $this->_id : |
| 298 | $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'); |
| 299 | CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id); |
| 300 | } else { |
| 301 | CRM_Utils_System::redirect('widget?reset=1'); |
| 302 | } |
| 303 | } |
| 304 | catch (Exception $e) { |
| 305 | CRM_Core_Session::setStatus(ts('Failed to create widget'), '', 'error'); |
| 306 | $transaction->rollback(); |
| 307 | } |
| 308 | |
| 309 | parent::postProcess(); |
| 310 | } |
| 311 | |
| 312 | function getProgressBars() { |
| 313 | $options = array( |
| 314 | '' => ts('- select -'), |
| 315 | ); |
| 316 | $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList(); |
| 317 | foreach ($pbList as $pb) { |
| 318 | $options[$pb['id']] = $pb['name']; |
| 319 | } |
| 320 | |
| 321 | return $options; |
| 322 | } |
| 323 | function getContributionPageOptions() { |
| 324 | $options = array( |
| 325 | '' => ts('- select -'), |
| 326 | ); |
| 327 | |
| 328 | $result = civicrm_api3('contribution_page', 'get'); |
| 329 | foreach ($result['values'] as $contribution_page) { |
| 330 | $options[$contribution_page['id']] = $contribution_page['title']; |
| 331 | } |
| 332 | |
| 333 | return $options; |
| 334 | } |
| 335 | |
| 336 | function getGroupOptions() { |
| 337 | $options = array( |
| 338 | '' => ts('- select -'), |
| 339 | ); |
| 340 | |
| 341 | $result = civicrm_api3('group', 'get', array( |
| 342 | 'group_type' => '2' |
| 343 | )); |
| 344 | |
| 345 | foreach ($result['values'] as $group) { |
| 346 | $options[$group['id']] = $group['title']; |
| 347 | } |
| 348 | |
| 349 | return $options; |
| 350 | } |
| 351 | |
| 352 | function getSizeOptions() { |
| 353 | $options = array( |
| 354 | 'thin' => ts('Thin'), |
| 355 | 'normal' => ts('Normal'), |
| 356 | 'wide' => ts('Wide'), |
| 357 | ); |
| 358 | |
| 359 | return $options; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Get the fields/elements defined in this form. |
| 364 | * |
| 365 | * @return array (string) |
| 366 | */ |
| 367 | function getRenderableElementNames() { |
| 368 | // The _elements list includes some items which should not be |
| 369 | // auto-rendered in the loop -- such as "qfKey" and "buttons". These |
| 370 | // items don't have labels. We'll identify renderable by filtering on |
| 371 | // the 'label'. |
| 372 | $elementNames = array(); |
| 373 | foreach ($this->_elements as $element) { |
| 374 | $label = $element->getLabel(); |
| 375 | if (!empty($label)) { |
| 376 | $elementNames[] = $element->getName(); |
| 377 | } |
| 378 | } |
| 379 | return $elementNames; |
| 380 | } |
| 381 | } |