From 9090550d241f9f7b3246b24bfd323bd988add749 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Tue, 28 Apr 2020 20:18:17 +0200 Subject: listen to brother Stan and clean some things --- app/class/Mediaopt.php | 236 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 app/class/Mediaopt.php (limited to 'app/class/Mediaopt.php') diff --git a/app/class/Mediaopt.php b/app/class/Mediaopt.php new file mode 100644 index 0000000..8bed1e0 --- /dev/null +++ b/app/class/Mediaopt.php @@ -0,0 +1,236 @@ +type = Model::mediatypes(); + $this->hydrate($datas); + } + + public function readoptions() + { + parse_str($this->options, $datas); + $this->hydrate($datas); + } + + public function generatecontent() + { + $mediamanager = new Modelmedia(); + $medialist = $mediamanager->getlistermedia($this->dir(), $this->type); + if (!$medialist) { + return false; + } else { + $mediamanager->medialistsort($medialist, $this->sortby, $this->order); + + $dirid = str_replace('/', '-', $this->path); + + $div = '
' . PHP_EOL; + + foreach ($medialist as $media) { + $div .= '
'; + $id = 'id="media_' . $media->id() . '"'; + $path = $media->getincludepath(); + $ext = $media->extension(); + if ($media->type() == 'image') { + $div .= '' . $media->id() . ''; + } elseif ($media->type() == 'sound') { + $div .= '
' . PHP_EOL; + } + + $div .= '
' . PHP_EOL; + + 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, Model::MEDIA_SORTBY)) { + $sortby = 'id'; + } + if ($this->sortby === $sortby) { + $order = $this->order * -1; + } else { + $order = $this->order; + } + $query = ['path' => $this->path, 'sortby' => $sortby, 'order' => $order]; + if (array_diff(self::TYPES, $this->type) != []) { + $query['type'] = $this->type; + } + return '?' . urldecode(http_build_query($query)); + } + + public function getpathadress(string $path): string + { + $query = ['path' => '/' . $path, 'sortby' => $this->sortby, 'order' => $this->order]; + if (array_diff(self::TYPES, $this->type) != []) { + $query['type'] = $this->type; + } + return '?' . urldecode(http_build_query($query)); + } + + 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 urldecode(http_build_query($query)); + } + + /** + * Get the code to insert directly + */ + public function getcode(): string + { + return '%MEDIA?' . $this->getquery() . '%'; + } + + public function getadress(): string + { + return '?' . $this->getquery(); + } + + + // ______________________________________________ G E T ________________________________________________________ + + + public function fullmatch() + { + return $this->fullmatch; + } + + public function options() + { + return $this->options; + } + + /** + * @return string formated like `/media/` + */ + public function path() + { + 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 ________________________________________________________ + + + public function setfullmatch(string $fullmatch) + { + $this->fullmatch = $fullmatch; + } + + + public function setoptions(string $options) + { + if (!empty($options)) { + $this->options = $options; + } + } + + public function setpath(string $path) + { + if (preg_match('%^\/' . rtrim(Model::MEDIA_DIR, DIRECTORY_SEPARATOR) . '%', $path)) { + $this->path = rtrim($path, DIRECTORY_SEPARATOR); + } elseif (!preg_match('%^\/%', $path)) { + $this->path = '/' . Model::MEDIA_DIR . rtrim($path, DIRECTORY_SEPARATOR); + } + } + + public function setsortby(string $sortby) + { + if (in_array($sortby, Model::MEDIA_SORTBY)) { + $this->sortby = $sortby; + } + } + + public function setorder(int $order) + { + if ($order === -1 || $order === 1) { + $this->order = $order; + } + } + + public function settype($type) + { + if (is_array($type)) { + $this->type = array_intersect(Model::mediatypes(), array_unique($type)); + } + } +} -- cgit v1.2.3