simple_theme_settings.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // This file is part of The Bootstrap 3 Moodle theme
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // Moodle 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * A wrapper round Moodle's settings API to simplify the common cases
  18. * for themers, often via "convention over configuration" and the reduction
  19. * in repetitive typing
  20. *
  21. * Assumes all strings are in the theme lang file, assumes the title
  22. * langstring is the same as the name, and that the description langstring
  23. * is the same as the title with 'desc' added to the end.
  24. *
  25. * @package theme_bootstrap
  26. * @copyright 2014 Bas Brands, www.basbrands.nl
  27. * @authors Bas Brands, David Scotson
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. defined('MOODLE_INTERNAL') || die();
  31. class simple_theme_settings {
  32. private $themename;
  33. private $settingspage;
  34. public function __construct($settingspage, $themename) {
  35. $this->themename = $themename;
  36. $this->settingspage = $settingspage;
  37. }
  38. private function name_for($setting, $suffix='') {
  39. return $this->themename.'/'.$setting.$suffix;
  40. }
  41. private function title_for($setting, $additional = null) {
  42. return get_string($setting, $this->themename, $additional);
  43. }
  44. private function description_for($setting) {
  45. return get_string($setting.'desc', $this->themename);
  46. }
  47. public function add_checkbox($setting, $default='0', $checked='1', $unchecked='0') {
  48. $checkbox = new admin_setting_configcheckbox(
  49. $this->name_for($setting),
  50. $this->title_for($setting),
  51. $this->description_for($setting),
  52. $default,
  53. $checked,
  54. $unchecked
  55. );
  56. $checkbox->set_updatedcallback('theme_reset_all_caches');
  57. $this->settingspage->add($checkbox);
  58. }
  59. public function add_text($setting, $default='') {
  60. $text = new admin_setting_configtext(
  61. $this->name_for($setting),
  62. $this->title_for($setting),
  63. $this->description_for($setting),
  64. $default
  65. );
  66. $text->set_updatedcallback('theme_reset_all_caches');
  67. $this->settingspage->add($text);
  68. }
  69. public function add_heading($setting) {
  70. $heading = new admin_setting_heading(
  71. $this->name_for($setting),
  72. $this->title_for($setting),
  73. $this->description_for($setting)
  74. );
  75. $this->settingspage->add($heading);
  76. }
  77. public function add_select($setting, $default, $options) {
  78. $select = new admin_setting_configselect(
  79. $this->name_for($setting),
  80. $this->title_for($setting),
  81. $this->description_for($setting),
  82. $default,
  83. $options
  84. );
  85. $select->set_updatedcallback('theme_reset_all_caches');
  86. $this->settingspage->add($select);
  87. }
  88. public function add_textarea($setting, $default='') {
  89. $textarea = new admin_setting_configtextarea(
  90. $this->name_for($setting),
  91. $this->title_for($setting),
  92. $this->description_for($setting),
  93. $default
  94. );
  95. $textarea->set_updatedcallback('theme_reset_all_caches');
  96. $this->settingspage->add($textarea);
  97. }
  98. public function add_htmleditor($setting, $default='') {
  99. $htmleditor = new admin_setting_confightmleditor(
  100. $this->name_for($setting),
  101. $this->title_for($setting),
  102. $this->description_for($setting),
  103. $default
  104. );
  105. $htmleditor->set_updatedcallback('theme_reset_all_caches');
  106. $this->settingspage->add($htmleditor);
  107. }
  108. public function add_colourpicker($setting, $default='#666') {
  109. $colorpicker = new admin_setting_configcolourpicker(
  110. $this->name_for($setting),
  111. $this->title_for($setting),
  112. $this->description_for($setting),
  113. $default,
  114. null // Don't hook up any javascript preview of color change.
  115. );
  116. $colorpicker->set_updatedcallback('theme_reset_all_caches');
  117. $this->settingspage->add($colorpicker);
  118. }
  119. public function add_file($setting) {
  120. $file = new admin_setting_configstoredfile(
  121. $this->name_for($setting),
  122. $this->title_for($setting),
  123. $this->description_for($setting),
  124. $setting // TODO find out what this does,
  125. // for now assume it just needs to be unique.
  126. );
  127. $file->set_updatedcallback('theme_reset_all_caches');
  128. $this->settingspage->add($file);
  129. }
  130. public function add_numbered_textareas($setting, $count, $default='') {
  131. for ($i = 1; $i <= $count; $i++) {
  132. $textarea = new admin_setting_configtextarea(
  133. $this->name_for($setting, $i),
  134. $this->title_for($setting, $i),
  135. $this->description_for($setting),
  136. $default
  137. );
  138. $textarea->set_updatedcallback('theme_reset_all_caches');
  139. $this->settingspage->add($textarea);
  140. }
  141. }
  142. }