public function banUser($id)
 {
     if ($this->user->banUser($id) !== 1) {
         Session::setError('something went wrong');
         Redirect::back();
     }
     Session::setMessage('Done');
     Redirect::to('/admin/users');
 }
 public function postAdd(MessageBindingModel $message)
 {
     $validator = $this->validateMessage(new Validation(), $message);
     if (!$validator->validate()) {
         Session::setError($validator->getErrors());
         Redirect::back();
     }
     $this->message->add($message->first_name, $message->last_name, $message->email, $message->phone, $message->content, $message->about, date("Y-m-d H:i:s"), false);
     Session::setMessage('Message is sent successfully');
     Redirect::to('');
 }
 public function post($id, $content)
 {
     $validator = new Validation();
     $validator->setRule('required', $content);
     if (!$validator->validate()) {
         Session::setError($validator->getErrors());
         Redirect::back();
     }
     if ($this->comment->add(Auth::getUserId(), $id, $content) !== 1) {
         Session::setError('something went wrong');
         Redirect::back();
     }
     Session::setMessage('Done');
     Redirect::to('/product/' . $id);
 }
 public function setRole($id, $role)
 {
     if (!in_array($role, array('admin', 'editor', 'user'))) {
         Session::setError('incorrect role');
         Redirect::back();
     }
     if ($role == 'user') {
         if ($this->user->deleteUserRole($id) === 0) {
             Session::setError('something went wrong');
             Redirect::back();
         }
         Session::setMessage('Done');
         Redirect::to('/admin/users');
     }
     if ($this->user->setRole($id, $role) !== 1) {
         Session::setError('something went wrong');
         Redirect::back();
     }
     Session::setMessage('Done');
     Redirect::to('/admin/users');
 }
 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');
 }
 public function postEdit($id, $name)
 {
     $validator = new Validation();
     $validator->setRule('required', $name, null, 'Name');
     if (!$validator->validate()) {
         Session::setError($validator->getErrors());
         Redirect::back();
     }
     $this->city->edit($id, $name);
     Session::setMessage('The city is edited');
     Redirect::to('');
 }
 public function delete($id)
 {
     if ($this->product->delete($id) !== 1) {
         Session::setError('can not delete this product');
         Redirect::back();
     }
     Session::setMessage('done');
     Redirect::to('');
 }
 public function delete($id)
 {
     /* @var $estate \Models\ViewModels\EstateViewModel */
     $estate = $this->estate->getEstate($id);
     /* @var $images \Models\ViewModels\ImageViewModel[] */
     $images = $this->image->getImagesByEstate($id);
     if ($this->estate->delete($id) !== 1) {
         Session::setError('something went wrong. try again');
         Redirect::back();
     }
     /* @var $imageController \Controllers\ImageController */
     $imageController = DependencyProvider::injectDependenciesToController(new ImageController());
     if (isset($estate->main_image_id)) {
         $mainImg = new ImageViewModel();
         $mainImg->id = $estate->main_image_id;
         $mainImg->name = $estate->image;
         $images[] = $mainImg;
     }
     $imageController->removeMultiple($images);
     Session::setMessage('The estate is deleted');
     Redirect::to('');
 }
 public function editProfile(UserModel $user, $new_password)
 {
     $validator = new Validation();
     $validator->setRule('required', $user->email, null, 'Email');
     $validator->setRule('required', $user->password, null, 'Current Password');
     $validator->setRule('email', $user->email, null, 'Email');
     if (!$validator->validate()) {
         Session::setError($validator->getErrors());
         Redirect::back();
     }
     /* @var $user \Models\UserModel */
     $userFromDb = $this->user->getUser(Auth::getUserId());
     if (!Common::verifyPassword($user->password, $userFromDb->password)) {
         Session::setError('Current password is not correct');
         Redirect::back();
     }
     if ($this->user->editUser(Auth::getUserId(), $user->email, Common::hashPassword($new_password)) !== 1) {
         Session::setError('Something is wrong. Try again.');
         Redirect::back();
     }
     Session::setMessage('The profile is edited');
     Redirect::to('');
 }
 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('');
 }
 public function postEdit($id, $name)
 {
     if ($this->category->edit($id, $name) !== 1) {
         Session::setError('something went wrong');
         Redirect::back();
     }
     Session::setMessage('done');
     Redirect::to('');
 }
 public function buy()
 {
     $totalSum = 0;
     $cart = Session::get('cart');
     $this->product->startTran();
     $productsFromCart = $this->getProductsFromCart($cart);
     foreach ($productsFromCart as $item) {
         if ($this->product->changeQuantity($item['id'], $item['cart_quantity']) !== 1) {
             $this->product->rollback();
             Session::setError('not enough available product');
             Redirect::back();
         }
         $totalSum += $item['price'] * $item['cart_quantity'];
     }
     $user = new User();
     if ($user->changeUserCash(Auth::getUserId(), $totalSum) !== 1) {
         $this->product->rollback();
         Session::setError('not enough money');
         Redirect::back();
     }
     foreach ($productsFromCart as $item) {
         if ($user->addProduct(Auth::getUserId(), $item['id'], $item['cart_quantity'], $item['price']) !== 1) {
             $this->product->rollback();
             Session::setError('something went wrong');
             Redirect::back();
         }
     }
     $this->product->commit();
     Session::remove('cart');
     Session::setMessage('Done');
     Redirect::to('user/cart');
 }