Guest User

Untitled

a guest
Jul 8th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.03 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  *Form processing handler
  5.  *
  6.  */
  7.  
  8. class FormProcessHandler
  9. {
  10.     static public $formException;
  11.    
  12.     static public function addItemToCart()
  13.     {
  14.         $id_product = FormValidationHandler::validateProduct();
  15.  
  16.         if($id_product)
  17.         {
  18. //            include('includes/class/Product.class.php');
  19. //            include('includes/class/ProductsTable.class.php');
  20.  
  21.             $productCheck = new ProductsTable();
  22.             $productCheck->id_product = $id_product;
  23.             $rstProductCheck = $productCheck->searchByProductID();
  24.             $rowProductCheck = $rstProductCheck->num_rows;
  25.  
  26.             if($rowProductCheck)
  27.             {
  28.                 if(!@$_SESSION['cartProducts']) $_SESSION['cartProducts'][] = $id_product;
  29.  
  30.                 if(!in_array($id_product, $_SESSION['cartProducts']))
  31.                 {
  32.                     $_SESSION['cartProducts'][] = $id_product;
  33.                 }
  34.                 return true;
  35.             }
  36.         }
  37.  
  38.         //self::$formException = 'addItemToCart.';
  39.         $_SESSION['error'] = ADDITEMTOCART;
  40.         return false;
  41.     }
  42.  
  43.     static public function removeItemFromCart()
  44.     {
  45.         $id_product = FormValidationHandler::validateProduct();
  46.  
  47.         if(($id_product) && (in_array($id_product, $_SESSION['cartProducts'])))
  48.         {
  49.             $key = array_search($id_product, $_SESSION['cartProducts']);  
  50.             unset($_SESSION['cartProducts'][$key]);
  51.             return true;
  52.         }
  53.  
  54.         //self::$formException = 'removeItemFromCart.';
  55.         $_SESSION['error'] = REMOVEITEMFROMCART;
  56.         return false;
  57.     }
  58.    
  59.     static public function logIn()
  60.     {
  61.         $email = FormValidationHandler::validateEmail();
  62.         $pass = FormValidationHandler::validatePass();
  63.        
  64.         if($email && $pass)
  65.         {
  66.             include('includes/class/User.class.php');
  67.             include('includes/class/UsersTable.class.php');
  68.  
  69.             $user = new UsersTable();
  70.             $user->email = $email;
  71.             $user->pass = $pass;
  72.             $rstCheckLogin = $user->checkUser();
  73.  
  74.             if (@$rstCheckLogin->num_rows == 1)
  75.             {
  76.                 echo "Encuentro resultados para LOGIN";
  77.                 $rowCheckLogin = $rstCheckLogin->fetch_assoc();
  78.                 setcookie("user",$rowCheckLogin['email'],time()+3600);
  79.                 return true;
  80.             }
  81.         }
  82.    
  83.         //self::$formException = 'logIn.';
  84.         $_SESSION['error'] = BADLOGIN;
  85.         return false;
  86.     }    
  87.        
  88.     static public function registerIn()
  89.     {
  90.         $name = FormValidationHandler::validateName();
  91.         $surname = FormValidationHandler::validateSurname();
  92.         $phone = FormValidationHandler::validatePhone();
  93.         $address = FormValidationHandler::validateAddress();
  94.         $email = FormValidationHandler::validateRegisterEmail();
  95.         $pass = FormValidationHandler::validateRegisterPass();
  96.        
  97.         $dataArray = array(
  98.             "Nombre"    => $name,
  99.             "Apellido" => $surname,
  100.             "Teléfono"   => $phone,
  101.             "Dirección" => $address,
  102.             "Email"   => $email,
  103.             "Password"    => $pass
  104.         );
  105.        
  106.         if(!array_search(false, $dataArray))
  107.         {
  108.             include('includes/class/User.class.php');
  109.             include('includes/class/UsersTableUpdate.class.php');
  110.  
  111.             $user = new UsersTableUpdate();
  112.            
  113.             $user->name = $name;
  114.             $user->last_name = $surname;
  115.             $user->phone = $phone;
  116.             $user->address = $address;      
  117.             $user->email = $email;
  118.             $user->pass = $pass;
  119.            
  120.             $rstUserInsert = $user->insert();
  121.  
  122.             if(!is_string($rstUserInsert))
  123.             {
  124.                 return true;
  125.             }
  126.            
  127.             self::$formException = 'registerIn.'.$rstUserInsert;
  128.             return false;
  129.         }
  130.        
  131.         //self::$formException = 'registerIn.';
  132.        
  133.         $badInput = array_keys($dataArray, false);
  134.         foreach($badInput as $key => $value )
  135.         {
  136.             $_SESSION['error'][] = $value;
  137.             //self::$formException .= $value.".";
  138.         }
  139.         return false;
  140.     }
  141.    
  142.     static public function searchIt()
  143.     {
  144.         $inputTarget = FormValidationHandler::validateTarget();
  145.  
  146.         if($inputTarget)
  147.         {
  148. //            $_SESSION['last_search'] = $inputTarget;
  149. //
  150. //            include('includes/class/Product.class.php');
  151. //            include('includes/class/ProductsTable.class.php');
  152. //            include('includes/class/Category.class.php');
  153. //            include('includes/class/CategoriesTable.class.php');
  154.              //Loading Product objects
  155.             $product = new ProductsTable();
  156.  
  157.             //Name search
  158.             $product->name=$inputTarget;
  159.             $rstGetByName = $product->searchByProductName();
  160.             //Product Code search
  161.             $product->product_code=$inputTarget;
  162.             $rstGetByProductCode = $product->searchByProductCode();
  163.             //Description search
  164.     //        $product->description=$inputTarget;
  165.     //        $rstGetByProductCode = $product->searchByDescription();
  166.  
  167.             //Loading Category objects
  168.             $category = new CategoriesTable();
  169.             //Name search
  170.             $category->name=$inputTarget;
  171.             $rstGetByCategoryName = $category->searchByCategoryName();    
  172.  
  173.             //Evaluating results
  174.             $searchOnProducts = array("Nombre de Producto" => $rstGetByName,
  175.                                       "Código de Producto" => $rstGetByProductCode
  176.                                 );
  177.             $searchOnCategories = array("Nombre de Categoría" => $rstGetByCategoryName);
  178.  
  179.             $rstSearchTargetArray = array ("Productos" => $searchOnProducts,
  180.                                            "Categorías" => $searchOnCategories
  181.                                         );
  182.  
  183.             foreach($rstSearchTargetArray as $searchSection => $searchOnSection)
  184.             {
  185.                 foreach ($searchOnSection as $category => $rstSearchArray)
  186.                 {
  187.                     if(!array_search(false, $searchOnSection))
  188.                     {
  189.                         $rstSearchArrayObj = $rstSearchArray->fetch_object();
  190.                         $_SESSION['searchResults'][$searchSection][$category] = $rstSearchArrayObj;
  191.                     }
  192.                 }    
  193.             }
  194.         }
  195.  
  196.         if(@$_SESSION['searchResults']) return true;
  197.        
  198.         $_SESSION['error'] = BADTARGET;
  199.         //self::$formException = 'searchIn.';
  200.         return false;
  201.     }
  202.    
  203.     static public function confirmOrder()
  204.     {
  205.         if(@$_COOKIE['user'])
  206.         {
  207.             include('includes/class/User.class.php');
  208.             include('includes/class/UsersTable.class.php');
  209.            
  210.             $user = new UsersTable();
  211.  
  212.             $user->email = $_COOKIE['user'];
  213.             $rstUser = $user->searchByEmail();
  214.             $rstUserObj=$rstUser->fetch_object();
  215.             $_COOKIE['id_user'] = $rstUserObj->id_user;
  216.  
  217.             self::setNewBill();
  218.             return true;
  219.         }
  220.        
  221.         $_SESSION['error'] = CONFIRMORDER;
  222.         //self::$formException = 'confirmOrder.notLogged';
  223.         return false;
  224.     }    
  225.    
  226.     static private function setNewBill()
  227.     {
  228.         include('includes/class/Bill.class.php');
  229.         include('includes/class/BillsTableUpdate.class.php');
  230.         include('includes/class/Order.class.php');
  231.         include('includes/class/OrdersTableUpdate.class.php');
  232.         include('includes/class/Stock.class.php');
  233.         include('includes/class/StocksTableUpdate.class.php');
  234.  
  235.         $itemQuantity = FormValidationHandler::validateQuantity();
  236.         $id_product = FormValidationHandler::validateProduct();
  237.        
  238.         $productQuantity = array($itemQuantity => $id_product);
  239.        
  240.         $billUpdate = new BillsTableUpdate();
  241.        
  242.         $billUpdate->id_user = $_COOKIE['id_user'];
  243.         $billUpdate->insert();
  244.         $rstBillUpdate = $billUpdate->lastBillbyUser();
  245.         $_COOKIE['id_bill'] = $rstBillUpdate->num_rows;
  246.  
  247.         foreach($_SESSION['cartProducts'] as $id_product)
  248.         {        
  249.             switch(array_search($id_product, $productQuantity))
  250.             {
  251.                 case "0":
  252.  
  253.                     $orderUpdate = new OrdersTableUpdate();
  254.  
  255.                     $orderUpdate->id_product = $id_product;
  256.                     $orderUpdate->id_bill = $_COOKIE['id_bill'];
  257.                     $orderUpdate->insert();
  258.                     $rstOrderUpdate = $orderUpdate->lastOrderbyUser();
  259.                     $_COOKIE['id_order'] = $rstOrderUpdate->num_rows;
  260.  
  261.                     $stockUpdate = new StocksTableUpdate();
  262.  
  263.                     $stockUpdate->id_product = $id_product;
  264.                     $stockUpdate->id_order = $_COOKIE['id_order'];
  265.                     $stockUpdate->updateStock();
  266.                    
  267.                     break;
  268.                
  269.                default:
  270.                    
  271.                    $orderUpdate = new OrdersTableUpdate();
  272.                    $stockUpdate = new StocksTableUpdate();
  273.                    
  274.                    for($i=1;$i<=$itemQuantity;$i++)
  275.                    {
  276.                         $orderUpdate->id_product = $id_product;
  277.                         $orderUpdate->id_bill = $_COOKIE['id_bill'];
  278.                         $orderUpdate->insert();
  279.                         $rstOrderUpdate = $orderUpdate->lastOrderbyUser();
  280.                         $_COOKIE['id_order'] = $rstOrderUpdate->num_rows;
  281.  
  282.                         $stockUpdate->id_product = $id_product;
  283.                         $stockUpdate->id_order = $_COOKIE['id_order'];
  284.                         $stockUpdate->updateStock();
  285.                    }
  286.                    
  287.                    break;
  288.             }
  289.         }
  290.        
  291.         $_SESSION['cartProducts'] = array();
  292.         return true;
  293.     }
  294. }
Add Comment
Please, Sign In to add comment