Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tools.php
1<?php
2
4
8
15class Tools
16{
17 public const IPROPERTY_ENTITY_ELEMENT = 'ELEMENT';
18 public const IPROPERTY_ENTITY_SECTION = 'SECTION';
19
20 public const CHECKBOX_VALUE_YES = 'Y';
21
22 protected const WEB_PROTOCOL_MASK = '/^(ftp|ftps|http|https):\/\//';
23
24 private static array $checkboxPropertyCache = [];
25
37 public static function process404($message = "", $defineConstant = true, $setStatus = true, $showPage = false, $pageFile = "")
38 {
40 global $APPLICATION;
41
42 $message = (string)$message;
43 $pageFile = (string)$pageFile;
44 if ($message !== '')
45 {
46 $APPLICATION->includeComponent(
47 'bitrix:system.show_message',
48 '.default',
49 [
50 'MESSAGE' => $message,
51 'STYLE' => 'errortext',
52 ],
53 null,
54 [
55 'HIDE_ICONS' => 'Y',
56 ]
57 );
58 }
59
60 if ($defineConstant && !defined('ERROR_404'))
61 {
62 define('ERROR_404', 'Y');
63 }
64
65 if ($setStatus)
66 {
67 \CHTTP::setStatus('404 Not Found');
68 }
69
70 if ($showPage)
71 {
72 if ($APPLICATION->RestartWorkarea())
73 {
74 if (!defined('BX_URLREWRITE'))
75 {
76 define('BX_URLREWRITE', true);
77 }
78 Main\Composite\Engine::setEnable(false);
79 if ($pageFile !== '')
80 {
81 require Main\Application::getDocumentRoot() . rel2abs('/', '/' . $pageFile);
82 }
83 else
84 {
85 require Main\Application::getDocumentRoot() . '/404.php';
86 }
87 die();
88 }
89 }
90 }
91
102 public static function getFieldImageData(array &$item, array $keys, $entity, $ipropertyKey = 'IPROPERTY_VALUES')
103 {
104 if (empty($item) || empty($keys))
105 return;
106
107 $entity = (string)$entity;
108 $ipropertyKey = (string)$ipropertyKey;
109 foreach ($keys as $fieldName)
110 {
111 if (!array_key_exists($fieldName, $item) || is_array($item[$fieldName]))
112 continue;
113 $imageData = false;
114 $imageId = (int)$item[$fieldName];
115 if ($imageId > 0)
116 $imageData = \CFile::getFileArray($imageId);
117 unset($imageId);
118 if (is_array($imageData))
119 {
120 if (isset($imageData['SAFE_SRC']))
121 {
122 $imageData['UNSAFE_SRC'] = $imageData['SRC'];
123 $imageData['SRC'] = $imageData['SAFE_SRC'];
124 }
125 else
126 {
127 if (!preg_match(self::WEB_PROTOCOL_MASK, $imageData['SRC']))
128 {
129 $imageData['UNSAFE_SRC'] = $imageData['SRC'];
130 $imageData['SAFE_SRC'] = Main\Web\Uri::urnEncode($imageData['SRC'], 'UTF-8');
131 $imageData['SRC'] = $imageData['SAFE_SRC'];
132 }
133 }
134 $imageData['ALT'] = '';
135 $imageData['TITLE'] = '';
136 if ($ipropertyKey != '' && isset($item[$ipropertyKey]) && is_array($item[$ipropertyKey]))
137 {
138 $entityPrefix = $entity.'_'.$fieldName;
139 if (isset($item[$ipropertyKey][$entityPrefix.'_FILE_ALT']))
140 $imageData['ALT'] = $item[$ipropertyKey][$entityPrefix.'_FILE_ALT'];
141 if (isset($item[$ipropertyKey][$entityPrefix.'_FILE_TITLE']))
142 $imageData['TITLE'] = $item[$ipropertyKey][$entityPrefix.'_FILE_TITLE'];
143 unset($entityPrefix);
144 }
145 if ($imageData['ALT'] == '' && isset($item['NAME']))
146 $imageData['ALT'] = $item['NAME'];
147 if ($imageData['TITLE'] == '' && isset($item['NAME']))
148 $imageData['TITLE'] = $item['NAME'];
149 }
150 $item[$fieldName] = $imageData;
151 unset($imageData);
152 }
153 unset($fieldName);
154 }
155
163 public static function getImageSrc(array $image, $safe = true)
164 {
165 $result = '';
166 if (empty($image) || !isset($image['SRC']))
167 {
168 return $result;
169 }
170 $safe = ($safe === true);
171
172 if ($safe)
173 {
174 if (isset($image['SAFE_SRC']))
175 {
176 $result = $image['SAFE_SRC'];
177 }
178 elseif (preg_match(self::WEB_PROTOCOL_MASK, $image['SRC']))
179 {
180 $result = $image['SRC'];
181 }
182 else
183 {
184 $result = Main\Web\Uri::urnEncode($image['SRC'], 'UTF-8');
185 }
186 }
187 else
188 {
189 $result = ($image['UNSAFE_SRC'] ?? $image['SRC']);
190 }
191
192 return $result;
193 }
194
207 public static function isCheckboxProperty(array $property): bool
208 {
209 $propertyId = (int)($property['ID'] ?? 0);
210 if ($propertyId <= 0)
211 {
212 return false;
213 }
214 if (!isset(self::$checkboxPropertyCache[$propertyId]))
215 {
216 $result = false;
217 if (
218 ($property['PROPERTY_TYPE'] ?? '') === PropertyTable::TYPE_LIST
219 && ($property['MULTIPLE'] ?? '') === 'N'
220 && (string)($property['USER_TYPE'] ?? '') === ''
221 && ($property['LIST_TYPE'] ?? '') === PropertyTable::CHECKBOX
222 )
223 {
224 $filter = [
225 '=PROPERTY_ID' => $propertyId,
226 ];
227 $cache = [
228 'ttl' => 86400,
229 ];
230 if (PropertyEnumerationTable::getCount($filter, $cache) === 1)
231 {
232 $variant = PropertyEnumerationTable::getRow([
233 'select' => [
234 'ID',
235 'PROPERTY_ID',
236 'VALUE',
237 ],
238 'filter' => $filter,
239 'cache' => $cache,
240 ]);
241 if (($variant['VALUE'] ?? '') === self::CHECKBOX_VALUE_YES)
242 {
243 $result = true;
244 }
245 }
246 }
247 self::$checkboxPropertyCache[$propertyId] = $result;
248 }
249
250 return self::$checkboxPropertyCache[$propertyId];
251 }
252
253 public static function clearCache(): void
254 {
255 self::$checkboxPropertyCache = [];
256 }
257}
static getImageSrc(array $image, $safe=true)
Definition tools.php:163
static getFieldImageData(array &$item, array $keys, $entity, $ipropertyKey='IPROPERTY_VALUES')
Definition tools.php:102
static process404($message="", $defineConstant=true, $setStatus=true, $showPage=false, $pageFile="")
Definition tools.php:37
static isCheckboxProperty(array $property)
Definition tools.php:207