ImportToFileWHMCS.php 5.2 KB

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