Themes.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. class FreshRSS_Themes extends Minz_Model {
  3. private static $themesUrl = '/themes/';
  4. private static $defaultIconsUrl = '/themes/icons/';
  5. public static $defaultTheme = 'Origine';
  6. public static function getList() {
  7. return array_values(array_diff(
  8. scandir(PUBLIC_PATH . self::$themesUrl),
  9. array('..', '.')
  10. ));
  11. }
  12. public static function get() {
  13. $themes_list = self::getList();
  14. $list = array();
  15. foreach ($themes_list as $theme_dir) {
  16. $theme = self::get_infos($theme_dir);
  17. if ($theme) {
  18. $list[$theme_dir] = $theme;
  19. }
  20. }
  21. return $list;
  22. }
  23. public static function get_infos($theme_id) {
  24. $theme_dir = PUBLIC_PATH . self::$themesUrl . $theme_id;
  25. if (is_dir($theme_dir)) {
  26. $json_filename = $theme_dir . '/metadata.json';
  27. if (file_exists($json_filename)) {
  28. $content = file_get_contents($json_filename);
  29. $res = json_decode($content, true);
  30. if ($res &&
  31. !empty($res['name']) &&
  32. isset($res['files']) &&
  33. is_array($res['files'])) {
  34. $res['id'] = $theme_id;
  35. return $res;
  36. }
  37. }
  38. }
  39. return false;
  40. }
  41. private static $themeIconsUrl;
  42. private static $themeIcons;
  43. public static function load($theme_id) {
  44. $infos = self::get_infos($theme_id);
  45. if (!$infos) {
  46. if ($theme_id !== self::$defaultTheme) { //Fall-back to default theme
  47. return self::load(self::$defaultTheme);
  48. }
  49. $themes_list = self::getList();
  50. if (!empty($themes_list)) {
  51. if ($theme_id !== $themes_list[0]) { //Fall-back to first theme
  52. return self::load($themes_list[0]);
  53. }
  54. }
  55. return false;
  56. }
  57. self::$themeIconsUrl = self::$themesUrl . $theme_id . '/icons/';
  58. self::$themeIcons = is_dir(PUBLIC_PATH . self::$themeIconsUrl) ? array_fill_keys(array_diff(
  59. scandir(PUBLIC_PATH . self::$themeIconsUrl),
  60. array('..', '.')
  61. ), 1) : array();
  62. return $infos;
  63. }
  64. public static function alt($name) {
  65. static $alts = array(
  66. 'add' => '➕', //✚
  67. 'all' => '☰',
  68. 'bookmark' => '✨', //★
  69. 'bookmark-add' => '➕', //✚
  70. 'bookmark-tag' => '📑',
  71. 'category' => '🗂️', //☷
  72. 'close' => '❌',
  73. 'configure' => '⚙️',
  74. 'debug' => '🐛',
  75. 'down' => '🔽', //▽
  76. 'error' => '❌',
  77. 'favorite' => '⭐', //★
  78. 'FreshRSS-logo' => '⊚',
  79. 'help' => 'ℹ️', //ⓘ
  80. 'icon' => '⊚',
  81. 'key' => '🔑', //⚿
  82. 'label' => '🏷️',
  83. 'link' => '↗️', //↗
  84. 'look' => '👀', //👁
  85. 'login' => '🔒',
  86. 'logout' => '🔓',
  87. 'next' => '⏩',
  88. 'non-starred' => '☆',
  89. 'notice' => 'ℹ️', //ⓘ
  90. 'prev' => '⏪',
  91. 'read' => '☑️', //☑
  92. 'rss' => '📣', //☄
  93. 'unread' => '🔲', //☐
  94. 'refresh' => '🔃', //↻
  95. 'search' => '🔍',
  96. 'share' => '♻️', //♺
  97. 'sort-down' => '⬇️', //↓
  98. 'sort-up' => '⬆️', //↑
  99. 'starred' => '⭐', //★
  100. 'stats' => '📈', //%
  101. 'tag' => '🔖', //⚐
  102. 'up' => '🔼', //△
  103. 'view-normal' => '📰', //☰
  104. 'view-global' => '📖', //☷
  105. 'view-reader' => '📜',
  106. 'warning' => '⚠️', //△
  107. );
  108. return isset($name) ? $alts[$name] : '';
  109. }
  110. public static function icon($name, $urlOnly = false) {
  111. $alt = self::alt($name);
  112. if ($alt == '') {
  113. return '';
  114. }
  115. $url = $name . '.svg';
  116. $url = isset(self::$themeIcons[$url]) ? (self::$themeIconsUrl . $url) : (self::$defaultIconsUrl . $url);
  117. if ($urlOnly) {
  118. return Minz_Url::display($url);
  119. }
  120. if (FreshRSS_Context::$user_conf && FreshRSS_Context::$user_conf->icons_as_emojis) {
  121. return '<span class="icon">' . $alt . '</span>';
  122. }
  123. return '<img class="icon" src="' . Minz_Url::display($url) . '" loading="lazy" alt="' . $alt . '" />';
  124. }
  125. }