ImportExportFileHelper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\helpers;
  3. use \MGModule\DNSManager2 as main;
  4. use \MGModule\DNSManager2\models\custom\zone;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns;
  6. class ImportExportFileHelper
  7. {
  8. public static $skipFileList = array(
  9. '.htaccess',
  10. 'index.html'
  11. );
  12. public static function exportZoneToFile($zoneId, $path, $fileName = false)
  13. {
  14. $zone = new zone\Zone($zoneId);
  15. $newFileName = $fileName ? $fileName : self::generateNewExportFileName($zone->name);
  16. $fileManager = new main\mgLibs\custom\FileManager($path);
  17. $importInfo = array();
  18. $importInfo['zoneName'] = $zone->name;
  19. $module = $zone->getModule();
  20. if(!$module->zoneExists())
  21. {
  22. throw new \Exception(main\mgLibs\lang::T('you_cannot_edit_this_zone_because_it_is_terminated_on_server'));
  23. }
  24. else
  25. {
  26. $importInfo['zoneRecords'] = $module->getRecords();
  27. }
  28. return $fileManager->saveDataToFile(json_encode($importInfo, JSON_PRETTY_PRINT), $newFileName);
  29. }
  30. public static function listFilesForZone($zone, $fileManager)
  31. {
  32. $files = $fileManager->getFilesFromStorage();
  33. foreach($files as $key => $filename)
  34. {
  35. $nameParts = explode('_', $filename);
  36. if($nameParts[0] !== $zone->name || in_array($filename, self::$skipFileList))
  37. {
  38. unset($files[$key]);
  39. }
  40. }
  41. return $files;
  42. }
  43. public static function loadFileContent($path, $fileName)
  44. {
  45. if(!$fileName)
  46. {
  47. throw new \Exception('You have to select a file');
  48. }
  49. $fileManager = new main\mgLibs\custom\FileManager($path);
  50. $cont = $fileManager->loadFileContent($fileName);
  51. if(!$cont)
  52. {
  53. return false;
  54. }
  55. $backupZone = json_decode($cont);
  56. return $backupZone;
  57. }
  58. public static function generateClientFileName($clientId)
  59. {
  60. return date("Y.m.d_H.i.s").'_'. $clientId;
  61. }
  62. public static function generateNewExportFileName($associateName)
  63. {
  64. return $associateName.'_'.date("Y.m.d_H.i.s");
  65. }
  66. public static function bulkImportZoneToFile($zone, $path, $fileName = false)
  67. {
  68. $newFileName = $fileName ? $fileName : self::generateNewExportFileName($zone->name);
  69. $fileManager = new main\mgLibs\custom\FileManager($path);
  70. $importInfo = array();
  71. $importInfo['zoneName'] = $zone->name;
  72. $module = $zone->getModule();
  73. if(!$module->zoneExists())
  74. {
  75. throw new \Exception(main\mgLibs\lang::T('you_cannot_edit_this_zone_because_it_is_terminated_on_server'));
  76. }
  77. else
  78. {
  79. $importInfo['zoneRecords'] = $module->getRecords();
  80. }
  81. $content = $fileManager->loadFileContent($newFileName);
  82. $decodedContent = json_decode($content);
  83. if(!$decodedContent)
  84. {
  85. $newContent = array();
  86. $newContent[$zone->name] = $importInfo;
  87. }
  88. else
  89. {
  90. $decodedContent->{$zone->name} = $importInfo;
  91. $newContent = $decodedContent;
  92. }
  93. return $fileManager->saveDataToFile(json_encode($newContent, JSON_PRETTY_PRINT), $newFileName, true);
  94. }
  95. public static function listFilesForBulkExport($fileManager, $serverName = false, $order = 0)
  96. {
  97. $files = $fileManager->getFilesFromStorage($order);
  98. foreach($files as $key => $filename)
  99. {
  100. $nameParts = explode('_', $filename);
  101. if($serverName && $nameParts[0] !== $serverName)
  102. {
  103. unset($files[$key]);
  104. }
  105. if($filename === '.' || $filename === '..' || in_array($filename, self::$skipFileList))
  106. {
  107. unset($files[$key]);
  108. }
  109. }
  110. return $files;
  111. }
  112. public static function listBulkExportZones($fileName, $path)
  113. {
  114. $fileManager = new main\mgLibs\custom\FileManager($path);
  115. $content = $fileManager->loadFileContent($fileName);
  116. $decodedContent = json_decode($content);
  117. return $decodedContent;
  118. }
  119. }