From e75e54d23d262940acd4c7df2a8cc7b7ab1751d5 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Wed, 7 Aug 2019 18:10:00 +0200 Subject: media insertion new synthax working --- app/class/controllermedia.php | 7 +- app/class/medialist.php | 97 ++++++++++++++++----- app/class/modelmedia.php | 196 ++++++++++++++++++++++++------------------ app/class/modelrender.php | 2 +- app/view/templates/media.php | 31 +++++-- composer.json | 2 +- 6 files changed, 220 insertions(+), 115 deletions(-) diff --git a/app/class/controllermedia.php b/app/class/controllermedia.php index 6bc38d9..4200253 100644 --- a/app/class/controllermedia.php +++ b/app/class/controllermedia.php @@ -31,14 +31,19 @@ class Controllermedia extends Controller $dir = rtrim($_GET['path'] ?? Model::MEDIA_DIR, DIRECTORY_SEPARATOR); + $sortby = isset($_GET['sortby']) ? $_GET['sortby'] : 'id'; + $order = isset($_GET['order']) ? $_GET['order'] : '1'; + $opt = ['dir' => $dir, 'sortby' => $sortby, 'order' => $order]; if(is_dir($dir)) { $medialist = $this->mediamanager->getlistermedia($dir . DIRECTORY_SEPARATOR); $faviconlist = $this->mediamanager->getlistermedia(Model::FAVICON_DIR); $dirlist = $this->mediamanager->listdir(Model::MEDIA_DIR); + + $this->mediamanager->medialistsort($medialist, $sortby, $order); - $this->showtemplate('media', ['medialist' => $medialist, 'faviconlist' => $faviconlist, 'dirlist' => $dirlist, 'dir' => $dir]); + $this->showtemplate('media', ['medialist' => $medialist, 'faviconlist' => $faviconlist, 'dirlist' => $dirlist, 'dir' => $dir, 'opt' => $opt]); } else { $this->routedirect('media'); } diff --git a/app/class/medialist.php b/app/class/medialist.php index f3bdac5..156ee2e 100644 --- a/app/class/medialist.php +++ b/app/class/medialist.php @@ -4,18 +4,29 @@ class Medialist { /** @var string full regex match */ protected $fullmatch; + /** @var string options */ protected $options = ''; + /** @var string directory of media */ protected $path = ''; + /** @var string */ protected $sortby = 'id'; - /** @var string */ + + /** @var int */ protected $order = 1; - /** @var string */ - protected $recursive = 'id'; - const SORT_BY_OPTIONS = ['id', 'size']; + /** @var int display media contents*/ + protected $display = 1; + + /** @var int display download links*/ + protected $links = 0; + + /** @var string ouput html code generated*/ + protected $content = ''; + + const SORT_BY_OPTIONS = ['id', 'size', 'type']; @@ -23,22 +34,22 @@ class Medialist - public function __construct(array $datas = []) - { + public function __construct(array $datas = []) + { $this->hydrate($datas); $this->readoptions(); + $this->generatecontent(); + } - } - - public function hydrate($datas) - { - foreach ($datas as $key => $value) { - $method = 'set' . $key; + public function hydrate($datas) + { + foreach ($datas as $key => $value) { + $method = 'set' . $key; - if (method_exists($this, $method)) { - $this->$method($value); - } - } + if (method_exists($this, $method)) { + $this->$method($value); + } + } } public function readoptions() @@ -47,6 +58,43 @@ class Medialist $this->hydrate($datas); } + public function generatecontent() + { + $mediamanager = new Modelmedia(); + $medialist = $mediamanager->getlistermedia(Model::MEDIA_DIR . $this->path . '/'); + if (!$medialist) { + $this->content = 'RENDERING ERROR : path : ' . Model::MEDIA_DIR . $this->path . '/ does not exist'; + return false; + } else { + + $mediamanager->medialistsort($medialist, $this->sortby, $this->order); + + $dirid = str_replace('/', '-', $this->path); + + $div = '
' . PHP_EOL; + + foreach ($medialist as $media) { + $div .= '
'; + if ($media->type() == 'image') { + $div .= '' . $media->id() . ''; + } elseif ($media->type() == 'sound') { + $div .= '
' . PHP_EOL; + } + + $div .= '
' . PHP_EOL; + + $this->content = $div; + + return true; + } + } + // __________________________________________________ G E T ____________________________________________________________ @@ -61,6 +109,11 @@ class Medialist return $this->options; } + public function content() + { + return $this->content; + } + // __________________________________________________ S E T ____________________________________________________________ @@ -74,7 +127,7 @@ class Medialist public function setoptions(string $options) { - if(!empty($options)) { + if (!empty($options)) { $this->options = $options; } } @@ -83,20 +136,18 @@ class Medialist { $this->path = $path; } - + public function setsortby(string $sortby) { - if(in_array($sortby, self::SORT_BY_OPTIONS)) { + if (in_array($sortby, self::SORT_BY_OPTIONS)) { $this->sortby = $sortby; } } public function setorder(int $order) { - if($order === -1 || $order === 1) { + if ($order === -1 || $order === 1) { $this->order = $order; } } - - -} \ No newline at end of file +} diff --git a/app/class/modelmedia.php b/app/class/modelmedia.php index 071483b..a30e902 100644 --- a/app/class/modelmedia.php +++ b/app/class/modelmedia.php @@ -3,10 +3,12 @@ class Modelmedia extends Model { + const MEDIA_SORTBY = ['id', 'size', 'type']; + public function basedircheck() { - if(!is_dir(Model::MEDIA_DIR)) { + if (!is_dir(Model::MEDIA_DIR)) { return mkdir(Model::MEDIA_DIR); } else { return true; @@ -15,7 +17,7 @@ class Modelmedia extends Model public function favicondircheck() { - if(!is_dir(Model::FAVICON_DIR)) { + if (!is_dir(Model::FAVICON_DIR)) { return mkdir(Model::FAVICON_DIR); } else { return true; @@ -24,107 +26,142 @@ class Modelmedia extends Model public function thumbnaildircheck() { - if(!is_dir(Model::THUMBNAIL_DIR)) { + if (!is_dir(Model::THUMBNAIL_DIR)) { return mkdir(Model::THUMBNAIL_DIR); } else { return true; } } - public function getmedia($entry, $dir) + /** + * Get the Media Object + * + * @param string $entry Id of the file + * @param string $dir Directory of media file + * + * @return Media|bool + */ + public function getmedia(string $entry, string $dir) { $fileinfo = pathinfo($entry); - if(isset($fileinfo['extension'])) { - $filepath = $fileinfo['dirname'] . '.' . $fileinfo['extension']; - + if (isset($fileinfo['extension'])) { $datas = array( 'id' => str_replace('.' . $fileinfo['extension'], '', $fileinfo['filename']), 'path' => $dir, 'extension' => $fileinfo['extension'] ); return new Media($datas); - } else { return false; } - - } + /** + * Display a list of media + * + * @param string $path + * @param string $sortby + * @param string $order + * + * @return array of Media objects + */ public function getlistermedia($dir, $type = "all") { - if ($handle = opendir($dir)) { - $list = []; - while (false !== ($entry = readdir($handle))) { - if ($entry != "." && $entry != "..") { - - $media = $this->getmedia($entry, $dir); - - if($media != false) { - - $media->analyse(); - - if (in_array($type, self::MEDIA_TYPES)) { - if ($media->type() == $type) { - $list[] = $media; - } - } else { - $list[] = $media; - } + if (is_dir($dir)) { + if ($handle = opendir($dir)) { + $list = []; + while (false !== ($entry = readdir($handle))) { + if ($entry != "." && $entry != "..") { - } + $media = $this->getmedia($entry, $dir); + if ($media != false) { + $media->analyse(); + if (in_array($type, self::MEDIA_TYPES)) { + if ($media->type() == $type) { + $list[] = $media; + } + } else { + $list[] = $media; + } + } + } } + return $list; } + } else { + return false; } + } + - return $list; + public function mediacompare($media1, $media2, $method = 'id', $order = 1) + { + $result = ($media1->$method() <=> $media2->$method()); + return $result * $order; + } + + public function buildsorter($sortby, $order) + { + return function ($media1, $media2) use ($sortby, $order) { + $result = $this->mediacompare($media1, $media2, $sortby, $order); + return $result; + }; + } + + + /** + * Sort an array of media + * + * @param array $medialist + * @param string $sortby + * @param int order Can be 1 or -1 + */ + public function medialistsort(array &$medialist, string $sortby = 'id', int $order = 1): bool + { + $sortby = (in_array($sortby, self::MEDIA_SORTBY)) ? $sortby : 'id'; + $order = ($order === 1 || $order === -1) ? $order : 1; + return usort($medialist, $this->buildsorter($sortby, $order)); } + public function listfavicon() { $glob = Model::FAVICON_DIR . '*.png'; $faviconlist = glob($glob); $count = strlen(Model::FAVICON_DIR); - $faviconlist = array_map(function($input) use($count) { + $faviconlist = array_map(function ($input) use ($count) { return substr($input, $count); }, $faviconlist); return $faviconlist; - } public function listdir($dir) { - - $result = array(); - - $cdir = scandir($dir); - $result['dirfilecount'] = 0; - foreach ($cdir as $key => $value) - { - if (!in_array($value,array(".",".."))) - { - if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) - { - $result[$value] = $this->listdir($dir . DIRECTORY_SEPARATOR . $value); - } - else - { - $result['dirfilecount'] ++; - } - } - } - - return $result; + $result = array(); + + $cdir = scandir($dir); + $result['dirfilecount'] = 0; + foreach ($cdir as $key => $value) { + if (!in_array($value, array(".", ".."))) { + if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { + $result[$value] = $this->listdir($dir . DIRECTORY_SEPARATOR . $value); + } else { + $result['dirfilecount']++; + } + } + } + + return $result; } /** @@ -138,21 +175,21 @@ class Modelmedia extends Model * * @return bool If upload process is a succes or not */ - function simpleupload(string $index, string $destination, $maxsize = false, $extensions = false, bool $jpgrename = false) : bool + function simpleupload(string $index, string $destination, $maxsize = false, $extensions = false, bool $jpgrename = false): bool { - //Test1: if the file is corectly uploaded + //Test1: if the file is corectly uploaded if (!isset($_FILES[$index]) || $_FILES[$index]['error'] > 0) return false; - //Test2: check file size + //Test2: check file size if ($maxsize !== false && $_FILES[$index]['size'] > $maxsize) return false; - //Test3: check extension - $ext = substr(strrchr($_FILES[$index]['name'],'.'),1); + //Test3: check extension + $ext = substr(strrchr($_FILES[$index]['name'], '.'), 1); if ($extensions !== false && !in_array($ext, $extensions)) return false; - if($jpgrename !== false) { + if ($jpgrename !== false) { $destination .= '.jpg'; } else { $destination .= '.' . $ext; } - //Move to dir + //Move to dir return move_uploaded_file($_FILES[$index]['tmp_name'], $destination); } @@ -164,37 +201,30 @@ class Modelmedia extends Model */ public function multiupload(string $index, string $target) { - if($target[strlen($target)-1] != DIRECTORY_SEPARATOR) - $target .= DIRECTORY_SEPARATOR; - $count=0; - foreach ($_FILES[$index]['name'] as $filename) - { - $fileinfo = pathinfo($filename); - $extension = idclean($fileinfo['extension']); - $id = idclean($fileinfo['filename']); - - $tmp=$_FILES['file']['tmp_name'][$count]; - $count=$count + 1; - $temp = $target . $id .'.' .$extension; - move_uploaded_file($tmp, $temp); - $temp=''; - $tmp=''; - } + if ($target[strlen($target) - 1] != DIRECTORY_SEPARATOR) + $target .= DIRECTORY_SEPARATOR; + $count = 0; + foreach ($_FILES[$index]['name'] as $filename) { + $fileinfo = pathinfo($filename); + $extension = idclean($fileinfo['extension']); + $id = idclean($fileinfo['filename']); + + $tmp = $_FILES['file']['tmp_name'][$count]; + $count = $count + 1; + $temp = $target . $id . '.' . $extension; + move_uploaded_file($tmp, $temp); + $temp = ''; + $tmp = ''; + } } public function adddir($dir, $name) { $newdir = $dir . DIRECTORY_SEPARATOR . $name; - if(!is_dir($newdir)) { + if (!is_dir($newdir)) { return mkdir($newdir); } else { return false; } } - } - - - - -?> \ No newline at end of file diff --git a/app/class/modelrender.php b/app/class/modelrender.php index b5c8552..2d3daf1 100644 --- a/app/class/modelrender.php +++ b/app/class/modelrender.php @@ -414,9 +414,9 @@ class Modelrender extends Modelart if(isset($matches)) { foreach ($matches as $match) { $medialist = new Medialist($match); + $text = str_replace($medialist->fullmatch(), $medialist->content(), $text); } } - return $text; } diff --git a/app/view/templates/media.php b/app/view/templates/media.php index b8dd094..c75ce68 100644 --- a/app/view/templates/media.php +++ b/app/view/templates/media.php @@ -20,7 +20,7 @@ ' . $dirname . ''; @@ -28,17 +28,17 @@ function treecount(array $dir, string $dirname, int $deepness, string $path, str $folder = '├─📁' . $dirname; } echo ''; - echo '' . str_repeat('  ', $deepness) . $folder . ''; + echo '' . str_repeat('  ', $deepness) . $folder . ''; echo '' . $dir['dirfilecount'] . ''; echo ''; foreach ($dir as $key => $value) { if (is_array($value)) { - treecount($value, $key, $deepness + 1, $path . DIRECTORY_SEPARATOR . $key, $currentdir); + treecount($value, $key, $deepness + 1, $path . DIRECTORY_SEPARATOR . $key, $currentdir, $opt); } } } -treecount($dirlist, 'media', 0, 'media', $dir); +treecount($dirlist, 'media', 0, 'media', $dir, $opt); ?> @@ -53,7 +53,17 @@ treecount($dirlist, 'media', 0, 'media', $dir);

-Print the whole content of the folder using this code : %MEDIA:% + +
+ Print this content on your page + +

+ %MEDIA?path=&sortby=&order=% +

+ +
+ +
@@ -72,7 +82,16 @@ Print the whole content of the folder using this code : %MEDIA: -idexttypesizewidthheightlenghcode + + id + ext + type + size + width + height + lengh + code +