From 227a2a35d64db87d889f7a44101f11a117b8e06a Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Sat, 18 Jan 2020 16:35:20 +0100 Subject: refactoring medialist --- app/class/Controllermedia.php | 16 ++---- app/class/Medialist.php | 110 ++++++++++++++++++++++++++++++++++-------- app/class/Model.php | 4 +- app/class/Modelmedia.php | 50 ++++++++++--------- app/class/Modelrender.php | 7 +-- 5 files changed, 125 insertions(+), 62 deletions(-) (limited to 'app/class') diff --git a/app/class/Controllermedia.php b/app/class/Controllermedia.php index b6b90c0..6a84028 100644 --- a/app/class/Controllermedia.php +++ b/app/class/Controllermedia.php @@ -33,26 +33,18 @@ class Controllermedia extends Controller throw new Exception("Media error : Cant create /media/thumbnail folder"); } + $mediaopt = new Medialist($_GET); - $dir = rtrim($_GET['path'] ?? Model::MEDIA_DIR, DIRECTORY_SEPARATOR); - $sortby = isset($_GET['sortby']) ? $_GET['sortby'] : 'id'; - $order = isset($_GET['order']) ? $_GET['order'] : '1'; - $type = $_GET['type'] ?? ['image', 'video', 'sound', 'other']; - $opt = ['dir' => $dir, 'sortby' => $sortby, 'order' => $order, 'type' => $type]; - - if(is_dir($dir)) { - $medialist = $this->mediamanager->getlistermedia($dir . DIRECTORY_SEPARATOR); - $faviconlist = $this->mediamanager->getlistermedia(Model::FAVICON_DIR); + if(is_dir($mediaopt->dir())) { + $medialist = $this->mediamanager->medialistopt($mediaopt); $dirlist = $this->mediamanager->listdir(Model::MEDIA_DIR); $pathlist = []; - $this->mediamanager->listpath($dirlist, '', $pathlist); - $this->mediamanager->medialistsort($medialist, $sortby, $order); - $this->showtemplate('media', ['medialist' => $medialist, 'faviconlist' => $faviconlist, 'dirlist' => $dirlist, 'pathlist' =>$pathlist, 'dir' => $dir, 'opt' => $opt]); + $this->showtemplate('media', ['medialist' => $medialist, 'dirlist' => $dirlist, 'pathlist' =>$pathlist, 'mediaopt' => $mediaopt]); } else { $this->routedirect('media'); } diff --git a/app/class/Medialist.php b/app/class/Medialist.php index da5ba4f..d36e5d1 100644 --- a/app/class/Medialist.php +++ b/app/class/Medialist.php @@ -7,8 +7,8 @@ class Medialist /** @var string full regex match */ protected $fullmatch; - /** @var string options */ - protected $options = ''; + /** @var string full filter code line */ + protected $filter = ''; /** @var string directory of media */ protected $path = ''; @@ -19,16 +19,20 @@ class Medialist /** @var int */ protected $order = 1; + /** @var array list of media type to display */ + protected $type = ['image', 'sound', 'video', 'other']; + /** @var int display media contents*/ protected $display = 1; /** @var int display download links*/ protected $links = 0; - /** @var string ouput html code generated*/ - protected $content = ''; + /** @var string display the file name of the file */ + protected $filename = 0; - const SORT_BY_OPTIONS = ['id', 'size', 'type']; + const SORT_BY_FILTER = ['id', 'size', 'type']; + const TYPES = ['image', 'sound', 'video', 'other']; @@ -39,8 +43,6 @@ class Medialist public function __construct(array $datas = []) { $this->hydrate($datas); - $this->readoptions(); - $this->generatecontent(); } public function hydrate($datas) @@ -54,16 +56,16 @@ class Medialist } } - public function readoptions() + public function readfilter() { - parse_str($this->options, $datas); + parse_str($this->filter, $datas); $this->hydrate($datas); } public function generatecontent() { $mediamanager = new Modelmedia(); - $medialist = $mediamanager->getlistermedia(Model::MEDIA_DIR . $this->path . '/'); + $medialist = $mediamanager->getlistermedia($this->dir(), $this->type); if (!$medialist) { $this->content = 'RENDERING ERROR : path : ' . Model::MEDIA_DIR . $this->path . '/ does not exist'; return false; @@ -91,10 +93,44 @@ class Medialist $div .= '' . PHP_EOL; - $this->content = $div; + return $div; + } + } + + /** + * Generate link adress for table header + * + * @param string $sortby + * @return string link adress + */ + public function getsortbyadress(string $sortby) : string + { + if(!in_array($sortby, self::SORT_BY_FILTER)) { + $sortby = 'id'; + } + if ($this->sortby === $sortby) { + $order = $this->order * -1; + } else { + $order = $this->order; + } + $query = ['path' => $this->path, 'sortby' => $sortby, 'order' => $order, 'type' => $this->type]; + return '?' . urldecode(http_build_query($query)); + + } + + public function getpathadress(string $path) : string + { + $query = ['path' => '/' . $path, 'sortby' => $this->sortby, 'order' => $this->order, 'type' => $this->type]; + return '?' . urldecode(http_build_query($query)); + } - return true; + public function getquery() + { + $query = ['path' => $this->path, 'sortby' => $this->sortby, 'order' => $this->order]; + if(array_diff( self::TYPES, $this->type) != []) { + $query['type'] = $this->type; } + return '%MEDIA?' . urldecode(http_build_query($query)). '%'; } @@ -106,17 +142,38 @@ class Medialist return $this->fullmatch; } - public function options() + public function filter() { - return $this->options; + return $this->filter; } - public function content() + public function path() { - return $this->content; + return $this->path; } + /** + * @return string formated like `media//` + */ + public function dir() + { + return ltrim($this->path, '/') . '/'; + } + public function sortby() + { + return $this->sortby; + } + + public function order() + { + return $this->order; + } + + public function type() + { + return $this->type; + } // __________________________________________________ S E T ____________________________________________________________ @@ -127,21 +184,25 @@ class Medialist } - public function setoptions(string $options) + public function setfilter(string $filter) { - if (!empty($options)) { - $this->options = $options; + if (!empty($filter)) { + $this->filter = $filter; } } public function setpath(string $path) { - $this->path = $path; + if(preg_match('%^\/' . Model::MEDIA_DIR . '%', $path)) { + $this->path = $path; + } elseif (!preg_match('%^\/%', $path)) { + $this->path = '/' . Model::MEDIA_DIR . $path; + } } public function setsortby(string $sortby) { - if (in_array($sortby, self::SORT_BY_OPTIONS)) { + if (in_array($sortby, self::SORT_BY_FILTER)) { $this->sortby = $sortby; } } @@ -152,4 +213,11 @@ class Medialist $this->order = $order; } } + + public function settype($type) + { + if(is_array($type)) { + $this->type = array_intersect(self::TYPES, array_unique($type)); + } + } } diff --git a/app/class/Model.php b/app/class/Model.php index 81ad626..bb07fea 100644 --- a/app/class/Model.php +++ b/app/class/Model.php @@ -11,8 +11,8 @@ abstract class Model const ICONS_DIR = 'assets' . DIRECTORY_SEPARATOR .'icons' . DIRECTORY_SEPARATOR; const FONT_DIR = 'fonts' . DIRECTORY_SEPARATOR; const MEDIA_DIR = 'media' . DIRECTORY_SEPARATOR; - const FAVICON_DIR = 'media' . DIRECTORY_SEPARATOR . 'favicon' . DIRECTORY_SEPARATOR; - const THUMBNAIL_DIR = 'media' . DIRECTORY_SEPARATOR . 'thumbnail' . DIRECTORY_SEPARATOR; + const FAVICON_DIR = self::MEDIA_DIR . 'favicon' . DIRECTORY_SEPARATOR; + const THUMBNAIL_DIR = self::MEDIA_DIR . 'thumbnail' . DIRECTORY_SEPARATOR; const TEMPLATES_DIR = '.'. DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR; const RENDER_DIR = 'assets'. DIRECTORY_SEPARATOR . 'render' . DIRECTORY_SEPARATOR; const HTML_RENDER_DIR = 'render' . DIRECTORY_SEPARATOR; diff --git a/app/class/Modelmedia.php b/app/class/Modelmedia.php index 6ac647f..d1da829 100644 --- a/app/class/Modelmedia.php +++ b/app/class/Modelmedia.php @@ -33,16 +33,23 @@ class Modelmedia extends Model } } + public function medialistopt(Medialist $mediaopt) + { + $medialist = $this->getlistermedia($mediaopt->dir(), $mediaopt->type()); + $this->medialistsort($medialist, $mediaopt->sortby(), $mediaopt->order()); + + return $medialist; + } + /** * Display a list of media * * @param string $path - * @param string $sortby - * @param string $order + * @param array $type * * @return array of Media objects */ - public function getlistermedia($dir, $type = "all") + public function getlistermedia($dir, $type = Model::MEDIA_TYPES) { if (is_dir($dir)) { if ($handle = opendir($dir)) { @@ -56,11 +63,7 @@ class Modelmedia extends Model $media->analyse(); - if (in_array($type, self::MEDIA_TYPES)) { - if ($media->type() == $type) { - $list[] = $media; - } - } else { + if (in_array($media->type(), $type)) { $list[] = $media; } } @@ -74,22 +77,6 @@ class Modelmedia extends Model } - - 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 * @@ -103,6 +90,21 @@ class Modelmedia extends Model $order = ($order === 1 || $order === -1) ? $order : 1; return usort($medialist, $this->buildsorter($sortby, $order)); } + + public function buildsorter($sortby, $order) + { + return function ($media1, $media2) use ($sortby, $order) { + $result = $this->mediacompare($media1, $media2, $sortby, $order); + return $result; + }; + } + + public function mediacompare($media1, $media2, $method = 'id', $order = 1) + { + $result = ($media1->$method() <=> $media2->$method()); + return $result * $order; + } + diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 778b327..593a420 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -437,16 +437,17 @@ class Modelrender extends Modelpage */ public function automedialist(string $text) { - preg_match_all('~\%MEDIA\?([a-zA-Z0-9\&=\-\/\%]*)\%~', $text, $out); + preg_match_all('~\%MEDIA\?([a-zA-Z0-9\[\]\&=\-\/\%]*)\%~', $text, $out); foreach ($out[0] as $key => $match) { - $matches[$key] = ['fullmatch' => $match, 'options' => $out[1][$key]]; + $matches[$key] = ['fullmatch' => $match, 'filter' => $out[1][$key]]; } if(isset($matches)) { foreach ($matches as $match) { $medialist = new Medialist($match); - $text = str_replace($medialist->fullmatch(), $medialist->content(), $text); + $medialist->readfilter(); + $text = str_replace($medialist->fullmatch(), $medialist->generatecontent(), $text); } } return $text; -- cgit v1.2.3