/** * Show one category */ public function show() { if (!empty(func_get_args())) { $this->params = func_get_args()[0]; if (!empty($this->params)) { $this->params = $this->params[0]; } } if (!preg_match('/[0-9]+$/', $this->params, $int)) { $this->redirectTo('categories'); } $this->category = Model::getOneFrom('categories', $int[0]); $this->posts = Model::findFromBy('posts', ['category_id' => $this->category['id']]); $this->render('categories/show'); }
/** * save category * @return bool|string */ public static function save() { if (isset($_POST['save'])) { if (isset($_POST['name'])) { $name = Helper::filter($_POST['name']); if (Model::findFromBy('categories', ['name' => $name])) { return self::$messages[] = "The Category is already exists !"; } else { return Model::insert('categories', ['name' => $name]); } } else { return self::$messages[] = "All fields are required !"; } } else { return self::$messages[] = "You should click on save botton"; } }
/** * save the post * @return bool|string */ public static function save() { if (isset($_POST['save'])) { if (isset($_POST['title'], $_POST['content'], $_POST['category_id'])) { $title = Helper::filter($_POST['title']); $content = Helper::filter($_POST['content']); $category_id = Helper::filter($_POST['category_id']); if (Model::findFromBy('posts', ['title' => $title])) { return self::$messages[] = "The title of this post is already exists !"; } else { return Model::insert('posts', ['title' => $title, 'content' => $content, 'category_id' => $category_id]); } } else { return self::$messages[] = "All fields are required !"; } } else { return self::$messages[] = "You should click on save botton"; } }
/** * Render the edit view * and get (id) from url * @return void */ public function edit() { if (isset($_SESSION['login']) && $_SESSION['login'] === true) { if (!empty(func_get_args())) { $this->params = func_get_args()[0]; if (!empty($this->params)) { $this->params = $this->params[0]; } } if (!preg_match('/[0-9]+$/', $this->params, $int)) { $this->redirectTo('posts'); } $this->idOfEditedPage = $int[0]; $this->post = Model::getOneFrom('posts', $int[0]); $this->category = Model::findFromBy('categories', ['id' => $this->post['category_id']]); $this->categories = Model::getAllFrom('categories'); $this->render('posts/edit'); } else { $this->redirectTo('posts/index'); } }
/** * Login the user * @return bool */ public static function login() { if (isset($_POST['submit']) && !empty($_POST['email']) && !empty($_POST['password'])) { $password = sha1($_POST['email'] . $_POST['password'] . SAULT); $email = $_POST['email']; $user = Model::findFromBy('users', ['email' => $email, 'password' => $password]); if ($user !== false) { $_SESSION['login'] = true; $_SESSION['email'] = $user['email']; $_SESSION['is_admin'] = $user['is_admin']; return true; } else { return false; } } else { return false; } }