/**
  * Delete selected gallery
  * Gallery must be empty, with no child galleries and no related articles
  */
 public static function deleteGalleryAction($mysqli)
 {
     // get posted gallery ID
     if (!empty($_POST["gallery"])) {
         // check for given gallery in DB
         include_once getcwd() . '/scripts/data-helpers/elrh_db_extractor.php';
         $result = ELRHDataExtractor::retrieveRow($mysqli, "SELECT g.id, (SELECT count(*) FROM elrh_gallery_images i WHERE i.gallery = g.id) AS images, (SELECT count(*) FROM elrh_gallery_galleries c WHERE c.parent = g.id) AS children, (SELECT count(*) FROM elrh_articles a WHERE a.gallery = g.id) AS articles FROM elrh_gallery_galleries g WHERE g.id='" . mysqli_real_escape_string($mysqli, $_POST["gallery"]) . "'");
         if (!empty($result) && $result[0] != "db_error") {
             // gallery details loaded
             if ($result["images"] == 0 && $result["children"] == 0 && $result["articles"] == 0) {
                 // perform delete
                 include_once getcwd() . '/scripts/data-helpers/elrh_db_manipulator.php';
                 $query = ELRHDataManipulator::deleteRecord($mysqli, "DELETE FROM elrh_gallery_galleries WHERE id='" . mysqli_real_escape_string($mysqli, $_POST["gallery"]) . "'");
                 if ($query) {
                     // gallery edited
                     return "admin_delete_gallery_success";
                 } else {
                     // delete query wasn't successful
                     return "admin_delete_gallery_fail";
                 }
             } else {
                 // cannot delete
                 return "admin_delete_gallery_restricted";
             }
         } else {
             // wrong gallery id
             return "admin_gallery_wrongid";
         }
     } else {
         // input not set correctly
         return "admin_gallery_noid";
     }
 }
 /**
  * Move selected image forwards in gallery (ord++)
  */
 public static function moveImageForwardsAction($mysqli, $iid)
 {
     // check given id
     if (!empty($iid)) {
         // check for given image in DB
         include_once getcwd() . '/scripts/data-helpers/elrh_db_extractor.php';
         $result = ELRHDataExtractor::retrieveRow($mysqli, "SELECT id, prev, next FROM elrh_gallery_images WHERE id='" . mysqli_real_escape_string($mysqli, $iid) . "'");
         if (!empty($result) && $result[0] != "db_error") {
             // image details loaded
             // check if image isn't last in gallery
             if ($result["next"] > 0) {
                 // 1st - get info for next image
                 $next = ELRHDataExtractor::retrieveRow($mysqli, "SELECT id, next FROM elrh_gallery_images WHERE id='" . $result["next"] . "'");
                 // 2nd - place original image one position "forward"
                 include_once getcwd() . '/scripts/data-helpers/elrh_db_manipulator.php';
                 $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_images SET ord=ord+1, prev='" . $result["next"] . "', next='" . $next["next"] . "' WHERE id='" . $result["id"] . "'");
                 // 3rd - place prev image one position "backward"
                 if ($query) {
                     $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_images SET ord=ord-1, prev='" . $result["prev"] . "', next='" . $result["id"] . "' WHERE id='" . $result["next"] . "'");
                 }
                 // 3rd - re-link next of next image
                 if ($query) {
                     $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_images SET prev='" . $result["id"] . "' WHERE id='" . $next["next"] . "'");
                 }
                 // 4th re-link prev of original image
                 if ($query) {
                     if ($result["prev"] > 0) {
                         $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_images SET next='" . $result["next"] . "' WHERE id='" . $result["prev"] . "'");
                     }
                 }
                 // inform about result
                 if ($query) {
                     // gallery edited
                     return "admin_move_forwards_success";
                 } else {
                     // delete query wasn't successful
                     return "admin_move_forwards_fail";
                 }
             } else {
                 // image is last
                 return "admin_image_last";
             }
         } else {
             // wrong image id
             return "admin_image_wrongid";
         }
     } else {
         // input not set correctly
         return "admin_image_noid";
     }
 }
 /**
  * Delete selected image
  */
 public static function deleteImageAction($mysqli)
 {
     // get posted image ID
     if (!empty($_POST["item"])) {
         // check for given image in DB
         include_once getcwd() . '/scripts/data-helpers/elrh_db_extractor.php';
         $result = ELRHDataExtractor::retrieveRow($mysqli, "SELECT id, gallery, ord, image, prev, next FROM elrh_gallery_images WHERE id='" . mysqli_real_escape_string($mysqli, $_POST["item"]) . "'");
         if (!empty($result) && $result[0] != "db_error") {
             // image details loaded
             include_once getcwd() . '/scripts/data-helpers/elrh_db_manipulator.php';
             // 1st - perform delete itself
             $query = ELRHDataManipulator::deleteRecord($mysqli, "DELETE FROM elrh_gallery_images WHERE id='" . $result["id"] . "'");
             // 2nd - delete related image files
             if ($query) {
                 unlink(getcwd() . "/content/gallery/" . $result["image"]);
                 unlink(getcwd() . "/content/gallery/thumbs/" . $result["image"]);
             }
             // 3rd - re-link prev and next images in gallery
             if ($query) {
                 if ($result["prev"] > 0) {
                     $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_images SET next='" . $result["next"] . "' WHERE id='" . $result["prev"] . "'");
                 }
             }
             if ($query) {
                 if ($result["next"] > 0) {
                     $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_images SET prev='" . $result["prev"] . "' WHERE id='" . $result["next"] . "'");
                 }
             }
             // 4th - adjust ord (ord--) for next and all other images
             if ($query) {
                 if ($result["next"] > 0) {
                     $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_images SET ord=ord-1 WHERE ord>'" . $result["ord"] . "' AND gallery='" . $result["gallery"] . "'");
                 }
             }
             // h4x for keeping "current_gallery" info
             $_POST["item"] = $result["gallery"];
             // inform about result
             if ($query) {
                 // gallery edited
                 return "admin_delete_image_success";
             } else {
                 // delete query wasn't successful
                 return "admin_delete_image_fail";
             }
         } else {
             // wrong image id
             return "admin_image_wrongid";
         }
     } else {
         // input not set correctly
         return "admin_image_noid";
     }
 }