Hide folder/image sections if none is present

This commit is contained in:
Keplyx 2018-04-23 17:15:10 +02:00
parent addf1a0997
commit 87268e2061
3 changed files with 150 additions and 63 deletions

View file

@ -221,9 +221,10 @@
} }
#download_album { #download_album {
height: 50px; height: 70px;
width: 200px; width: 200px;
display: inline-flex; display: inline-flex;
position: relative;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
background-color: #1a1a1a; background-color: #1a1a1a;
@ -234,11 +235,20 @@
margin: 5px; margin: 5px;
} }
#download_album p { #download_text {
margin: 0; margin-top: 5px;
position: absolute;
top: 0;
} }
#download_album svg { #album_photo_count {
color: #929ba8;
margin-bottom: 5px;
position: absolute;
bottom: 0;
}
#download_text svg {
width: 24px; width: 24px;
height: 24px; height: 24px;
margin-right: 5px; margin-right: 5px;

View file

@ -1 +0,0 @@
<div class="no_photo"><?= $placeHolder ?></div>

View file

@ -2,8 +2,12 @@
ob_start(); // Start reading html ob_start(); // Start reading html
define("urlParam", "path"); define("urlParam", "path");
define("photoRoot", "photos");
// Get active path from url and prevent from seeing folders before photos/ /**
* Get active path from url and prevent from seeing folders before 'photos/'
* @return string current path
*/
function getActivePath() function getActivePath()
{ {
$dir = $_GET[urlParam]; $dir = $_GET[urlParam];
@ -11,24 +15,35 @@ function getActivePath()
$currentPath = ""; $currentPath = "";
foreach ($folders as $value) { foreach ($folders as $value) {
if ($value != ".." && $value != "." && $value != "") { if ($value != ".." && $value != "." && $value != "") {
$currentPath .= DIRECTORY_SEPARATOR.$value; $currentPath .= DIRECTORY_SEPARATOR . $value;
} }
} }
return $currentPath; return $currentPath;
} }
// Get active folder from the active path /**
function GetActiveFolder($path) { * Get active folder from the active path
* @param string $path path representing the active folder
* @return string active folder name
*/
function GetActiveFolder($path)
{
$dir = explode(DIRECTORY_SEPARATOR, $path); $dir = explode(DIRECTORY_SEPARATOR, $path);
return $dir[sizeof($dir) - 1]; // Last item after / return $dir[sizeof($dir) - 1]; // Last item after /
} }
function isAlbumAvailable($path) { /**
$dir = "photos".$path; * Check whether the current album is available for download as a .zip file
* @param string $path path to search the album in
* @return bool True if an album is available, false otherwise
*/
function isAlbumAvailable($path)
{
$dir = photoRoot . $path;
$files = scandir($dir); $files = scandir($dir);
$valid = false; $valid = false;
foreach ($files as $key => $value) { foreach ($files as $key => $value) {
$path = realpath($dir.DIRECTORY_SEPARATOR.$value); $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) { if (!is_dir($path)) {
$valid = pathinfo($path, PATHINFO_EXTENSION) == "zip"; $valid = pathinfo($path, PATHINFO_EXTENSION) == "zip";
if ($valid) if ($valid)
@ -38,65 +53,123 @@ function isAlbumAvailable($path) {
return $valid; return $valid;
} }
/**
// Get all directories in the specified path * Get all directories in the specified path and creates them on the page
function getDirectories($dir) * @param string $path path to search directories in
*/
function createDirectories($path)
{ {
$dir = "photos".$dir; $path = photoRoot . $path;
$files = scandir($dir); $files = scandir($path);
$displayedItems = 0; $displayedItems = 0;
foreach ($files as $key => $value) { foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value); $realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (is_dir($path) && $value != "." && $value != "..") { if (isValidDirectory($realPath, $value)) {
$folderTitle = $value; $folderTitle = $value;
$folderLink = "?".urlParam."=".getActivePath().DIRECTORY_SEPARATOR.$value; $folderLink = "?" . urlParam . "=" . getActivePath() . DIRECTORY_SEPARATOR . $value;
include("includes/photos/folder_template.php"); include("includes/photos/folder_template.php");
$displayedItems++; $displayedItems++;
} }
} }
if ($displayedItems == 0) {
$placeHolder = "Pas d'autres albums !";
include("includes/photos/place_holder.php");
}
} }
// Get all photos in the specified path /**
function getPhotos($dir) * Get all photos in the specified path and creates them on the page
* @param string $path path to search photos in
*/
function createPhotos($path)
{ {
$dir = "photos_thumb".$dir; $path = photoRoot . "_thumb" . $path;
$files = scandir($dir); $files = scandir($path);
$displayedItems = 0; $displayedItems = 0;
foreach ($files as $key => $value) { foreach ($files as $key => $value) {
$path = realpath($dir.DIRECTORY_SEPARATOR.$value); $realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) { if (isValidImage($realPath)) {
$ext = pathinfo($path, PATHINFO_EXTENSION); $imageSrc = $path . DIRECTORY_SEPARATOR . $value;
if ($ext == "jpg" || $ext == "jpeg" || $ext == "png") { $imageId = "photo-" . $displayedItems;
$imageSrc = $dir.DIRECTORY_SEPARATOR.$value; include("includes/photos/photo_template.php");
$imageId = "photo-".$displayedItems; $displayedItems++;
include("includes/photos/photo_template.php");
$displayedItems++;
}
} }
} }
if ($displayedItems == 0) {
$placeHolder = "Pas de photos ici !";
include("includes/photos/place_holder.php");
}
} }
// Creates buttons representing the actual path for easier navigation /**
function generatePath($dir) * Counts directories in the specified folder
* @param string $path path to search directories in
* @return int directories count
*/
function getDirectoriesCount($path)
{ {
$folders = explode(DIRECTORY_SEPARATOR, $dir); $path = photoRoot . $path;
$files = scandir($path);
$dirCount = 0;
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (isValidDirectory($realPath, $value)) {
$dirCount++;
}
}
return $dirCount;
}
/**
* Counts images in the specified folder
* @param string $path path to search photos in
* @return int photo count
*/
function getPhotoCount($path)
{
$path = photoRoot . $path;
$files = scandir($path);
$fileCount = 0;
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (isValidImage($realPath)) {
$fileCount++;
}
}
return $fileCount;
}
/**
* Check if the given image is valid
* @param string $imagePath absolute path of the image
* @return bool True if the file is a jpg, jpeg or png, false otherwise
*/
function isValidImage($imagePath)
{
$ext = pathinfo($imagePath, PATHINFO_EXTENSION);
return !is_dir($imagePath) && ($ext == "jpg" || $ext == "jpeg" || $ext == "png");
}
/**
* Check if the given folder is valid (is not '.' or '..')
* @param string $directoryPath directory path
* @param string $directory directory name
* @return bool True if the directory is valid, false otherwise
*/
function isValidDirectory($directoryPath, $directory)
{
return is_dir($directoryPath) && $directory != "." && $directory != "..";
}
/**
* Creates buttons representing the actual path for easier navigation
* @param string $path Actual Path
*/
function generatePath($path)
{
$folders = explode(DIRECTORY_SEPARATOR, $path);
$currentPath = ""; $currentPath = "";
$pathTitle = "Menu"; $pathTitle = "Menu";
$pathLink = "?".urlParam."="; $pathLink = "?" . urlParam . "=";
include("includes/photos/path_template.php"); include("includes/photos/path_template.php");
foreach ($folders as $value) { foreach ($folders as $value) {
if ($value != "") { if ($value != "") {
$pathTitle = $value; $pathTitle = $value;
$currentPath .= DIRECTORY_SEPARATOR.$value; $currentPath .= DIRECTORY_SEPARATOR . $value;
$pathLink = "?".urlParam."=".$currentPath; $pathLink = "?" . urlParam . "=" . $currentPath;
include("includes/photos/path_template.php"); include("includes/photos/path_template.php");
} }
} }
@ -120,7 +193,7 @@ function generatePath($dir)
<i id="download" class="fas fa-download"></i> <i id="download" class="fas fa-download"></i>
</a> </a>
<a href="" id="img_big_link"> <a href="" id="img_big_link">
<i id="fullscreen" class="fas fa-expand-arrows-alt" ></i> <i id="fullscreen" class="fas fa-expand-arrows-alt"></i>
</a> </a>
</div> </div>
</div> </div>
@ -134,23 +207,28 @@ function generatePath($dir)
generatePath(getActivePath()); generatePath(getActivePath());
?> ?>
</ul> </ul>
<div class="photos_folder"> <?php if (getDirectoriesCount(getActivePath()) > 0): ?>
<?php <div class="photos_folder">
getDirectories(getActivePath()); <?php
?> createDirectories(getActivePath());
</div> ?>
<?php if(isAlbumAvailable(getActivePath())): ?> </div>
<a download="" href="photos<?php echo getActivePath().DIRECTORY_SEPARATOR.GetActiveFolder(getActivePath())?>.zip" id="download_album"> <?php endif; ?>
<i class="fas fa-download"></i> <?php if (isAlbumAvailable(getActivePath())): ?>
<p>Télécharger l'album</p> <a download=""
href="photos<?php echo getActivePath() . DIRECTORY_SEPARATOR . GetActiveFolder(getActivePath()) ?>.zip"
id="download_album">
<span id="download_text"><i class="fas fa-download"></i>Télécharger l'album</span>
<span id="album_photo_count"><?php echo getPhotoCount(getActivePath()) ?> photos</span>
</a> </a>
<?php endif; ?> <?php endif; ?>
<?php if (getPhotoCount(getActivePath()) > 0): ?>
<div class="photos"> <div class="photos">
<?php <?php
getPhotos(getActivePath()); createPhotos(getActivePath());
?> ?>
</div> </div>
<?php endif; ?>
<script src="assets/scripts/photosScript.js"></script> <script src="assets/scripts/photosScript.js"></script>
<?php <?php
$pageContent = ob_get_clean(); // Store html content in variable $pageContent = ob_get_clean(); // Store html content in variable