Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *Form processing handler
- *
- */
- class FormProcessHandler
- {
- static public $formException;
- static public function addItemToCart()
- {
- $id_product = FormValidationHandler::validateProduct();
- if($id_product)
- {
- // include('includes/class/Product.class.php');
- // include('includes/class/ProductsTable.class.php');
- $productCheck = new ProductsTable();
- $productCheck->id_product = $id_product;
- $rstProductCheck = $productCheck->searchByProductID();
- $rowProductCheck = $rstProductCheck->num_rows;
- if($rowProductCheck)
- {
- if(!@$_SESSION['cartProducts']) $_SESSION['cartProducts'][] = $id_product;
- if(!in_array($id_product, $_SESSION['cartProducts']))
- {
- $_SESSION['cartProducts'][] = $id_product;
- }
- return true;
- }
- }
- //self::$formException = 'addItemToCart.';
- $_SESSION['error'] = ADDITEMTOCART;
- return false;
- }
- static public function removeItemFromCart()
- {
- $id_product = FormValidationHandler::validateProduct();
- if(($id_product) && (in_array($id_product, $_SESSION['cartProducts'])))
- {
- $key = array_search($id_product, $_SESSION['cartProducts']);
- unset($_SESSION['cartProducts'][$key]);
- return true;
- }
- //self::$formException = 'removeItemFromCart.';
- $_SESSION['error'] = REMOVEITEMFROMCART;
- return false;
- }
- static public function logIn()
- {
- $email = FormValidationHandler::validateEmail();
- $pass = FormValidationHandler::validatePass();
- if($email && $pass)
- {
- include('includes/class/User.class.php');
- include('includes/class/UsersTable.class.php');
- $user = new UsersTable();
- $user->email = $email;
- $user->pass = $pass;
- $rstCheckLogin = $user->checkUser();
- if (@$rstCheckLogin->num_rows == 1)
- {
- echo "Encuentro resultados para LOGIN";
- $rowCheckLogin = $rstCheckLogin->fetch_assoc();
- setcookie("user",$rowCheckLogin['email'],time()+3600);
- return true;
- }
- }
- //self::$formException = 'logIn.';
- $_SESSION['error'] = BADLOGIN;
- return false;
- }
- static public function registerIn()
- {
- $name = FormValidationHandler::validateName();
- $surname = FormValidationHandler::validateSurname();
- $phone = FormValidationHandler::validatePhone();
- $address = FormValidationHandler::validateAddress();
- $email = FormValidationHandler::validateRegisterEmail();
- $pass = FormValidationHandler::validateRegisterPass();
- $dataArray = array(
- "Nombre" => $name,
- "Apellido" => $surname,
- "Teléfono" => $phone,
- "Dirección" => $address,
- "Email" => $email,
- "Password" => $pass
- );
- if(!array_search(false, $dataArray))
- {
- include('includes/class/User.class.php');
- include('includes/class/UsersTableUpdate.class.php');
- $user = new UsersTableUpdate();
- $user->name = $name;
- $user->last_name = $surname;
- $user->phone = $phone;
- $user->address = $address;
- $user->email = $email;
- $user->pass = $pass;
- $rstUserInsert = $user->insert();
- if(!is_string($rstUserInsert))
- {
- return true;
- }
- self::$formException = 'registerIn.'.$rstUserInsert;
- return false;
- }
- //self::$formException = 'registerIn.';
- $badInput = array_keys($dataArray, false);
- foreach($badInput as $key => $value )
- {
- $_SESSION['error'][] = $value;
- //self::$formException .= $value.".";
- }
- return false;
- }
- static public function searchIt()
- {
- $inputTarget = FormValidationHandler::validateTarget();
- if($inputTarget)
- {
- // $_SESSION['last_search'] = $inputTarget;
- //
- // include('includes/class/Product.class.php');
- // include('includes/class/ProductsTable.class.php');
- // include('includes/class/Category.class.php');
- // include('includes/class/CategoriesTable.class.php');
- //Loading Product objects
- $product = new ProductsTable();
- //Name search
- $product->name=$inputTarget;
- $rstGetByName = $product->searchByProductName();
- //Product Code search
- $product->product_code=$inputTarget;
- $rstGetByProductCode = $product->searchByProductCode();
- //Description search
- // $product->description=$inputTarget;
- // $rstGetByProductCode = $product->searchByDescription();
- //Loading Category objects
- $category = new CategoriesTable();
- //Name search
- $category->name=$inputTarget;
- $rstGetByCategoryName = $category->searchByCategoryName();
- //Evaluating results
- $searchOnProducts = array("Nombre de Producto" => $rstGetByName,
- "Código de Producto" => $rstGetByProductCode
- );
- $searchOnCategories = array("Nombre de Categoría" => $rstGetByCategoryName);
- $rstSearchTargetArray = array ("Productos" => $searchOnProducts,
- "Categorías" => $searchOnCategories
- );
- foreach($rstSearchTargetArray as $searchSection => $searchOnSection)
- {
- foreach ($searchOnSection as $category => $rstSearchArray)
- {
- if(!array_search(false, $searchOnSection))
- {
- $rstSearchArrayObj = $rstSearchArray->fetch_object();
- $_SESSION['searchResults'][$searchSection][$category] = $rstSearchArrayObj;
- }
- }
- }
- }
- if(@$_SESSION['searchResults']) return true;
- $_SESSION['error'] = BADTARGET;
- //self::$formException = 'searchIn.';
- return false;
- }
- static public function confirmOrder()
- {
- if(@$_COOKIE['user'])
- {
- include('includes/class/User.class.php');
- include('includes/class/UsersTable.class.php');
- $user = new UsersTable();
- $user->email = $_COOKIE['user'];
- $rstUser = $user->searchByEmail();
- $rstUserObj=$rstUser->fetch_object();
- $_COOKIE['id_user'] = $rstUserObj->id_user;
- self::setNewBill();
- return true;
- }
- $_SESSION['error'] = CONFIRMORDER;
- //self::$formException = 'confirmOrder.notLogged';
- return false;
- }
- static private function setNewBill()
- {
- include('includes/class/Bill.class.php');
- include('includes/class/BillsTableUpdate.class.php');
- include('includes/class/Order.class.php');
- include('includes/class/OrdersTableUpdate.class.php');
- include('includes/class/Stock.class.php');
- include('includes/class/StocksTableUpdate.class.php');
- $itemQuantity = FormValidationHandler::validateQuantity();
- $id_product = FormValidationHandler::validateProduct();
- $productQuantity = array($itemQuantity => $id_product);
- $billUpdate = new BillsTableUpdate();
- $billUpdate->id_user = $_COOKIE['id_user'];
- $billUpdate->insert();
- $rstBillUpdate = $billUpdate->lastBillbyUser();
- $_COOKIE['id_bill'] = $rstBillUpdate->num_rows;
- foreach($_SESSION['cartProducts'] as $id_product)
- {
- switch(array_search($id_product, $productQuantity))
- {
- case "0":
- $orderUpdate = new OrdersTableUpdate();
- $orderUpdate->id_product = $id_product;
- $orderUpdate->id_bill = $_COOKIE['id_bill'];
- $orderUpdate->insert();
- $rstOrderUpdate = $orderUpdate->lastOrderbyUser();
- $_COOKIE['id_order'] = $rstOrderUpdate->num_rows;
- $stockUpdate = new StocksTableUpdate();
- $stockUpdate->id_product = $id_product;
- $stockUpdate->id_order = $_COOKIE['id_order'];
- $stockUpdate->updateStock();
- break;
- default:
- $orderUpdate = new OrdersTableUpdate();
- $stockUpdate = new StocksTableUpdate();
- for($i=1;$i<=$itemQuantity;$i++)
- {
- $orderUpdate->id_product = $id_product;
- $orderUpdate->id_bill = $_COOKIE['id_bill'];
- $orderUpdate->insert();
- $rstOrderUpdate = $orderUpdate->lastOrderbyUser();
- $_COOKIE['id_order'] = $rstOrderUpdate->num_rows;
- $stockUpdate->id_product = $id_product;
- $stockUpdate->id_order = $_COOKIE['id_order'];
- $stockUpdate->updateStock();
- }
- break;
- }
- }
- $_SESSION['cartProducts'] = array();
- return true;
- }
- }
Add Comment
Please, Sign In to add comment