Beta I Bug fixes: #31066(Newsletter signup ability to change the button color and...
[com.zyxware.civiwci.git] / CRM / Wci / Form / CreateWidget.php
index 04f82629c7fc5923637f48cff2af6e3d20d1ec2e..585a14972c4099e5290b9975893242c781391c4a 100644 (file)
@@ -3,37 +3,7 @@
 require_once 'CRM/Core/Form.php';
 require_once 'wci-helper-functions.php';
 require_once 'CRM/Wci/BAO/ProgressBar.php';
-?>
 
-<script type="text/javascript">
-cj(function ( $ ) {
-  function setState() {
-    if ($('#override').is(':checked') == true) {
-      $('#custom_template').attr("disabled",false);
-    }
-    else {
-      $('#custom_template').attr("disabled",true);
-    }
-    if( $('#title').val() != "") {
-      $('#embd_code').parents('.crm-section').show();    
-    } else {
-      $('#embd_code').parents('.crm-section').hide();
-    }
-//    $('#embd_code').attr("disabled",true);
-  }
-  $(document).ready(setState)
-  $('#override').click(setState);
-
-/*  
-  $("input[name='_qf_CreateWidget_savenprev']").on('click', function( e ) {
-    e.preventDefault();
-    alert( document.title );
-
-  });*/
-});
-</script>
-
-<?php
 /**
  * Form controller class
  *
@@ -50,6 +20,7 @@ class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
   protected $_id;
   
   function preProcess() {
+    CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'createwidget.js');
     parent::preProcess();
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, 
           FALSE, NULL, 'REQUEST' );
@@ -57,84 +28,103 @@ class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
     $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
         'text',
         FALSE,
-        '#2786C2',
+        '#BF0F0F',
       ),
-      'color_title_bg' => array(ts('Widget title background color'),
+      'color_title_bg' => array(ts('Title background color'),
         'text',
         FALSE,
         '#FFFFFF',
       ),
       'color_bar' => array(ts('Progress Bar Color'),
+        'text',
+        FALSE,
+        '#BF0F0F',
+      ),
+      'color_widget_bg' => array(ts('Background color'),
         'text',
         FALSE,
         '#FFFFFF',
       ),
-      'color_widget_bg' => array(ts('Widget background color'),
+      'color_description' => array(ts('Description color'),
         'text',
         FALSE,
-        '#96C0E7',
+        '#000000',
       ),
-      'color_description' => array(ts('Widget description color'),
+      'color_border' => array(ts('Border color'),
         'text',
         FALSE,
-        '#96C0E7',
+        '#BF0F0F',
       ),
-      'color_border' => array(ts('Widget border color'),
+      'color_button' => array(ts('Button text color'),
         'text',
         FALSE,
-        '#96C0E7',
+        '#FFFFFF',
       ),
-      'color_button' => array(ts('Widget button text color'),
+      'color_button_bg' => array(ts('Button background color'),
         'text',
         FALSE,
-        '#000000',
+        '#BF0F0F',
+      ),
+      'color_btn_newsletter' => array(ts('Newsletter Button text color'),
+        'text',
+        FALSE,
+        '#FFFFFF',
+      ),
+      'color_btn_newsletter_bg' => array(ts('Newsletter Button color'),
+        'text',
+        FALSE,
+        '#BF0F0F',
       ),
-      'color_button_bg' => array(ts('Widget button background color'),
+      'newsletter_text' => array(ts('Newsletter text'),
         'text',
         FALSE,
-        '#96C0E7',
+        'Get the monthly newsletter',
       ),
-      'color_button_bg' => array(ts('Widget button background color'),
+      'color_newsletter_text' => array(ts('Newsletter text color'),
         'text',
         FALSE,
-        '#96C0E7',
+        '#BF0F0F',
       ),
       );
   }
 
   function setDefaultValues() {
     $defaults = array();
-    
-    $defaults['size_variant'] = 'normal';
-    foreach ($this->_colorFields as $name => $val) {
-      $defaults[$name] = $val[3];
+    if (!isset($this->_id)) {
+      $defaults['size_variant'] = 'normal';
+      foreach ($this->_colorFields as $name => $val) {
+        $defaults[$name] = $val[3];
+      }
     }
-
     return $defaults;
   }
 
   function buildQuickForm() {
     // add form elements
-    $this->add('text', 'title', ts('Title'),true);
-    $this->add('text', 'logo_image', ts('Logo image'));
-    $this->add('text', 'image', ts('Image'));
+    $this->add('text', 'title', ts('Title'),true)->setSize(45);
+    $this->add('text', 'logo_image', ts('Logo image'))->setSize(45);
+    $this->add('text', 'image', ts('Image'))->setSize(45);
     $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
-    $this->add('text', 'button_title', ts('Contribution button title'));
+    $this->add('text', 'button_title', ts('Contribution button title'))->setSize(45);
     $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
     $this->addWysiwyg('description', ts('Description'), '');
     $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
     $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
+    // $fieldset = $this->addElement('fieldset')->setLabel('Advanced Settings');
+    $this->add('checkbox', 'hide_title', ts('Hide Title'));
+    $this->add('checkbox', 'hide_border', ts('Hide border'));
+    $this->add('checkbox', 'hide_pbcap', ts('Hide progress bar caption'));
     foreach ($this->_colorFields as $name => $val) {
-      $this->add($val[1],
+      $this->addElement($val[1],
         $name,
         $val[0],
         $name,
         $val[2]
       );
     }
-    $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
+    $this->add('textarea', 'style_rules', ts('Additional Style Rules'), 'cols="70"')->setRows(5);
     $this->add('checkbox', 'override', ts('Override default template'));
-    $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>'));
+    $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);
 
     $this->addElement('submit','preview','name="Save and Preview" id="preview"');
     $this->addButtons(array(
@@ -149,21 +139,22 @@ class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
         ),
     ));
     
-    // $this->add('textarea', 'embd_code', ts('Code to embed:'));
-    
     // export form elements
     $this->assign('elementNames', $this->getRenderableElementNames());
 
     if (isset($this->_id)) {  
       /** Updating existing widget*/
+      
+      /*$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 
+where w.id=" . $this->_id;*/
 
-      $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 
-where w.id=" . $this->_id;
+      $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id;
       $params = array();
       
       $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
 
       while ($dao->fetch()) {
+
         $wid_page[$dao->id] = array();
         CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
 
@@ -179,14 +170,14 @@ where w.id=" . $this->_id;
               'button_title' => $wid_page[$dao->id]['button_title']));
 
         $this->setDefaults(array(
-              'progress_bar' => $dao->pbid));
-        $description = base64_decode($wid_page[$dao->id]['description']);
+              'progress_bar' => $dao->progress_bar_id/*$dao->pbid*/));
+        $description = $wid_page[$dao->id]['description'];
         $this->setDefaults(array(
               'description' => $description));
         $this->setDefaults(array(
               'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
         $this->setDefaults(array(
-              'size_variant' => $wid_page[$dao->id]['size_variant']));
+              'size_variant' => $dao->size_variant));
         $this->setDefaults(array(
               'color_title' => $wid_page[$dao->id]['color_title']));
         $this->setDefaults(array(
@@ -207,38 +198,50 @@ where w.id=" . $this->_id;
               'style_rules' => $wid_page[$dao->id]['style_rules']));
         $this->setDefaults(array(
               'override' => $wid_page[$dao->id]['override']));
+        $this->setDefaults(array(
+              'hide_title' => $wid_page[$dao->id]['hide_title']));
+        $this->setDefaults(array(
+              'hide_border' => $wid_page[$dao->id]['hide_border']));
+        $this->setDefaults(array(
+              'hide_pbcap' => $wid_page[$dao->id]['hide_pbcap']));
+
+        $this->setDefaults(array(
+              'color_btn_newsletter' => $wid_page[$dao->id]['color_btn_newsletter']));
+        $this->setDefaults(array(
+              'color_btn_newsletter_bg' => $wid_page[$dao->id]['color_btn_newsletter_bg']));
+        $this->setDefaults(array(
+              'newsletter_text' => $wid_page[$dao->id]['newsletter_text']));
+        $this->setDefaults(array(
+              'color_newsletter_text' => $wid_page[$dao->id]['color_newsletter_text']));
+                            
         if(true == $wid_page[$dao->id]['override']) {
-          $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);
+          $cust_templ =  html_entity_decode($wid_page[$dao->id]['custom_template']);
           $this->setDefaults(array(
               'custom_template' => $cust_templ));
+        } else {
+          $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
+          $elem = $this->getElement('custom_template');
+          $elem->setValue($output);
         }
       }
-      // $widget_controller_path = getWciWidgetControllerPath();
-      // 
-      // $emb_code = "<script src=\"http://code.jquery.com/jquery-1.9.1.min.js\"></script>
-// <script type=\"text/javascript\" src=\"" . $widget_controller_path . "?widgetId=" . $this->_id . "\"></script>
-// <script>
-// $( document ).ready(function() {
-// $('#widgetwci').html(wciwidgetcode);
-// });
-// </script>
-// <div id='widgetwci'>
-// </div>";
-      // $this->getElement('embd_code')->setValue($emb_code);
+      CRM_Utils_System::setTitle(ts('Edit Widget'));
     }
     else {
+      CRM_Utils_System::setTitle(ts('Create Widget'));
       /** Keep template in civicrm-wci/templates folder*/
-      $output = file_get_contents('templates/CRM/Wci/Page/Widget.tpl',FILE_USE_INCLUDE_PATH);
+      $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
       $elem = $this->getElement('custom_template');
-      $elem->setValue($output); 
+      $elem->setValue($output);
     }
     parent::buildQuickForm();
   }
 
   function postProcess() {
     $values = $this->exportValues();
-
     $override = 0;
+    $hide_title = 0;
+    $hide_border = 0;
+    $hide_pbcap = 0;
     $cust_tmpl = "";
     $cust_tmpl_col = "";
     $sql = "";
@@ -250,20 +253,30 @@ where w.id=" . $this->_id;
     */
     if(isset($values['override'])){
       $override = $values['override'];
-      $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
+      $cust_tmpl = str_replace("'", "''", $values['custom_template']);
       $cust_tmpl_col = "custom_template";
       $coma = ",";
       $equals = " = ";
       $quote = "'";
     }
+    if(isset($values['hide_title'])){
+        $hide_title = $values['hide_title'];
+    }
+    if(isset($values['hide_border'])){
+        $hide_border = $values['hide_border'];
+    }
+    if(isset($values['hide_pbcap'])){
+        $hide_pbcap = $values['hide_pbcap'];
+    }
+    $title = str_replace("'", "''", $values['title']);
     
     if (isset($this->_id)) {
-      $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title'] 
+      $sql = "UPDATE civicrm_wci_widget SET title = '". $title
         . "', logo_image = '" . $values['logo_image'] . "', image = '" 
         . $values['image'] . "', button_title = '" . $values['button_title'] 
         . "', button_link_to = '" . $values['button_link_to'] 
         . "', progress_bar_id = '" . $values['progress_bar'] 
-        . "', description = '" . base64_encode($values['description']) 
+        . "', description = '" . str_replace("'", "''", $values['description']) 
         . "', email_signup_group_id = '" . $values['email_signup_group_id'] 
         . "', size_variant = '" . $values['size_variant'] 
         . "', color_title = '" . $values['color_title'] 
@@ -273,8 +286,15 @@ where w.id=" . $this->_id;
         . "', color_description = '" . $values['color_description'] 
         . "', color_border = '" . $values['color_border'] 
         . "', color_button = '" . $values['color_button'] 
-        . "', color_button_bg = '" . $values['color_button_bg'] 
-        . "', style_rules = '" . $values['style_rules'] . "', override = '" 
+        . "', color_button_bg = '" . $values['color_button_bg']
+        . "', hide_title = '" . $hide_title
+        . "', hide_border = '" . $hide_border
+        . "', hide_pbcap = '" . $hide_pbcap
+        . "', color_btn_newsletter = '" . $values['color_btn_newsletter']
+        . "', color_btn_newsletter_bg = '" . $values['color_btn_newsletter_bg']
+        . "', newsletter_text = '" . $values['newsletter_text']
+        . "', color_newsletter_text = '" . $values['color_newsletter_text']
+        . "', style_rules = '" . str_replace("'", "''", $values['style_rules']) . "', override = '" 
         . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
     }
     else {
@@ -282,17 +302,23 @@ where w.id=" . $this->_id;
       button_title, button_link_to, progress_bar_id, description, 
       email_signup_group_id, size_variant, color_title, color_title_bg, 
       color_progress_bar, color_widget_bg, color_description, color_border, 
-      color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." ) 
-      VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" . 
+      color_button, color_button_bg, hide_title, hide_border, hide_pbcap, 
+      color_btn_newsletter, color_btn_newsletter_bg, newsletter_text, color_newsletter_text, 
+      style_rules, override" . $coma . $cust_tmpl_col ." ) 
+      VALUES ('" . $title . "','" . $values['logo_image'] . "','" . 
       $values['image'] . "','" . $values['button_title'] . "','" . 
       $values['button_link_to'] . "','" . $values['progress_bar'] . "','" . 
-      base64_encode($values['description']) . "','" . 
+      str_replace("'", "''", $values['description']) . "','" . 
       $values['email_signup_group_id'] . "','" . 
       $values['size_variant'] . "','" . $values['color_title'] . "','" . 
       $values['color_title_bg'] . "','" . $values['color_bar'] . "','" . 
       $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
       $values['color_border'] . "','" . $values['color_button'] . "','" . 
-      $values['color_button_bg'] . "','" . $values['style_rules'] . "','" . 
+      $values['color_button_bg'] . "','" . $hide_title . "','" .
+      $hide_border . "','" . $hide_pbcap . "','" .
+      $values['color_btn_newsletter'] . "','" . $values['color_btn_newsletter_bg'] . "','" .
+      $values['newsletter_text'] . "','" . $values['color_newsletter_text'] . "','" .
+      str_replace("'", "''", $values['style_rules']) . "','" . 
       $override . $quote . $coma . $quote . $cust_tmpl
         . "')";
     }
@@ -300,19 +326,21 @@ where w.id=" . $this->_id;
     $errorScope = CRM_Core_TemporaryErrorScope::useException();
     try {
       $transaction = new CRM_Core_Transaction();
+      CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;");
       CRM_Core_DAO::executeQuery($sql);
+      CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
       $transaction->commit();
-      
+      CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success');
       if(isset($_REQUEST['_qf_CreateWidget_next'])) {
-        $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
-        CRM_Utils_System::redirect('civicrm/wci/widget?action=update&reset=1&id=' . $widget_id);
+        (isset($this->_id)) ? $widget_id = $this->_id : 
+              $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
+        CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
       } else {
-        CRM_Utils_System::redirect('civicrm/wci/widget?reset=1');
+        CRM_Utils_System::redirect('widget?reset=1');
       }
     }    
     catch (Exception $e) {
-      //TODO
-      print_r($e->getMessage());
+      CRM_Core_Session::setStatus(ts('Failed to create widget'), '', 'error');
       $transaction->rollback();
     }