#29754 Progress bar listing page
[com.zyxware.civiwci.git] / CRM / Wci / Form / ProgressBar.php
index e3a72753e4815b52a0a6d1a27939aea68e949e86..1d337e134c2a1ce439a550cdd460a05d52536f75 100644 (file)
@@ -3,6 +3,7 @@
 require_once 'CRM/Core/Form.php';
 require_once 'wci-helper-functions.php';
 require_once 'CRM/Wci/BAO/ProgressBar.php';
+require_once 'CRM/Wci/DAO/ProgressBarFormula.php';
 
 /**
  * Form controller class
@@ -10,13 +11,88 @@ require_once 'CRM/Wci/BAO/ProgressBar.php';
  * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
  */
 class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
-  
+  private $_id;  
   function preProcess() {
-  
+    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'REQUEST');
     CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'addmore.js');
     parent::preProcess();
   }
+  function fill_data() {
+//    $_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'REQUEST');
+    $count = 1;
+    if (isset($this->_id)) {  
+      /** Updating existing progress bar*/
+      $query = "SELECT * FROM civicrm_wci_progress_bar where id=" . $this->_id;
+      $params = array();
+      
+      $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_WCI_DAO_ProgressBar');
+
+      while ($dao->fetch()) {
+        $con_page[$dao->id] = array();
+        CRM_Core_DAO::storeValues($dao, $con_page[$dao->id]);
+        $this->setDefaults(array(
+              'progressbar_name' => $con_page[$dao->id]['name']));
+        $this->setDefaults(array(
+              'starting_amount' => $con_page[$dao->id]['starting_amount']));
+        $this->setDefaults(array(
+              'goal_amount' => $con_page[$dao->id]['goal_amount']));
+      }
+       
+      $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $this->_id;
+      $params = array();
+
+      $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_WCI_DAO_ProgressBarFormula');
+
+      while ($dao->fetch()) {
+        $for_page[$dao->id] = array();
+        CRM_Core_DAO::storeValues($dao, $for_page[$dao->id]);
+
+        $this->add(
+          'select', // field type
+          'contribution_page_'.$count, // field name
+          'Contribution page', // field label
+          getContributionPageOptions(), // list of options
+          true // is required
+        );
+        $this->add(
+          'text', // field type
+          'percentage_'.$count, // field name
+          'Percentage', // field label
+          true // is required
+        );
+        //save formula id 
+        $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id]['id']);
+
+        $this->setDefaults(array(
+              'contribution_page_'.$count => $for_page[$dao->id]['contribution_page_id']));
+        $this->setDefaults(array(
+              'percentage_'.$count => $for_page[$dao->id]['percentage']));
+
+        $count++;
+      }
+      $count--; // because last iteration increments it to the next
+    }  else {
+      /** New progress bar*/
+      $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
+      );
+    }
+    
+    $this->addElement('hidden', 'contrib_count', $count);
+  }
+  
   function buildQuickForm() {
+  
     $this->add(
       'text', // field type
       'progressbar_name', // field name
@@ -35,7 +111,7 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
       'Goal amount', // field label
       true // is required
     );
-    $this->add(
+/*    $this->add(
       'select', // field type
       'contribution_page_1', // field name
       'Contribution page', // field label
@@ -47,12 +123,12 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
       'percentage_1', // field name
       'Percentage', // field label
       true // is required
-    );
+    );*/
+    
+    $this->fill_data();
     
     $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add more');
 
-    $this->addElement('hidden', 'contrib_count', '1');
-
     $this->addButtons(array(
       array(
         'type' => 'submit',
@@ -68,30 +144,66 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
   }
 
   function postProcess() {
+    $errorScope = CRM_Core_TemporaryErrorScope::useException();
+    if (isset($this->_id)) {
+      try {
+        $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;
 
-    $sql = "INSERT INTO civicrm_wci_progress_bar (name, starting_amount, goal_amount) 
-    VALUES ('" . $_REQUEST['progressbar_name'] . "','" . $_REQUEST['starting_amount'] . "','" . $_REQUEST['goal_amount'] . "')";
+        $transaction = new CRM_Core_Transaction();
+        CRM_Core_DAO::executeQuery($sql);
 
-    CRM_Core_DAO::executeQuery($sql);
-    try {
-      $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
-      for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
-        $page = 'contribution_page_' . (string)$i;
-        $perc = 'percentage_' . (string)$i;
+        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) 
+              VALUES ('" . $_REQUEST[$page] . "','" . $this->_id . "','" . $_REQUEST[$perc] . "')";
+          }
 
-        $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage) 
-        VALUES ('" . $_REQUEST[$page] . "','" . $progressbar_id . "','" . $_REQUEST[$perc] . "')";
-        
+          CRM_Core_DAO::executeQuery($sql);
+        endfor;
+        $transaction->commit();
+      }
+      catch (Exception $e) {
+        //TODO
+        print_r($e->getMessage());
+        $transaction->rollback();
+      }
+    
+    } else {
+      $sql = "INSERT INTO civicrm_wci_progress_bar (name, starting_amount, goal_amount) 
+      VALUES ('" . $_REQUEST['progressbar_name'] . "','" . $_REQUEST['starting_amount'] . "','" . $_REQUEST['goal_amount'] . "')";
+      try {
+        $transaction = new CRM_Core_Transaction();
         CRM_Core_DAO::executeQuery($sql);
-      endfor;
-    }    
-    catch (Exception $e) {
-      //TODO
-      print_r($e);
-    }      
+        $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
+        for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
+          $page = 'contribution_page_' . (string)$i;
+          $perc = 'percentage_' . (string)$i;
+
+          $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage) 
+          VALUES ('" . $_REQUEST[$page] . "','" . $progressbar_id . "','" . $_REQUEST[$perc] . "')";
+          
+          CRM_Core_DAO::executeQuery($sql);
+        endfor;
+        $transaction->commit();
+      }    
+      catch (Exception $e) {
+        //TODO
+        print_r($e->getMessage());
+        $transaction->rollback();
+      }
+      $elem = $this->getElement('contrib_count');
+      $elem->setValue('1');    
+    }
     parent::postProcess();
-    $elem = $this->getElement('contrib_count');
-    $elem->setValue('1');    
   }
 
   /**