Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
storage.php
1<?php
2
4
8
9class Storage
10{
11
17 public static function getStorage()
18 {
19 static $storage;
20
21 if (!is_null($storage))
22 {
23 return $storage;
24 }
25
26 $storage = false;
27
28 if (!Main\Loader::includeModule('disk'))
29 {
30 return $storage;
31 }
32
33 $storageId = Main\Config\Option::get('mail', 'disk_attachment_storage_id', 0);
34 if ($storageId > 0)
35 {
36 $storage = \Bitrix\Disk\Storage::loadById($storageId);
37 if (!$storage || $storage->getModuleId() != 'mail')
38 {
39 $storage = false;
40 }
41 }
42
43 if (!$storage)
44 {
45 $driver = \Bitrix\Disk\Driver::getInstance();
46
47 $storage = $driver->addStorageIfNotExist(array(
48 'NAME' => Loc::getMessage('MAIL_ATTACHMENT_STORAGE_NAME'),
49 'USE_INTERNAL_RIGHTS' => false,
50 'MODULE_ID' => 'mail',
51 'ENTITY_TYPE' => Mail\Disk\ProxyType\Mail::className(),
52 'ENTITY_ID' => 'mail',
53 ));
54 if ($storage)
55 {
56 Main\Config\Option::set('mail', 'disk_attachment_storage_id', $storage->getId());
57 }
58 else
59 {
60 $storage = false;
61 }
62 }
63
64 return $storage;
65 }
66
72 public static function getUrlManager()
73 {
74 static $urlManager;
75
76 if (!is_null($urlManager))
77 {
78 return $urlManager;
79 }
80
81 $urlManager = false;
82
83 if (!Main\Loader::includeModule('disk'))
84 {
85 return $urlManager;
86 }
87
88 $urlManager = \Bitrix\Disk\Driver::getInstance()->getUrlManager();
89
90 return $urlManager;
91 }
92
100 public static function getObjectsByFileId($fileId, $limit = 0)
101 {
102 $storage = static::getStorage();
103
104 if (!$storage)
105 {
106 return array();
107 }
108
109 return \Bitrix\Disk\File::getModelList(array(
110 'filter' => array(
111 '=STORAGE_ID' => $storage->getId(),
112 '=TYPE' => \Bitrix\Disk\Internals\ObjectTable::TYPE_FILE,
113 '=FILE_ID' => $fileId,
114 ),
115 'limit' => $limit,
116 ));
117 }
118
126 public static function getObjectByAttachment(array $attachment, $create = false)
127 {
128 $list = static::getObjectsByFileId($attachment['FILE_ID'], 1);
129 $object = reset($list);
130
131 if (empty($object) && $create)
132 {
133 $object = static::registerAttachment($attachment);
134 }
135
136 return $object;
137 }
138
145 public static function registerAttachment(array $attachment)
146 {
147 $storage = static::getStorage();
148
149 if (!$storage)
150 {
151 return false;
152 }
153
154 $folder = $storage->getChild(array(
155 '=NAME' => date('Y-m'),
156 '=TYPE' => \Bitrix\Disk\Internals\FolderTable::TYPE,
157 ));
158
159 if (!$folder)
160 {
161 $folder = $storage->addFolder(array(
162 'NAME' => date('Y-m'),
163 'CREATED_BY' => 1, // @TODO
164 ));
165 }
166
167 if (!$folder)
168 {
169 $folder = $storage;
170 }
171
172 return $folder->addFile(
173 array(
174 'NAME' => \Bitrix\Disk\Ui\Text::correctFilename($attachment['FILE_NAME']) ?: sprintf('%x', rand(0, 0xffffff)),
175 'FILE_ID' => $attachment['FILE_ID'],
176 'SIZE' => $attachment['FILE_SIZE'],
177 'CREATED_BY' => 1, // @TODO
178 ),
179 array(),
180 true
181 );
182 }
183
190 public static function unregisterAttachment($fileId)
191 {
192 foreach (static::getObjectsByFileId($fileId) as $item)
193 {
194 $item->delete(1); // @TODO
195 }
196 }
197
198}
static getObjectsByFileId($fileId, $limit=0)
Definition storage.php:100
static getObjectByAttachment(array $attachment, $create=false)
Definition storage.php:126
static registerAttachment(array $attachment)
Definition storage.php:145
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29