2014-04-16 17:54:18 +02:00
|
|
|
<?php
|
|
|
|
|
2014-04-27 16:36:15 +02:00
|
|
|
namespace ManiaControl\Plugins;
|
2014-04-16 17:54:18 +02:00
|
|
|
|
|
|
|
use FML\Controls\Frame;
|
|
|
|
use FML\Controls\Label;
|
|
|
|
use FML\Controls\Labels\Label_Button;
|
|
|
|
use FML\Controls\Labels\Label_Text;
|
|
|
|
use FML\Controls\Quads\Quad_Icons64x64_1;
|
2014-04-27 16:22:12 +02:00
|
|
|
use FML\Script\Features\Paging;
|
2014-04-16 17:54:18 +02:00
|
|
|
use FML\Script\Script;
|
|
|
|
use ManiaControl\Admin\AuthenticationManager;
|
2014-07-25 15:45:10 +02:00
|
|
|
use ManiaControl\Configurator\ConfiguratorMenu;
|
2014-04-16 17:54:18 +02:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
|
|
|
use ManiaControl\Players\Player;
|
2014-06-27 01:18:55 +02:00
|
|
|
use ManiaControl\Utils\WebReader;
|
2014-04-16 17:54:18 +02:00
|
|
|
|
|
|
|
/**
|
2014-05-09 19:40:20 +02:00
|
|
|
* Configurator for installing Plugins
|
2014-04-16 17:54:18 +02:00
|
|
|
*
|
2014-05-02 17:40:47 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2016-05-04 09:57:31 +02:00
|
|
|
* @copyright 2014-2016 ManiaControl Team
|
2014-05-02 17:40:47 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-04-16 17:54:18 +02:00
|
|
|
*/
|
2014-07-01 21:23:50 +02:00
|
|
|
class InstallMenu implements ConfiguratorMenu, ManialinkPageAnswerListener {
|
2014-04-27 16:36:15 +02:00
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-05-09 19:40:20 +02:00
|
|
|
const SETTING_PERMISSION_INSTALL_PLUGINS = 'Install Plugins';
|
2014-05-18 16:26:52 +02:00
|
|
|
const ACTION_PREFIX_INSTALL_PLUGIN = 'PluginInstallMenu.Install.';
|
2014-06-20 00:47:32 +02:00
|
|
|
const ACTION_REFRESH_LIST = 'PluginInstallMenu.RefreshList';
|
2014-04-16 17:54:18 +02:00
|
|
|
|
2014-05-09 19:40:20 +02:00
|
|
|
/*
|
2014-07-25 16:28:47 +02:00
|
|
|
* Private properties
|
2014-05-09 19:40:20 +02:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2014-05-09 19:40:20 +02:00
|
|
|
private $maniaControl = null;
|
|
|
|
|
2014-04-16 17:54:18 +02:00
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Create a new plugin install menu instance
|
2014-04-16 17:54:18 +02:00
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
// Permissions
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_INSTALL_PLUGINS, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
|
2014-08-03 01:34:18 +02:00
|
|
|
|
|
|
|
// Callbacks
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_REFRESH_LIST, $this, 'handleRefreshListAction');
|
2014-04-16 17:54:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-24 23:15:25 +02:00
|
|
|
/**
|
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
|
|
|
|
*/
|
|
|
|
public static function getTitle() {
|
|
|
|
return 'Install Plugins';
|
|
|
|
}
|
|
|
|
|
2014-04-16 17:54:18 +02:00
|
|
|
/**
|
2014-05-09 19:40:20 +02:00
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
|
2014-04-16 17:54:18 +02:00
|
|
|
*/
|
2014-04-30 05:27:23 +02:00
|
|
|
public function getMenu($width, $height, Script $script, Player $player) {
|
2014-05-02 17:40:47 +02:00
|
|
|
$paging = new Paging();
|
|
|
|
$script->addFeature($paging);
|
|
|
|
$frame = new Frame();
|
2014-04-16 17:54:18 +02:00
|
|
|
|
|
|
|
// Config
|
2014-06-20 00:47:32 +02:00
|
|
|
$pagerSize = 9.;
|
|
|
|
$entryHeight = 5.;
|
|
|
|
$posY = 0.;
|
|
|
|
$pageFrame = null;
|
2014-04-16 17:54:18 +02:00
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
|
2014-08-25 15:29:45 +02:00
|
|
|
$response = WebReader::getUrl($url);
|
2014-06-27 01:18:55 +02:00
|
|
|
$dataJson = $response->getContent();
|
2014-05-02 17:40:47 +02:00
|
|
|
$pluginList = json_decode($dataJson);
|
|
|
|
$index = 0;
|
2014-04-16 17:54:18 +02:00
|
|
|
|
2014-06-20 00:47:32 +02:00
|
|
|
if (!is_array($pluginList)) {
|
|
|
|
// Error text
|
|
|
|
$errorFrame = $this->getErrorFrame();
|
|
|
|
$frame->add($errorFrame);
|
|
|
|
} else if (empty($pluginList)) {
|
|
|
|
// Empty text
|
|
|
|
$emptyFrame = $this->getEmptyFrame();
|
|
|
|
$frame->add($emptyFrame);
|
|
|
|
} else {
|
|
|
|
// Build plugin list
|
|
|
|
// Pagers
|
|
|
|
$pagerPrev = new Quad_Icons64x64_1();
|
|
|
|
$frame->add($pagerPrev);
|
2014-08-13 11:05:52 +02:00
|
|
|
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2)->setSize($pagerSize, $pagerSize)->setSubStyle($pagerPrev::SUBSTYLE_ArrowPrev);
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
$pagerNext = clone $pagerPrev;
|
|
|
|
$frame->add($pagerNext);
|
|
|
|
$pagerNext->setX($width * 0.45);
|
|
|
|
|
|
|
|
$pageCountLabel = new Label_Text();
|
|
|
|
$frame->add($pageCountLabel);
|
2014-08-13 11:05:52 +02:00
|
|
|
$pageCountLabel->setHAlign($pageCountLabel::RIGHT)->setPosition($width * 0.35, $height * -0.44, 1)->setStyle($pageCountLabel::STYLE_TextTitle1)->setTextSize(2);
|
2014-06-20 00:47:32 +02:00
|
|
|
|
2014-08-13 11:05:52 +02:00
|
|
|
$paging->addButton($pagerNext)->addButton($pagerPrev)->setLabel($pageCountLabel);
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
// Info tooltip
|
|
|
|
$infoTooltipLabel = new Label();
|
|
|
|
$frame->add($infoTooltipLabel);
|
2014-08-13 11:05:52 +02:00
|
|
|
$infoTooltipLabel->setAlign($infoTooltipLabel::LEFT, $infoTooltipLabel::TOP)->setPosition($width * -0.45, $height * -0.22)->setSize($width * 0.7, $entryHeight)->setTextSize(1)->setTranslate(true)->setVisible(false)->setAutoNewLine(true)->setMaxLines(5);
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
// List plugins
|
2014-05-02 17:40:47 +02:00
|
|
|
foreach ($pluginList as $plugin) {
|
2014-08-13 11:05:52 +02:00
|
|
|
if ($this->maniaControl->getPluginManager()->isPluginIdInstalled($plugin->id)
|
2014-08-05 02:17:41 +02:00
|
|
|
) {
|
2014-06-20 00:47:32 +02:00
|
|
|
// Already installed -> Skip
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-16 17:54:18 +02:00
|
|
|
|
2014-06-20 00:47:32 +02:00
|
|
|
if ($index % 10 === 0) {
|
|
|
|
// New page
|
|
|
|
$pageFrame = new Frame();
|
|
|
|
$frame->add($pageFrame);
|
|
|
|
$paging->addPage($pageFrame);
|
|
|
|
$posY = $height * 0.41;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pluginFrame = new Frame();
|
|
|
|
$pageFrame->add($pluginFrame);
|
|
|
|
$pluginFrame->setY($posY);
|
|
|
|
|
|
|
|
$nameLabel = new Label_Text();
|
|
|
|
$pluginFrame->add($nameLabel);
|
2014-08-13 11:05:52 +02:00
|
|
|
$nameLabel->setHAlign($nameLabel::LEFT)->setX($width * -0.46)->setSize($width * 0.62, $entryHeight)->setStyle($nameLabel::STYLE_TextCardSmall)->setTextSize(2)->setText($plugin->name);
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
$description = "Author: {$plugin->author}\nVersion: {$plugin->currentVersion->version}\nDesc: {$plugin->description}";
|
|
|
|
$nameLabel->addTooltipLabelFeature($infoTooltipLabel, $description);
|
2014-04-16 17:54:18 +02:00
|
|
|
|
2014-06-20 00:47:32 +02:00
|
|
|
if (!$this->isPluginCompatible($plugin)) {
|
|
|
|
// Incompatibility label
|
|
|
|
$infoLabel = new Label_Text();
|
|
|
|
$pluginFrame->add($infoLabel);
|
2014-08-13 11:05:52 +02:00
|
|
|
$infoLabel->setHAlign($infoLabel::RIGHT)->setX($width * 0.47)->setSize($width * 0.33, $entryHeight)->setTextSize(1)->setTextColor('f30');
|
2014-06-20 00:47:32 +02:00
|
|
|
if ($plugin->currentVersion->min_mc_version > ManiaControl::VERSION) {
|
|
|
|
$infoLabel->setText("Needs at least MC-Version '{$plugin->currentVersion->min_mc_version}'");
|
|
|
|
} else {
|
|
|
|
$infoLabel->setText("Needs at most MC-Version '{$plugin->currentVersion->max_mc_version}'");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Install button
|
2014-04-16 17:54:18 +02:00
|
|
|
$installButton = new Label_Button();
|
|
|
|
$pluginFrame->add($installButton);
|
2014-08-13 11:05:52 +02:00
|
|
|
$installButton->setHAlign($installButton::RIGHT)->setX($width * 0.47)->setStyle($installButton::STYLE_CardButtonSmall)->setText('Install')->setTranslate(true)->setAction(self::ACTION_PREFIX_INSTALL_PLUGIN . $plugin->id);
|
2014-04-16 17:54:18 +02:00
|
|
|
}
|
2014-06-20 00:47:32 +02:00
|
|
|
|
2014-07-01 21:43:08 +02:00
|
|
|
if ($plugin->currentVersion->verified > 0) {
|
|
|
|
// Suggested quad
|
|
|
|
$suggestedQuad = new Quad_Icons64x64_1();
|
|
|
|
$pluginFrame->add($suggestedQuad);
|
2014-08-13 11:05:52 +02:00
|
|
|
$suggestedQuad->setPosition($width * 0.45, $entryHeight * 0.12, 2)->setSize(4, 4)->setSubStyle($suggestedQuad::SUBSTYLE_StateSuggested);
|
2014-07-01 21:43:08 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 00:47:32 +02:00
|
|
|
$posY -= $entryHeight;
|
|
|
|
$index++;
|
2014-04-16 17:54:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $frame;
|
|
|
|
}
|
|
|
|
|
2014-06-20 00:47:32 +02:00
|
|
|
/**
|
|
|
|
* Build the Frame to display when an Error occurred
|
|
|
|
*
|
|
|
|
* @return Frame
|
|
|
|
*/
|
|
|
|
private function getErrorFrame() {
|
|
|
|
$frame = new Frame();
|
|
|
|
|
|
|
|
$infoLabel = new Label_Text();
|
|
|
|
$frame->add($infoLabel);
|
2014-08-13 11:05:52 +02:00
|
|
|
$infoLabel->setVAlign($infoLabel::BOTTOM)->setY(2)->setSize(100, 25)->setTextColor('f30')->setTranslate(true)->setText('An error occurred. Please try again later.');
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
$refreshQuad = new Quad_Icons64x64_1();
|
|
|
|
$frame->add($refreshQuad);
|
2014-08-13 11:05:52 +02:00
|
|
|
$refreshQuad->setY(-4)->setSize(8, 8)->setSubStyle($refreshQuad::SUBSTYLE_Refresh)->setAction(self::ACTION_REFRESH_LIST);
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
return $frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the Frame to display when no Plugins are left to install
|
|
|
|
*
|
|
|
|
* @return Frame
|
|
|
|
*/
|
|
|
|
private function getEmptyFrame() {
|
|
|
|
$frame = new Frame();
|
|
|
|
|
|
|
|
$infoLabel = new Label_Text();
|
|
|
|
$frame->add($infoLabel);
|
2014-08-13 11:05:52 +02:00
|
|
|
$infoLabel->setSize(100, 50)->setTextColor('0f3')->setTranslate(true)->setText('No other plugins available.');
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
return $frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given Plugin can be installed without Issues
|
|
|
|
*
|
|
|
|
* @param object $plugin
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isPluginCompatible($plugin) {
|
|
|
|
if ($plugin->currentVersion->min_mc_version > 0 && $plugin->currentVersion->min_mc_version > ManiaControl::VERSION) {
|
|
|
|
// ManiaControl needs to be updated
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($plugin->currentVersion->max_mc_version > 0 && $plugin->currentVersion->max_mc_version < ManiaControl::VERSION) {
|
|
|
|
// Plugin is outdated
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-16 17:54:18 +02:00
|
|
|
/**
|
2014-05-09 19:40:20 +02:00
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
|
2014-04-16 17:54:18 +02:00
|
|
|
*/
|
|
|
|
public function saveConfigData(array $configData, Player $player) {
|
|
|
|
}
|
2014-06-20 00:47:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the Refresh MLAction
|
|
|
|
*
|
|
|
|
* @param array $actionCallback
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function handleRefreshListAction(array $actionCallback, Player $player) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getConfigurator()->showMenu($player, $this);
|
2014-06-20 00:47:32 +02:00
|
|
|
}
|
|
|
|
}
|