| @@ -1,0 +1,148 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Classes; | |
| 4 | + | |
| 5 | +use AcyMailing\Core\AcymClass; | |
| 6 | + | |
| 7 | +class PluginClass extends AcymClass | |
| 8 | +{ | |
| 9 | + private array $plugins = []; | |
| 10 | + | |
| 11 | + public function __construct() | |
| 12 | + { | |
| 13 | + parent::__construct(); | |
| 14 | + | |
| 15 | + $this->table = 'plugin'; | |
| 16 | + $this->pkey = 'id'; | |
| 17 | + } | |
| 18 | + | |
| 19 | + public function getPlugins(): array | |
| 20 | + { | |
| 21 | + global $acymPluginByFolderName; | |
| 22 | + if (empty($acymPluginByFolderName)) { | |
| 23 | + $acymPluginByFolderName = $this->getAll('folder_name'); | |
| 24 | + } | |
| 25 | + | |
| 26 | + $this->plugins = $acymPluginByFolderName; | |
| 27 | + | |
| 28 | + return $this->plugins; | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function getOnePluginByFolderName(string $folderName): ?object | |
| 32 | + { | |
| 33 | + $plugins = $this->getPlugins(); | |
| 34 | + | |
| 35 | + return $plugins[$folderName] ?? null; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public function getNotUptoDatePlugins(): array | |
| 39 | + { | |
| 40 | + $result = acym_loadResult('SHOW TABLES LIKE "%_acym_plugin"'); | |
| 41 | + if (empty($result)) { | |
| 42 | + return []; | |
| 43 | + } | |
| 44 | + | |
| 45 | + return acym_loadResultArray('SELECT folder_name FROM #__acym_plugin WHERE uptodate = 0'); | |
| 46 | + } | |
| 47 | + | |
| 48 | + public function addIntegrationIfMissing(object $plugin): void | |
| 49 | + { | |
| 50 | + if (empty($plugin->pluginDescription->name)) { | |
| 51 | + return; | |
| 52 | + } | |
| 53 | + | |
| 54 | + $plugins = $this->getPlugins(); | |
| 55 | + $data = $plugins[$plugin->name] ?? null; | |
| 56 | + | |
| 57 | + // Prepare the missing entry in the db | |
| 58 | + $newPlugin = new \stdClass(); | |
| 59 | + $newPlugin->title = $plugin->pluginDescription->name; | |
| 60 | + $newPlugin->folder_name = $plugin->name; | |
| 61 | + $newPlugin->version = '1.0'; | |
| 62 | + $newPlugin->active = 1; | |
| 63 | + $newPlugin->category = $plugin->pluginDescription->category; | |
| 64 | + $newPlugin->level = 'starter'; | |
| 65 | + $newPlugin->uptodate = 1; | |
| 66 | + $newPlugin->description = $plugin->pluginDescription->description; | |
| 67 | + $newPlugin->latest_version = '1.0'; | |
| 68 | + $newPlugin->type = 'PLUGIN'; | |
| 69 | + | |
| 70 | + if (!empty($data)) { | |
| 71 | + $newPlugin->id = $data->id; | |
| 72 | + $newPlugin->settings = $data->settings; | |
| 73 | + | |
| 74 | + if ($data->type !== 'ADDON') { | |
| 75 | + return; | |
| 76 | + } | |
| 77 | + | |
| 78 | + // The integration has just been activated and the old addon was installed | |
| 79 | + | |
| 80 | + // Remove the old addon's files | |
| 81 | + if (file_exists(ACYM_ADDONS_FOLDER_PATH.$plugin->name)) { | |
| 82 | + acym_deleteFolder(ACYM_ADDONS_FOLDER_PATH.$plugin->name); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + $this->save($newPlugin); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function enable(string $folderName): void | |
| 90 | + { | |
| 91 | + $plugin = $this->getOnePluginByFolderName($folderName); | |
| 92 | + if (empty($plugin)) return; | |
| 93 | + | |
| 94 | + $plugin->active = 1; | |
| 95 | + $this->save($plugin); | |
| 96 | + } | |
| 97 | + | |
| 98 | + public function disable(string $folderName): void | |
| 99 | + { | |
| 100 | + $plugin = $this->getOnePluginByFolderName($folderName); | |
| 101 | + if (empty($plugin)) return; | |
| 102 | + | |
| 103 | + $plugin->active = 0; | |
| 104 | + $this->save($plugin); | |
| 105 | + } | |
| 106 | + | |
| 107 | + public function deleteByFolderName(string $folderName): void | |
| 108 | + { | |
| 109 | + $plugin = $this->getOnePluginByFolderName($folderName); | |
| 110 | + if (empty($plugin)) return; | |
| 111 | + | |
| 112 | + parent::delete([$plugin->id]); | |
| 113 | + } | |
| 114 | + | |
| 115 | + public function updateAddon(string $addon): ?int | |
| 116 | + { | |
| 117 | + $plugin = $this->getOnePluginByFolderName($addon); | |
| 118 | + | |
| 119 | + if (empty($plugin)) { | |
| 120 | + return null; | |
| 121 | + } | |
| 122 | + | |
| 123 | + $pluginClass = new PluginClass(); | |
| 124 | + $pluginClass->downloadAddon($addon); | |
| 125 | + | |
| 126 | + $pluginToSave = new \stdClass(); | |
| 127 | + $pluginToSave->id = $plugin->id; | |
| 128 | + $pluginToSave->version = $plugin->latest_version; | |
| 129 | + $pluginToSave->uptodate = 1; | |
| 130 | + | |
| 131 | + return (int)$this->save($pluginToSave); | |
| 132 | + } | |
| 133 | + | |
| 134 | + public function downloadAddon(string $name, bool $ajax = true): ?string | |
| 135 | + { | |
| 136 | + | |
| 137 | + return null; | |
| 138 | + } | |
| 139 | + | |
| 140 | + private function handleError(string $error, bool $ajax): string | |
| 141 | + { | |
| 142 | + if ($ajax) { | |
| 143 | + acym_sendAjaxResponse(acym_translation($error), [], false); | |
| 144 | + } | |
| 145 | + | |
| 146 | + return acym_translation($error); | |
| 147 | + } | |
| 148 | +} | |