overwritethemebackgroundadminpanel.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * UI to overwrite his GNU social instance's background
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Apply/Store the custom background preferences
  28. *
  29. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class OverwriteThemeBackgroundAdminPanelAction extends AdminPanelAction
  33. {
  34. /**
  35. * Title of the page
  36. *
  37. * @return string Title of the page
  38. */
  39. public function title(): string
  40. {
  41. return _m('Overwrite Theme Background');
  42. }
  43. /**
  44. * Instructions for use
  45. *
  46. * @return string instructions for use
  47. */
  48. public function getInstructions(): string
  49. {
  50. return _m('Customize your theme\'s background easily');
  51. }
  52. /**
  53. * Show the site admin panel form
  54. *
  55. * @return void
  56. */
  57. public function showForm()
  58. {
  59. $form = new OverwriteThemeBackgroundAdminPanelForm($this);
  60. $form->show();
  61. return;
  62. }
  63. /**
  64. * Save settings from the form
  65. *
  66. * @return void
  67. */
  68. public function saveSettings()
  69. {
  70. static $settings = [
  71. 'overwritethemebackground' => [
  72. 'background-color',
  73. 'background-image',
  74. 'sslbackground-image',
  75. 'background-repeat',
  76. 'background-attachment',
  77. 'background-position'
  78. ]
  79. ];
  80. $values = [];
  81. foreach ($settings as $section => $parts) {
  82. foreach ($parts as $setting) {
  83. $values[$section][$setting] = $this->trimmed($setting);
  84. }
  85. }
  86. // This throws an exception on validation errors
  87. $this->validate($values);
  88. // assert(all values are valid);
  89. $config = new Config();
  90. $config->query('START TRANSACTION');
  91. foreach ($settings as $section => $parts) {
  92. foreach ($parts as $setting) {
  93. Config::save($section, $setting, $values[$section][$setting]);
  94. }
  95. }
  96. $config->query('COMMIT');
  97. return;
  98. }
  99. /**
  100. * Validate form values
  101. *
  102. * @param $values
  103. * @throws ClientException
  104. */
  105. public function validate(&$values)
  106. {
  107. // Validate background
  108. if (!empty($values['overwritethemebackground']['background-image']) &&
  109. !common_valid_http_url($values['overwritethemebackground']['background-image'])) {
  110. // TRANS: Client error displayed when a background URL is not valid.
  111. $this->clientError(_m('Invalid background URL.'));
  112. }
  113. if (!empty($values['overwritethemebackground']['sslbackground-image']) &&
  114. !common_valid_http_url($values['overwritethemebackground']['sslbackground-image'], true)) {
  115. // TRANS: Client error displayed when a SSL background URL is invalid.
  116. $this->clientError(_m('Invalid SSL background URL.'));
  117. }
  118. }
  119. }
  120. /**
  121. * Friendly UI for setting the custom background preferences
  122. *
  123. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  124. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  125. */
  126. class OverwriteThemeBackgroundAdminPanelForm extends AdminForm
  127. {
  128. /**
  129. * ID of the form
  130. *
  131. * @return int ID of the form
  132. */
  133. public function id()
  134. {
  135. return 'form_site_admin_panel';
  136. }
  137. /**
  138. * class of the form
  139. *
  140. * @return string class of the form
  141. */
  142. public function formClass()
  143. {
  144. return 'form_settings';
  145. }
  146. /**
  147. * Action of the form
  148. *
  149. * @return string URL of the action
  150. */
  151. public function action()
  152. {
  153. return common_local_url('overwritethemebackgroundAdminPanel');
  154. }
  155. /**
  156. * Data elements of the form
  157. *
  158. * @return void
  159. */
  160. public function formData()
  161. {
  162. $this->out->elementStart('fieldset', ['id' => 'settings_site_background']);
  163. // TRANS: Fieldset legend for form to change background.
  164. $this->out->element('legend', null, _m('Background'));
  165. $this->out->elementStart('ul', 'form_data');
  166. /* Background colour */
  167. $this->li();
  168. $this->input(
  169. 'background-color',
  170. // TRANS: Field label for GNU social site background.
  171. _m('Site background color'),
  172. // TRANS: Title for field label for GNU social site background.
  173. 'Background color for the site (hexadecimal with #).',
  174. 'overwritethemebackground'
  175. );
  176. $this->unli();
  177. /* Background image */
  178. $this->li();
  179. $this->input(
  180. 'background-image',
  181. // TRANS: Field label for GNU social site background.
  182. _m('Site background'),
  183. // TRANS: Title for field label for GNU social site background.
  184. 'Background for the site (full URL).',
  185. 'overwritethemebackground'
  186. );
  187. $this->unli();
  188. $this->li();
  189. $this->input(
  190. 'sslbackground-image',
  191. // TRANS: Field label for SSL GNU social site background.
  192. _m('SSL background'),
  193. // TRANS: Title for field label for SSL GNU social site background.
  194. 'Background to show on SSL pages (full URL).',
  195. 'overwritethemebackground'
  196. );
  197. $this->unli();
  198. /* Background repeat */
  199. $this->li();
  200. // TRANS: Dropdown label on site settings panel.
  201. $this->out->dropdown(
  202. 'background-repeat',
  203. _m('Background repeat'),
  204. // TRANS: Dropdown title on site settings panel.
  205. ['Repeat horizontally and vertically', 'Repeat Horizontally', 'Repeat Vertically', 'Don\'t repeat'],
  206. _m('repeat horizontally and/or vertically'),
  207. false,
  208. common_config('overwritethemebackground', 'background-repeat') ?? 'repeat'
  209. );
  210. $this->unli();
  211. /* Background attachment */
  212. $this->li();
  213. // TRANS: Dropdown label on site settings panel.
  214. $this->out->dropdown(
  215. 'background-attachment',
  216. _m('Background attachment'),
  217. // TRANS: Dropdown title on site settings panel.
  218. ['Scroll with page', 'Stay fixed'],
  219. _m('Whether the background image should scroll or be fixed (will not scroll with the rest of the page)'),
  220. false,
  221. common_config('overwritethemebackground', 'background-attachment') ?? 'scroll'
  222. );
  223. $this->unli();
  224. /* Background position */
  225. $background_position_options = [
  226. 'initial',
  227. 'left top',
  228. 'left center',
  229. 'left bottom',
  230. 'right top',
  231. 'right center',
  232. 'right bottom',
  233. 'center top',
  234. 'center center',
  235. 'center bottom'
  236. ];
  237. $this->li();
  238. // TRANS: Dropdown label on site settings panel.
  239. $this->out->dropdown(
  240. 'background-position',
  241. _m('Background position'),
  242. // TRANS: Dropdown title on site settings panel.
  243. $background_position_options,
  244. _m('Sets the starting position of a background image'),
  245. false,
  246. common_config('overwritethemebackground', 'background-attachment') ?? 'initial'
  247. );
  248. $this->unli();
  249. $this->out->elementEnd('ul');
  250. $this->out->elementEnd('fieldset');
  251. }
  252. /**
  253. * Action elements
  254. *
  255. * @return void
  256. */
  257. public function formActions()
  258. {
  259. $this->out->submit(
  260. 'submit',
  261. // TRANS: Button text for saving site settings.
  262. _m('BUTTON', 'Save'),
  263. 'submit',
  264. null,
  265. // TRANS: Button title for saving site settings.
  266. _m('Save the site settings.')
  267. );
  268. }
  269. }