Move admin/addons/{addon} to src/Module

- Add Module\Admin\Addons\Details class
- Add route for admin/addons/{addon}
- Remove addons pages from mod/admin
This commit is contained in:
Hypolite Petovan 2019-04-21 15:47:25 -04:00
parent 9bbb438534
commit 87e51ddd67
4 changed files with 168 additions and 121 deletions

View file

@ -10,7 +10,6 @@ use Friendica\BaseModule;
use Friendica\Content\Feature; use Friendica\Content\Feature;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Content\Text\Markdown; use Friendica\Content\Text\Markdown;
use Friendica\Core\Addon;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Logger; use Friendica\Core\Logger;
@ -91,17 +90,6 @@ function admin_post(App $a)
case 'users': case 'users':
admin_page_users_post($a); admin_page_users_post($a);
break; break;
case 'addons':
if ($a->argc > 2 &&
is_file("addon/" . $a->argv[2] . "/" . $a->argv[2] . ".php")) {
include_once "addon/" . $a->argv[2] . "/" . $a->argv[2] . ".php";
if (function_exists($a->argv[2] . '_addon_admin_post')) {
$func = $a->argv[2] . '_addon_admin_post';
$func($a);
}
}
$return_path = 'admin/addons/' . $a->argv[2];
break;
case 'themes': case 'themes':
if ($a->argc < 2) { if ($a->argc < 2) {
if ($a->isAjax()) { if ($a->isAjax()) {
@ -220,17 +208,7 @@ function admin_content(App $a)
]] ]]
]; ];
/* get addons admin page */
$r = q("SELECT `name` FROM `addon` WHERE `plugin_admin` = 1 ORDER BY `name`");
$aside_tools['addons_admin'] = []; $aside_tools['addons_admin'] = [];
$addons_admin = [];
foreach ($r as $h) {
$addon = $h['name'];
$aside_tools['addons_admin'][] = ["admin/addons/" . $addon, $addon, "addon"];
// temp addons with admin
$addons_admin[] = $addon;
}
$t = Renderer::getMarkupTemplate('admin/aside.tpl'); $t = Renderer::getMarkupTemplate('admin/aside.tpl');
$a->page['aside'] .= Renderer::replaceMacros($t, [ $a->page['aside'] .= Renderer::replaceMacros($t, [
@ -253,9 +231,6 @@ function admin_content(App $a)
case 'users': case 'users':
$o = admin_page_users($a); $o = admin_page_users($a);
break; break;
case 'addons':
$o = admin_page_addons($a, $addons_admin);
break;
case 'themes': case 'themes':
$o = admin_page_themes($a); $o = admin_page_themes($a);
break; break;
@ -1668,102 +1643,6 @@ function admin_page_users(App $a)
return $o; return $o;
} }
/**
* @brief Addons admin page
*
* This function generates the admin panel page for managing addons on the
* friendica node. If an addon name is given a single page showing the details
* for this addon is generated. If no name is given, a list of available
* addons is shown.
*
* The template used for displaying the list of addons and the details of the
* addon are the same as used for the templates.
*
* The returned string returned hulds the HTML code of the page.
*
* @param App $a
* @param array $addons_admin A list of admin addon names
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
function admin_page_addons(App $a, array $addons_admin)
{
/*
* Single addon
*/
if ($a->argc == 3) {
$addon = $a->argv[2];
if (!is_file("addon/$addon/$addon.php")) {
notice(L10n::t("Item not found."));
return '';
}
if (defaults($_GET, 'a', '') == "t") {
BaseModule::checkFormSecurityTokenRedirectOnError('/admin/addons', 'admin_themes', 't');
// Toggle addon status
if (Addon::isEnabled($addon)) {
Addon::uninstall($addon);
info(L10n::t("Addon %s disabled.", $addon));
} else {
Addon::install($addon);
info(L10n::t("Addon %s enabled.", $addon));
}
Addon::saveEnabledList();
$a->internalRedirect('admin/addons');
return ''; // NOTREACHED
}
// display addon details
if (Addon::isEnabled($addon)) {
$status = "on";
$action = L10n::t("Disable");
} else {
$status = "off";
$action = L10n::t("Enable");
}
$readme = null;
if (is_file("addon/$addon/README.md")) {
$readme = Markdown::convert(file_get_contents("addon/$addon/README.md"), false);
} elseif (is_file("addon/$addon/README")) {
$readme = "<pre>" . file_get_contents("addon/$addon/README") . "</pre>";
}
$admin_form = "";
if (in_array($addon, $addons_admin)) {
require_once "addon/$addon/$addon.php";
$func = $addon . '_addon_admin';
$func($a, $admin_form);
}
$t = Renderer::getMarkupTemplate('admin/addon_details.tpl');
return Renderer::replaceMacros($t, [
'$title' => L10n::t('Administration'),
'$page' => L10n::t('Addons'),
'$toggle' => L10n::t('Toggle'),
'$settings' => L10n::t('Settings'),
'$baseurl' => $a->getBaseURL(true),
'$addon' => $addon,
'$status' => $status,
'$action' => $action,
'$info' => Addon::getInfo($addon),
'$str_author' => L10n::t('Author: '),
'$str_maintainer' => L10n::t('Maintainer: '),
'$admin_form' => $admin_form,
'$function' => 'addons',
'$screenshot' => '',
'$readme' => $readme,
'$form_security_token' => BaseModule::getFormSecurityToken("admin_themes"),
]);
}
}
/** /**
* @param array $themes * @param array $themes
* @param string $th * @param string $th

View file

@ -122,6 +122,8 @@ class Router
$collector->addRoute(['GET'] , '[/]' , Module\Admin\Summary::class); $collector->addRoute(['GET'] , '[/]' , Module\Admin\Summary::class);
$collector->addRoute(['GET', 'POST'], '/addons' , Module\Admin\Addons\Index::class); $collector->addRoute(['GET', 'POST'], '/addons' , Module\Admin\Addons\Index::class);
$collector->addRoute(['GET', 'POST'], '/addons/{addon}' , Module\Admin\Addons\Details::class);
$collector->addRoute(['GET'] , '/federation' , Module\Admin\Federation::class); $collector->addRoute(['GET'] , '/federation' , Module\Admin\Federation::class);
$collector->addRoute(['GET', 'POST'], '/themes' , Module\Admin\Themes\Index::class); $collector->addRoute(['GET', 'POST'], '/themes' , Module\Admin\Themes\Index::class);

View file

@ -0,0 +1,129 @@
<?php
namespace Friendica\Module\Admin\Addons;
use Friendica\Content\Text\Markdown;
use Friendica\Core\Addon;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Database\DBA;
use Friendica\Module\BaseAdminModule;
use Friendica\Util\Strings;
class Details extends BaseAdminModule
{
public static function post()
{
parent::post();
$a = self::getApp();
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$addon = $a->argv[2];
$addon = Strings::sanitizeFilePathItem($addon);
if (is_file('addon/' . $addon . '/' . $addon . '.php')) {
include_once 'addon/' . $addon . '/' . $addon . '.php';
if (function_exists($addon . '_addon_admin_post')) {
$func = $addon . '_addon_admin_post';
$func($a);
}
$a->internalRedirect('admin/addons/' . $addon);
}
}
$a->internalRedirect('admin/addons');
}
public static function content()
{
parent::content();
$a = self::getApp();
$addons_admin = [];
$addonsAdminStmt = DBA::select('addon', ['name'], ['plugin_admin' => 1], ['order' => ['name']]);
foreach (DBA::toArray($addonsAdminStmt) as $addon) {
$addonName = $addon['name'];
// temp addons with admin
$addons_admin[] = $addonName;
}
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$addon = $a->argv[2];
$addon = Strings::sanitizeFilePathItem($addon);
if (!is_file("addon/$addon/$addon.php")) {
notice(L10n::t('Item not found.'));
$a->internalRedirect('admin/addons');
}
if (defaults($_GET, 'action', '') == 'toggle') {
parent::checkFormSecurityTokenRedirectOnError('/admin/addons', 'admin_themes', 't');
// Toggle addon status
if (Addon::isEnabled($addon)) {
Addon::uninstall($addon);
info(L10n::t('Addon %s disabled.', $addon));
} else {
Addon::install($addon);
info(L10n::t('Addon %s enabled.', $addon));
}
Addon::saveEnabledList();
$a->internalRedirect('admin/addons/' . $addon);
}
// display addon details
if (Addon::isEnabled($addon)) {
$status = 'on';
$action = L10n::t('Disable');
} else {
$status = 'off';
$action = L10n::t('Enable');
}
$readme = null;
if (is_file("addon/$addon/README.md")) {
$readme = Markdown::convert(file_get_contents("addon/$addon/README.md"), false);
} elseif (is_file("addon/$addon/README")) {
$readme = '<pre>' . file_get_contents("addon/$addon/README") . '</pre>';
}
$admin_form = '';
if (in_array($addon, $addons_admin)) {
require_once "addon/$addon/$addon.php";
$func = $addon . '_addon_admin';
$func($a, $admin_form);
}
$t = Renderer::getMarkupTemplate('admin/addons/details.tpl');
return Renderer::replaceMacros($t, [
'$title' => L10n::t('Administration'),
'$page' => L10n::t('Addons'),
'$toggle' => L10n::t('Toggle'),
'$settings' => L10n::t('Settings'),
'$baseurl' => $a->getBaseURL(true),
'$addon' => $addon,
'$status' => $status,
'$action' => $action,
'$info' => Addon::getInfo($addon),
'$str_author' => L10n::t('Author: '),
'$str_maintainer' => L10n::t('Maintainer: '),
'$admin_form' => $admin_form,
'$function' => 'addons',
'$screenshot' => '',
'$readme' => $readme,
'$form_security_token' => parent::getFormSecurityToken('admin_themes'),
]);
}
$a->internalRedirect('admin/addons');
}
}

View file

@ -0,0 +1,37 @@
<div id="adminpage">
<h1>{{$title}} - {{$page}}</h1>
<p><span class="toggleaddon icon {{$status}}"></span> {{$info.name}} - {{$info.version}} : <a href="{{$baseurl}}/admin/{{$function}}/{{$addon}}/?action=toggle&amp;t={{$form_security_token}}">{{$action}}</a></p>
<p>{{$info.description nofilter}}</p>
<p class="author">{{$str_author}}
{{foreach $info.author as $a name=authors}}
{{if $a.link}}<a href="{{$a.link}}">{{$a.name}}</a>{{else}}{{$a.name}}{{/if}}{{if $smarty.foreach.authors.last}}{{else}}, {{/if}}
{{/foreach}}
</p>
<p class="maintainer">{{$str_maintainer}}
{{foreach $info.maintainer as $a name=maintainers}}
{{if $a.link}}<a href="{{$a.link}}">{{$a.name}}</a>{{else}}{{$a.name}}{{/if}}{{if $smarty.foreach.maintainers.last}}{{else}}, {{/if}}
{{/foreach}}
</p>
{{if $screenshot}}
<a href="{{$screenshot.0}}" class="screenshot"><img src="{{$screenshot.0}}" alt="{{$screenshot.1}}" /></a>
{{/if}}
{{if $admin_form}}
<h3>{{$settings}}</h3>
<form method="post" action="{{$baseurl}}/admin/{{$function}}/{{$addon}}">
{{$admin_form nofilter}}
</form>
{{/if}}
{{if $readme}}
<h3>Readme</h3>
<div id="addon_readme">
{{$readme nofilter}}
</div>
{{/if}}
</div>