ImportToFile.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\task;
  3. use \Exception;
  4. use \MGModule\DNSManager2\mgLibs\custom\manager;
  5. use \MGModule\DNSManager2\mgLibs\custom\TaskManager;
  6. use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
  7. use \MGModule\DNSManager2\models\custom\server;
  8. use \MGModule\DNSManager2\models\custom\task;
  9. use \MGModule\DNSManager2\mgLibs\custom\helpers;
  10. use \MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
  11. use MGModule\DNSManager2\models\custom\zone\Repository as ZoneRepository;
  12. use MGModule\DNSManager2\models\custom\zone\Zone;
  13. use MGModule\DNSManager2\models\whmcs\domains\domain;
  14. class ImportToFile extends TaskAbstract
  15. {
  16. protected $abort_after_repeats = array('main' => 0);
  17. protected $taskTypeCode = TaskTypeCodesCodes::IMPORTTOFILE;
  18. public function mainDescription()
  19. {
  20. $server_from = new server\Server($this->getParams('from'));
  21. return 'Import from: ' . $server_from->name.' to: '.$this->getParams('toFile');
  22. }
  23. public function main($params)
  24. {
  25. $cron_import_run_each = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_IMPORT_RUN_EACH?:5);
  26. if(!$this->isReadyToRun( $cron_import_run_each ))
  27. {
  28. return true;
  29. }
  30. LogHelper::addSuccessLog('Cron Importer', 'Cron Importer Started');
  31. if($this->getStatus() == task\TaskStatusEnum::START)
  32. {
  33. $this->addChild('fetchZonesList')->run();
  34. $this->setStatus(task\TaskStatusEnum::IN_PROGRESS);
  35. }
  36. elseif($this->getStatus() == task\TaskStatusEnum::IN_PROGRESS)
  37. {
  38. $zones_per_run = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_IMPORT_ZONES_PER_RUN);
  39. $childs = $this->getXChilds($zones_per_run, 'import', task\TaskStatusEnum::START);
  40. if(count($childs) < $zones_per_run)
  41. {
  42. $childs += $this->getXChilds($zones_per_run - count($childs), 'import');
  43. }
  44. foreach($childs as $child)
  45. {
  46. $child->run();
  47. }
  48. $this->setFinishedStatusOnCronRun('import', $zones_per_run);
  49. }
  50. LogHelper::addSuccessLog('Cron Importer', 'End Of Cron Importer Run');
  51. }
  52. public function fetchZonesList($params)
  53. {
  54. if($this->isInCliMode())
  55. {
  56. LogHelper::addSuccessLog('Cron Importer', 'Fetching Zones List For Server Id: '.$this->parent->getParams('from').' Started.');
  57. }
  58. $server_from = new server\Server($this->parent->getParams('from'));
  59. foreach($server_from->getModule()->getZones() as $domain => $ip)
  60. {
  61. #fix - should backup to file all zones
  62. // if(ZoneRepo::factory()->byServerID($server_from->id)->byName($domain)->one() === FALSE)
  63. // {
  64. $this->addResult(array(
  65. 'domain' => $domain,
  66. 'ip' => $ip,
  67. ));
  68. // }
  69. }
  70. $this->setStatus(task\TaskStatusEnum::FINISHED);
  71. if($this->isInCliMode())
  72. {
  73. LogHelper::addSuccessLog('Cron Importer', 'Fetching Zones List For Server Id: '.$this->parent->getParams('from').' Completed.');
  74. }
  75. }
  76. public function import($params)
  77. {
  78. try
  79. {
  80. $server_from = new server\Server($this->parent->getParams('from'));
  81. $module_from = $server_from->getModule();
  82. $module_from->setDomain(helpers\IdnaHelper::idnaDecode($params['domain']));
  83. if($module_from->isIPRequired())
  84. {
  85. $module_from->setIP($params['ip']);
  86. }
  87. $zone = new Zone();
  88. $zone->clientid = $this->getParams('clientid');
  89. $zone->type = $this->getParams('type');
  90. $zone->relid = $this->getParams('relid');
  91. $zone->name = helpers\IdnaHelper::idnaDecode( $this->getParams('domain'));
  92. $zone->ip = $this->getParams('ip');
  93. $zone->serverid = $server_from->id;
  94. $zone->status = 1;
  95. if(!$module_from->zoneExists())
  96. {
  97. $zone->status = 0;
  98. }
  99. helpers\ImportExportFileHelper::bulkImportZoneToFile($zone, 'zonesFilesStorage'.DIRECTORY_SEPARATOR.'bulkZones', $this->parent->getParams('toFile'));
  100. $this->setStatus(task\TaskStatusEnum::FINISHED);
  101. $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
  102. $result->data['status'] = 'imported';
  103. $result->save();
  104. LogHelper::addSuccessLogUsingZone('Cron Importer - Import Zone', 'Zone Imported', $zone);
  105. }
  106. catch(Exception $exc)
  107. {
  108. $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
  109. $result->data['status'] = $exc->getMessage();
  110. $result->save();
  111. LogHelper::addFailLog('Cron Importer - Import Failed', $exc->getMessage());
  112. }
  113. }
  114. }