fixed beta 1 bugs, 31062, 31063, 31101, 31067, 31065
[com.zyxware.civiwci.git] / CRM / Wci / Form / ProgressBar.php
index 29501d33dd8453805eb62eff068c0ccd219e754d..0be406e9b9709035ac9794da50ec3cf005acc05f 100644 (file)
@@ -19,7 +19,7 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
   }
   function fillData() {
     $count = 1;
-    if (isset($this->_id)) {  
+    if (isset($this->_id)) {
       /** Updating existing progress bar*/
       $query = "SELECT * FROM civicrm_wci_progress_bar where id=" . $this->_id;
       $params = array();
@@ -51,13 +51,13 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
           'contribution_page_'.$count, // field name
           'Contribution page', // field label
           getContributionPageOptions(), // list of options
-          true // is required
+          false // is required
         );
         $this->add(
           'text', // field type
           'percentage_'.$count, // field name
-          'Percentage', // field label
-          true // is required
+          'Percentage of contribution taken', // field label
+          false // is required
         );
         //save formula id 
         $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id]['id']);
@@ -69,6 +69,7 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
 
         $count++;
       }
+      CRM_Utils_System::setTitle(ts('Edit Progress Bar'));
       $count--; // because last iteration increments it to the next
     }  
     else {
@@ -83,9 +84,10 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
       $this->add(
         'text', // field type
         'percentage_1', // field name
-        'Percentage', // field label
+        'Percentage of contribution taken', // field label
         true // is required
       );
+      CRM_Utils_System::setTitle(ts('Create Progress Bar'));
     }
     
     $this->addElement('hidden', 'contrib_count', $count);
@@ -98,36 +100,23 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
       'progressbar_name', // field name
       'Name', // field label
       true // is required
-    );
+    )->setSize(35);
     $this->add(
       'text', // field type
       'starting_amount', // field name
       'Starting amount', // field label
       true // is required
-    );
+    )->setSize(35);
     $this->add(
       'text', // field type
       'goal_amount', // field name
       'Goal amount', // field label
       true // is required
-    );
-/*    $this->add(
-      'select', // field type
-      'contribution_page_1', // field name
-      'Contribution page', // field label
-      getContributionPageOptions(), // list of options
-      true // is required
-    );
-    $this->add(
-      'text', // field type
-      'percentage_1', // field name
-      'Percentage', // field label
-      true // is required
-    );*/
+    )->setSize(35);
     
     $this->fillData();
     
-    $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add more');
+    $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add another contribution page');
 
     $this->addButtons(array(
       array(
@@ -139,7 +128,7 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
 
     // export form elements
     $this->assign('elementNames', $this->getRenderableElementNames());
-
+    
     parent::buildQuickForm();
   }
 
@@ -147,35 +136,32 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
     $errorScope = CRM_Core_TemporaryErrorScope::useException();
     if (isset($this->_id)) {
       try {
+        $transaction = new CRM_Core_Transaction();
+        
         $sql = "UPDATE civicrm_wci_progress_bar SET name = '". $_REQUEST['progressbar_name'] . 
           "', starting_amount = '" . $_REQUEST['starting_amount'] . 
           "', goal_amount = '" . $_REQUEST['goal_amount'] . 
           "' where id =".$this->_id;
 
-        $transaction = new CRM_Core_Transaction();
         CRM_Core_DAO::executeQuery($sql);
-
-        for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
+        /** Delete existiing formula fields and add fields fresh*/
+        CRM_Core_DAO::executeQuery('DELETE FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id=' . $this->_id);
+        
+        for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++) {
           $page = 'contribution_page_' . (string)$i;
           $perc = 'percentage_' . (string)$i;
-          if (isset($_REQUEST['contrib_elem_'.$i])) {
-            $sql = "UPDATE civicrm_wci_progress_bar_formula SET contribution_page_id = '". $_REQUEST[$page] . "',
-              percentage = '". $_REQUEST[$perc] . "'
-              WHERE id = " . (int)$_REQUEST['contrib_elem_'.$i];
-          } 
-          else {
-            $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage) 
+          $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage) 
               VALUES ('" . $_REQUEST[$page] . "','" . $this->_id . "','" . $_REQUEST[$perc] . "')";
-          }
 
           CRM_Core_DAO::executeQuery($sql);
-        endfor;
+        }
+        
         $transaction->commit();
-        CRM_Utils_System::redirect('civicrm/wci/progress-bar?reset=1');
+        CRM_Core_Session::setStatus(ts('Progress bar created successfuly'), '', 'success');
+        CRM_Utils_System::redirect('progress-bar?reset=1');
       }
       catch (Exception $e) {
-        //TODO
-        print_r($e->getMessage());
+        CRM_Core_Session::setStatus(ts('Failed to create progress bar'), '', 'error');
         $transaction->rollback();
       }