public function getCategory($id)
 {
     $result['categories'] = $this->category->getCategories();
     $result['title'] = 'Shop';
     $result['currentCategory'] = $id;
     $result['isEditor'] = Auth::isUserInRole(array('editor', 'admin'));
     $result['isAdmin'] = Auth::isUserInRole(array('admin'));
     if ($result['isEditor']) {
         $result['products'] = $this->product->getProductsForCategoryWitnUnavailable($id);
     } else {
         $result['products'] = $this->product->getProductsForCategory($id);
     }
     $all_promotion = $this->promotion->getHighestActivePromotion();
     foreach ($result['products'] as $k => $p) {
         $productPromotion = max($all_promotion['discount'], $p['discount'], $p['category_discount']);
         if (is_numeric($productPromotion)) {
             $result['products'][$k]['promotion_price'] = $p['price'] - $p['price'] * ($productPromotion / 100);
         }
     }
     View::make('index', $result);
     if (Auth::isAuth()) {
         View::appendTemplateToLayout('topBar', 'top_bar/user');
     } else {
         View::appendTemplateToLayout('topBar', 'top_bar/guest');
     }
     View::appendTemplateToLayout('header', 'includes/header')->appendTemplateToLayout('footer', 'includes/footer')->appendTemplateToLayout('catMenu', 'side_bar/category_menu')->render();
 }
 public function getProduct($id)
 {
     $result['comments'] = $this->comment->getCommentsByProduct($id);
     $result['categories'] = $this->category->getCategories();
     $result['isEditor'] = Auth::isUserInRole(array('editor', 'admin'));
     $result['isAdmin'] = Auth::isUserInRole(array('admin'));
     if ($result['isEditor']) {
         $result['product'] = $this->product->getProductWitnUnavailable($id);
     } else {
         $result['product'] = $this->product->getProduct($id);
     }
     $all_promotion = $this->promotion->getHighestActivePromotion();
     $productPromotion = max($all_promotion['discount'], $result['product']['discount'], $result['product']['category_discount']);
     if (is_numeric($productPromotion)) {
         $result['product']['promotion_price'] = $result['product']['price'] - $result['product']['price'] * ($productPromotion / 100);
     }
     $result['title'] = 'Shop';
     $result['currentCategory'] = $result['product']['category_id'];
     View::make('product', $result);
     if (Auth::isAuth()) {
         View::appendTemplateToLayout('topBar', 'top_bar.user');
     } else {
         View::appendTemplateToLayout('topBar', 'top_bar.guest');
     }
     View::appendTemplateToLayout('header', 'includes.header')->appendTemplateToLayout('footer', 'includes.footer')->appendTemplateToLayout('catMenu', 'side_bar.category_menu')->render();
 }
 /**
  *@HttpPost()
  *@ValidateAntiForgeryToken()
  */
 public function create(CreatePromotionBindingModel $newPromotion)
 {
     if ($newPromotion == null || !$newPromotion->isValid()) {
         $_SESSION['warrning'] = 'Invalid promotion data';
         return new RedirectActionResult('promotions/newpromotion');
     }
     $promotion = new Promotion($newPromotion->getName(), $newPromotion->getStartDate(), $newPromotion->getEndDate(), $newPromotion->getDiscount(), $newPromotion->getUserCriteriaId() == 0 ? null : $newPromotion->getUserCriteriaId(), null, $newPromotion->getProductId() == 0 ? null : $newPromotion->getProductId(), $newPromotion->getCategoryId() == 0 ? null : $newPromotion->getCategoryId());
     $userId = $this->currentUser();
     if (!$this->roleProvider->isAdmin($userId) && !$this - roleProvider()->isEditor($userId)) {
         $promotion->setUserId($userId);
     }
     $this->shopData->getPromotionRepository()->addPromotion($promotion);
     return new RedirectActionResult('home/index');
 }
 public function postAdd(PromotionModel $promotion)
 {
     $validator = new Validation();
     $validator->setRule('required', $promotion->discount, null, 'discount');
     $validator->setRule('required', $promotion->date, null, 'date');
     $validator->setRule('date', $promotion->date, null, 'date');
     if (!$validator->validate()) {
         Session::setError($validator->getErrors()[0]);
         Redirect::back();
     }
     if ($this->promotion->add($promotion->discount, $promotion->date, $promotion->category_id == 0 ? null : $promotion->category_id, $promotion->product_id == 0 ? null : $promotion->product_id) !== 1) {
         Session::setError('something went wrong');
         Redirect::back();
     }
     Session::setMessage('done');
     Redirect::to('');
 }
 private function getProductsFromCart($cart)
 {
     $all_promotion = $this->promotion->getHighestActivePromotion();
     $productsFromCart = array();
     foreach ($cart as $id => $q) {
         if ($currentProduct = $this->product->getProduct($id)) {
             $productPromotion = max($all_promotion['discount'], $currentProduct['discount'], $currentProduct['category_discount']);
             if (is_numeric($productPromotion)) {
                 $currentProduct['price'] = $currentProduct['price'] - $currentProduct['price'] * ($productPromotion / 100);
             }
             $currentProduct['cart_quantity'] = $q['quantity'];
             $productsFromCart[] = $currentProduct;
         }
     }
     return $productsFromCart;
 }
Esempio n. 6
0
 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 addPromotion(Promotion $promotion)
 {
     $result = $this->db->prepare("\n\t\t\tINSERT INTO promotion(name, start_date, end_date, discount, user_criteria_id, user_id, product_id, category_id)\n\t\t\tVALUES(?, ?, ?, ?, ?, ?, ?, ?)\n\t\t");
     $result->execute([$promotion->getName(), $promotion->getStartDate(), $promotion->getEndDate(), $promotion->getDiscount(), $promotion->getUserCriteriaId(), $promotion->getUserId(), $promotion->getProductId(), $promotion->getCategoryId()]);
 }