translater/src/BackendBehaviors.php

87 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2023-03-14 23:26:31 +00:00
<?php
2023-10-21 09:45:02 +00:00
2023-03-14 23:26:31 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\translater;
2023-10-21 09:45:02 +00:00
use Dotclear\App;
2023-07-29 12:54:35 +00:00
use Dotclear\Core\Backend\ModulesList;
use Dotclear\Helper\Html\Form\Submit;
2023-04-07 21:32:00 +00:00
use Dotclear\Helper\Html\Html;
2023-03-14 23:26:31 +00:00
2023-10-21 09:45:02 +00:00
/**
* @brief translater backend behaviors class.
* @ingroup translater
*
* @author Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-03-14 23:26:31 +00:00
class BackendBehaviors
{
2023-10-21 09:45:02 +00:00
/**
* Translater instance.
*
* @var ?Translater $translater
*/
2023-03-14 23:26:31 +00:00
private static $translater = null;
/**
2023-10-21 09:45:02 +00:00
* Create instance of Translater once.
2023-03-14 23:26:31 +00:00
*
2023-07-29 12:54:35 +00:00
* @return Translater Translater instance
2023-03-14 23:26:31 +00:00
*/
private static function translater(): Translater
{
2023-10-21 09:45:02 +00:00
if (is_null(self::$translater)) {
2023-03-14 23:26:31 +00:00
self::$translater = new Translater(false);
}
return self::$translater;
}
/**
2023-10-21 09:45:02 +00:00
* Add button to go to module translation.
2023-03-14 23:26:31 +00:00
*
2023-10-21 09:45:02 +00:00
* @param ModulesList $list ModulesList instance
* @param string $id Module id
* @param array<string, mixed> $prop Module properties
2023-03-14 23:26:31 +00:00
*
2023-10-21 09:45:02 +00:00
* @return ?string HTML submit button
2023-03-14 23:26:31 +00:00
*/
2023-07-29 12:54:35 +00:00
public static function adminModulesGetActions(ModulesList $list, string $id, array $prop): ?string
2023-03-14 23:26:31 +00:00
{
2023-10-21 09:45:02 +00:00
if (!is_string($prop['type'])) {
return null;
}
2023-03-14 23:26:31 +00:00
if ($list->getList() != $prop['type'] . '-activate'
|| !self::translater()->getSetting($prop['type'] . '_menu')
2023-10-21 09:45:02 +00:00
|| !App::auth()->isSuperAdmin()
2023-03-14 23:26:31 +00:00
) {
return null;
}
if (self::translater()->hide_default
2023-03-14 23:26:31 +00:00
&& in_array($id, My::defaultDistribModules($prop['type']))
) {
return null;
}
2023-10-21 09:45:02 +00:00
return (new Submit(['translater[' . Html::escapeHTML($id) . ']']))->value(__('Translate'))->render();
2023-03-14 23:26:31 +00:00
}
/**
2023-10-21 09:45:02 +00:00
* Redirect to module translation.
2023-03-14 23:26:31 +00:00
*
2023-10-21 09:45:02 +00:00
* @param ModulesList $list ModulesList instance
* @param array<int, string> $modules Selected modules ids
* @param string $type List type (plugin|theme)
2023-03-14 23:26:31 +00:00
*/
2023-07-29 12:54:35 +00:00
public static function adminModulesDoActions(ModulesList $list, array $modules, string $type): void
2023-03-14 23:26:31 +00:00
{
if (empty($_POST['translater']) || !is_array($_POST['translater'])) {
return;
}
2023-07-29 12:54:35 +00:00
My::redirect(['part' => 'module', 'type' => $type, 'module' => key($_POST['translater'])], '#module-lang');
2023-03-14 23:26:31 +00:00
}
}