Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
audiocall.php
1<?php
10
13
18
19Loc::loadMessages(__FILE__);
20
26{
27 const AUDIO_DIR = 'http://dl.bitrix24.com/sender/audiocall/';
28 const METADATA_FILE = 'http://dl.bitrix24.com/sender/audiocall/metadata.json';
29
34 public static function getSupportedLangs()
35 {
36 $data = self::getMetadata();
37 return $data ? $data['langs'] : [];
38 }
39
48 public static function onPresetTemplateList($templateType = null, $templateId = null, $messageCode = null)
49 {
50 if ($templateType && $templateType !== 'BASE')
51 {
52 return array();
53 }
54 if ($messageCode && $messageCode !== Message\iBase::CODE_AUDIO_CALL)
55 {
56 return array();
57 }
58
59 return static::getTemplates($templateId);
60 }
61
67 public static function getAudioFileUrlByCode($code)
68 {
69 if (!self::presetExists($code))
70 {
71 return false;
72 }
73 $filePath = static::AUDIO_DIR . static::getCodeWithLang($code) . '.mp3';
74 return $filePath;
75 }
76
82 public static function presetExists($code)
83 {
84 return !!self::getDurationByCode($code);
85 }
86
92 public static function getDurationByCode($code)
93 {
94 $code = self::getCodeWithLang($code);
95 $data = self::getMetadata();
96 return $data['durations'][$code] ?? false;
97 }
98
103 public static function getDefaultCode()
104 {
105 $messageCode = Message\iBase::CODE_AUDIO_CALL;
106 foreach (Texts::getListByType($messageCode) as $item)
107 {
108 $code = mb_strtolower($item['CODE']);
109 if (self::presetExists($code))
110 {
111 return $code;
112 }
113 }
114 return false;
115 }
116
122 private static function getCodeWithLang($code)
123 {
124 $lang = self::getLang();
125 return $lang . '_' . $code;
126 }
127
132 private static function getLang()
133 {
134 $lang = mb_strtolower(LANGUAGE_ID);
135 $supportedLangs = static::getSupportedLangs();
136 $lang = in_array($lang, $supportedLangs) ? $lang : array_shift($supportedLangs);
137 return $lang;
138 }
139
145 private static function getTemplates($templateId = null)
146 {
147 $result = [];
148 $messageCode = Message\iBase::CODE_AUDIO_CALL;
149
150 foreach (Texts::getListByType($messageCode) as $item)
151 {
152 $code = mb_strtolower($item['CODE']);
153 $presetCode = mb_strtolower($messageCode."_".$code);
154 if (!self::presetExists($code))
155 {
156 continue;
157 }
158 if($templateId && $presetCode !== $templateId)
159 {
160 continue;
161 }
162
163 $result[] = array(
164 'ID' => $presetCode,
165 'TYPE' => Type::getCode(Type::BASE),
166 'MESSAGE_CODE' => array($messageCode),
167 'VERSION' => 2,
168 'HOT' => $item['HOT'],
169 'ICON' => $item['ICON'],
170
171 'NAME' => $item['NAME'],
172 'DESC' => $item['DESC'],
173 'FIELDS' => array(
174 'AUDIO_FILE' => [
175 'CODE' => 'AUDIO_FILE',
176 'VALUE' => static::getAudioFileUrlByCode($code),
177 ]
178 ),
179 );
180 }
181
182 return $result;
183 }
184
190 private static function getMetadata()
191 {
192 static $failed = false;
193 if ($failed)
194 {
195 return false;
196 }
197
198 $cacheTtl = 86400; // 24 hours
199 $cacheId = 'sender_audiocall_metadata';
200 $cachePath = '/sender/audiocall_metadata/';
201 $cache = \Bitrix\Main\Application::getInstance()->getCache();
202 if($cache->initCache($cacheTtl, $cacheId, $cachePath))
203 {
204 return $cache->getVars();
205 }
206 else
207 {
208 $result = false;
209
210 $cache->startDataCache();
211
212 $request = new HttpClient([
213 "socketTimeout" => 5,
214 "streamTimeout" => 5
215 ]);
216 $request->get(static::METADATA_FILE);
217 if($request->getStatus() == 200)
218 {
219 try
220 {
221 $result = Json::decode($request->getResult());
222 }
223 catch (ArgumentException $e)
224 {
225 }
226 }
227 if (is_array($result))
228 {
229 $cache->endDataCache($result);
230 }
231 else
232 {
233 $failed = true;
234 $cache->abortDataCache();
235 }
236 return $result;
237 }
238 }
239}
static loadMessages($file)
Definition loc.php:64
static onPresetTemplateList($templateType=null, $templateId=null, $messageCode=null)
Definition audiocall.php:48