choosethemesettings.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * ChooseTheme - GNU social plugin enabling user to select preferred theme
  4. * Copyright (C) 2015, kollektivet0x242.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @author Knut Erik Hollund <knut.erik@unlike.no>
  20. * @copyright 2015 kollektivet0x242. http://www.kollektivet0x242.no
  21. *
  22. * @license GNU Affero General Public License http://www.gnu.org/licenses/
  23. */
  24. defined('GNUSOCIAL') || die();
  25. class ChooseThemeSettingsAction extends SettingsAction
  26. {
  27. /**
  28. * Title of the page
  29. * @return string Page title
  30. * @throws Exception
  31. */
  32. public function title(): string
  33. {
  34. // TRANS: Page title.
  35. return _m('Choose theme settings');
  36. }
  37. /**
  38. * Instructions for use
  39. * @return string Instructions for use
  40. * @throws Exception
  41. */
  42. public function getInstructions(): string
  43. {
  44. // TRANS: Page instructions.
  45. return _m('Choose theme');
  46. }
  47. /**
  48. * Show the form for ChooseTheme
  49. * @return void
  50. */
  51. public function showContent(): void
  52. {
  53. $site_theme = common_config('site', 'theme');
  54. $prefs = $this->scoped->getPref('chosen_theme', 'theme', $site_theme);
  55. if ($prefs === null) {
  56. common_debug('No chosen theme found in database for user.');
  57. }
  58. //Get a list of available themes on instance
  59. $available_themes = Theme::listAvailable();
  60. $chosenone = array_search($prefs, $available_themes, true);
  61. $form = new ChooseThemeForm($this, $chosenone);
  62. $form->show();
  63. }
  64. /**
  65. * Handler method
  66. *
  67. * @return string
  68. * @throws Exception
  69. */
  70. public function doPost(): string
  71. {
  72. //Get a list of available themes on instance
  73. $available_themes = Theme::listAvailable();
  74. $chosen_theme = $available_themes[(int)$this->arg('dwct', '0')];
  75. $this->msg = 'Settings saved.';
  76. $success = $this->scoped->setPref('chosen_theme', 'theme', $chosen_theme);
  77. // TRANS: Confirmation shown when user profile settings are saved.
  78. if (!$success) {
  79. $this->msg = 'No valid theme chosen.';
  80. }
  81. return _m($this->msg);
  82. }
  83. }
  84. class ChooseThemeForm extends Form
  85. {
  86. protected $prefs = null;
  87. public function __construct($out, $prefs)
  88. {
  89. parent::__construct($out);
  90. if ($prefs != null) {
  91. $this->prefs = $prefs;
  92. } else {
  93. $this->prefs = common_config('site', 'theme');
  94. }
  95. }
  96. /**
  97. * Visible or invisible data elements
  98. *
  99. * Display the form fields that make up the data of the form.
  100. * Sub-classes should overload this to show their data.
  101. * @return void
  102. * @throws Exception
  103. */
  104. public function formData(): void
  105. {
  106. //Get a list of available themes on instance
  107. $available_themes = Theme::listAvailable();
  108. //Remove theme 'licenses' from selectable themes.
  109. //The 'licenses' theme is not an actual theme and
  110. //will just mess-up the gui.
  111. $key = array_search('licenses', $available_themes);
  112. if ($key != false) {
  113. unset($available_themes[$key]);
  114. }
  115. $this->elementStart('fieldset');
  116. $this->elementStart('ul', 'form_data');
  117. $this->elementStart('li');
  118. $this->dropdown('dwct', _m('Themes'), $available_themes, _m('Select a theme'), false, $this->prefs);
  119. $this->elementEnd('li');
  120. $this->elementEnd('ul');
  121. $this->elementEnd('fieldset');
  122. }
  123. /**
  124. * Buttons for form actions
  125. *
  126. * Submit and cancel buttons (or whatever)
  127. * Sub-classes should overload this to show their own buttons.
  128. * @return void
  129. */
  130. public function formActions(): void
  131. {
  132. $this->submit('submit', _('Save'));
  133. }
  134. /**
  135. * ID of the form
  136. *
  137. * Should be unique on the page. Sub-classes should overload this
  138. * to show their own IDs.
  139. * @return string ID of the form
  140. */
  141. public function id(): string
  142. {
  143. return 'form_choosetheme_prefs';
  144. }
  145. /**
  146. * Action of the form.
  147. *
  148. * URL to post to. Should be overloaded by subclasses to give
  149. * somewhere to post to.
  150. * @return string URL to post to
  151. */
  152. public function action(): string
  153. {
  154. return common_local_url('choosethemesettings');
  155. }
  156. /**
  157. * Class of the form. May include space-separated list of multiple classes.
  158. *
  159. * @return string the form's class
  160. */
  161. public function formClass(): string
  162. {
  163. return 'form_settings';
  164. }
  165. }