public function getEdit($id)
 {
     $result['isEditor'] = Auth::isUserInRole(array('editor', 'admin'));
     $result['isAdmin'] = Auth::isUserInRole(array('admin'));
     if ($result['isEditor']) {
         $result = array('product' => $this->product->getProductWitnUnavailable($id));
     } else {
         $result = array('product' => $this->product->getProduct($id));
     }
     $result['title'] = 'Shop';
     $result['action'] = '/product/edit/' . $result['product']['id'];
     $result['submit'] = 'edit';
     $categories = $this->category->getCategories();
     foreach ($categories as $c) {
         $currentCategory = array();
         $currentCategory['text'] = $c['name'];
         $currentCategory['options'] = array('value' => $c['id']);
         if ($id == $c['id']) {
             $currentCategory['options']['selected'] = 'true';
         }
         $result['categories'][] = $currentCategory;
     }
     View::make('product.add', $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')->render();
 }
 public function sellProduct($id, $quantity, $upid)
 {
     $this->user->startTran();
     if ($this->user->changeProductQuantity(Auth::getUserId(), $id, $quantity, $upid) !== 1) {
         Session::setError('not enough products');
         $this->user->rollback();
         Redirect::back();
     }
     $userProduct = $this->user->getProduct(Auth::getUserId(), $id, $upid);
     if ($userProduct['quantity'] < 1) {
         if ($this->user->deleteProduct(Auth::getUserId(), $id, $upid) !== 1) {
             Session::setError('something went wrong');
             $this->user->rollback();
             Redirect::back();
         }
     }
     $soldProducts = $this->product->getProduct($id);
     if ($this->product->addQuantity($soldProducts['id'], $quantity) !== 1) {
         Session::setError('something went wrong');
         $this->user->rollback();
         Redirect::back();
     }
     if ($this->user->addCash(Auth::getUserId(), $soldProducts['price'] * $quantity) !== 1) {
         Session::setError('something went wrong');
         $this->user->rollback();
         Redirect::back();
     }
     $this->user->commit();
     Session::setMessage('You sold ' . $quantity . ' of ' . $userProduct['name']);
     Redirect::to('/user/' . Auth::getUserId() . '/products');
 }
 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;
 }