Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
3
4
6use Bitrix\Main\Entity\FieldError;
10
12{
14 const SUCCESS = 1;
15 const FAIL = 0;
16 const EMPTY_REQUIRED = 4;
18 const PREVIEW_IMAGE_SIZE = 150;
19
20 const SIMPLE_APP_TEMPLATE = "simple";
21
33 public static function createApp($appCode = "", $data = array(), $initConfig = array())
34 {
35 $result = self::SUCCESS;
36 $fields = $data;
37 $fields["CODE"] = $appCode;
38 $dbResult = AppTable::add($fields);
39 if (!$dbResult->isSuccess())
40 {
41 $errors = $dbResult->getErrors();
42 if ($errors[0]->getCode() == FieldError::INVALID_VALUE)
43 {
45 }
46 elseif ($errors[0]->getCode() == FieldError::EMPTY_REQUIRED)
47 {
48 $result = self::EMPTY_REQUIRED;
49 }
50 }
51 else
52 {
53
54 self::addConfig($appCode, "global", $initConfig);
55 }
56
57 return $result;
58 }
59
60 private static function getTemplateList()
61 {
62 return array(
63 "simple",
64 "api"
65 );
66 }
67
75 public static function removeApp($appCode)
76 {
77 $result = AppTable::delete($appCode);
78 return $result->isSuccess();
79 }
80
87 public static function registerFileInApp(&$fileArray, $appCode)
88 {
89 $result = AppTable::getById($appCode);
90 $appData = $result->fetchAll();
91 if (count($appData) > 0)
92 {
93 if(!is_array($appData[0]["FILES"]))
94 $appData[0]["FILES"] = [];
95 $appData[0]["FILES"][] = $fileArray["fileID"];
96 AppTable::update($appCode, array("FILES" => $appData[0]["FILES"]));
97 $arImage = \CFile::ResizeImageGet(
98 $fileArray["fileID"],
99 array("width" => self::PREVIEW_IMAGE_SIZE, "height" => self::PREVIEW_IMAGE_SIZE),
100 BX_RESIZE_IMAGE_EXACT,
101 false,
102 false,
103 true
104 );
105 $fileArray["img_source_src"] = $arImage["src"];
106 }
107
108 }
109
116 public static function unregisterFileInApp($fileId, $appCode)
117 {
118 $result = AppTable::getById($appCode);
119 $appData = $result->fetchAll();
120 if (count($appData) > 0)
121 {
122
123 $index = array_search($fileId, $appData[0]["FILES"]);
124 if ($index !== false)
125 {
126 unset($appData[0]["FILES"][$index]);
127 AppTable::update($appCode, array("FILES" => $appData[0]["FILES"]));
128 }
129 die();
130
131 }
132
133 }
134
147 public static function addConfig($appCode = "", $platform, $config = array())
148 {
149 if (ConfigTable::isExists($appCode, $platform))
150 {
151 return false;
152 }
153
154 $fields = array(
155 "APP_CODE" => $appCode,
156 "PLATFORM" => $platform,
157 "PARAMS" => $config
158 );
159
160 $result = ConfigTable::add($fields);
161
162 return $result->isSuccess();
163
164 }
165
175 public static function removeConfig($appCode = "", $platform = array())
176 {
177 $filter = array(
178 "APP_CODE" => $appCode,
179 "PLATFORM" => $platform,
180 );
181
182 $result = ConfigTable::delete($filter);
183
184 return $result->isSuccess();
185
186 }
187
199 public static function updateConfig($appCode = "", $platform = "", $config = array())
200 {
201 if (!ConfigTable::isExists($appCode, $platform))
202 {
203 return false;
204 }
205
206 $map = new ConfigMap();
207 foreach ($config as $paramName => $value)
208 {
209 if (!$map->has($paramName))
210 {
211 unset($config[$paramName]);
212 }
213 }
214
215 $data = array(
216 "PARAMS" => $config
217 );
218
219 $result = ConfigTable::update(array("APP_CODE" => $appCode, "PLATFORM" => $platform), $data);
220
221 return $result->isSuccess();
222
223 }
224
235 public static function getConfigJSON($appCode, $platform = false)
236 {
237 $map = new \Bitrix\MobileApp\Designer\ConfigMap();
238 $res = ConfigTable::getList(array(
239 "filter" => array(
240 "APP_CODE" => $appCode,
241 )
242 ));
243
244 $configs = $res->fetchAll();
245 $targetConfig = array();
246
247 for ($i = 0; $i < count($configs); $i++)
248 {
249 if ($configs[$i]["PLATFORM"] == $platform)
250 {
251 $targetConfig = $configs[$i];
252 break;
253 }
254 elseif ($configs[$i]["PLATFORM"] == "global")
255 {
256 $targetConfig = $configs[$i];
257 }
258 }
259
260 $params = array_key_exists("PARAMS", $targetConfig) ? $targetConfig["PARAMS"]: array() ;
261 $imageParamList = $map->getParamsByType(ParameterType::IMAGE);
262 $imageSetParamList = $map->getParamsByType(ParameterType::IMAGE_SET);
263 $structuredConfig = array();
264
265 foreach ($params as $key => $value)
266 {
267 if (!$map->has($key))
268 {
269 continue;
270 }
271
272 if (array_key_exists($key, $imageParamList))
273 {
274 $imagePath = \CFile::GetPath($value);
275 if($imagePath <> '')
276 $value = $imagePath;
277 else
278 continue;
279 }
280
281 if (array_key_exists($key, $imageSetParamList))
282 {
283 $tmpValue = array();
284 foreach ($value as $imageCode => $imageId)
285 {
286 $imagePath = \CFile::GetPath($imageId);
287 if($imagePath <> '')
288 $tmpValue[$imageCode] = $imagePath;
289 else
290 continue;
291 }
292 $value = $tmpValue;
293 }
294 $structuredConfig = array_merge_recursive(self::nameSpaceToArray($key, $value), $structuredConfig);
295 }
296
297 if(toUpper(SITE_CHARSET) != "UTF-8")
298 {
299 $structuredConfig = Encoding::convertEncodingArray($structuredConfig, SITE_CHARSET, "UTF-8");
300 }
301
302 self::addVirtualParams($structuredConfig, $platform);
303
304 return json_encode($structuredConfig);
305 }
306
319 public static function copyFromTemplate($folder, $appCode, $useOffline = false, $templateCode = "simple")
320 {
321 if(!in_array($templateCode, self::getTemplateList()))
322 {
323 $templateCode = "simple";
324 }
325
326 $appFolderPath = Application::getDocumentRoot() . "/" . $folder . "/";
327 $offlineTemplate = Application::getDocumentRoot() . "/bitrix/modules/mobileapp/templates_app/offline/";
328 $templatePath = Application::getDocumentRoot() . "/bitrix/modules/mobileapp/templates_app/".$templateCode."/";
329
330 $directory = new Directory($templatePath);
331 if($directory->isExists())
332 {
333 if (!Directory::isDirectoryExists($appFolderPath))
334 {
335 if($useOffline)
336 {
337 CopyDirFiles($offlineTemplate, $appFolderPath."/offline");
338 }
339
340 $items = $directory->getChildren();
341 foreach ($items as $entry)
342 {
346 $filePath = $entry->getPath();
347 $appFilePath = $appFolderPath . $entry->getName();
348
349 if($entry instanceof Directory)
350 {
351 CopyDirFiles($filePath, $appFolderPath."/".$entry->getName(),true,true);
352 }
353 else
354 {
355 $file = new File($entry->getPath());
356 File::putFileContents(
357 $appFilePath,
358 str_replace(Array("#folder#", "#code#"), Array($folder, $appCode),$file->getContents())
359 );
360 }
361 }
362 }
363 }
364 }
365
373 public static function bindTemplate($templateId, $folder, $createNew)
374 {
375 $arFields = Array("TEMPLATE" => Array());
376 if ($createNew)
377 {
378 CopyDirFiles(
379 Application::getDocumentRoot() . "/bitrix/modules/mobileapp/templates/default_app/",
380 Application::getDocumentRoot() . "/bitrix/templates/" . $templateId, True, True
381 );
382
383 File::putFileContents(
384 Application::getDocumentRoot() . "/bitrix/templates/" . $templateId . "/description.php",
385 str_replace(Array("#mobile_template_name#"), Array($templateId), File::getFileContents(Application::getDocumentRoot() . "/bitrix/templates/" . $templateId . "/description.php"))
386 );
387
388 $arFields["TEMPLATE"][] = Array(
389 "SORT" => 1,
390 "CONDITION" => "CSite::InDir('/" . $folder . "/')",
391 "TEMPLATE" => $templateId
392 );
393 }
394
395 $default_site_id = \CSite::GetDefSite();
396 if ($default_site_id)
397 {
398 $dbTemplates = \CSite::GetTemplateList($default_site_id);
399 $arFields["LID"] = $default_site_id;
400 $isTemplateFound = false;
401 while ($template = $dbTemplates->Fetch())
402 {
403 $arFields["TEMPLATE"][] = array(
404 "TEMPLATE" => $template['TEMPLATE'],
405 "SORT" => $template['SORT'],
406 "CONDITION" => $template['CONDITION']
407 );
408
409 if ($template["TEMPLATE"] == $templateId && !$createNew && !$isTemplateFound)
410 {
411 $isTemplateFound = true;
412
413 $arFields["TEMPLATE"][] = Array(
414 "SORT" => 1,
415 "CONDITION" => "CSite::InDir('/" . $folder . "/')",
416 "TEMPLATE" => $templateId
417 );
418 }
419 }
420
421 $obSite = new \CSite;
422 $obSite->Update($default_site_id, $arFields);
423 }
424 }
425
431 public static function getAppFiles($appCode)
432 {
433 $result = AppTable::getById($appCode);
434 $appData = $result->fetchAll();
435 $files = [];
436 if (count($appData) > 0 && is_array($appData[0]['FILES']))
437 {
438 //TODO fix, use module_id in the filter
439 $result = \CFile::GetList(['ID' => 'desc'], ['@ID' => implode(',', $appData[0]['FILES'])]);
440 while ($file = $result->Fetch())
441 {
442 $image = \CFile::ResizeImageGet(
443 $file['ID'],
444 ['width' => self::PREVIEW_IMAGE_SIZE, 'height' => self::PREVIEW_IMAGE_SIZE],
445 BX_RESIZE_IMAGE_EXACT,
446 false,
447 false,
448 true
449 );
450 $files['file_' . $file['ID']] = [
451 'id' => $file['ID'],
452 'src' => \CFile::GetFileSRC($file),
453 'preview' => $image['src']
454 ];
455 }
456 }
457
458 return $files;
459 }
460
489 private static function nameSpaceToArray($namespace, $value)
490 {
491 $keys = explode("/", $namespace);
492 $result = array();
493 $temp = & $result;
494 for ($i = 0; $i < count($keys); $i++)
495 {
496 $temp = & $temp[$keys[$i]];
497 }
498
499 $temp = $value;
500
501 return $result;
502
503 }
504
505 private static function addVirtualParams(&$structuredConfig, $platform)
506 {
507 if($structuredConfig["offline"] && !empty($structuredConfig["offline"]["file_list"]))
508 {
509 $offlineParams = &$structuredConfig["offline"];
510 $offlineParams["file_list"]["bitrix_mobile_core.js"] = Tools::getMobileJSCorePath();
511 $changeMark = Tools::getArrayFilesHash($offlineParams["file_list"]);
512 $offlineParams["change_mark"] = $changeMark;
513 }
514
515 if($structuredConfig["buttons"]["badge"])
516 {
517 $structuredConfig["buttons_badge"] = $structuredConfig["buttons"]["badge"];
518 unset($structuredConfig["buttons"]["badge"]);
519 }
520
521 $structuredConfig["info"] = array(
522 "designer_version"=> ConfigMap::VERSION,
523 "platform"=>$platform
524 );
525 }
526
527
528}
529
530
531
532
533
static isDirectoryExists($path)
static isExists($appCode, $platform)
Definition config.php:141
static createApp($appCode="", $data=array(), $initConfig=array())
Definition manager.php:33
static getConfigJSON($appCode, $platform=false)
Definition manager.php:235
static registerFileInApp(&$fileArray, $appCode)
Definition manager.php:87
static bindTemplate($templateId, $folder, $createNew)
Definition manager.php:373
static removeConfig($appCode="", $platform=array())
Definition manager.php:175
static unregisterFileInApp($fileId, $appCode)
Definition manager.php:116
static updateConfig($appCode="", $platform="", $config=array())
Definition manager.php:199
static addConfig($appCode="", $platform, $config=array())
Definition manager.php:147
static getArrayFilesHash($fileList=array())
Definition tools.php:61