/**
  * Add new / Edit selected gallery
  */
 public static function editGalleryAction($mysqli)
 {
     // check for gallery name - MANDATORY atribute
     if (!empty($_POST["name"])) {
         // get posted gallery ID
         if (!empty($_POST["gid"])) {
             // edit existing gallery
             // check if exists
             include_once getcwd() . '/scripts/data-helpers/elrh_db_extractor.php';
             $result = ELRHDataExtractor::retrieveRow($mysqli, "SELECT id FROM elrh_gallery_galleries WHERE id='" . mysqli_real_escape_string($mysqli, $_POST["gid"]) . "'");
             if (!empty($result) && $result[0] != "db_error") {
                 // perform edit action
                 include_once getcwd() . '/scripts/data-helpers/elrh_db_manipulator.php';
                 $query = ELRHDataManipulator::editRecord($mysqli, "UPDATE elrh_gallery_galleries SET name='" . mysqli_real_escape_string($mysqli, $_POST["name"]) . "', parent='" . mysqli_real_escape_string($mysqli, $_POST["parent"]) . "', dscr='" . mysqli_real_escape_string($mysqli, $_POST["dscr"]) . "' WHERE id='" . mysqli_real_escape_string($mysqli, $_POST["gid"]) . "'");
                 if ($query) {
                     // gallery edited
                     return "admin_edit_gallery_success";
                 } else {
                     // edit query wasn't successful
                     return "admin_edit_gallery_fail";
                 }
             } else {
                 // wrong gallery id
                 return "admin_gallery_wrongid";
             }
         } else {
             // add new gallery
             include_once getcwd() . '/scripts/data-helpers/elrh_db_manipulator.php';
             $query = ELRHDataManipulator::addRecord($mysqli, "INSERT INTO elrh_gallery_galleries (parent, author, created, name, dscr) VALUES (" . mysqli_real_escape_string($mysqli, $_POST["parent"]) . ", '" . mysqli_real_escape_string($mysqli, $_SESSION["user"]) . "', Now(), '" . mysqli_real_escape_string($mysqli, $_POST["name"]) . "', '" . mysqli_real_escape_string($mysqli, $_POST["dscr"]) . "')");
             if ($query) {
                 // get newly inserted ID
                 // update it for further loading of gallery details
                 $_POST["gid"] = $mysqli->insert_id;
                 // gallery added
                 return "admin_add_gallery_success";
             } else {
                 // edit query wasn't successful
                 return "admin_add_gallery_fail";
             }
         }
     } else {
         // no name given
         return "admin_edit_gallery_noname";
     }
 }
 /**
  * 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";
     }
 }