diff --git a/activites/Admin/Admin.php b/activites/Admin/Admin.php new file mode 100644 index 0000000..d665b35 --- /dev/null +++ b/activites/Admin/Admin.php @@ -0,0 +1,79 @@ +checkAdmin(); + $this->currentDomain = CURRENT_DOMAIN; + $this->basePath = BASE_PATH; + } + + + public function redirect($url){ + + header("Location: ". trim($this->currentDomain, '/ ') . '/' . trim($url, '/ ')); + exit; + + } + public function redirectBack() + { + header("Location: ". $_SERVER['HTTP_REFERER']); + exit; + } + + + // saveImage($_FILES['image'], 'post-image', 'logo'); + // saveImage($_FILES['image'], 'post-image'); + protected function saveImage($image, $imagePath, $imageName = null) + { + + if($imageName) + { + $extension = explode('/', $image['type'])[1]; + $imageName = $imageName . '.' . $extension; + } + else{ + $extension = explode('/', $image['type'])[1]; + $imageName = date("Y-m-d-H-i-s"). '.' . $extension; + } + + $imageTemp = $image['tmp_name']; + $imagePath = 'public/' . $imagePath . '/'; + if(is_uploaded_file($imageTemp)) + { + if(move_uploaded_file($imageTemp, $imagePath . $imageName)) + { + return $imagePath . $imageName; + } + else{ + return false; + + } + } + else{ + return false; + } + + } + + + // removeImage('/public/image/hassan.png/'); + protected function removeImage($path) + { + // $path = trim($this->basePath, '/ ') . '/' . trim($path, '/ '); + $path = trim($path, '/ '); + if(file_exists($path)){ + unlink($path); + } + + } + + + +} \ No newline at end of file diff --git a/activites/Admin/Banner.php b/activites/Admin/Banner.php new file mode 100644 index 0000000..035a446 --- /dev/null +++ b/activites/Admin/Banner.php @@ -0,0 +1,74 @@ +select('SELECT * FROM banners ORDER BY `id` DESC'); + require_once(BASE_PATH . '/template/admin/banners/index.php'); + } + + public function create() + { + + require_once(BASE_PATH . '/template/admin/banners/create.php'); + + } + + public function store($request) + { + $db = new DataBase(); + $request['image'] = $this->saveImage($request['image'], 'banner-image'); + if($request['image']) + { + $db->insert('banners', array_keys($request), $request); + $this->redirect('admin/banner'); + } + else{ + $this->redirect('admin/banner'); + } + + } + + public function edit($id) + { + $db = new DataBase(); + $banner = $db->select('SELECT * FROM banners WHERE id = ?;', [$id])->fetch(); + require_once(BASE_PATH . '/template/admin/banners/edit.php'); + } + + public function update($request, $id) + { + $db = new DataBase(); + { + if($request['image']['tmp_name'] != null) + { + $banner = $db->select('SELECT * FROM banners WHERE id = ?;', [$id])->fetch(); + $this->removeImage($banner['image']); + $request['image'] = $this->saveImage($request['image'], 'banner-image'); + } + + else{ + unset($request['image']); + } + $db->update('banners', $id , array_keys($request), $request); + $this->redirect('admin/banner'); + } + + + } + + public function delete($id) + { + $db = new DataBase(); + $banner = $db->select('SELECT * FROM banners WHERE id = ?;', [$id])->fetch(); + $this->removeImage($banner['image']); + $db->delete('banners', $id); + $this->redirectBack(); + } +} \ No newline at end of file diff --git a/activites/Admin/Category.php b/activites/Admin/Category.php new file mode 100644 index 0000000..c40417e --- /dev/null +++ b/activites/Admin/Category.php @@ -0,0 +1,51 @@ +select("SELECT * FROM categories"); + require_once (BASE_PATH . '/template/admin/category/index.php'); + } + + + public function create() + { + require_once (BASE_PATH . '/template/admin/category/create.php'); + } + + public function store($request){ + $db = new Database(); + $db->insert('categories', array_keys($request), $request); + $this->redirect('admin/category'); + } + + public function edit($id) + { + $db = new Database(); + $category = $db->select("SELECT * FROM categories WHERE id = ?", [$id])->fetch(); + require_once (BASE_PATH . '/template/admin/category/edit.php'); + } + + public function update($request, $id) + { + $db = new Database(); + $db->update('categories', $id, array_keys($request), $request); + $this->redirect('admin/category'); + } + + public function delete($id) + { + $db = new Database(); + $db->delete('categories', $id); + $this->redirectBack(); + } + + +} \ No newline at end of file diff --git a/activites/Admin/Comment.php b/activites/Admin/Comment.php new file mode 100644 index 0000000..701434c --- /dev/null +++ b/activites/Admin/Comment.php @@ -0,0 +1,39 @@ +select('SELECT comments.*, posts.title AS post_title, users.email AS email FROM comments LEFT JOIN posts ON comments.post_id = posts.id LEFT JOIN users ON comments.user_id = users.id ORDER BY `id` DESC'); + $unseenComments = $db->select('SELECT * FROM comments WHERE status = ?', ['unseen']); + foreach($unseenComments as $comment){ + $db->update('comments', $comment['id'], ['status'], ['seen']); + } + require_once(BASE_PATH . '/template/admin/comments/index.php'); + } + + public function changeStatus($id) + { + $db = new DataBase(); + $comment = $db->select('SELECT * FROM comments WHERE id = ?;', [$id])->fetch(); + if(empty($comment)){ + $this->redirectBack(); + } + if($comment['status'] == 'seen'){ + $db->update('comments', $id, ['status'], ['approved']); + } + else{ + $db->update('comments', $id, ['status'], ['seen']); + } + $this->redirectBack(); + } + + + + +} \ No newline at end of file diff --git a/activites/Admin/Dashboard.php b/activites/Admin/Dashboard.php new file mode 100644 index 0000000..2e6411c --- /dev/null +++ b/activites/Admin/Dashboard.php @@ -0,0 +1,34 @@ +select('SELECT COUNT(*) FROM `posts` ;')->fetch(); + $postsViews = $db->select('SELECT SUM(view) FROM `posts` ;')->fetch(); + + $commentsCount = $db->select('SELECT COUNT(*) FROM `comments` ;')->fetch(); + $commentsUnseenCount = $db->select("SELECT COUNT(*) FROM `comments` WHERE `status` = 'unseen' ;")->fetch(); + $commentsApprovedCount = $db->select("SELECT COUNT(*) FROM `comments` WHERE `status` = 'approved' ;")->fetch(); + $userCount = $db->select("SELECT COUNT(*) FROM `users` WHERE `permission` = 'user';")->fetch(); + $adminCount = $db->select("SELECT COUNT(*) FROM `users` WHERE `permission` = 'admin' ;")->fetch(); + $categoryCount = $db->select("SELECT COUNT(*) FROM `categories` ;")->fetch(); + $postsWithView = $db->select('SELECT * FROM `posts` ORDER BY `view` DESC LIMIT 0,5 ;'); + + + $postsComments = $db->select("SELECT `posts`.`id`, `posts`.`title`, COUNT(`comments`.`post_id`) AS 'comment_count' FROM `posts` LEFT JOIN `comments` ON `posts`.`id` = `comments`.`post_id` GROUP BY `posts`.`id` ORDER BY `comment_count` DESC LIMIT 0,5 ;"); + + + $lastComments = $db->select('SELECT comments.id, comments.comment, comments.status, comments.post_id, users.username FROM comments, users WHERE comments.user_id = users.id order by comments.created_at DESC LIMIT 0,5 ;'); + + + require_once (BASE_PATH . "/template/admin/dashboard/index.php"); + } +} \ No newline at end of file diff --git a/activites/Admin/Menu.php b/activites/Admin/Menu.php new file mode 100644 index 0000000..bb9c89b --- /dev/null +++ b/activites/Admin/Menu.php @@ -0,0 +1,52 @@ +select('SELECT m1.*, m2.name AS parent_name FROM menus m1 LEFT JOIN menus m2 ON m1.parent_id = m2.id ORDER BY id DESC'); + require_once(BASE_PATH . '/template/admin/menus/index.php'); + } + + public function create() + { + $db = new DataBase(); + $menus = $db->select('SELECT * FROM menus WHERE parent_id IS NULL ORDER BY `id` DESC '); + require_once(BASE_PATH . '/template/admin/menus/create.php'); + } + + public function store($request) + { + $db = new DataBase(); + $db->insert('menus', array_keys(array_filter($request)), array_filter($request)); + $this->redirect('admin/menu'); + } + + public function edit($id) + { + $db = new DataBase(); + $menu = $db->select('SELECT * FROM menus WHERE id = ?;', [$id])->fetch(); + $menus = $db->select('SELECT * FROM menus WHERE parent_id IS NULL;'); + require_once(BASE_PATH . '/template/admin/menus/edit.php'); + } + + public function update($request, $id) + { + $db = new DataBase(); + $db->update('menus', $id, array_keys($request), $request); + $this->redirect('admin/menu'); + + } + + public function delete($id) + { + $db = new DataBase(); + $db->delete('menus', $id); + $this->redirect('admin/menu'); + } +} \ No newline at end of file diff --git a/activites/Admin/Post.php b/activites/Admin/Post.php new file mode 100644 index 0000000..2c1d14f --- /dev/null +++ b/activites/Admin/Post.php @@ -0,0 +1,120 @@ +select("SELECT * FROM posts"); + require_once (BASE_PATH . '/template/admin/post/index.php'); + } + + + public function create() + { + $db = new Database(); + $categories = $db->select('SELECT * FROM categories'); + require_once (BASE_PATH . '/template/admin/post/create.php'); + } + + public function store($request){ + $realTimestamp = substr($request['published_at'], 0, 10); + $request['published_at'] = date("Y-m-d H:i:s", (int)$realTimestamp); + $db = new Database(); + if($request['cat_id'] != null){ + $request['image'] = $this->saveImage($request['image'], 'post-image'); + if($request['image']){ + $request = array_merge($request, ['user_id' => 1]); + $db->insert('posts', array_keys($request), $request); + $this->redirect('admin/post'); + } + else{ + $this->redirect('admin/post'); + } + } + else{ + $this->redirect('admin/post'); + } + } + + public function edit($id) + { + $db = new Database(); + $post = $db->select("SELECT * FROM posts WHERE id = ?", [$id])->fetch(); + $categories = $db->select('SELECT * FROM categories'); + require_once (BASE_PATH . '/template/admin/post/edit.php'); + } + + public function update($request, $id) + { + $realTimestamp = substr($request['published_at'], 0, 10); + $request['published_at'] = date("Y-m-d H:i:s", (int)$realTimestamp); + $db = new Database(); + if ($request['cat_id'] != null) { + if($request['image']['tmp_name'] != null){ + $post = $db->select("SELECT * FROM posts WHERE id = ?", [$id])->fetch(); + $this->removeImage($post['image']); + $request['image'] = $this->saveImage($request['image'], 'post-image'); + } + else{ + unset($request['image']); + } + $request = array_merge($request, ['user_id' => 1]); + $db->update('posts', $id, array_keys($request), $request); + $this->redirect('admin/post'); + } + + } + + public function delete($id) + { + $db = new Database(); + $post = $db->select("SELECT * FROM posts WHERE id = ?", [$id])->fetch(); + $this->removeImage($post['image']); + $db->delete('posts', $id); + $this->redirectBack(); + } + + public function breakingNews($id) + { + $db = new Database(); + $post = $db->select("SELECT * FROM posts WHERE id = ?", [$id])->fetch(); + if(empty($post)) + { + $this->redirectBack(); + } + + if($post['breaking_news'] == 1) { + $db->update('posts', $id, ['breaking_news'], [2]); + } + else{ + $db->update('posts', $id, ['breaking_news'], [1]); + } + $this->redirectBack(); + } + + public function selected($id) + { + $db = new Database(); + $post = $db->select("SELECT * FROM posts WHERE id = ?", [$id])->fetch(); + if(empty($post)) + { + $this->redirectBack(); + } + + if($post['selected'] == 1) { + $db->update('posts', $id, ['selected'], [2]); + } + else{ + $db->update('posts', $id, ['selected'], [1]); + } + $this->redirectBack(); + } + + +} \ No newline at end of file diff --git a/activites/Admin/User.php b/activites/Admin/User.php new file mode 100644 index 0000000..e448d9f --- /dev/null +++ b/activites/Admin/User.php @@ -0,0 +1,56 @@ +select('SELECT * FROM users ORDER BY `id` DESC'); + require_once(BASE_PATH . '/template/admin/users/index.php'); + } + + public function edit($id) + { + $db = new DataBase(); + $user = $db->select('SELECT * FROM users WHERE id = ?;', [$id])->fetch(); + require_once(BASE_PATH . '/template/admin/users/edit.php'); + } + + public function update($request, $id) + { + $db = new DataBase(); + $request = ['username' => $request['username'], 'permission' => $request['permission']]; + $db->update('users', $id, array_keys($request), $request); + $this->redirect('admin/user'); + + } + + public function delete($id) + { + $db = new DataBase(); + $db->delete('users', $id); + $this->redirect('admin/user'); + } + + + + public function permission($id) + { + $db = new DataBase(); + $user = $db->select('SELECT * FROM users WHERE id = ?;', [$id])->fetch(); + if(empty($user)){ + $this->redirectBack(); + } + if($user['permission'] == 'user'){ + $db->update('users', $id, ['permission'], ['admin']); + } + else{ + $db->update('users', $id, ['permission'], ['user']); + } + $this->redirectBack(); + } +} \ No newline at end of file diff --git a/activites/Admin/WebSetting.php b/activites/Admin/WebSetting.php new file mode 100644 index 0000000..0c9201e --- /dev/null +++ b/activites/Admin/WebSetting.php @@ -0,0 +1,51 @@ +select("SELECT * FROM `websetting`;")->fetch(); + require_once (BASE_PATH . "/template/admin/web-setting/index.php"); + } + + + public function set() + { + $db= new DataBase(); + $setting= $db->select("SELECT * FROM `websetting`;")->fetch(); + require_once (BASE_PATH . "/template/admin/web-setting/set.php"); + } + + + public function store($request) + { + $db= new DataBase(); + $setting= $db->select("SELECT * FROM `websetting`;")->fetch(); + if($request['logo']['tmp_name'] != ""){ + $request['logo']= $this->saveImage($request['logo'],'setting','logo'); + } + else{ + unset($request['logo']); + } + if($request['icon']['tmp_name'] != ""){ + $request['icon']= $this->saveImage($request['icon'],'setting','icon'); + } + else{ + unset($request['icon']); + } + if(!empty($setting)) + $db->update('websetting', $setting['id'],array_keys($request),$request); + else + $db->insert('websetting',array_keys($request),$request); + $this->redirect('admin/web-setting'); + + } + + } \ No newline at end of file diff --git a/activites/Auth/Auth.php b/activites/Auth/Auth.php new file mode 100644 index 0000000..2e3bdc4 --- /dev/null +++ b/activites/Auth/Auth.php @@ -0,0 +1,303 @@ +فعال سازی حساب کاربری +
' . $username . 'عزیز برای فعال سازی حساب کاربری خود لطفا روی لینک زیر کلیک نمایید
+ + '; + return $message; + } + + public function sendMail($emailAddress, $subject, $body) + { + //Create an instance; passing `true` enables exceptions + $mail = new PHPMailer(true); + +try { + //Server settings + $mail->CharSet = "UTF-8"; + $mail->isSMTP(); //Send using SMTP + $mail->Host = MAIL_HOST; //Set the SMTP server to send through + $mail->SMTPAuth = SMTP_AUTH; //Enable SMTP authentication + $mail->Username = MAIL_USERNAME; //SMTP username + $mail->Password = MAIL_PASSWORD; //SMTP password + $mail->SMTPSecure = 'tls'; + $mail->Port = MAIL_PORT; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` + + //Recipients + $mail->setFrom(SENDER_MAIL, SENDER_NAME); + $mail->addAddress($emailAddress); //Add a recipient + + + //Content + $mail->isHTML(true); //Set email format to HTML + $mail->Subject = $subject; + $mail->Body = $body; + + $mail->send(); + echo 'Message has been sent'; + return true; +} catch (Exception $e) { + echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; + return false; +} + } + + + + public function register(){ + require_once(BASE_PATH .'/template/auth/register.php'); + } + + + public function registerStore($request) + { + if(empty($request['email']) || empty($request['username']) || empty($request['password'])) + { + flash('register_error', 'تمامی فیلد ها الزامی میباشند'); + $this->redirectBack(); + } + else if(strlen($request['password']) < 8 ) + { + flash('register_error', 'رمز عبور باید حداقل ۸ کاراکتر باشد'); + $this->redirectBack(); + } + else if(!filter_var($request['email'], FILTER_VALIDATE_EMAIL)) + { + flash('register_error', 'ایمیل وارد شده معتبر نمیباشد'); + $this->redirectBack(); + } + else{ + $db = new Database(); + $user = $db->select("SELECT * FROM users WHERE email = ?", [$request['email']])->fetch(); + if($user != null){ + flash('register_error', 'ایمیل از قبل وجود دارد'); + $this->redirectBack(); + } + else{ + $randomToken = $this->random(); + $activationMessage = $this->activationMessage($request['username'], $randomToken); + $result = $this->sendMail($request['email'], 'فعال سازی حساب کاربری', $activationMessage); + if($result) + { + $request['verify_token'] = $randomToken; + $request['password'] = $this->hash($request['password']); + $db->insert('users', array_keys($request), $request); + $this->redirect('login'); + } + else{ + flash('register_error', 'ایمیل فعال سازی ارسال نشد'); + $this->redirectBack(); + } + } + + } + } + + + public function activation($verifyToken) + { + $db = new Database(); + $user = $db->select("SELECT * FROM users WHERE verify_token = ? AND is_active = 0", [$verifyToken])->fetch(); + if($user == null){ + $this->redirect('login'); + } + else{ + $result = $db->update('users', $user['id'], ['is_active'], [1]); + $this->redirect('login'); + } + } + + + public function login(){ + require_once(BASE_PATH .'/template/auth/login.php'); + } + + + public function checkLogin($request) + { + if(empty($request['email']) || empty($request['password'])) + { + flash('login_error', 'تمامی فیلد ها الزامی میباشند'); + $this->redirectBack(); + } + else{ + $db = new Database(); + $user = $db->select("SELECT * FROM users WHERE email = ?", [$request['email']])->fetch(); + if($user != null){ + if(password_verify($request['password'], $user['password']) && $user['is_active'] == 1) + { + $_SESSION['user'] = $user['id']; + $this->redirect('admin'); + } + else{ + flash('login_error', 'کلمه عبور اشتباه است'); + $this->redirectBack(); + } + } + else{ + flash('login_error', 'کاربر یافت نشد'); + $this->redirectBack(); + } + } + + } + + + public function checkAdmin(){ + if(isset($_SESSION['user'])) + { + $db = new Database(); + $user = $db->select("SELECT * FROM users WHERE id = ?", [$_SESSION['user']])->fetch(); + if($user != null){ + if($user['permission'] != 'admin'){ + $this->redirect('home'); + } + + } + else{ + $this->redirect('home'); + } + } + else{ + $this->redirect('home'); + } + } + + public function logout() + { + if(isset($_SESSION['user'])) + { + unset($_SESSION['user']); + session_destroy(); + } + $this->redirect('login'); + + } + + + public function forgot(){ + require_once(BASE_PATH .'/template/auth/forgot-password.php'); + } + + + + public function forgotMessage($username, $forgotToken) + { + $message = ' +' . $username . 'عزیز برای بازیابی رمز عبور خود لطفا روی لینک زیر کلیک نمایید
+ + '; + return $message; + } + + public function forgotRequest($request) + { + if(empty($request['email'])) + { + flash('forgot_error', ' فیلد ایمیل الزامی میباشد'); + $this->redirectBack(); + } + else if(!filter_var($request['email'], FILTER_VALIDATE_EMAIL)) + { + flash('forgot_error', ' ایمیل وارد شده صحیح نمیباشد'); + $this->redirectBack(); + } + else{ + $db = new Database(); + $user = $db->select("SELECT * FROM users WHERE email = ?", [$request['email']])->fetch(); + if($user == null) + { + flash('forgot_error', ' ایمیل وارد شده وجود ندارد'); + $this->redirectBack(); + } + else{ + $randomToken = $this->random(); + $forgotMessage = $this->forgotMessage($user['username'], $randomToken); + $result = $this->sendMail($request['email'], 'بازیابی رمز عبور', $forgotMessage); + if($result) + { + $db->update('users', $user['id'], ['forgot_token', 'forgot_token_expire'], [$randomToken, date("Y-m-d H:i:s", strtotime('+15 minutes'))]); + $this->redirect('login'); + } + else{ + flash('forgot_error', ' ایمیل ارسال نشد'); + $this->redirectBack(); + } + + } + } + } + + public function resetPasswordView($forgot_token){ + require_once(BASE_PATH .'/template/auth/reset-password.php'); + } + + + public function resetPassword($request, $forgot_token) + { + if(!isset($request['password']) || strlen($request['password']) < 8) + { + flash('reset_error', 'یا رمز عبور نباید کمتر از ۸ کاراکتر باشد رمز عبور نباید خالی باشد'); + $this->redirectBack(); + } + else{ + $db = new Database(); + $user = $db->select("SELECT * FROM users WHERE forgot_token = ?", [$forgot_token])->fetch(); + if($user == null){ + flash('reset_error', 'کاربری با این مشخصات یافت نشد'); + $this->redirectBack(); + } + else{ + if($user['forgot_token_expire'] < date('Y-m-d H:i:s')) + { + flash('reset_error', 'مهلت استفاده از این توکن به پایان رسیده است'); + $this->redirectBack(); + } + if($user) + { + $db->update('users', $user['id'], ['password'], [$this->hash($request['password'])]); + $this->redirect('login'); + } + else{ + $this->redirectBack(); + } + } + } + } + + +} \ No newline at end of file diff --git a/activites/Home.php b/activites/Home.php new file mode 100644 index 0000000..134088b --- /dev/null +++ b/activites/Home.php @@ -0,0 +1,118 @@ +select('SELECT * FROM websetting')->fetch(); + + $menus = $db->select('SELECT * FROM menus WHERE parent_id IS NULL')->fetchAll(); + + $topSelectedPosts = $db->select('SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username, (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts WHERE posts.selected = 2 ORDER BY created_at DESC LIMIT 0, 3')->fetchAll(); + + $breakingNews = $db->select('SELECT * FROM posts WHERE breaking_news = 2 ORDER BY created_at DESC LIMIT 0,1')->fetch(); + + $lastPosts = $db->select('SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username, (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts ORDER BY created_at DESC LIMIT 0, 6')->fetchAll(); + + $bodyBanner = $db->select('SELECT * FROM banners ORDER BY created_at DESC LIMIT 0,1')->fetch(); + $sidebarBanner = $db->select('SELECT * FROM banners ORDER BY created_at DESC LIMIT 0,1')->fetch(); + + $popularPosts =$db->select('SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username, (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts ORDER BY view DESC LIMIT 0, 3')->fetchAll(); + + $mostCommentsPosts =$db->select('SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username, (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts ORDER BY comments_count DESC LIMIT 0, 4')->fetchAll(); + + + require_once (BASE_PATH . '/template/app/index.php'); + } + + + public function show($id) + { + + $db = new Database(); + + + $post =$db->select('SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username, (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts WHERE id = ?', [$id])->fetch(); + + $comments = $db->select("SELECT *, (SELECT username FROM users WHERE users.id = comments.user_id) AS username FROM comments WHERE post_id = ? AND status = 'approved'", [$id])->fetchAll(); + + + + $setting = $db->select('SELECT * FROM websetting')->fetch(); + + $menus = $db->select('SELECT * FROM menus WHERE parent_id IS NULL')->fetchAll(); + + + + $sidebarBanner = $db->select('SELECT * FROM banners ORDER BY created_at DESC LIMIT 0,1')->fetch(); + + $popularPosts =$db->select('SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username, (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts ORDER BY view DESC LIMIT 0, 3')->fetchAll(); + + $mostCommentsPosts =$db->select('SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username, (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts ORDER BY comments_count DESC LIMIT 0, 4')->fetchAll(); + + require_once (BASE_PATH . '/template/app/show-post.php'); + + } + + + public function commentStore($request){ + + if(isset($_SESSION['user'])) + { + if($_SESSION['user'] != null) + { + $db = new Database(); + $db->insert('comments', ['user_id', 'post_id', 'comment'], [$_SESSION['user'], $request['post_id'], $request['comment']]); + $this->redirectBack(); + } + else{ + $this->redirectBack(); + } + } + else{ + $this->redirectBack(); + } + + } + + + + public function category($id) + { + $db = new DataBase(); + $category = $db->select("SELECT * FROM `categories` WHERE `id` = ? ORDER BY `id` DESC ;", [$id])->fetch(); + + $topSelectedPosts = $db->select("SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username , (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts where posts.selected = 2 ORDER BY `created_at` DESC LIMIT 0,1 ;")->fetchAll(); + + + $categoryPosts = $db->select("SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username , (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts WHERE cat_id = ? ORDER BY `created_at` DESC LIMIT 0,6 ;", [$id])->fetchAll(); + + $popularPosts = $db->select("SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username , (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts ORDER BY `view` DESC LIMIT 0,3 ;")->fetchAll(); + + $breakingNews = $db->select("SELECT * FROM posts WHERE breaking_news = 2 ORDER BY `created_at` DESC LIMIT 0,1 ;")->fetch(); + + $mostCommentsPosts = $db->select("SELECT posts.*, (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id) AS comments_count, (SELECT username FROM users WHERE users.id = posts.user_id) AS username , (SELECT name FROM categories WHERE categories.id = posts.cat_id) AS category FROM posts ORDER BY `comments_count` DESC LIMIT 0,4 ;")->fetchAll(); + + $menus = $db->select('SELECT *, (SELECT COUNT(*) FROM `menus` AS `submenus` WHERE `submenus`.`parent_id` = `menus`.`id` ) as `submenu_count` FROM `menus` WHERE `parent_id` IS NULL ;')->fetchAll(); + + $setting= $db->select("SELECT * FROM `websetting`;")->fetch(); + + $sidebarBanner= $db->select("SELECT * FROM `banners` LIMIT 0,1;")->fetch(); + $bodyBanner= $db->select("SELECT * FROM `banners` ORDER BY created_at DESC LIMIT 0,1;")->fetch(); + + require_once (BASE_PATH . "/template/app/show-category.php"); + } + + + protected function redirectBack(){ + header("Location: " . $_SERVER['HTTP_REFERER']); + exit; + } + +} \ No newline at end of file