1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
<?php
namespace Wcms;
class Medialist extends Item
{
/** @var string full regex match */
protected $fullmatch;
/** @var string full options code line */
protected $options = '';
/** @var string directory of media */
protected $path = '';
/** @var string */
protected $sortby = 'id';
/** @var int */
protected $order = 1;
/** @var array list of media type to display */
protected $type = [];
/** @var int display media contents*/
protected $display = 1;
/** @var int display download links*/
protected $links = 0;
/** @var int display the file name of the file */
protected $filename = 0;
public const TYPES = ['image', 'sound', 'video', 'other'];
// ______________________________________________ F U N ________________________________________________________
public function __construct(array $datas = [])
{
$this->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 = '<div class="medialist" id="' . $dirid . '">' . PHP_EOL;
foreach ($medialist as $media) {
$div .= '<div class="content ' . $media->type() . '">';
$id = 'id="media_' . $media->id() . '"';
$path = $media->getincludepath();
$ext = $media->extension();
if ($media->type() == 'image') {
$div .= '<img alt="' . $media->id() . '" ' . $id . ' src="' . $path . '" >';
} elseif ($media->type() == 'sound') {
$div .= '<audio ' . $id . ' controls src="' . $path . '" </audio>';
} elseif ($media->type() == 'video') {
$source = '<source src="' . $path . '" type="video/' . $ext . '" ' . $id . '>';
$div .= '<video controls>' . $source . '</video>';
} else {
$name = $media->id() . '.' . $ext;
$div .= '<a href="' . $path . '" target="_blank" class="media" ' . $id . '>' . $name . '</a>';
}
$div .= '</div>' . PHP_EOL;
}
$div .= '</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 '%MEDIA?' . urldecode(http_build_query($query)) . '%';
}
// ______________________________________________ G E T ________________________________________________________
public function fullmatch()
{
return $this->fullmatch;
}
public function options()
{
return $this->options;
}
/**
* @return string formated like `/media/<folder>`
*/
public function path()
{
return $this->path;
}
/**
* @return string formated like `media/<folder>/`
*/
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));
}
}
}
|