Advertisement
Guest User

Help

a guest
Oct 24th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.37 KB | None | 0 0
  1. <?php
  2. date_default_timezone_set("Asia/Manila");
  3.  
  4. include('includes/header.php');
  5. require_once('../connection.php');
  6. include('includes/functions.php');
  7. include('includes/html_form_functions.php');
  8. // get information of current user logged in for later use
  9. // $current_page = $_POST['page'];
  10.  
  11. if (!$_SESSION['logged_in']) {
  12.     header('Location: login.php');
  13.     die();
  14. } else {
  15.     // get page_name
  16.     if (isset($_GET['page'])) {
  17.         $selected_page = select_page($_GET['page']);
  18.         // check if page exist, if not then redirect
  19.         if ($selected_page['page_id'] != $_GET['page']) {
  20.             redirect_to("index.php?p={$_GET['p']}");
  21.             exit();
  22.         } else {
  23.             $parent_id = $_GET['page'];
  24.             $page_name = select_page($_GET['page']);
  25.         }
  26.     } else {
  27.         $page_name = null;
  28.     }
  29.  
  30.     if (isset($_POST['submit'])) {
  31.  
  32.         // handle image file
  33.  
  34.         if (isset($_FILES['file'])) {
  35.             $name = $_FILES['file']['name'];
  36.  
  37.             $allowed = ['png', 'jpg', 'gif', 'bmp'];
  38.  
  39.             $path = '../uploads/';
  40.             $temp = $_FILES['file']['tmp_name'];
  41.             $ext = explode('.', $name);
  42.             $ext = strtolower(end($ext));
  43.             $file = uniqid('', true) . time() . '.' . $ext;
  44.  
  45.             if (in_array($ext, $allowed) && move_uploaded_file($temp, '../uploads/' . $file)) {
  46.  
  47.                 $query = "INSERT INTO images (name,image_path) VALUES ('$file' , '$path')";
  48.                 $pdo->query($query);
  49.  
  50.             } else {
  51.                 $message = "Invalid image type.";
  52.             }
  53.         }
  54.  
  55.         // validate user inputs
  56.         if (isset($_POST['article-title'], $_POST['article-content'])) {
  57.             $article_title = $_POST['article-title'];
  58.             $article_content = $_POST['article-content'];
  59.  
  60.             if (empty($article_title) or empty($article_content)) {
  61.                 $message = 'Please fill all fields';
  62.             } else {
  63.                 // execute code if no errors
  64.                 $article_title = $_POST['article-title'];
  65.                 $article_content = $_POST['article-content'];
  66.  
  67.                 $current_user = get_user_information($_SESSION['username']);
  68.                 $author_id = $current_user['id'];
  69.  
  70.                 $date_added = date('Y-m-d H:i:s');
  71.                 $parent_id = $_GET['page'];
  72.  
  73.                 $query = "INSERT INTO article
  74.                 (title,content,author_id,date_added,parent_id)
  75.                 VALUES (:article_title,:article_content,:author_id,:date_added,:parent_id)";
  76.                 try {
  77.                     global $pdo;
  78.                     $stmt = $pdo->prepare($query);
  79.                     $stmt->bindParam(":article_title", $article_title);
  80.                     $stmt->bindParam(":article_content", $article_content);
  81.                     $stmt->bindParam(":author_id", $author_id);
  82.                     $stmt->bindParam(":date_added", $date_added);
  83.                     $stmt->bindParam(":parent_id", $parent_id);
  84.  
  85.                     $stmt->execute();
  86.                     if ($stmt->rowCount() == 1) {
  87.                         $message = "Added Successfully!";
  88.  
  89.                         createLog("Add Article");
  90.  
  91.                     } else {
  92.                         $message = "Could Not Add";
  93.                     }
  94.                 } catch (PDOException $e) {
  95.                     echo $e->getMessage();
  96.                 } //end catch
  97.                 // header("Location: admission.php");
  98.             } //end else
  99.         } //end: isset($_POST['article-title'] , $_POST['article-content']
  100.     } elseif (isset($_POST['cancel'])) {
  101.         redirect_to("index.php?p={$_GET['p']}");
  102.     }
  103. } //end else
  104.  
  105. ?>
  106.  
  107.     <div id="index-container">
  108.         <div id="index-header">
  109.             <a href="index.php?p"><img src="images/logo-with-abbr.png" id="logo"/></a>
  110.  
  111.             <p class="clear"/>
  112.  
  113.             <div id="top-nav">
  114.                 <ul>
  115.                     <li><a href="#">Users</a> <span class="separator"> | </span></li>
  116.                     <li><a href="logs.php?p=log_viewer">Log Viewer</a> <span class="separator"> | </span></li>
  117.                     <li><a href="logout.php" onclick="return confirm('Logout?');">Log out</a> <span
  118.                             class="separator"> | </span></li>
  119.                     <li><a href="../index.php" target="_blank"><span id="view-site-style">View Site</span></a></li>
  120.                 </ul>
  121.             </div>
  122.             <p class="clear"/>
  123.         </div>
  124.  
  125.         <div id="index-article">
  126.             <div id="menu-nav">
  127.                 <div id="menu-nav-navigation">
  128.                     <ul id="menu-list">
  129.  
  130.                         <li class=<?php if (isMenuSelected($_GET['p'], 'home')) {
  131.                             echo "current";
  132.                         } else {
  133.                             echo " ";
  134.                         } ?>><a href="homepage.php?p=home" class="link-buttons"> Home</a>
  135.                         </li>
  136.  
  137.                         <li class=<?php if (isMenuSelected($_GET['p'], 'admission')) {
  138.                             echo "current";
  139.                         } else {
  140.                             echo " ";
  141.                         } ?>><a href="index.php?p=admission" class="link-buttons">Admission</a>
  142.                         </li>
  143.  
  144.  
  145.                         <li class=<?php if (isMenuSelected($_GET['p'], 'announcement')) {
  146.                             echo "current";
  147.                         } else {
  148.                             echo "";
  149.                         } ?>><a href="announcement.php?p=announcement" class="link-buttons">Announcement</a>
  150.                         </li>
  151.  
  152.  
  153.                         <li class=<?php if (isMenuSelected($_GET['p'], 'news and events')) {
  154.                             echo "current";
  155.                         } else {
  156.                             echo "";
  157.                         } ?>><a href="index.php?p=news+and+events" class="link-buttons">News and Events</a>
  158.                         </li>
  159.  
  160.                         <li class=<?php if (isMenuSelected($_GET['p'], 'about')) {
  161.                             echo "current";
  162.                         } else {
  163.                             echo "";
  164.                         } ?>><a href="index.php?p=about" class="link-buttons">About </a>
  165.                         </li>
  166.  
  167.                         <li class=<?php if (isMenuSelected($_GET['p'], 'contact us')) {
  168.                             echo "current";
  169.                         } else {
  170.                             echo "";
  171.                         } ?>><a href="index.php?p=contact+us" class="link-buttons">Contact </a>
  172.                         </li>
  173.  
  174.                         <li class=<?php if (isMenuSelected($_GET['p'], 'image gallery')) {
  175.                             echo "current";
  176.                         } else {
  177.                             echo "";
  178.                         } ?>>
  179.                             <a href="admin_gallery.php?p=image+gallery" class="link-buttons">Image Gallery</a>
  180.                         </li>
  181.                         <!--  <a href="add_news.php" ><li >News Content</li></a>
  182.                          <a href="#" ><li >Events</li></a> -->
  183.                     </ul>
  184.                 </div>
  185.                 <!-- end menu-nav-navigation -->
  186.             </div>
  187.             <!--  END MENU-NAV -->
  188.  
  189.             <div id="article-content">
  190.                 <p id="article-content-head">Add Article: <span
  191.                         id="page-name"><?php echo $page_name['page_name']; ?></span></p>
  192.  
  193.                 <form action="add_article.php?page=<?php echo urlencode($_GET['page']) . "&p=" . $_GET['p']; ?>"
  194.                       method="POST"
  195.                       id="add-article-form" class="my_form">
  196.                     <?php if (isset($message)) { ?>
  197.                         <div class="alert alert-info alert-dismissable">
  198.                             <button type="button" class="close" data-dismiss="alert"
  199.                                     aria-hidden="true">
  200.                                 &times;
  201.                             </button>
  202.                             <?php echo $message; ?>
  203.                         </div>
  204.                     <?php } ?>
  205.                     <label for="add-article-title">Title</label>
  206.                     <br/>
  207.                     <input type="text" size="50" maxlength="42" name="article-title" id="add-article-title"/>
  208.                     <br/>
  209.                     <label for="add-article-content">Content</label>
  210.                     <br/>
  211.                     <textarea name="article-content" id="add-article-content"></textarea>
  212.                     <br/>
  213.                 </form>
  214.  
  215.                 <div id="upload-file">
  216.                     <form action="add_article.php?page=<?php echo urlencode($_GET['page']) . "&p=" . $_GET['p']; ?>"
  217.                           method="POST" enctype="multipart/form-data" id="image-uploader">
  218.                         <label>Upload image</label>
  219.                         <input type="file" name="file">
  220.                         <label for>Or Remove image</label>
  221.                         <br/>
  222.                         <input type="button" name="remove" id="btn-remove" onclick="clearFileInputField('upload-file')"
  223.                                href="javascript:noAction();" value="Remove">
  224.                     </form>
  225.                 </div>
  226.  
  227.                 <button class="btn btn-success" name="submit" id="btn-save" onclick="submitForms()"><span
  228.                         class="glyphicon glyphicon-floppy-save"></span>
  229.                     Save
  230.                 </button>
  231.                 <button class="btn btn-success" name="cancel" id="btn-cancel">
  232.                     Cancel
  233.                 </button>
  234.  
  235.             </div>
  236.             <!-- end article-content -->
  237.         </div>
  238.         <!-- END index-article -->
  239.  
  240.         <!-- DO NOT DELETE THIS SECTION -->
  241.         <div id="index-footer">
  242.             <p>DWNHS CMS &copy;2014</p>
  243.         </div>
  244.     </div><!-- end index-container -->
  245.  
  246.     <script>
  247.         function clearFileInputField(tagId) {
  248.             document.getElementById(tagId).innerHTML =
  249.                 document.getElementById(tagId).innerHTML;
  250.         }
  251.  
  252.         submitForms = function(){
  253.             document.getElementById("add-article-form").submit();
  254.             document.getElementById("image-uploader").submit();
  255.         }
  256.     </script>
  257. <?php
  258. include_once('includes/footer.php');
  259.  
  260. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement