Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.67 KB | None | 0 0
  1. <?php
  2. require_once '../src/lib.php';
  3. require_once '../connection.php';
  4. session_start();
  5. if (!isset($_SESSION['login'])) {
  6.     header('Location: index.php');
  7.     exit();
  8. }
  9. //if for every page for logged user!!!
  10. $user = loggedUser($connection);
  11. ?>
  12.  
  13. <!DOCTYPE html>
  14. <html lang="pl">
  15. <?php
  16. include '../widget/head.php';
  17. ?>
  18. <body>
  19. <?php
  20. include '../widget/header.php';
  21. ?>
  22. <div class="container text-center">
  23.     <h1>World Cup 2018</h1>
  24.     <hr/>
  25.  
  26.     <?php
  27.  
  28.         if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_POST['action'] == 'saveImage') {
  29.             if (isset($_POST['delete_image']) && isset($_POST['image_id'])) {
  30.                 $imageId = $_POST['image_id'];
  31.                 $path = ImageRepository::loadImagePath($connection, $user->getId());
  32.                 unlink($path);
  33.                 $toDelete = ImageRepository::loadImageById($connection, $imageId);
  34.                 ImageRepository::delete($connection, $toDelete);
  35.                 header('Location: editImages.php');
  36.             }
  37.  
  38.             if (($_FILES['imageFile']['error'] == 0) && ($_FILES['imageFile']['type'] == 'image/jpeg')
  39.                 && isset($_POST['userId']) && isset($_POST['image_id'])) {
  40.                 $imageId = $_POST['image_id'];
  41.                 $userId = $_POST['userId'];
  42.  
  43.                 $filename = $_FILES['imageFile']['name'];
  44.                 $path = '../content/' . $userId . '/';
  45.                 if (!file_exists($path)) {
  46.                     mkdir($path);
  47.                 }
  48.                 $path .= $filename;
  49.                 if(!file_exists($path)) {
  50.                     $upload = move_uploaded_file($_FILES['imageFile']['tmp_name'], $path);
  51.                 } else {
  52.                     echo "<div class=\"text-center alert alert-danger\">";
  53.                     echo '<strong>Zdjęcie o podanej nazwie już istnieje!</strong>';
  54.                     echo "</div>";
  55.                     die();
  56.                 }
  57.                 if ($upload) {
  58.                     $pathToDelete = ImageRepository::loadImagePath($connection, $user->getId());
  59.                     unlink($pathToDelete);
  60.                     $toDelete = ImageRepository::loadImageById($connection, $imageId);
  61.                     ImageRepository::delete($connection, $toDelete);
  62.  
  63.                     $image = new Image();
  64.                     $image
  65.                         ->setImagePath($path)
  66.                         ->setUserId($_POST['userId']);
  67.                     $upload = ImageRepository::saveToDB($connection, $image);
  68.                     header('Location: editImages.php');
  69.                 } else {
  70.                     echo "<div class=\"text-center alert alert-danger\">";
  71.                     echo '<strong>Wystąpił błąd podczas edycji zdjęcia!</strong>';
  72.                     echo "</div>";
  73.                     die();
  74.                 }
  75.             }
  76.         }
  77.  
  78.     $images = ImageRepository::loadImageDetailsByUserId($connection, $user->getId());
  79.     foreach ($images as $photo) {
  80.         ?>
  81.  
  82.         <div class='img-thumbnail1'>
  83.             <img src="<?php echo $photo['image_path'] ?> " width='450' height='300'/>
  84.             <form method="POST" action="#" enctype="multipart/form-data">
  85.                 <div class="file forms">
  86.                     <input type="file" name="imageFile"/>
  87.                     <input type="hidden" name="userId" value="<?php echo $user->getId(); ?>"/>
  88.                     <input type="hidden" name="image_id" value="<?php echo $photo['id']; ?>"/>
  89.                 </div>
  90.                 <br/>
  91.                 <input type="hidden" name="action" value="saveImage"/>
  92.                 <button type="submit" class="btn btn-warning links">Edytuj zdjęcie</button>
  93.                 <div>
  94.                     <input type="submit" class="btn btn-danger links" name="delete_image" value="Usuń zdjęcie"/>
  95.                     <input type='hidden' name='image_id' value="<?php echo $photo['id']; ?> ">
  96.                 </div>
  97.             </form>
  98.         </div>
  99.         <?php
  100.     }
  101.     ?>
  102.     <hr/>
  103.     <h3><a href="userPanel.php" class="btn btn-default links">Powrót do profilu</a></h3>
  104. </div>
  105. <?php
  106. include '../widget/footer.php';
  107. include '../widget/scripts.php';
  108. ?>
  109. </body>
  110. </html>
  111.  
  112.  
  113. <?php
  114.  
  115. class ImageRepository
  116. {
  117.     public static function saveToDB(PDO $connection, Image $image)
  118.     {
  119.         $id = $image->getId();
  120.         $imagePath = $image->getImagePath();
  121.         $userId = $image->getUserId();
  122.  
  123.         if ($id == -1) {
  124.             $sql = "INSERT INTO images (image_path, user_id) VALUES (:image_path, :user_id)";
  125.  
  126.             $result = $connection->prepare($sql);
  127.             $result->bindParam('image_path', $imagePath);
  128.             $result->bindParam('user_id', $userId);
  129.             $result->execute();
  130.  
  131.             $id = $connection->lastInsertId();
  132.             return true;
  133.         }
  134.         return false;
  135.     }
  136.  
  137.  
  138.     public static function loadImageDetailsByUserId(PDO $connection, $userId)
  139.     {
  140.         $sql = "SELECT * FROM images WHERE user_id = :user_id";
  141.  
  142.         $result = $connection->prepare($sql);
  143.         if (!$result) {
  144.             die("Query Error!" . $connection->errorInfo());
  145.         }
  146.  
  147.         $pathArray = [];
  148.         $result->bindParam('user_id', $userId);
  149.         $result->execute();
  150.  
  151.         while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
  152.             $pathArray[] = $row;
  153.         }
  154.         return $pathArray;
  155.     }
  156.  
  157.     public static function loadImagePath(PDO $connection, $userId)
  158.     {
  159.         $sql = "SELECT image_path FROM images WHERE user_id = :user_id";
  160.  
  161.         $result = $connection->prepare($sql);
  162.         if (!$result) {
  163.             die("Query Error!" . $connection->errorInfo());
  164.         }
  165.  
  166.         $result->bindParam('user_id', $userId);
  167.         $result->execute();
  168.  
  169.         while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
  170.             $path = $row['image_path'];
  171.         }
  172.         return $path;
  173.     }
  174.  
  175.     public static function loadImageById(PDO $connection, $id)
  176.     {
  177.         $sql = "SELECT * FROM images WHERE id = :id";
  178.  
  179.         $result = $connection->prepare($sql);
  180.  
  181.         if (!$result) {
  182.             die("Query Error!" . $connection->errorInfo());
  183.         }
  184.  
  185.         $result->bindParam('id', $id);
  186.         $result->execute();
  187.  
  188.         if ($result->rowCount() > 0) {
  189.             $row = $result->fetch(PDO::FETCH_ASSOC);
  190.             $image = new Image();
  191.             $image
  192.                 ->setId($row['id'])
  193.                 ->setImagePath($row['image_path'])
  194.                 ->setUserId($row['user_id'])
  195.                 ->setCreatedAt($row['created_at']);
  196.  
  197.             return $image;
  198.         }
  199.  
  200.         return false;
  201.     }
  202.  
  203.     public static function delete(PDO $connection, Image $image)
  204.     {
  205.         $id = $image->getId();
  206.  
  207.         if ($id != -1) {
  208.             $sql = "DELETE FROM images WHERE id = :id";
  209.             $result = $connection->prepare($sql);
  210.  
  211.             $result->bindParam('id', $id);
  212.             $result->execute();
  213.  
  214.             if ($result) {
  215.                 $id = -1;
  216.                 return true;
  217.             }
  218.             return false;
  219.         }
  220.         return true;
  221.     }
  222.  
  223.     public static function countAllImagesByUserId(PDO $connection, $userId)
  224.     {
  225.         $sql = "SELECT count(id) as count FROM images WHERE user_id = :user_id";
  226.  
  227.         $result = $connection->prepare($sql);
  228.         $result->bindParam('user_id', $userId);
  229.         $result->execute();
  230.  
  231.         if (!$result) {
  232.             die("Connection Error" . $connection->errorInfo());
  233.         }
  234.  
  235.         if ($result->rowCount() > 0) {
  236.             foreach ($result as $row) {
  237.                 return $row['count'];
  238.             }
  239.         }
  240.  
  241.         return false;
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement