🤩 File Manager - Mr.X
PHP:
8.3.33
Server:
Apache
OS:
Linux 5.14.0-611.49.1.el9_7.x86_64
User:
websparkit
Navigate
Upload:
Upload
New File
New Folder
Editing:Admin.php
<?php class Admin extends Controller { private $userModel; private $serviceModel; private $teamModel; private $testimonialModel; private $messageModel; private $settingModel; public function __construct(){ $this->userModel = $this->model('User'); $this->serviceModel = $this->model('Service'); $this->teamModel = $this->model('TeamMember'); $this->testimonialModel = $this->model('Testimonial'); $this->messageModel = $this->model('ContactMessage'); $this->settingModel = $this->model('Setting'); } public function index(){ if($this->isLoggedIn()){ $this->redirect('admin/dashboard'); } else { $this->redirect('admin/login'); } } public function login(){ if($this->isLoggedIn()){ $this->redirect('admin/dashboard'); } if($_SERVER['REQUEST_METHOD'] == 'POST'){ $data = [ 'username' => trim($_POST['username']), 'password' => trim($_POST['password']), 'username_err' => '', 'password_err' => '' ]; if(empty($data['username'])){ $data['username_err'] = 'Please enter username'; } if(empty($data['password'])){ $data['password_err'] = 'Please enter password'; } if($this->userModel->findUserByUsername($data['username'])){ // User found } else { $data['username_err'] = 'No user found'; } if(empty($data['username_err']) && empty($data['password_err'])){ $loggedInUser = $this->userModel->login($data['username'], $data['password']); if($loggedInUser){ $this->createUserSession($loggedInUser); } else { $data['password_err'] = 'Password incorrect'; $this->view('admin/login', $data); } } else { $this->view('admin/login', $data); } } else { $data = [ 'username' => '', 'password' => '', 'username_err' => '', 'password_err' => '' ]; $this->view('admin/login', $data); } } public function dashboard(){ if(!$this->isLoggedIn()){ $this->redirect('admin/login'); } $services = $this->serviceModel->getServices(); $team = $this->teamModel->getTeamMembers(); $testimonials = $this->testimonialModel->getTestimonials(); $messages = $this->messageModel->getMessages(); $data = [ 'title' => 'Dashboard', 'services_count' => count($services), 'team_count' => count($team), 'testimonials_count' => count($testimonials), 'messages_count' => count($messages) ]; $this->view('admin/dashboard', $data); } // Manage Services Route public function services($action = null, $id = null){ if(!$this->isLoggedIn()){ $this->redirect('admin/login'); } $limit = 5; $page = isset($_GET['page']) ? intval($_GET['page']) : 1; if($page < 1) $page = 1; $offset = ($page - 1) * $limit; if($_SERVER['REQUEST_METHOD'] == 'POST'){ if($action == 'add'){ $data = [ 'title' => trim($_POST['title']), 'description' => trim($_POST['description']), 'icon' => trim($_POST['icon']), 'order_index' => intval($_POST['order_index']), 'title_err' => '', 'description_err' => '' ]; if(empty($data['title'])){ $data['title_err'] = 'Please enter title'; } if(empty($data['description'])){ $data['description_err'] = 'Please enter description'; } if(empty($data['title_err']) && empty($data['description_err'])){ if($this->serviceModel->addService($data)){ $this->redirect('admin/services'); } else { die('Something went wrong'); } } else { $total_records = $this->serviceModel->getServicesCount(); $total_pages = ceil($total_records / $limit); $services = $this->serviceModel->getServicesPaginated($limit, $offset); $data['services'] = $services; $data['current_page'] = $page; $data['total_pages'] = $total_pages; $data['action'] = 'add'; $this->view('admin/services', $data); } } elseif($action == 'edit' && !empty($id)){ $data = [ 'id' => $id, 'title' => trim($_POST['title']), 'description' => trim($_POST['description']), 'icon' => trim($_POST['icon']), 'order_index' => intval($_POST['order_index']), 'title_err' => '', 'description_err' => '' ]; if(empty($data['title'])){ $data['title_err'] = 'Please enter title'; } if(empty($data['description'])){ $data['description_err'] = 'Please enter description'; } if(empty($data['title_err']) && empty($data['description_err'])){ if($this->serviceModel->updateService($data)){ $this->redirect('admin/services'); } else { die('Something went wrong'); } } else { $total_records = $this->serviceModel->getServicesCount(); $total_pages = ceil($total_records / $limit); $services = $this->serviceModel->getServicesPaginated($limit, $offset); $data['services'] = $services; $data['current_page'] = $page; $data['total_pages'] = $total_pages; $data['action'] = 'edit'; $this->view('admin/services', $data); } } } else { $total_records = $this->serviceModel->getServicesCount(); $total_pages = ceil($total_records / $limit); $services = $this->serviceModel->getServicesPaginated($limit, $offset); $data = [ 'title' => 'Manage Services', 'services' => $services, 'action' => $action, 'selected_service' => null, 'current_page' => $page, 'total_pages' => $total_pages ]; if($action == 'edit' && !empty($id)){ $data['selected_service'] = $this->serviceModel->getServiceById($id); } elseif($action == 'delete' && !empty($id)){ if($this->serviceModel->deleteService($id)){ $this->redirect('admin/services'); } else { die('Something went wrong'); } } $this->view('admin/services', $data); } } // Manage Team Route public function team($action = null, $id = null){ if(!$this->isLoggedIn()){ $this->redirect('admin/login'); } $limit = 5; $page = isset($_GET['page']) ? intval($_GET['page']) : 1; if($page < 1) $page = 1; $offset = ($page - 1) * $limit; if($_SERVER['REQUEST_METHOD'] == 'POST'){ if($action == 'add'){ $data = [ 'name' => trim($_POST['name']), 'designation' => trim($_POST['designation']), 'photo_url' => trim($_POST['photo_url']), 'order_index' => intval($_POST['order_index']), 'name_err' => '', 'designation_err' => '' ]; if(empty($data['name'])){ $data['name_err'] = 'Please enter name'; } if(empty($data['designation'])){ $data['designation_err'] = 'Please enter designation'; } if(empty($data['name_err']) && empty($data['designation_err'])){ if($this->teamModel->addTeamMember($data)){ $this->redirect('admin/team'); } else { die('Something went wrong'); } } else { $total_records = $this->teamModel->getTeamMembersCount(); $total_pages = ceil($total_records / $limit); $team = $this->teamModel->getTeamMembersPaginated($limit, $offset); $data['team'] = $team; $data['current_page'] = $page; $data['total_pages'] = $total_pages; $data['action'] = 'add'; $this->view('admin/team', $data); } } elseif($action == 'edit' && !empty($id)){ $data = [ 'id' => $id, 'name' => trim($_POST['name']), 'designation' => trim($_POST['designation']), 'photo_url' => trim($_POST['photo_url']), 'order_index' => intval($_POST['order_index']), 'name_err' => '', 'designation_err' => '' ]; if(empty($data['name'])){ $data['name_err'] = 'Please enter name'; } if(empty($data['designation'])){ $data['designation_err'] = 'Please enter designation'; } if(empty($data['name_err']) && empty($data['designation_err'])){ if($this->teamModel->updateTeamMember($data)){ $this->redirect('admin/team'); } else { die('Something went wrong'); } } else { $total_records = $this->teamModel->getTeamMembersCount(); $total_pages = ceil($total_records / $limit); $team = $this->teamModel->getTeamMembersPaginated($limit, $offset); $data['team'] = $team; $data['current_page'] = $page; $data['total_pages'] = $total_pages; $data['action'] = 'edit'; $this->view('admin/team', $data); } } } else { $total_records = $this->teamModel->getTeamMembersCount(); $total_pages = ceil($total_records / $limit); $team = $this->teamModel->getTeamMembersPaginated($limit, $offset); $data = [ 'title' => 'Manage Team', 'team' => $team, 'action' => $action, 'selected_member' => null, 'current_page' => $page, 'total_pages' => $total_pages ]; if($action == 'edit' && !empty($id)){ $data['selected_member'] = $this->teamModel->getTeamMemberById($id); } elseif($action == 'delete' && !empty($id)){ if($this->teamModel->deleteTeamMember($id)){ $this->redirect('admin/team'); } else { die('Something went wrong'); } } $this->view('admin/team', $data); } } // Manage Testimonials Route public function testimonials($action = null, $id = null){ if(!$this->isLoggedIn()){ $this->redirect('admin/login'); } $limit = 5; $page = isset($_GET['page']) ? intval($_GET['page']) : 1; if($page < 1) $page = 1; $offset = ($page - 1) * $limit; if($_SERVER['REQUEST_METHOD'] == 'POST'){ if($action == 'add'){ $data = [ 'client_name' => trim($_POST['client_name']), 'content' => trim($_POST['content']), 'name_err' => '', 'content_err' => '' ]; if(empty($data['client_name'])){ $data['name_err'] = 'Please enter client name'; } if(empty($data['content'])){ $data['content_err'] = 'Please enter testimonial content'; } if(empty($data['name_err']) && empty($data['content_err'])){ if($this->testimonialModel->addTestimonial($data)){ $this->redirect('admin/testimonials'); } else { die('Something went wrong'); } } else { $total_records = $this->testimonialModel->getTestimonialsCount(); $total_pages = ceil($total_records / $limit); $testimonials = $this->testimonialModel->getTestimonialsPaginated($limit, $offset); $data['testimonials'] = $testimonials; $data['current_page'] = $page; $data['total_pages'] = $total_pages; $data['action'] = 'add'; $this->view('admin/testimonials', $data); } } elseif($action == 'edit' && !empty($id)){ $data = [ 'id' => $id, 'client_name' => trim($_POST['client_name']), 'content' => trim($_POST['content']), 'name_err' => '', 'content_err' => '' ]; if(empty($data['client_name'])){ $data['name_err'] = 'Please enter client name'; } if(empty($data['content'])){ $data['content_err'] = 'Please enter testimonial content'; } if(empty($data['name_err']) && empty($data['content_err'])){ if($this->testimonialModel->updateTestimonial($data)){ $this->redirect('admin/testimonials'); } else { die('Something went wrong'); } } else { $total_records = $this->testimonialModel->getTestimonialsCount(); $total_pages = ceil($total_records / $limit); $testimonials = $this->testimonialModel->getTestimonialsPaginated($limit, $offset); $data['testimonials'] = $testimonials; $data['current_page'] = $page; $data['total_pages'] = $total_pages; $data['action'] = 'edit'; $this->view('admin/testimonials', $data); } } } else { $total_records = $this->testimonialModel->getTestimonialsCount(); $total_pages = ceil($total_records / $limit); $testimonials = $this->testimonialModel->getTestimonialsPaginated($limit, $offset); $data = [ 'title' => 'Manage Testimonials', 'testimonials' => $testimonials, 'action' => $action, 'selected_testimonial' => null, 'current_page' => $page, 'total_pages' => $total_pages ]; if($action == 'edit' && !empty($id)){ $data['selected_testimonial'] = $this->testimonialModel->getTestimonialById($id); } elseif($action == 'delete' && !empty($id)){ if($this->testimonialModel->deleteTestimonial($id)){ $this->redirect('admin/testimonials'); } else { die('Something went wrong'); } } $this->view('admin/testimonials', $data); } } // Manage Messages Route public function messages($action = null, $id = null){ if(!$this->isLoggedIn()){ $this->redirect('admin/login'); } $limit = 5; $page = isset($_GET['page']) ? intval($_GET['page']) : 1; if($page < 1) $page = 1; $offset = ($page - 1) * $limit; if($action == 'delete' && !empty($id)){ if($this->messageModel->deleteMessage($id)){ $this->redirect('admin/messages'); } else { die('Something went wrong'); } } $total_records = $this->messageModel->getMessagesCount(); $total_pages = ceil($total_records / $limit); $messages = $this->messageModel->getMessagesPaginated($limit, $offset); $data = [ 'title' => 'Contact Messages', 'messages' => $messages, 'selected_message' => null, 'current_page' => $page, 'total_pages' => $total_pages ]; if($action == 'view' && !empty($id)){ $data['selected_message'] = $this->messageModel->getMessageById($id); } $this->view('admin/messages', $data); } // Site Settings Route public function settings(){ if(!$this->isLoggedIn()){ $this->redirect('admin/login'); } if($_SERVER['REQUEST_METHOD'] == 'POST'){ $success = true; foreach($_POST as $key => $value){ if(!$this->settingModel->updateSetting($key, trim($value))){ $success = false; } } if($success){ $_SESSION['settings_updated'] = true; } $this->redirect('admin/settings'); } else { $settings = $this->settingModel->getSettings(); $data = [ 'title' => 'Site Settings', 'settings' => $settings ]; $this->view('admin/settings', $data); } } public function logout(){ unset($_SESSION['admin_id']); unset($_SESSION['admin_username']); session_destroy(); $this->redirect('admin/login'); } public function createUserSession($user){ $_SESSION['admin_id'] = $user->id; $_SESSION['admin_username'] = $user->username; $this->redirect('admin/dashboard'); } public function isLoggedIn(){ if(isset($_SESSION['admin_id'])){ return true; } else { return false; } } public function redirect($page){ header('Location: ' . URLROOT . '/' . $page); exit; } }
Save Changes
Cancel
Create New File
Create New Folder