Themes.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 'add-white' => '✚',
  68. 'all' => '☰',
  69. 'bookmark' => '★',
  70. 'bookmark-add' => '✚',
  71. 'category' => '☷',
  72. 'category-white' => '☷',
  73. 'close' => '❌',
  74. 'configure' => '⚙',
  75. 'down' => '▽',
  76. 'favorite' => '★',
  77. 'FreshRSS-logo' => '⊚',
  78. 'help' => 'ⓘ',
  79. 'icon' => '⊚',
  80. 'import' => '⤓',
  81. 'key' => '⚿',
  82. 'label' => '🏷️',
  83. 'link' => '↗',
  84. 'look' => '👁',
  85. 'login' => '🔒',
  86. 'logout' => '🔓',
  87. 'next' => '⏩',
  88. 'non-starred' => '☆',
  89. 'prev' => '⏪',
  90. 'read' => '☑',
  91. 'rss' => '☄',
  92. 'unread' => '☐',
  93. 'refresh' => '🔃', //↻
  94. 'search' => '🔍',
  95. 'share' => '♺',
  96. 'starred' => '★',
  97. 'stats' => '%',
  98. 'tag' => '⚐',
  99. 'up' => '△',
  100. 'view-normal' => '☰',
  101. 'view-global' => '☷',
  102. 'view-reader' => '☕',
  103. );
  104. return isset($name) ? $alts[$name] : '';
  105. }
  106. public static function icon($name, $urlOnly = false, $altOnly = false) {
  107. $alt = self::alt($name);
  108. if ($alt == '') {
  109. return '';
  110. }
  111. $url = $name . '.svg';
  112. $url = isset(self::$themeIcons[$url]) ? (self::$themeIconsUrl . $url) : (self::$defaultIconsUrl . $url);
  113. return $urlOnly ? Minz_Url::display($url) : '<img class="icon" src="' . Minz_Url::display($url) . '" alt="' . $alt . '" />';
  114. }
  115. }