Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fileinpututility.php
1<?php
2namespace Bitrix\Main\UI;
3
5
7{
8 protected static $instance = null;
9
10 const SESSION_VAR_PREFIX = "MFI_UPLOADED_FILES_";
11 const SESSION_LIST = "MFI_SESSIONS";
12 const SESSION_TTL = 86400;
13
17 public static function instance()
18 {
19 if (!isset(static::$instance))
20 static::$instance = new static();
21
22 return static::$instance;
23 }
24
25 public function __construct()
26 {
27 }
28
29 public function registerControl($CID, $controlId = "")
30 {
31 if (func_num_args() == 1)
32 {
33 $controlId = $CID;
34 $CID = "";
35 }
36 $CID = (!empty($CID) ? $CID : md5(randString(15)));
37 $this->initSession($CID, $controlId);
38 return $CID;
39 }
40
41 public function registerFile($CID, $fileId)
42 {
43 \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID][] = $fileId;
44 }
45
46 public function unRegisterFile($CID, $fileId)
47 {
48 if (isset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID]))
49 {
50 $key = array_search($fileId, \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID]);
51 if($key !== false)
52 {
53 unset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID][$key]);
54 return true;
55 }
56 }
57 return false;
58 }
59
60 public function checkFiles($controlId, $arFiles)
61 {
62 $arSessionFilesList = $this->getSessionControlFiles($controlId);
63
64 if(is_array($arFiles))
65 {
66 foreach($arFiles as $key => $fileId)
67 {
68 if(!in_array($fileId, $arSessionFilesList))
69 {
70 unset($arFiles[$key]);
71 }
72 }
73
74 $arFiles = array_values($arFiles);
75 }
76
77 return $arFiles;
78 }
79
80 public function checkDeletedFiles($controlId)
81 {
82 $arSessionFilesList = $this->getSessionControlFiles($controlId);
83 $deletedRequestName = $controlId.'_deleted';
84
85 $result = array();
86
87 $request = Context::getCurrent()->getRequest();
88 $requestValues = $request->getValues();
89
90 // HACK for correct use file delete from BX.UI.ComponentAjax.doSubmit
91 if (isset($requestValues['data']) && is_array($requestValues['data']))
92 {
93 $requestValues = $requestValues['data'];
94 }
95
96 if(isset($requestValues[$deletedRequestName]) && is_array($requestValues[$deletedRequestName]))
97 {
98 foreach($requestValues[$deletedRequestName] as $deletedFile)
99 {
100 if(
101 in_array($deletedFile, $arSessionFilesList)
102 && \CFile::SaveFile(array(
103 'old_file' => $deletedFile,
104 'del' => 'Y',
105 ), ''))
106 {
107 $result[] = $deletedFile;
108 }
109 }
110 }
111
112 return $result;
113 }
114
115 public function checkFile($CID, $fileId)
116 {
117 return isset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID])
118 && in_array($fileId, \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID]);
119 }
120
121 public function getControlByCid($CID)
122 {
123 $ts = time();
124 $found = null;
125 foreach (\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST] as $controlId => $d)
126 {
127 if (array_key_exists($CID, $d))
128 {
129 $r = $d[$CID];
130 if($r["SESSID"] != bitrix_sessid()
131 || $ts-$r["TS"] > self::SESSION_TTL)
132 {
133 unset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId][$CID]);
134 unset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID]);
135 }
136 else
137 {
138 $found = $controlId;
139 break;
140 }
141 }
142 }
143 return $found;
144 }
145 public function isCidRegistered($CID)
146 {
147 return !is_null($this->getControlByCid($CID));
148 }
149
150 public function getUserFieldCid(array $userField)
151 {
152 $fieldName = $userField['MULTIPLE'] === 'Y' ? preg_replace("/\[.*\]$/", '', $userField['FIELD_NAME']) : $userField['FIELD_NAME'];
153 return $userField["ENTITY_ID"]."-".$userField["ID"]."-".$fieldName;
154 }
155
156 protected function initSession($CID, $controlId)
157 {
158 $ts = time();
159
160 if(!isset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId]))
161 {
162 \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId] = array();
163 }
164 else
165 {
166 foreach(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId] as $key => $arSession)
167 {
168 if($arSession["SESSID"] != bitrix_sessid()
169 || $ts-$arSession["TS"] > self::SESSION_TTL)
170 {
171 unset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId][$key]);
172 unset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$key]);
173 }
174 }
175 }
176 if (!array_key_exists($CID, \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId]))
177 {
178 \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId][$CID] = array(
179 "TS" => $ts,
180 "SESSID" => bitrix_sessid()
181 );
182 \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID] = array();
183 }
184 }
185
186 protected function getSessionControlFiles($controlId)
187 {
188 $res = array();
189
190 if(isset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId]))
191 {
192 foreach(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_LIST][$controlId] as $CID => $arSession)
193 {
194 if(isset(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID]) && is_array(\Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID]))
195 {
196 $res = array_merge($res, \Bitrix\Main\Application::getInstance()->getSession()[self::SESSION_VAR_PREFIX.$CID]);
197 }
198 }
199 }
200
201 return $res;
202 }
203}
static getCurrent()
Definition context.php:241
registerControl($CID, $controlId="")