| 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 | '#2786C2', |
| 32 | ), |
| 33 | 'color_title_bg' => array(ts('Widget title background color'), |
| 34 | 'text', |
| 35 | FALSE, |
| 36 | '#FFFFFF', |
| 37 | ), |
| 38 | 'color_bar' => array(ts('Progress Bar Color'), |
| 39 | 'text', |
| 40 | FALSE, |
| 41 | '#FFFFFF', |
| 42 | ), |
| 43 | 'color_widget_bg' => array(ts('Widget background color'), |
| 44 | 'text', |
| 45 | FALSE, |
| 46 | '#96C0E7', |
| 47 | ), |
| 48 | 'color_description' => array(ts('Widget description color'), |
| 49 | 'text', |
| 50 | FALSE, |
| 51 | '#000000', |
| 52 | ), |
| 53 | 'color_border' => array(ts('Widget border color'), |
| 54 | 'text', |
| 55 | FALSE, |
| 56 | '#96C0E7', |
| 57 | ), |
| 58 | 'color_button' => array(ts('Widget button text color'), |
| 59 | 'text', |
| 60 | FALSE, |
| 61 | '#000000', |
| 62 | ), |
| 63 | 'color_button_bg' => array(ts('Widget button background color'), |
| 64 | 'text', |
| 65 | FALSE, |
| 66 | '#96C0E7', |
| 67 | ), |
| 68 | 'color_button_bg' => array(ts('Widget button background color'), |
| 69 | 'text', |
| 70 | FALSE, |
| 71 | '#96C0E7', |
| 72 | ), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | function setDefaultValues() { |
| 77 | $defaults = array(); |
| 78 | if (!isset($this->_id)) { |
| 79 | $defaults['size_variant'] = 'normal'; |
| 80 | foreach ($this->_colorFields as $name => $val) { |
| 81 | $defaults[$name] = $val[3]; |
| 82 | } |
| 83 | } |
| 84 | return $defaults; |
| 85 | } |
| 86 | |
| 87 | function buildQuickForm() { |
| 88 | // add form elements |
| 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) { |
| 99 | $this->add($val[1], |
| 100 | $name, |
| 101 | $val[0], |
| 102 | $name, |
| 103 | $val[2] |
| 104 | ); |
| 105 | } |
| 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>')); |
| 109 | |
| 110 | $this->addElement('submit','preview','name="Save and Preview" id="preview"'); |
| 111 | $this->addButtons(array( |
| 112 | array( |
| 113 | 'type' => 'submit', |
| 114 | 'name' => ts('Save'), |
| 115 | 'isDefault' => TRUE, |
| 116 | ), |
| 117 | array( |
| 118 | 'type' => 'next', |
| 119 | 'name' => ts('Save & Preview'), |
| 120 | ), |
| 121 | )); |
| 122 | |
| 123 | // export form elements |
| 124 | $this->assign('elementNames', $this->getRenderableElementNames()); |
| 125 | |
| 126 | if (isset($this->_id)) { |
| 127 | /** Updating existing widget*/ |
| 128 | |
| 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;*/ |
| 131 | |
| 132 | $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id; |
| 133 | $params = array(); |
| 134 | |
| 135 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget'); |
| 136 | |
| 137 | while ($dao->fetch()) { |
| 138 | |
| 139 | $wid_page[$dao->id] = array(); |
| 140 | CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]); |
| 141 | |
| 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'])); |
| 152 | |
| 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)); |
| 186 | } else { |
| 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); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | else { |
| 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); |
| 198 | } |
| 199 | parent::buildQuickForm(); |
| 200 | } |
| 201 | |
| 202 | function postProcess() { |
| 203 | $values = $this->exportValues(); |
| 204 | |
| 205 | $override = 0; |
| 206 | $cust_tmpl = ""; |
| 207 | $cust_tmpl_col = ""; |
| 208 | $sql = ""; |
| 209 | $coma = ""; |
| 210 | $equals = ""; |
| 211 | $quote = ""; |
| 212 | /** If override check is checked state then only save the custom_template to the |
| 213 | database. otherwise wci uses default tpl file. |
| 214 | */ |
| 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"; |
| 219 | $coma = ","; |
| 220 | $equals = " = "; |
| 221 | $quote = "'"; |
| 222 | } |
| 223 | |
| 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 ; |
| 243 | } |
| 244 | else { |
| 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 |
| 261 | . "')"; |
| 262 | } |
| 263 | |
| 264 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); |
| 265 | try { |
| 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(); |
| 271 | |
| 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); |
| 276 | } else { |
| 277 | CRM_Utils_System::redirect('widget?reset=1'); |
| 278 | } |
| 279 | } |
| 280 | catch (Exception $e) { |
| 281 | //TODO |
| 282 | print_r($e->getMessage()); |
| 283 | $transaction->rollback(); |
| 284 | } |
| 285 | |
| 286 | parent::postProcess(); |
| 287 | } |
| 288 | |
| 289 | function getProgressBars() { |
| 290 | $options = array( |
| 291 | '' => ts('- select -'), |
| 292 | ); |
| 293 | $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList(); |
| 294 | foreach ($pbList as $pb) { |
| 295 | $options[$pb['id']] = $pb['name']; |
| 296 | } |
| 297 | |
| 298 | return $options; |
| 299 | } |
| 300 | function getContributionPageOptions() { |
| 301 | $options = array( |
| 302 | '' => ts('- select -'), |
| 303 | ); |
| 304 | |
| 305 | $result = civicrm_api3('contribution_page', 'get'); |
| 306 | foreach ($result['values'] as $contribution_page) { |
| 307 | $options[$contribution_page['id']] = $contribution_page['title']; |
| 308 | } |
| 309 | |
| 310 | return $options; |
| 311 | } |
| 312 | |
| 313 | function getGroupOptions() { |
| 314 | $options = array( |
| 315 | '' => ts('- select -'), |
| 316 | ); |
| 317 | |
| 318 | $result = civicrm_api3('group', 'get', array( |
| 319 | 'group_type' => '2' |
| 320 | )); |
| 321 | |
| 322 | foreach ($result['values'] as $group) { |
| 323 | $options[$group['id']] = $group['title']; |
| 324 | } |
| 325 | |
| 326 | return $options; |
| 327 | } |
| 328 | |
| 329 | function getSizeOptions() { |
| 330 | $options = array( |
| 331 | 'thin' => ts('Thin'), |
| 332 | 'normal' => ts('Normal'), |
| 333 | 'wide' => ts('Wide'), |
| 334 | ); |
| 335 | |
| 336 | return $options; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Get the fields/elements defined in this form. |
| 341 | * |
| 342 | * @return array (string) |
| 343 | */ |
| 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 |
| 348 | // the 'label'. |
| 349 | $elementNames = array(); |
| 350 | foreach ($this->_elements as $element) { |
| 351 | $label = $element->getLabel(); |
| 352 | if (!empty($label)) { |
| 353 | $elementNames[] = $element->getName(); |
| 354 | } |
| 355 | } |
| 356 | return $elementNames; |
| 357 | } |
| 358 | } |