#29754 Progress bar listing page
authorJagadedes <jagadees.pillai@zyxware.com>
Wed, 1 Oct 2014 11:21:53 +0000 (16:51 +0530)
committerJagadedes <jagadees.pillai@zyxware.com>
Wed, 1 Oct 2014 11:21:53 +0000 (16:51 +0530)
CRM/Wci/DAO/ProgressBarFormula.php
CRM/Wci/Form/ProgressBar.php
CRM/Wci/Page/ProgressBarList.php
addmore.js

index 46d5ea7987faa018a316d8ce9f90fd07bf1475e8..56736ad3771c9e23c7e4c27030cac82c003ab323 100644 (file)
@@ -123,12 +123,12 @@ class CRM_WCI_DAO_ProgressBarFormula extends CRM_Core_DAO
    */
   static function getReferenceColumns()
   {
-    if (!self::$_links) {
+    /*if (!self::$_links) {
       self::$_links = array(
         new CRM_Core_EntityReference(self::getTableName() , 'progress_bar_id', 'civicrm_wci_progress_bar', 'id') ,
         new CRM_Core_EntityReference(self::getTableName() , 'contribution_page_id', 'civicrm_contribution_page', 'id') ,
       );
-    }
+    }*/
     return self::$_links;
   }
   /**
index 9d84ccecab42e6d4d66b7fc19056fbb1e3147981..1d337e134c2a1ce439a550cdd460a05d52536f75 100644 (file)
@@ -13,16 +13,16 @@ require_once 'CRM/Wci/DAO/ProgressBarFormula.php';
 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 FillData() {
-    $_id = CRM_Utils_Request::retrieve('id', 'Positive',
-      $this, FALSE, NULL, 'REQUEST'
-    );
-    if (isset($_id)) {
-      $query = "SELECT * FROM civicrm_wci_progress_bar where id=" . $_id;
+  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');
@@ -38,25 +38,57 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
               'goal_amount' => $con_page[$dao->id]['goal_amount']));
       }
        
-      $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $_id;
+      $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $this->_id;
       $params = array();
-      echo $query . '<br>';
+
       $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_WCI_DAO_ProgressBarFormula');
 
-      $count = 1;
-      echo count($dao);
       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() {
@@ -79,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
@@ -91,14 +123,12 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
       'percentage_1', // field name
       'Percentage', // field label
       true // is required
-    );
+    );*/
     
-    $this->FillData();
+    $this->fill_data();
     
     $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add more');
 
-    $this->addElement('hidden', 'contrib_count', '1');
-
     $this->addButtons(array(
       array(
         'type' => 'submit',
@@ -114,12 +144,42 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
   }
 
   function postProcess() {
-    if (isset($_id)){
+    $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;
+
+        $transaction = new CRM_Core_Transaction();
+        CRM_Core_DAO::executeQuery($sql);
+
+        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] . "')";
+          }
+
+          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'] . "')";
-      $errorScope = CRM_Core_TemporaryErrorScope::useException();
       try {
         $transaction = new CRM_Core_Transaction();
         CRM_Core_DAO::executeQuery($sql);
@@ -133,6 +193,7 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
           
           CRM_Core_DAO::executeQuery($sql);
         endfor;
+        $transaction->commit();
       }    
       catch (Exception $e) {
         //TODO
index e9f9716729041bcc7830d7bc69e753e64b3bd0a0..513eea2a4d6028b88112bbd60489363836b8ce01 100644 (file)
@@ -11,16 +11,13 @@ class CRM_Wci_Page_ProgressBarList extends CRM_Core_Page {
       // default to 'browse'
       $this, FALSE, 'browse'
     );
-    echo $action;
     // assign vars to templates
     $this->assign('action', $action);
     $id = CRM_Utils_Request::retrieve('id', 'Positive',
       $this, FALSE, 0
     );
 
-
     if ($action & CRM_Core_Action::UPDATE) {
-      echo "UPDATE clicked";
       $controller = new CRM_Core_Controller_Simple('CRM_Wci_Form_ProgressBar',
         'Edit Progressbar',
         CRM_Core_Action::UPDATE
@@ -28,12 +25,22 @@ class CRM_Wci_Page_ProgressBarList extends CRM_Core_Page {
       $controller->set('id', $id);
       $controller->process();
       return $controller->run();
-//      return;
     } elseif ($action & CRM_Core_Action::DELETE) {
-      echo "delete clicked";
-//      return;
-    } 
-  
+      try {
+        $transaction = new CRM_Core_Transaction();
+        $sql = "DELETE FROM civicrm_wci_progress_bar_formula where progress_bar_id = " . $id;
+        CRM_Core_DAO::executeQuery($sql);
+        
+        $sql = "DELETE FROM civicrm_wci_progress_bar where id = " . $id;
+        CRM_Core_DAO::executeQuery($sql);
+        $transaction->commit();
+      }
+      catch (Exception $e) {
+        //TODO
+        print_r($e->getMessage());
+        $transaction->rollback();      
+      } 
+    }
     // Example: Set the page-title dynamically; alternatively, declare a static title in xml/Menu/*.xml
     CRM_Utils_System::setTitle(ts('ProgressBarList'));
 
@@ -48,8 +55,7 @@ class CRM_Wci_Page_ProgressBarList extends CRM_Core_Page {
     while ($dao->fetch()) {
       $con_page[$dao->id] = array();
       CRM_Core_DAO::storeValues($dao, $con_page[$dao->id]);
-      //print_r($con_page[$dao->id]['starting_amount']);
-      
+     
       $action = array_sum(array_keys($this->actionLinks())); 
       //build the normal action links.
       $con_page[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
@@ -63,12 +69,6 @@ class CRM_Wci_Page_ProgressBarList extends CRM_Core_Page {
     if (isset($con_page)) {
       $this->assign('rows', $con_page);
     }
-
-/*
-    $query = "SELECT * FROM civicrm_wci_progress_bar";
-    $dao = CRM_Core_DAO::singleValueQuery($query);
-      print_r($dao);
-*/
     return parent::run();
   }
   
@@ -76,8 +76,7 @@ function &actionLinks() {
     // check if variable _actionsLinks is populated
     if (!isset(self::$_actionLinks)) {
       // helper variable for nicer formatting
-      $deleteExtra = ts('Are you sure you want to delete this Contribution page?');
-      $copyExtra = ts('Are you sure you want to make a copy of this Contribution page?');
+      $deleteExtra = ts('Are you sure you want to delete this Progressbar page?');
 
       self::$_actionLinks = array(
         CRM_Core_Action::UPDATE => array(
index 870c18376eab07cf2d733908744a6422972464cc..afb31d2976b058384100d08d13b1d8f0d9e3b253 100644 (file)
@@ -77,7 +77,8 @@ cj(function ( $ ) {
 
     $('#crm-section-con-'+ rem_name_ar[1] +'').remove();
     $('#crm-section-per-'+ rem_name_ar[1] +'').remove();
-
+    var count = parseInt($('input[name=contrib_count]').val());
+    count--;
+    $('input[name=contrib_count]').val(count);
   });
 });