public function edit(Product $product) { $query = "Update products Set name = ?, categoryId = ?, price = ?, quantity = ?, editorId = ?\n WHERE products.id = ?"; $params = [$product->getName(), $product->getCategoryId(), $product->getPrice(), $product->getQuantity(), $product->getEditorId(), $product->getId()]; $this->db->query($query, $params); $result = $this->db->row(); return $result; }
public function removeFromCart() { $cart = \Lib\Session::get('cart'); $db = Register::get('db'); $product_id = $this->http->get('product_id'); $product_quantity = $this->http->get('product_quantity'); if ($product_quantity < 1) { Location::To(URL . 'cart/show'); } $product = new Product($product_id); if (!empty($cart)) { foreach ($cart as $p) { if ($p['product_id'] == $product_id) { $orders = new Order($p['order_id']); $quantity = $orders->getQuantity(); if ($quantity >= $product_quantity) { $orders->setQuantity($quantity - $product_quantity); $p_q = $product->getQuantity(); $product->setQuantity($p_q + $product_quantity); $product->changeProductSold(-$product_quantity); $db->query('START TRANSACTION'); if ($product->writeData(true) && $orders->writeData(true)) { $db->query('COMMIT'); } else { $db->query('ROLLBACK'); } if ($orders->getQuantity() == 0) { $orders->delete(); } } } } } $this->ActNumberOfProducts(); Location::To(URL . 'cart/show'); }
private function change($http) { $id = $http->post('id'); //echo print_r($http); //return; if (!empty($id)) { $product_m = new Product($http->post('id')); $product_m->setCategory($http->post("category")); $product_m->setSubCategory($http->post("sub_category")); $product_m->setName($http->post("name")); $product_m->setDescription($http->post("product_description")); $price = $http->post("price"); $tab = explode(",", $price); if (count($tab) == 2) { $price = $tab[0] . "." . $tab[1]; } $product_m->setPrice($price); $product_m->setQuantity($http->post("quantity")); $id_img_to_delete = $http->post("main_img_to_delete"); $main_img_delated = false; if ($product_m->getId() == $id_img_to_delete) { $images_location = 'views/public/'; if (file_exists($images_location . $product_m->getImage())) { unlink($images_location . $product_m->getImage()); $product_m->setImage(''); $main_img_delated = true; } } $additional_img_m = new ProductAdditionalImages(); $lenght = $http->post('length'); if ($lenght) { for ($start = 0; $start < $lenght; $start++) { $id = $http->post('img_to_del_nr_' . $start); $images_location = 'views/public/'; if (file_exists($images_location . $additional_img_m->getAdditionalImage($id))) { unlink($images_location . $additional_img_m->getAdditionalImage($id)); } $additional_img_m->deleteAdditionalImages($id); } } $files = $_FILES['files']['name'][0]; $images_location = 'views/public/img/produkty/'; $product_additional_img = new ProductAdditionalImages(); $test = $_FILES['files']['name']; if (!empty($test)) { foreach ($_FILES['files']['name'] as $key => $value) { $time = time(); $random = rand(1000, 10000); $new_name = $time . $random . $_FILES['files']['name'][$key]; $tmp = $_FILES['files']['tmp_name'][$key]; $to_save = $images_location . $new_name; move_uploaded_file($tmp, $to_save); if ($key == 0 && ($main_img_delated || $product_m->getImage() == '')) { $product_m->setImage('img/produkty/' . $new_name); } else { $product_additional_img->setAdditionalImages($product_m->getId(), 'img/produkty/' . $new_name); } } } $product_m->writeData(true); $to_delete = $http->post('to_delete'); if ($to_delete) { $product_additional_fields_m = new ProductAdditional(); for ($start = 0; $start < $to_delete; $start++) { $value = $http->post('field_id_to_delete_' . $start); $product_additional_fields_m->deleteAdditionalFields($value); } } $num = $http->post('num_of_variables'); if ($num) { $product_additional_info = new ProductAdditional(); for ($i = 0; $i < $num; $i++) { $var = $http->post('product_variable_nr_' . $i); $val = $http->post('product_value_nr_' . $i); $product_additional_info->setAdditionalFields($http->post('id'), $var, $val); } } $to_change = $http->post('num_of_variables_to_change'); if ($to_change) { $product_additional_info = new ProductAdditional(); for ($i = 0; $i < $to_change; $i++) { $var = $http->post('ch_product_variable_nr_' . $i); $val = $http->post('ch_product_value_nr_' . $i); $product_additional_info->changeAdditionalFields($http->post('id_ch_product_variable_nr_' . $i), $var, $val); } } $promo = $http->post("promo"); if ($promo == 'false') { $promotions = new Promotion(); $promotions->deletePromotionByProductId($http->post('id')); } if ($promo == 'true') { $promotions = new Promotions(); if ($promotions->isPromo($http->post('id'))) { $percent = $http->post("percent"); if ($percent != 0 && ($percent > 0 && $percent < 100)) { $promo = new Promotions($http->post('id')); $pro = $promo->getPromotion(); $pro->setPercent($http->post("percent") / 100); $pro->writeData(true); } } else { $percent = $http->post("percent"); if ($percent != 0 && ($percent > 0 && $percent < 100)) { $promotions = new Promotion(); $promotions->setProductId($http->post('id')); $promotions->setPercent($http->post("percent") / 100); $promotions->writeData(); } } } $return = array(); $return['id'] = $product_m->getId(); $return['name'] = $product_m->getName(); $return['img'] = $product_m->getImage(); $return['price'] = $product_m->getPrice(); $return['quantity'] = $product_m->getQuantity(); return $return; } }
public function updateProduct(Product $product) { $result = $this->db->prepare("\n\t\t\tUPDATE product\n\t\t\tSET name = ?,\n\t\t\t\tquantity = ?,\n\t\t\t\tdescription = ?,\n\t\t\t\tcategory_id = ?\n\t\t\tWHERE id = ?\n\t\t"); $result->execute([$product->getName(), $product->getQuantity(), $product->getDescription(), $product->getCategoryId(), $product->getId()]); }