modules-galleries-nevena/module.php

131 lines
3.8 KiB
PHP

<?php
use Lewp\FileHierarchy;
use Lewp\Resolve;
use Lewp\RwdPicture;
return new class extends Lewp\Module
{
const KEY_START_LANDSCAPE_ID = "start_landscape_id";
const KEY_START_PORTRAIT_ID = "start_portrait_id";
const KEY_START_MODULE_ID = "start_module_id";
const KEY_GALLERY_FOLDER_ID = "gallery_folder_id";
private function createBigImageContainer()
{
return $this->createAndSetupElement(
'div',
'',
[
'class' => 'big-image'
]
);
}
private function createBigImage(
string $landscape_id,
string $portrait_id,
string $module_id
) {
$rwd = new RwdPicture($landscape_id, $portrait_id, $module_id);
return $this->importNode($rwd->getPictureTag(), true);
}
private function bigImageAvailable(array $options) : bool
{
$retval = true;
if (!isset($options[self::KEY_START_LANDSCAPE_ID])) {
$retval = false;
}
if (!isset($options[self::KEY_START_PORTRAIT_ID])) {
$retval = false;
}
if (!isset($options[self::KEY_START_MODULE_ID])) {
$retval = false;
}
return $retval;
}
private function galleryAvailable(array $options) : bool
{
$retval = true;
if (!isset($options[self::KEY_GALLERY_FOLDER_ID])) {
$retval = false;
}
return $retval;
}
private function createGallery(array $options)
{
$div = $this->createAndSetupElement(
'div',
'',
[
'class' => 'gallery-images'
]
);
// get all filenames
$folderpaths = $this->findFolderPaths('res-images');
for ($i = 0; $i < sizeof($folderpaths); ++$i) {
if ($folderpaths[$i] === false) {
continue;
}
// folder path of gallery
$gallery_folder = Resolve::toFilepath([
$folderpaths[$i],
$options[self::KEY_GALLERY_FOLDER_ID]
]);
// all images in the folder
$gallery_filenames = scandir($gallery_folder);
// check if folder is empty or not available
if (
($gallery_filenames === false)
|| (sizeof($gallery_filenames) == 2)
) {
continue;
}
foreach ($gallery_filenames as $image) {
if ($image === '..' || $image === '.') {
continue;
}
$landscape =
$options[self::KEY_GALLERY_FOLDER_ID]
.Resolve::ID_SEPARATOR
.$image;
$rwd = new RwdPicture(
$landscape,
$landscape,
$this->getModuleId()
);
$div->appendChild(
$this->importNode($rwd->getPictureTag(), true)
);
}
break; // the loop should only run one time to the end
}
return $div;
}
public function run(array $options = []) : bool
{
$big_image_container = $this->createBigImageContainer();
if ($this->bigImageAvailable($options)) {
$big_image = $this->createBigImage(
$options[self::KEY_START_LANDSCAPE_ID],
$options[self::KEY_START_PORTRAIT_ID],
$options[self::KEY_START_MODULE_ID]
);
$big_image_container->appendChild($big_image);
}
$this->appendChild($big_image_container);
if ($this->galleryAvailable($options)) {
$gallery = $this->createGallery($options);
$this->appendChild($gallery);
}
return true;
}
};