lib.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * Renderers to align Moodle's HTML with that expected by Bootstrap
  18. *
  19. * @package theme_bootstrap
  20. * @copyright 2014 Bas Brands, www.basbrands.nl
  21. * @authors Bas Brands, David Scotson
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Parses CSS before it is cached.
  27. *
  28. * This function can make alterations and replace patterns within the CSS.
  29. *
  30. * @param string $css The CSS
  31. * @param theme_config $theme The theme config object.
  32. * @return string The parsed CSS The parsed CSS.
  33. */
  34. function theme_bootstrap_process_css($css, $theme) {
  35. $settings = get_object_vars($theme->settings);
  36. $css = theme_bootstrap_delete_css($settings, $css);
  37. $settings['brandcss'] = theme_bootstrap_brand_font_css($settings);
  38. return theme_bootstrap_replace_settings($settings, $css);
  39. }
  40. function theme_bootstrap_delete_css($settings, $css) {
  41. if ($settings['deletecss'] == true) {
  42. $find[] = '/-webkit-border-radius:[^;]*;/';
  43. $find[] = '/-webkit-box-shadow:[^;]*;/';
  44. $find[] = '/-moz-border-radius:[^;]*;/';
  45. $find[] = '/-moz-box-shadow:[^;]*;/';
  46. return preg_replace($find, '', $css);
  47. } else {
  48. return $css;
  49. }
  50. }
  51. /**
  52. * For each setting called e.g. "customcss" this looks for the string
  53. * "[[setting:customcss]]" in the CSS and replaces it with
  54. * the value held in the $settings array for the key
  55. * "customcss".
  56. *
  57. * @param array $settings containing setting names and values
  58. * @param string $css The CSS
  59. * @return string The CSS with replacements made
  60. */
  61. function theme_bootstrap_replace_settings($settings, $css) {
  62. foreach ($settings as $name => $value) {
  63. $find[] = "[[setting:$name]]";
  64. $replace[] = $value;
  65. }
  66. return str_replace($find, $replace, $css);
  67. }
  68. function theme_bootstrap_brand_font_css($settings) {
  69. $fontname = $settings['brandfont'];
  70. if ($fontname === '') {
  71. return '';
  72. }
  73. $fontweight = $settings['brandfontweight'];
  74. return ".navbar-default .navbar-brand,
  75. .navbar-inverse .navbar-brand {
  76. font-family: $fontname, serif;
  77. font-weight: $fontweight;
  78. }";
  79. }
  80. /**
  81. * This function creates the dynamic HTML needed for the
  82. * layout and then passes it back in an object so it can
  83. * be echo'd to the page.
  84. *
  85. * This keeps the logic out of the layout files.
  86. */
  87. function theme_bootstrap_html_for_settings($PAGE) {
  88. $settings = $PAGE->theme->settings;
  89. $html = new stdClass;
  90. if ($settings->inversenavbar == true) {
  91. $html->navbarclass = 'navbar navbar-inverse';
  92. } else {
  93. $html->navbarclass = 'navbar navbar-default';
  94. }
  95. $fluid = (!empty($PAGE->layout_options['fluid']));
  96. if ($fluid || $settings->fluidwidth == true) {
  97. $html->containerclass = 'container-fluid';
  98. } else {
  99. $html->containerclass = 'container';
  100. }
  101. $html->brandfontlink = theme_bootstrap_brand_font_link($settings);
  102. return $html;
  103. }
  104. function theme_bootstrap_brand_font_link($settings) {
  105. global $SITE;
  106. $fontname = $settings->brandfont;
  107. if ($fontname === '') {
  108. return '';
  109. }
  110. $fontname = urlencode($fontname);
  111. $text = urlencode(str_replace(' ', '', $SITE->shortname));
  112. $fontweight = $settings->brandfontweight;
  113. $fontitalic = '';
  114. if ($settings->brandfontitalic == true) {
  115. $fontitalic = 'italic';
  116. }
  117. return '<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family='
  118. .$fontname.':'.$fontweight.$fontitalic.'&amp;text='.$text.'">';
  119. }
  120. function bootstrap_grid($hassidepre, $hassidepost) {
  121. if ($hassidepre && $hassidepost) {
  122. $regions = array('content' => 'col-sm-4 col-sm-push-4 col-md-6 col-md-push-3');
  123. $regions['pre'] = 'col-sm-4 col-sm-pull-4 col-md-3 col-md-pull-6';
  124. $regions['post'] = 'col-sm-4 col-md-3';
  125. } else if ($hassidepre && !$hassidepost) {
  126. $regions = array('content' => 'col-sm-8 col-sm-push-4 col-md-9 col-md-push-3');
  127. $regions['pre'] = 'col-sm-4 col-sm-pull-8 col-md-3 col-md-pull-9';
  128. $regions['post'] = 'emtpy';
  129. } else if (!$hassidepre && $hassidepost) {
  130. $regions = array('content' => 'col-sm-8 col-md-9');
  131. $regions['pre'] = 'empty';
  132. $regions['post'] = 'col-sm-4 col-md-3';
  133. } else if (!$hassidepre && !$hassidepost) {
  134. $regions = array('content' => 'col-md-12');
  135. $regions['pre'] = 'empty';
  136. $regions['post'] = 'empty';
  137. }
  138. return $regions;
  139. }