| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers\Plugins; |
| 4 |
|
| 5 |
use AcyMailing\Classes\PluginClass; |
| 6 |
use AcyMailing\Helpers\WorkflowHelper; |
| 7 |
|
| 8 |
trait Available |
| 9 |
{ |
| 10 |
public function available(): void |
| 11 |
{ |
| 12 |
acym_setVar('layout', 'available'); |
| 13 |
$data = []; |
| 14 |
$data['tabs'] = $this->tabs; |
| 15 |
$data['tab'] = 'available'; |
| 16 |
$data['types'] = $this->types; |
| 17 |
$data['level'] = $this->level; |
| 18 |
$data['workflowHelper'] = new WorkflowHelper(); |
| 19 |
|
| 20 |
parent::display($data); |
| 21 |
} |
| 22 |
|
| 23 |
public function download(bool $ajax = true, string $pluginFromUpdate = ''): string |
| 24 |
{ |
| 25 |
$plugin = []; |
| 26 |
|
| 27 |
if (empty($pluginFromUpdate)) { |
| 28 |
$this->isLatestAcyMailingVersion(); |
| 29 |
$plugin = acym_getVar('array', 'plugin'); |
| 30 |
} else { |
| 31 |
$plugins = acym_fileGetContent(ACYM_UPDATEME_API_URL.'public/addons'); |
| 32 |
$plugins = @json_decode($plugins, true); |
| 33 |
if (empty($plugins)) { |
| 34 |
$this->handleError('ACYM_ISSUE_WHILE_INSTALLING', $ajax); |
| 35 |
} |
| 36 |
|
| 37 |
foreach ($plugins as $onePlugin) { |
| 38 |
if ($pluginFromUpdate === $onePlugin['file_name']) { |
| 39 |
$plugin = $onePlugin; |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (empty($plugin)) { |
| 45 |
return acym_translation('ACYM_ADD_ON_NOT_FOUND'); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
$pluginClass = new PluginClass(); |
| 50 |
$errorMessage = $pluginClass->downloadAddon($plugin['file_name'], $ajax); |
| 51 |
if (!empty($errorMessage)) { |
| 52 |
return $errorMessage; |
| 53 |
} |
| 54 |
|
| 55 |
$pluginToSave = new \stdClass(); |
| 56 |
$pluginToSave->title = $plugin['name']; |
| 57 |
$pluginToSave->folder_name = $plugin['file_name']; |
| 58 |
$pluginToSave->version = $plugin['version']; |
| 59 |
$pluginToSave->latest_version = $plugin['version']; |
| 60 |
$pluginToSave->description = $plugin['description']; |
| 61 |
$pluginToSave->active = 1; |
| 62 |
$pluginToSave->category = $plugin['category']; |
| 63 |
$pluginToSave->level = $plugin['level']; |
| 64 |
$pluginToSave->uptodate = 1; |
| 65 |
$pluginToSave->id = $pluginClass->save($pluginToSave); |
| 66 |
|
| 67 |
if (!empty($pluginToSave->id)) { |
| 68 |
if ($ajax) { |
| 69 |
acym_sendAjaxResponse(acym_translation('ACYM_ADD_ON_SUCCESSFULLY_INSTALLED')); |
| 70 |
} |
| 71 |
|
| 72 |
return ''; |
| 73 |
} |
| 74 |
|
| 75 |
return $this->handleError('ACYM_ISSUE_WHILE_INSTALLING', $ajax); |
| 76 |
} |
| 77 |
|
| 78 |
public function getAllPluginsAjax(): void |
| 79 |
{ |
| 80 |
acym_sendAjaxResponse('', $this->getAllPlugins()); |
| 81 |
} |
| 82 |
|
| 83 |
public function getAllPlugins(): array |
| 84 |
{ |
| 85 |
$pluginClass = new PluginClass(); |
| 86 |
$plugins = $pluginClass->getMatchingElements(['ordering' => 'title']); |
| 87 |
|
| 88 |
foreach ($plugins['elements'] as $key => $plugin) { |
| 89 |
if (!empty($plugin->settings)) { |
| 90 |
$plugins['elements'][$key]->settings = json_decode($plugin->settings, true); |
| 91 |
} |
| 92 |
} |
| 93 |
acym_trigger('onAcymAddSettings', [&$plugins['elements']]); |
| 94 |
|
| 95 |
return $plugins['elements']; |
| 96 |
} |
| 97 |
|
| 98 |
private function handleError(string $error, bool $ajax): string |
| 99 |
{ |
| 100 |
if ($ajax) { |
| 101 |
acym_sendAjaxResponse(acym_translation($error), [], false); |
| 102 |
} |
| 103 |
|
| 104 |
return acym_translation($error); |
| 105 |
} |
| 106 |
} |
| 107 |
|