Call PluginInstaller::getInstance(), set install option

This commit is contained in:
billz 2024-12-20 18:53:35 -08:00
parent 247b35b254
commit 9b087f88a7

View File

@ -153,6 +153,9 @@ function DisplaySystem(&$extraFooterScripts)
*/ */
function getUserPlugins() function getUserPlugins()
{ {
$pluginInstaller = \RaspAP\Plugins\PluginInstaller::getInstance();
$installedPlugins = $pluginInstaller->getPlugins();
try { try {
$submodules = getSubmodules(RASPI_PLUGINS_URL); $submodules = getSubmodules(RASPI_PLUGINS_URL);
$plugins = []; $plugins = [];
@ -161,12 +164,24 @@ function getUserPlugins()
$manifest = getPluginManifest($manifestUrl); $manifest = getPluginManifest($manifestUrl);
if ($manifest) { if ($manifest) {
$namespace = $manifest['namespace'] ?? '';
$installed = false;
foreach ($installedPlugins as $plugin) {
if (str_contains($plugin, $namespace)) {
$installed = true;
break;
}
}
$plugins[] = [ $plugins[] = [
'version' => $manifest['version'] ?? 'unknown', 'version' => $manifest['version'] ?? 'unknown',
'name' => $manifest['name'] ?? 'unknown', 'name' => $manifest['name'] ?? 'unknown',
'description' => $manifest['description'] ?? 'No description provided', 'description' => $manifest['description'] ?? 'No description provided',
'plugin_uri' => $manifest['plugin_uri'] ?? $submodule['url'], 'plugin_uri' => $manifest['plugin_uri'] ?? $submodule['url'],
'namespace' => $namespace,
'fa-icon' => $manifest['icon'] ?? 'fas fa-plug', 'fa-icon' => $manifest['icon'] ?? 'fas fa-plug',
'installed' => $installed
]; ];
} }
} }
@ -218,7 +233,8 @@ function getSubmodules(string $repoUrl): array
} }
/** /**
* Returns a plugin's associated manifest in JSON format * Decodes a plugin's associated manifest JSON.
* Returns an array of key-value pairs
* *
* @param string $url * @param string $url
* @return array $json * @return array $json
@ -233,7 +249,7 @@ function getPluginManifest(string $url): ?array
]; ];
$context = stream_context_create($options); $context = stream_context_create($options);
$content= file_get_contents($url, false, $context); $content = file_get_contents($url, false, $context);
if ($content === false) { if ($content === false) {
return null; return null;
@ -259,13 +275,26 @@ function getHTMLPluginsTable(array $plugins): string
$html .= '<th scope="col">Name</th>'; $html .= '<th scope="col">Name</th>';
$html .= '<th scope="col">Version</th>'; $html .= '<th scope="col">Version</th>';
$html .= '<th scope="col">Description</th>'; $html .= '<th scope="col">Description</th>';
$html .= '<th scope="col"></th>';
$html .= '</tr></thead></tbody>'; $html .= '</tr></thead></tbody>';
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
$name = '<i class="' . htmlspecialchars($plugin['fa-icon']) . ' link-secondary me-2"></i><a href="' . htmlspecialchars($plugin['plugin_uri']) . '" target="_blank">'. htmlspecialchars($plugin['name']). '</a>'; $installed = $plugin['installed'];
$html .= '<tr><td>' . $name . '</td>'; if ($installed === true ) {
$html .= '<td>' . htmlspecialchars($plugin['version']) . '</td>'; $status = 'Installed';
$html .= '<td>' . htmlspecialchars($plugin['description']) . '</td>'; } else {
$status = '<button type="button" class="btn btn-outline btn-primary btn-sm text-nowrap"
name="install-plugin" data-bs-toggle="modal" data-bs-target="#installPlugin" />'
. _("Install now") .'</button>';
}
$name = '<i class="' . htmlspecialchars($plugin['fa-icon']) . ' link-secondary me-2"></i><a href="'
. htmlspecialchars($plugin['plugin_uri'])
. '" target="_blank">'
. htmlspecialchars($plugin['name']). '</a>';
$html .= '<tr><td>' .$name. '</td>';
$html .= '<td>' .htmlspecialchars($plugin['version']). '</td>';
$html .= '<td>' .htmlspecialchars($plugin['description']). '</td>';
$html .= '<td>' .$status. '</td>';
} }
$html .= '</tbody></table>'; $html .= '</tbody></table>';
return $html; return $html;