[ChooseTheme] Fixed plugin

This commit is contained in:
Diogo Cordeiro 2019-08-04 14:07:00 +01:00
parent 66e55d1a1f
commit 2f341a3369
2 changed files with 105 additions and 91 deletions

View File

@ -22,22 +22,26 @@
* @license GNU Affero General Public License http://www.gnu.org/licenses/ * @license GNU Affero General Public License http://www.gnu.org/licenses/
*/ */
class ChooseThemePlugin extends Plugin { defined('GNUSOCIAL') || die();
class ChooseThemePlugin extends Plugin
{
const PLUGIN_VERSION = '0.1.0'; const PLUGIN_VERSION = '0.1.0';
public function onRouterInitialized(URLMapper $m) { public function onRouterInitialized(URLMapper $m)
{
$m->connect('main/choosethemesettings', ['action' => 'choosethemesettings']); $m->connect('main/choosethemesettings', ['action' => 'choosethemesettings']);
} }
public function onPluginVersion(array &$versions): bool { public function onPluginVersion(array &$versions): bool
{
$versions[] = array('name' => 'ChooseTheme', $versions[] = ['name' => 'ChooseTheme',
'version' => self::PLUGIN_VERSION, 'version' => self::PLUGIN_VERSION,
'author' => 'Knut Erik "abjectio" Hollund', 'author' => 'Knut Erik "abjectio" Hollund',
'homepage' => 'https://gitlab.com/kollektivet0x242/gsp-choosetheme', 'homepage' => 'https://gitlab.com/kollektivet0x242/gsp-choosetheme',
'rawdescription' => 'rawdescription' =>
// TRANS: Plugin description. // TRANS: Module description.
_m('Allowing user to select the preferred theme.')); _m('Allowing user to select the preferred theme.')];
return true; return true;
} }
@ -46,30 +50,34 @@ class ChooseThemePlugin extends Plugin {
* *
* @param Action $action action being executed * @param Action $action action being executed
* *
* @return boolean hook return * @return bool hook return
* @throws Exception
*/ */
function onEndAccountSettingsNav(Action $action) { public function onEndAccountSettingsNav(Action $action): bool
{
$action_name = $action->getActionName(); $action_name = $action->getActionName();
$action->menuItem(common_local_url('choosethemesettings'), $action->menuItem(
// TRANS: Poll plugin menu item on user settings page. common_local_url('choosethemesettings'),
_m('MENU', 'Theme'), // TRANS: Poll plugin menu item on user settings page.
// TRANS: Poll plugin tooltip for user settings menu item. _m('MENU', 'Theme'),
_m('Choose Theme'), // TRANS: Poll plugin tooltip for user settings menu item.
$action_name === 'themesettings'); _m('Choose Theme'),
$action_name === 'themesettings'
);
return true; return true;
} }
function onStartShowStylesheets(Action $action) { public function onStartShowStylesheets(Action $action)
{
//get the theme and set the current config for site and theme. //get the theme and set the current config for site and theme.
if($action->getScoped() instanceof Profile) { if ($action->getScoped() instanceof Profile) {
$site_theme = common_config('site','theme'); $site_theme = common_config('site', 'theme');
$user_theme = $action->getScoped()->getPref('chosen_theme', 'theme', $site_theme); $user_theme = $action->getScoped()->getPref('chosen_theme', 'theme', $site_theme);
common_config_set('site', 'theme', $user_theme); common_config_set('site', 'theme', $user_theme);
} }
return true; return true;
} }
} }

View File

@ -22,18 +22,17 @@
* @license GNU Affero General Public License http://www.gnu.org/licenses/ * @license GNU Affero General Public License http://www.gnu.org/licenses/
*/ */
if (!defined('STATUSNET') && !defined('GNUSOCIAL')) { defined('GNUSOCIAL') || die();
exit(1);
}
class ChooseThemeSettingsAction extends SettingsAction {
class ChooseThemeSettingsAction extends SettingsAction
{
/** /**
* Title of the page * Title of the page
* @return string Page title * @return string Page title
* @throws Exception
*/ */
function title() { public function title(): string
{
// TRANS: Page title. // TRANS: Page title.
return _m('Choose theme settings'); return _m('Choose theme settings');
} }
@ -41,8 +40,10 @@ class ChooseThemeSettingsAction extends SettingsAction {
/** /**
* Instructions for use * Instructions for use
* @return string Instructions for use * @return string Instructions for use
* @throws Exception
*/ */
function getInstructions() { public function getInstructions(): string
{
// TRANS: Page instructions. // TRANS: Page instructions.
return _m('Choose theme'); return _m('Choose theme');
} }
@ -51,17 +52,17 @@ class ChooseThemeSettingsAction extends SettingsAction {
* Show the form for ChooseTheme * Show the form for ChooseTheme
* @return void * @return void
*/ */
function showContent() { public function showContent(): void
{
$site_theme = common_config('site','theme'); $site_theme = common_config('site', 'theme');
$prefs = $this->scoped->getPref('chosen_theme', 'theme',$site_theme); $prefs = $this->scoped->getPref('chosen_theme', 'theme', $site_theme);
if ($prefs === null) { if ($prefs === null) {
common_debug('No chosen theme found in database for user.'); common_debug('No chosen theme found in database for user.');
} }
//Get a list of available themes on instance //Get a list of available themes on instance
$available_themes = Theme::listAvailable(); $available_themes = Theme::listAvailable();
$chosenone = array_search($prefs,$available_themes,true); $chosenone = array_search($prefs, $available_themes, true);
$form = new ChooseThemeForm($this, $chosenone); $form = new ChooseThemeForm($this, $chosenone);
$form->show(); $form->show();
} }
@ -70,42 +71,43 @@ class ChooseThemeSettingsAction extends SettingsAction {
/** /**
* Handler method * Handler method
* *
* @param array $argarray is ignored since it's now passed in in prepare() * @return string
* @return void * @throws Exception
*/ */
function handlePost() { public function doPost(): string
{
//Get a list of available themes on instance
$available_themes = Theme::listAvailable();
$chosen_theme = $available_themes[(int)$this->arg('dwct', '0')];
//Get a list of available themes on instance $this->msg = 'Settings saved.';
$available_themes = Theme::listAvailable();
$chosen_theme = $available_themes[(int)$this->arg('dwct','0')];
$this->success = true; $success = $this->scoped->setPref('chosen_theme', 'theme', $chosen_theme);
$this->msg = _m('Settings saved.');
$this->success = $this->scoped->setPref('chosen_theme', 'theme', $chosen_theme);
// TRANS: Confirmation shown when user profile settings are saved. // TRANS: Confirmation shown when user profile settings are saved.
if(!$this->success) $this->msg = _('No valid theme chosen.'); if (!$success) {
$this->msg = 'No valid theme chosen.';
}
$this->showForm(_($this->msg), $this->success); return _m($this->msg);
} }
} }
class ChooseThemeForm extends Form { class ChooseThemeForm extends Form
{
protected $prefs = null; protected $prefs = null;
function __construct($out, $prefs) { public function __construct($out, $prefs)
{
parent::__construct($out); parent::__construct($out);
if ($prefs!=null) { if ($prefs != null) {
$this->prefs = $prefs; $this->prefs = $prefs;
} else { } else {
$prefs = common_config('site','theme'); $this->prefs = common_config('site', 'theme');
} }
}
}
/** /**
* Visible or invisible data elements * Visible or invisible data elements
@ -113,29 +115,30 @@ class ChooseThemeForm extends Form {
* Display the form fields that make up the data of the form. * Display the form fields that make up the data of the form.
* Sub-classes should overload this to show their data. * Sub-classes should overload this to show their data.
* @return void * @return void
* @throws Exception
*/ */
function formData() { public function formData(): void
{
//Get a list of available themes on instance //Get a list of available themes on instance
$available_themes = Theme::listAvailable(); $available_themes = Theme::listAvailable();
//Remove theme 'licenses' from selectable themes. //Remove theme 'licenses' from selectable themes.
//The 'licenses' theme is not an actual theme and //The 'licenses' theme is not an actual theme and
//will just mess-up the gui. //will just mess-up the gui.
$key = array_search('licenses',$available_themes); $key = array_search('licenses', $available_themes);
if($key!=false){ if ($key != false) {
unset($available_themes[$key]); unset($available_themes[$key]);
} }
$this->elementStart('fieldset'); $this->elementStart('fieldset');
$this->elementStart('ul', 'form_data'); $this->elementStart('ul', 'form_data');
$this->elementStart('li'); $this->elementStart('li');
$this->dropdown('dwct',_m('Themes'),$available_themes,_m('Select a theme'),false, $this->prefs); $this->dropdown('dwct', _m('Themes'), $available_themes, _m('Select a theme'), false, $this->prefs);
$this->elementEnd('li'); $this->elementEnd('li');
$this->elementEnd('ul'); $this->elementEnd('ul');
$this->elementEnd('fieldset'); $this->elementEnd('fieldset');
} }
/** /**
@ -146,7 +149,7 @@ class ChooseThemeForm extends Form {
* @return void * @return void
*/ */
function formActions() public function formActions(): void
{ {
$this->submit('submit', _('Save')); $this->submit('submit', _('Save'));
} }
@ -156,10 +159,11 @@ class ChooseThemeForm extends Form {
* *
* Should be unique on the page. Sub-classes should overload this * Should be unique on the page. Sub-classes should overload this
* to show their own IDs. * to show their own IDs.
* @return int ID of the form * @return string ID of the form
*/ */
function id() { public function id(): string
{
return 'form_choosetheme_prefs'; return 'form_choosetheme_prefs';
} }
@ -171,7 +175,8 @@ class ChooseThemeForm extends Form {
* @return string URL to post to * @return string URL to post to
*/ */
function action() { public function action(): string
{
return common_local_url('choosethemesettings'); return common_local_url('choosethemesettings');
} }
@ -181,7 +186,8 @@ class ChooseThemeForm extends Form {
* @return string the form's class * @return string the form's class
*/ */
function formClass() { public function formClass(): string
{
return 'form_settings'; return 'form_settings';
} }
} }