|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Media file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package App |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright YetiForce S.A. |
|
8
|
|
|
* @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
9
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App\Layout; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Media class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Media |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string Media table name. */ |
|
20
|
|
|
public const TABLE_NAME_MEDIA = 'u_#__file_upload'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Images. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $images; |
|
28
|
|
|
|
|
29
|
|
|
public static function getImages(): array |
|
30
|
|
|
{ |
|
31
|
|
|
if (null === self::$images) { |
|
|
|
|
|
|
32
|
|
|
self::$images = []; |
|
33
|
|
|
$dataReader = (new \App\Db\Query())->from(static::TABLE_NAME_MEDIA)->where(['status' => 1, 'fieldname' => 'image'])->createCommand()->query(); |
|
34
|
|
|
while ($row = $dataReader->read()) { |
|
35
|
|
|
$path = $row['path']; |
|
36
|
|
|
if (IS_PUBLIC_DIR && 0 === strpos($path, 'public_html/')) { |
|
37
|
|
|
$path = $path = substr($path, 12, \strlen($path)); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
$row['src'] = "{$path}{$row['key']}.{$row['ext']}"; |
|
40
|
|
|
$row['relativePath'] = "{$row['path']}{$row['key']}.{$row['ext']}"; |
|
41
|
|
|
self::$images[$row['key']] = $row; |
|
42
|
|
|
} |
|
43
|
|
|
$dataReader->close(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return self::$images; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get image data. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $key |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getImage(string $key): array |
|
57
|
|
|
{ |
|
58
|
|
|
return self::getImages()[$key] ?? []; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Delete image file. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $key |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function removeImage(string $key): bool |
|
69
|
|
|
{ |
|
70
|
|
|
$dbCommand = \App\Db::getInstance()->createCommand(); |
|
71
|
|
|
return ($path = self::getImages()[$key]['relativePath'] ?? '') && file_exists($path) && $dbCommand->delete(self::TABLE_NAME_MEDIA, ['key' => $key])->execute() && unlink($path); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get image URL. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $key |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function getImageUrl(string $key): string |
|
82
|
|
|
{ |
|
83
|
|
|
$path = self::getImages()[$key]['src'] ?? ''; |
|
84
|
|
|
if ($path && !file_exists(self::getImages()[$key]['relativePath'])) { |
|
85
|
|
|
$path = ''; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $path; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get image HTML. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $value json |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function getImageHtml(string $value): string |
|
99
|
|
|
{ |
|
100
|
|
|
$icon = ''; |
|
101
|
|
|
if ($value && !\App\Json::isEmpty($value)) { |
|
102
|
|
|
['type' => $type, 'name' => $name] = \App\Json::decode($value); |
|
103
|
|
|
if ('icon' === $type) { |
|
104
|
|
|
$icon = "<span class=\"{$name} mr-1\"></span>"; |
|
105
|
|
|
} elseif ('image' === $type && ($src = self::getImageUrl($name))) { |
|
106
|
|
|
$icon = '<img class="icon-img--picklist mr-1" src="' . $src . '">'; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $icon; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|