Themes.php 3.1 KB

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