| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\task;
- use \Exception;
- use \MGModule\DNSManager2\mgLibs\custom\manager;
- use \MGModule\DNSManager2\mgLibs\custom\TaskManager;
- use MGModule\DNSManager2\mgLibs\MySQL\result;
- use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
- use \MGModule\DNSManager2\models\custom\server;
- use \MGModule\DNSManager2\models\custom\task;
- use \MGModule\DNSManager2\mgLibs\custom\helpers;
- use \MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
- use MGModule\DNSManager2\models\custom\zone\Repository as ZoneRepository;
- use MGModule\DNSManager2\models\custom\zone\Zone;
- use MGModule\DNSManager2\helpers\custom\ClientFilesManage;
- class ImportToFileWHMCS extends TaskAbstract
- {
- protected $abort_after_repeats = ['main' => 0];
- protected $taskTypeCode = TaskTypeCodesCodes::IMPORTTOFILEWHMCS;
- public function mainDescription()
- {
- // $server_from = new server\Server($this->getParams('from'));
- return 'Import from: WHMCS to: ' . $this->getParams('toFile');
- }
- public function main($params)
- {
- $cron_import_run_each = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_IMPORT_RUN_EACH ? : 5);
- if(!$this->isReadyToRun( $cron_import_run_each ))
- {
- return true;
- }
- LogHelper::addSuccessLog('Cron Importer', 'Cron Importer Started');
- if ($this->getStatus() == task\TaskStatusEnum::START)
- {
- $this->addChild('fetchZonesList', [], $this->task->clientid)->run();
- $this->setStatus(task\TaskStatusEnum::WAITING);
- }
- else if ($this->getStatus() == task\TaskStatusEnum::IN_PROGRESS)
- {
- $zones_per_run = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_IMPORT_ZONES_PER_RUN);
- $childs = $this->getXChilds($zones_per_run, 'import', task\TaskStatusEnum::START);
- if (count($childs) < $zones_per_run)
- {
- $childs += $this->getXChilds($zones_per_run - count($childs), 'import');
- }
- foreach ($childs as $child)
- {
- $child->run();
- }
- $this->setFinishedStatusOnCronRun('import', $zones_per_run);
- }
- LogHelper::addSuccessLog('Cron Importer', 'End Of Cron Importer Run');
- }
- public function fetchZonesList($params)
- {
- if ($this->isInCliMode())
- {
- LogHelper::addSuccessLog('Cron Importer', 'Fetching Zones List For Server Id: ' . $this->parent->getParams('from') . ' Started.');
- }
- $zones = ZoneRepository::factory()->byClientID($this->task->clientid);
- foreach ($zones->get() as $zone)
- {
- $arZone = $zone->toArray()['Zone'];
- unset($arZone['status']);
- $this->addResult($arZone);
- }
- $this->setStatus(task\TaskStatusEnum::FINISHED);
- if ($this->isInCliMode())
- {
- LogHelper::addSuccessLog('Cron Importer', 'Fetching Zones List From WHMCS Completed.');
- }
- }
- public function import($params)
- {
- try
- {
- $server_from = new server\Server($this->getParams('serverid'));
- $module_from = $server_from->getModule();
- $module_from->setDomain($params['name']);
- $zone = new Zone();
- $zone->clientid = $this->getParams('clientid');
- $zone->type = $this->getParams('type');
- $zone->relid = $this->getParams('relid');
- $zone->name = $this->getParams('name');
- $zone->ip = $this->getParams('ip');
- $zone->connectedWithType = $this->getParams('connectedWithType');
- $zone->connectedWithRelid = $this->getParams('connectedWithRelid');
- $zone->serverid = $this->getParams('serverid');
- $zone->created_at = $this->getParams('created_at');
- $zone->updated_at = $this->getParams('updated_at');
- $zone->status = 1; // TODO status
- $file = $this->parent->getParams('toFile');
- $createdBy = $this->parent->getParams('createdBy');
- ClientFilesManage::saveIfNotExists($file, $zone->clientid, ClientFilesManage::BACKUP, $createdBy);
- helpers\ImportExportFileHelper::bulkImportZoneToFile($zone, 'zonesFilesStorage' . DIRECTORY_SEPARATOR . 'bulkZones', $file);
- $this->setStatus(task\TaskStatusEnum::FINISHED);
- $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
- $result->data['status'] = 'imported';
- $result->save();
- LogHelper::addSuccessLogUsingZone('Cron Importer - Import Zone', 'Zone Imported', $zone);
- }
- catch (Exception $exc)
- {
- $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
- $result->data['status'] = $exc->getMessage();
- $result->save();
- LogHelper::addFailLog('Cron Importer - Import Failed', $exc->getMessage());
- }
- }
- }
|