|
| 1 | +<?php |
| 2 | +require_once 'auth.php'; |
| 3 | + |
| 4 | +require '../app.php'; |
| 5 | +use \Michelf\MarkdownExtra, |
| 6 | + \Michelf\Markdown; |
| 7 | + require_once '../vendor/markdown/Markdown.inc.php'; |
| 8 | + |
| 9 | + |
| 10 | +$url_action = (empty($_REQUEST['action'])) ? 'logIn' : $_REQUEST['action']; |
| 11 | + |
| 12 | +if (isset($url_action)) { |
| 13 | + if (is_callable($url_action)) { |
| 14 | + call_user_func($url_action); |
| 15 | + } else { |
| 16 | + echo 'Function does not exist, request terminated'; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +if (is_array($_SESSION) &&$_SESSION['username'] ==UserAuth) { |
| 21 | + $data = array(); |
| 22 | + |
| 23 | + if (isset($_GET['status'])&& $_GET['status']=='create') { |
| 24 | + if ($_SERVER['REQUEST_METHOD'] === 'POST' ) { |
| 25 | + $article = array(); |
| 26 | + $article['title'] = $_POST['title']; |
| 27 | + $article['content'] = Markdown::defaultTransform($_POST['content']); |
| 28 | + |
| 29 | + $article['saved_at'] = new MongoDate(); |
| 30 | + |
| 31 | + if ( empty($article['title']) || empty($article['content']) ) { |
| 32 | + $data['status'] = 'Please fill out both inputs.'; |
| 33 | + }else { |
| 34 | + // then create a new row in the table |
| 35 | + $conn->posts->insert($article); |
| 36 | + $data['status'] = 'Row has successfully been inserted.'; |
| 37 | + } |
| 38 | + } |
| 39 | + view('admin/create', $data); |
| 40 | + }elseif(isset($_GET['status'])&& $_GET['status']=='edit'){ |
| 41 | + $id = $_REQUEST['id']; |
| 42 | + $data['status'] =null; |
| 43 | + |
| 44 | + if ($_SERVER['REQUEST_METHOD'] === 'POST' ) { |
| 45 | + $article = array(); |
| 46 | + $article['title'] = $_POST['title']; |
| 47 | + $article['content'] = Markdown::defaultTransform($_POST['content']); |
| 48 | + $article['saved_at'] = new MongoDate(); |
| 49 | + |
| 50 | + if ( empty($article['title']) || empty($article['content']) ) { |
| 51 | + $data['status'] = 'Please fill out both inputs.'; |
| 52 | + }else { |
| 53 | + // then create a new row in the table |
| 54 | + $conn->posts->update(array('_id' => new MongoId($id)), $article); |
| 55 | + $data['status'] = 'Row has successfully been update.'; |
| 56 | + } |
| 57 | + } |
| 58 | + //var_dump(Blog\Functions\getById($id,'posts',$conn)); |
| 59 | + |
| 60 | + view('admin/edit',array( |
| 61 | + 'article' => Blog\Functions\getById($id,'posts',$conn), |
| 62 | + 'status' => $data['status'] |
| 63 | + )); |
| 64 | + |
| 65 | + } |
| 66 | + else{ |
| 67 | + $currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number |
| 68 | + $data = Blog\Functions\get($currentPage,'posts',$conn); |
| 69 | + |
| 70 | + |
| 71 | + view('admin/dashboard',array( |
| 72 | + 'currentPage' => $data[0], |
| 73 | + 'totalPages' => $data[1], |
| 74 | + 'cursor' => $data[2], |
| 75 | + |
| 76 | + )); |
| 77 | + |
| 78 | + } |
| 79 | +} |
0 commit comments