GalleryManager.php
5.87 KB
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
<?php
namespace common\components;
use Yii;
/**
* Класс для работы с галереей
* Class GalleryManager
*/
class GalleryManager
{
public $sorts = null;
public $image = null;
public $imageTitle = null;
/** @var array Новые Сформированы параметры галереи */
private $_newGalleryParams = [];
/** @var null Сформированы параметры галереи с post запросса */
private $_postParamsGalleryManager = null;
/** поле сортировки */
const NAME_SORT = 'sort';
/** поле фотки */
const NAME_IMAGE = 'image';
/** поле фотки */
const NAME_IMAGE_TITLE = 'title';
/** image path */
const NAME_IMAGE_PATH = 'image_path';
/**
* @param $json
* @return array
*/
public function getFileNames($json)
{
$fileNames = [];
if (is_string($json)) {
$params = json_decode($json, true);
if ($params) {
foreach ($params as $param) {
$fileNames[] = $param['image'];
}
}
}
return $fileNames;
}
private function fileExist($filePath, $fileName)
{
return (file_exists($filePath . '/' . $fileName) && $fileName) ? true : false;
}
/**
* получаем параметры для сохранения
* @param $fileNames
* @return string (json)
*/
public function getOptionsForSaving($fileNames, $filePath)
{
$this->getGalleryManagerPostParams();
$this->generateGalleryParams($fileNames);
$newFiles = $this->getNewFiles($fileNames, true);
if ($newFiles) {
foreach ($newFiles as $fileName) {
$param = ($this->getGalleryManagerPostParams($fileName))
? $this->getGalleryManagerPostParams($fileName)
: $this->generateGalleryParams($fileName);
if ($this->fileExist($filePath, $param[self::NAME_IMAGE])) {
$this->_newGalleryParams[] = $param;
}
}
}
$this->mergeOldFiles($fileNames, $filePath);
if ($this->_newGalleryParams) {
uasort($this->_newGalleryParams, [$this, 'gallerySort']);
}
return json_encode($this->_newGalleryParams);
}
/**
* склеиваем старые файлы с новыми
* @param $fileNames
* @param $filePath
*/
public function mergeOldFiles($fileNames, $filePath)
{
if (is_string($fileNames) && json_decode($fileNames)) {
$oldFifes = json_decode($fileNames, true);
foreach ($oldFifes as $param) {
$fileName = (isset($param[self::NAME_IMAGE])) ? $param[self::NAME_IMAGE] : '';
if ($this->fileExist($filePath, $fileName)) {
$this->_newGalleryParams[] = ($this->getGalleryManagerPostParams($fileName))
? $this->getGalleryManagerPostParams($fileName)
: $param;
}
}
}
}
/**
* Получаем новые файлы из параметров
* @param $fileNames
* @param bool $clearLineFromNewFiles - очищаем строку с файлами от новых файлов
* @return array
*/
private function getNewFiles(& $fileNames, $clearLineFromNewFiles = false)
{
$fileNames = explode(',', $fileNames);
$newFiles = [];
if ($fileNames) {
foreach ($fileNames as $key => $param) {
if (preg_match('/^.*.(jpg|png|jpeg|gif)$/i', $param)) {
$newFiles[] = $param;
if ($clearLineFromNewFiles === true) {
unset($fileNames[$key]);
}
}
}
}
$fileNames = implode(',', $fileNames);
return $newFiles;
}
/**
* @param $fileName
* @return array
*/
public function generateGalleryParams($fileName)
{
return [
self::NAME_IMAGE => $fileName,
self::NAME_IMAGE_TITLE => '',
self::NAME_SORT => 1,
self::NAME_IMAGE_PATH => '',
];
}
/**
* @param null $fileName
* @return null
*/
private function getGalleryManagerPostParams($fileName = null)
{
$galleryParams = null;
if ($this->_postParamsGalleryManager === null) {
$galleryParams = Yii::$app->getRequest()->post('GalleryManager', null);
if ($galleryParams) {
foreach ($galleryParams[self::NAME_IMAGE] as $key => $value) {
$filename = $value;
if (strripos($value, '/') !== false) {
$filename = substr($value, strripos($value, '/') + 1);
}
$this->_postParamsGalleryManager[$filename] = [
self::NAME_IMAGE => $filename,
self::NAME_IMAGE_TITLE => $galleryParams[self::NAME_IMAGE_TITLE][$key],
self::NAME_SORT => $galleryParams[self::NAME_SORT][$key],
self::NAME_IMAGE_PATH => '',
];
}
}
}
if ($fileName !== null) {
$galleryParams = isset($this->_postParamsGalleryManager[$fileName]) ? $this->_postParamsGalleryManager[$fileName] : null;
}
return ($fileName === null) ? $this->_postParamsGalleryManager : $galleryParams;
}
/** Сортируем галлерею */
private function gallerySort($a, $b)
{
$a = isset($a['sort']) ? $a['sort'] : null;
$b = isset($b['sort']) ? $b['sort'] : null;
if ($a == $b || ($a === null || $b === null)) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
}