1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://github.com/rkit/filemanager-yii2 |
5
|
|
|
* @copyright Copyright (c) 2015 Igor Romanov |
6
|
|
|
* @license [MIT](http://opensource.org/licenses/MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace rkit\filemanager\helpers; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\helpers\ArrayHelper; |
13
|
|
|
|
14
|
|
|
class FileRules |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Get description for max size of file |
18
|
|
|
* |
19
|
|
|
* @param int $rules |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
1 |
|
private static function getMaxSizeDescription($rules) |
23
|
|
|
{ |
24
|
1 |
|
$maxSize = Yii::$app->formatter->asShortSize($rules); |
25
|
1 |
|
return Yii::t('filemanager-yii2', 'Max. file size') . ': ' . $maxSize . ' '; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get description for extensions of file |
30
|
|
|
* |
31
|
|
|
* @param array $rules |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
1 |
|
private static function getExtensionDescription($rules) |
35
|
|
|
{ |
36
|
1 |
|
$extensions = strtoupper(implode(', ', $rules)); |
37
|
1 |
|
return Yii::t('filemanager-yii2', 'File types') . ': ' . $extensions . ' '; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get description for size of image |
42
|
|
|
* |
43
|
|
|
* @param array $rules |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
9 |
|
private static function getImageSizeDescription($rules) |
47
|
|
|
{ |
48
|
9 |
|
$maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
49
|
9 |
|
$minWidth = ArrayHelper::getValue($rules, 'minWidth'); |
50
|
9 |
|
$maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
51
|
9 |
|
$minHeight = ArrayHelper::getValue($rules, 'minHeight'); |
52
|
|
|
|
53
|
9 |
|
$text = ''; |
54
|
9 |
|
if (self::isImageWithStrictSize($rules)) { |
55
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Image size') . ': ' . $maxWidth . 'x' . $maxHeight . 'px;'; |
56
|
9 |
|
} elseif (self::isImageWithMinAndMaxSize($rules)) { |
57
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px;'; |
58
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px;'; |
59
|
8 |
|
} elseif (self::isImageWithMinSize($rules)) { |
60
|
2 |
|
$text .= Yii::t('filemanager-yii2', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px;'; |
61
|
2 |
|
$text .= self::prepareImageSizeDescription($rules, ['minWidth', 'minHeight']); |
62
|
7 |
|
} elseif (self::isImageWithMaxSize($rules)) { |
63
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px;'; |
64
|
1 |
|
$text .= self::prepareImageSizeDescription($rules, ['maxWidth', 'maxHeight']); |
65
|
1 |
|
} else { |
66
|
4 |
|
$text .= self::prepareImageSizeDescription($rules); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
$text = mb_substr($text, 0, -1); |
70
|
9 |
|
$text = str_replace(';', '<br>', $text); |
71
|
|
|
|
72
|
9 |
|
return $text; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* This rules for image with strict size |
77
|
|
|
* |
78
|
|
|
* @param array $rules |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
9 |
|
private static function isImageWithStrictSize($rules) |
82
|
|
|
{ |
83
|
9 |
|
$maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
84
|
9 |
|
$minWidth = ArrayHelper::getValue($rules, 'minWidth'); |
85
|
9 |
|
$maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
86
|
9 |
|
$minHeight = ArrayHelper::getValue($rules, 'minHeight'); |
87
|
|
|
|
88
|
9 |
|
return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* This rules for image with min and max size |
93
|
|
|
* |
94
|
|
|
* @param array $rules |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
8 |
|
private static function isImageWithMinAndMaxSize($rules) |
98
|
|
|
{ |
99
|
8 |
|
return count($rules) == 4; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* This rules for image with min size |
104
|
|
|
* |
105
|
|
|
* @param array $rules |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
7 |
|
private static function isImageWithMinSize($rules) |
109
|
|
|
{ |
110
|
7 |
|
$minWidth = ArrayHelper::getValue($rules, 'minWidth'); |
111
|
7 |
|
$minHeight = ArrayHelper::getValue($rules, 'minHeight'); |
112
|
|
|
|
113
|
7 |
|
return (count($rules) == 2 || count($rules) == 3) && $minWidth && $minHeight; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* This rules for image with max size |
118
|
|
|
* |
119
|
|
|
* @param array $rules |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
5 |
|
private static function isImageWithMaxSize($rules) |
123
|
|
|
{ |
124
|
5 |
|
$maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
125
|
5 |
|
$maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
126
|
|
|
|
127
|
5 |
|
return (count($rules) == 2 || count($rules) == 3) && $maxWidth && $maxHeight; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Prepare description for size of image |
132
|
|
|
* |
133
|
|
|
* @param array $rules |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
7 |
|
private static function prepareImageSizeDescription($rules, $exclude = []) |
137
|
|
|
{ |
138
|
7 |
|
foreach ($exclude as $item) { |
139
|
3 |
|
unset($rules[$item]); |
140
|
7 |
|
} |
141
|
|
|
|
142
|
7 |
|
$text = ''; |
143
|
7 |
|
foreach ($rules as $rule => $value) { |
144
|
|
|
switch ($rule) { |
145
|
4 |
|
case 'minWidth': |
146
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Min. width') . ' ' . $value . 'px;'; |
147
|
1 |
|
break; |
148
|
3 |
|
case 'minHeight': |
149
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Min. height') . ' ' . $value . 'px;'; |
150
|
1 |
|
break; |
151
|
2 |
|
case 'maxWidth': |
152
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Max. width') . ' ' . $value . 'px;'; |
153
|
1 |
|
break; |
154
|
1 |
|
case 'maxHeight': |
155
|
1 |
|
$text .= Yii::t('filemanager-yii2', 'Max. height') . ' ' . $value . 'px;'; |
156
|
1 |
|
break; |
157
|
|
|
} |
158
|
7 |
|
} |
159
|
|
|
|
160
|
7 |
|
return $text; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get rules description |
165
|
|
|
* |
166
|
|
|
* @param array $rules |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
9 |
|
public static function getDescription($rules) |
170
|
|
|
{ |
171
|
9 |
|
$text = ''; |
172
|
9 |
|
if (isset($rules['imageSize'])) { |
173
|
9 |
|
$text .= self::getImageSizeDescription($rules['imageSize']); |
174
|
9 |
|
$text = !empty($text) ? $text . '<br>' : $text; |
175
|
9 |
|
} |
176
|
|
|
|
177
|
9 |
|
if (isset($rules['extensions'])) { |
178
|
1 |
|
$text .= self::getExtensionDescription($rules['extensions']); |
179
|
1 |
|
$text = isset($rules['maxSize']) ? $text . '<br>' : $text; |
180
|
1 |
|
} |
181
|
|
|
|
182
|
9 |
|
if (isset($rules['maxSize'])) { |
183
|
1 |
|
$text .= self::getMaxSizeDescription($rules['maxSize']); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
9 |
|
return $text; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|