Skip to content

Commit

Permalink
GP-44570 Fix and refactor task export
Browse files Browse the repository at this point in the history
  • Loading branch information
pfigel committed Sep 16, 2024
1 parent b86f40b commit 6aeaa63
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions CRM/Sqltasks/Page/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,20 @@ public function run() {

if ($task_id) {
$task = CRM_Sqltasks_BAO_SqlTask::findById($task_id);

$task_data = $task->exportData([
'abort_on_error',
'config',
'category',
'description',
'input_required',
'last_modified',
'parallel_exec',
'run_permissions',
'scheduled',
]);

$file_name = preg_replace('/[^A-Za-z0-9_\- ]/', '', $task->name) . '.sqltask';
$file_content = json_encode($task_data, JSON_PRETTY_PRINT);
$file_name = $this->getTaskFileName($task);
$file_content = $this->getTaskFileContent($task);

CRM_Utils_System::download($file_name, 'application/json', $file_content);
}
else {
$tasks = CRM_Sqltasks_Task::getAllTasks();
$tasks = CRM_Sqltasks_BAO_SqlTask::generator();
if (!empty($tasks)) {
$zip = new ZipArchive();
$fileURL = CRM_Core_Config::singleton()->uploadDir . "sqltasks_" . date('Ymd') . ".zip";
if ($zip->open($fileURL, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === TRUE) {
foreach ($tasks as $task) {
$taskFileName = preg_replace('/[^A-Za-z0-9_\- ]/', '', $task->getAttribute('name')) . '.sqltask';
$zip->addFromString($taskFileName, $task->exportConfiguration());
$taskFileName = $this->getTaskFileName($task);
$zip->addFromString($taskFileName, $this->getTaskFileContent($task));
}
$zip->close();
$null = NULL;
Expand All @@ -79,4 +66,23 @@ public function run() {
}
}

private function getTaskFileContent(CRM_Sqltasks_BAO_SqlTask $task) {
$task_data = $task->exportData([
'abort_on_error',
'config',
'category',
'description',
'input_required',
'last_modified',
'parallel_exec',
'run_permissions',
'scheduled',
]);
return json_encode($task_data, JSON_PRETTY_PRINT);
}

private function getTaskFileName(CRM_Sqltasks_BAO_SqlTask $task) {
return preg_replace('/[^A-Za-z0-9_\- ]/', '', $task->name) . '.sqltask';
}

}

0 comments on commit 6aeaa63

Please sign in to comment.