getLoggedInUser(); if (!$userDetails) { return $this->redirect()->toUrl($this->getBaseUrl()); } return new ViewModel(); } public function loginAction() { if ($this->getLoggedInUser()) { return $this->redirect()->toUrl($this->getBaseUrl() . "/admin/index/home"); } if ($this->getRequest()->isXmlHttpRequest()) { try { $request = $this->getRequest()->getPost(); $email = trim($request['email']); $password = trim($request['password']); if ($email === "") { return new JsonModel(array("success" => false, "message" => "Please enter username", "errorCode" => 1)); } if ($password === "") { return new JsonModel(array("success" => false, "message" => "Please enter password", "errorCode" => 2)); } $user = $this->userTable()->loginCheck($email, $password); if ($user["success"]) { session_unset(); $this->getAuthDbTable() ->setTableName('user') ->setIdentityColumn('email') ->setCredentialColumn('password') ->setIdentity($email) ->setCredential($user["password"]); $this->getAuthService()->setAdapter($this->getAuthDbTable())->setStorage($this->getSessionStorage()); $result = $this->getAuthService()->authenticate(); if ($result->isValid()) { $resultRow = (array)$this->getAuthDbTable()->getResultRowObject(array('user_id', 'email',)); $this->getSessionStorage()->write($resultRow); return new JsonModel(array("success" => true, "message" => "Successfully logged in")); } else { return new JsonModel(array("success" => false, "message" => "Invalid credentials")); } } else { return new JsonModel(array("success" => false, "message" => "Invalid credentials")); } } catch (\Exception $e) { return new JsonModel(array("success" => false, "message" => "Something went wrong. Please try again after sometime.", "errorCode" => $e->getMessage())); } } $viewModel = new ViewModel(); $viewModel->setVariables(array( "baseUrl" => $this->getBaseUrl(), ))->setTerminal(true); return $viewModel; } public function clean($string) { $string = str_replace(' ', '-', $string); return preg_replace('/[^A-Za-z0-9\-]/', '', $string); } public function blogsAction() { $userDetails = $this->getLoggedInUser(); if (!$userDetails) { return $this->redirect()->toUrl($this->getBaseUrl()); } $blog = $this->blogTable()->getAllBlog(); $this->layout()->setVariable('activeTab', 3); return new ViewModel(array('blog' => $blog)); } public function addBlogAction() { $userDetails = $this->getLoggedInUser(); if (!$userDetails) { return $this->redirect()->toUrl($this->getBaseUrl()); } if ($this->getRequest()->isXmlHttpRequest()) { try { $request = $this->getRequest()->getPost(); $files = $this->getRequest()->getFiles(); $blog_title = trim($request['blog_title']); $category = trim($request['category']); $url = trim($this->clean($request['blog_url']), "-"); $author = trim($request['author']); $date = trim($request['date']); $description = trim($request['description']); $tag = trim($request['tag']); $seo_title = trim($request['seo_title']); $seo_description = trim($request['seo_description']); $seo_keyword = trim($request['seo_keyword']); $fileName = rand(0000, 9999) . $files['files']['name']; $directoryPath = "/data/images/"; @mkdir(getcwd() . "/public" . $directoryPath); $target_file = getcwd() . "/public" . $directoryPath . $fileName; @chmod($target_file, 0777); try{ if (move_uploaded_file($files["files"]["tmp_name"], $target_file)) { $finalPath = $directoryPath . $fileName; @chmod($target_file, 0777); } } catch (Exception $ex) { return new JsonModel(array("success" => false, "message" => "unable to move file")); } if ($blog_title === "") { return new JsonModel(array("success" => false, "message" => "Please enter title!")); } if ($category === "") { return new JsonModel(array("success" => false, "message" => "Please enter category!")); } if ($author === "") { return new JsonModel(array("success" => false, "message" => "Please enter author name!")); } if ($date === "") { return new JsonModel(array("success" => false, "message" => "Please enter date!")); } if ($fileName === "") { return new JsonModel(array("success" => false, "message" => "Please upload Image")); } if ($description === "") { return new JsonModel(array("success" => false, "message" => "Please enter description!")); } if ($tag === "") { return new JsonModel(array("success" => false, "message" => "Please enter tags!")); } if ($seo_title === "") { return new JsonModel(array("success" => false, "message" => "Please enter seo title!")); } if ($seo_description === "") { return new JsonModel(array("success" => false, "message" => "Please enter seo description!")); } if ($seo_keyword === "") { return new JsonModel(array("success" => false, "message" => "Please enter seo keyword!")); } $data = array( "title" => $blog_title, "category" => $category, "blog_url" => $url, "author" => $author, "blog_date" => date("Y-m-d",strtotime($date)), "banner" => $finalPath, "description" => $description, "tags" => $tag, "seo_title" => $seo_title, "seo_description" => $seo_description, "seo_keywords" => $seo_keyword, ); $isTitleExist = $this->blogTable()->IsBlogExist(array("title" => $blog_title, "status" => 1), "id"); if ($isTitleExist) { return new JsonModel(array("success" => FALSE, "message" => "Title " . $blog_title . " is already exists, Please choose another")); } $saveBlog = $this->blogTable()->addBlog($data); if ($saveBlog['success']) { return new JsonModel(array("success" => true, "message" => "Blog has been added successfully")); } return new JsonModel(array("success" => false, "message" => "Unable to add Blog")); } catch (\Exception $e) { return new JsonModel(array("success" => false, "message" => "something went wrong")); } } $this->layout()->setVariable('activeTab', 3); return new ViewModel(); } public function blogListDataAction() { try { $records["aaData"] = array(); $userId = $this->getLoggedInUserId(); if (!$userId) { return new JsonModel($records); } $sortCol = isset($_GET['iSortCol_0']) ? $_GET['iSortCol_0'] : 0; $sortType = isset($_GET['sSortDir_0']) ? $_GET['sSortDir_0'] : "desc"; $categoryCount = $this->blogTable()->getBlogCount(); $iTotalRecords = $categoryCount; $iDisplayLength = intval($_GET['iDisplayLength']); $iDisplayLength = $iDisplayLength < 0 ? $iTotalRecords : $iDisplayLength; $iDisplayStart = intval($_GET['iDisplayStart']); $sEcho = intval($_GET['sEcho']); $blogsList = $this->blogTable()->getAllBlogsForAdmin($iDisplayLength, $iDisplayStart, $sortCol, $sortType); $i = $iDisplayStart; $basePath = $this->getBaseUrl(); foreach ($blogsList as $datar) { $i++; $records["aaData"][] = array( $i, ucwords($datar['title']), $datar['blog_date'], '
', ); } $records["sEcho"] = $sEcho; $records["iTotalRecords"] = $iTotalRecords; $records["iTotalDisplayRecords"] = $iTotalRecords; return new JsonModel($records); } catch (\Exception $e) { print_r($e->getMessage()); return array(); } } public function deleteBlogAction() { $userDetails = $this->getLoggedInUser(); if (!$userDetails) { return $this->redirect()->toUrl($this->getBaseUrl()); } $request = $this->getRequest()->getPost(); $blog_id = $request['id']; $data = array("status" => 0); $where = array("id" => $blog_id); $update = $this->blogTable()->updateBlog($data, $where); if ($update['success']) { return new JsonModel(array("success" => true, "message" => "Blog has been deleted successfully")); } else { return new JsonModel(array("success" => false, "message" => "something went wrong")); } } public function editBlogAction() { $userDetails = $this->getLoggedInUser(); if (!$userDetails) { return $this->redirect()->toUrl($this->getBaseUrl()); } $blogId = $this->params()->fromRoute("id", 0); $blogId=base64_decode($blogId); $blog = $this->blogTable()->BlogById($blogId); //print_r($blog);exit; $this->layout()->setVariable('activeTab', 3); return new ViewModel(array('blog' => $blog)); } public function updateBlogAction() { $userDetails = $this->getLoggedInUser(); if (!$userDetails) { return $this->redirect()->toUrl($this->getBaseUrl()); } if ($this->getRequest()->isXmlHttpRequest()) { try { $request = $this->getRequest()->getPost(); $files = $this->getRequest()->getFiles(); $blog_Id = trim($request['blogId']); $blog_title = trim($request['blog_title']); $category = trim($request['category']); $url = trim($this->clean($request['blog_url']), "-"); $author = trim($request['author']); $date = trim($request['date']); $description = trim($request['description']); $tag = trim($request['tag']); $seo_title = trim($request['seo_title']); $seo_description = trim($request['seo_description']); $seo_keyword = trim($request['seo_keyword']); $fileName = rand(0000, 9999) . $files['files']['name']; $directoryPath = "/data/images/"; @mkdir(getcwd() . "/public" . $directoryPath); $target_file = getcwd() . "/public" . $directoryPath . $fileName; @chmod($target_file, 0777); if (move_uploaded_file($files["files"]["tmp_name"], $target_file)) { $finalPath = $directoryPath . $fileName; @chmod($target_file, 0777); } else { $finalPath = ""; } if ($blog_title === "") { return new JsonModel(array("success" => false, "message" => "Please enter title!")); } if ($category === "") { return new JsonModel(array("success" => false, "message" => "Please enter category!")); } if ($author === "") { return new JsonModel(array("success" => false, "message" => "Please enter author name!")); } if ($date === "") { return new JsonModel(array("success" => false, "message" => "Please enter date!")); } if ($description === "") { return new JsonModel(array("success" => false, "message" => "Please enter description!")); } if ($tag === "") { return new JsonModel(array("success" => false, "message" => "Please enter tags!")); } if ($seo_title === "") { return new JsonModel(array("success" => false, "message" => "Please enter seo title!")); } if ($seo_description === "") { return new JsonModel(array("success" => false, "message" => "Please enter seo description!")); } if ($seo_keyword === "") { return new JsonModel(array("success" => false, "message" => "Please enter seo keyword!")); } $data = array( "title" => $blog_title, "category" => $category, "blog_url" => $url, "author" => $author, "blog_date" => date("Y-m-d",strtotime($date)), "description" => $description, "tags" => $tag, "seo_title" => $seo_title, "seo_description" => $seo_description, "seo_keywords" => $seo_keyword, ); if ($finalPath) { $data['banner'] = $finalPath; } $isTitleExist = $this->blogTable()->BlogExist($blog_title, $blog_Id); if ($isTitleExist) { return new JsonModel(array("success" => FALSE, "message" => "Blog " . $blog_title . " is already exists, Please choose another")); } $where = array("id" => $blog_Id); $update = $this->blogTable()->updateBlog($data, $where); if ($update) { return new JsonModel(array("success" => true, "message" => "Blog has been updated successfully")); } return new JsonModel(array("success" => false, "message" => "Unable to update Blog")); } catch (\Exception $e) { return new JsonModel(array("success" => false, "message" => "something went wrong")); } } $this->layout()->setVariable('activeTab', 3); return new ViewModel(); } public function logoutAction() { try { if ($this->getAuthService()->hasIdentity()) { // $this->getSessionManager()->forgotMe(); $this->getAuthService()->clearIdentity(); session_unset(); session_destroy(); } } catch (\Exception $e) { return array(); } return $this->redirect()->toUrl($this->getBaseUrl() . "/"); } }