Themes.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 && isset($res['files']) && is_array($res['files'])) {
  31. $res['id'] = $theme_id;
  32. return $res;
  33. }
  34. }
  35. }
  36. return false;
  37. }
  38. private static $themeIconsUrl;
  39. private static $themeIcons;
  40. public static function load($theme_id) {
  41. $infos = self::get_infos($theme_id);
  42. if (!$infos) {
  43. if ($theme_id !== self::$defaultTheme) { //Fall-back to default theme
  44. return self::load(self::$defaultTheme);
  45. }
  46. $themes_list = self::getList();
  47. if (!empty($themes_list)) {
  48. if ($theme_id !== $themes_list[0]) { //Fall-back to first theme
  49. return self::load($themes_list[0]);
  50. }
  51. }
  52. return false;
  53. }
  54. self::$themeIconsUrl = self::$themesUrl . $theme_id . '/icons/';
  55. self::$themeIcons = is_dir(PUBLIC_PATH . self::$themeIconsUrl) ? array_fill_keys(array_diff(
  56. scandir(PUBLIC_PATH . self::$themeIconsUrl),
  57. array('..', '.')
  58. ), 1) : array();
  59. return $infos;
  60. }
  61. public static function icon($name, $urlOnly = false) {
  62. static $alts = array(
  63. 'add' => '✚',
  64. 'all' => '☰',
  65. 'bookmark' => '★',
  66. 'category' => '☷',
  67. 'category-white' => '☷',
  68. 'close' => '❌',
  69. 'configure' => '⚙',
  70. 'down' => '▽',
  71. 'favorite' => '★',
  72. 'help' => 'ⓘ',
  73. 'key' => '⚿',
  74. 'link' => '↗',
  75. 'login' => '🔒',
  76. 'logout' => '🔓',
  77. 'next' => '⏩',
  78. 'non-starred' => '☆',
  79. 'prev' => '⏪',
  80. 'read' => '☑',
  81. 'rss' => '0',
  82. 'unread' => '☐',
  83. 'refresh' => '🔃', //↻
  84. 'search' => '🔍',
  85. 'share' => '♺',
  86. 'starred' => '★',
  87. 'tag' => '⚐',
  88. 'up' => '△',
  89. );
  90. if (!isset($alts[$name])) {
  91. return '';
  92. }
  93. $url = $name . '.svg';
  94. $url = isset(self::$themeIcons[$url]) ? (self::$themeIconsUrl . $url) :
  95. (self::$defaultIconsUrl . $url);
  96. return $urlOnly ? Minz_Url::display($url) :
  97. '<img class="icon" src="' . Minz_Url::display($url) . '" alt="' . $alts[$name] . '" />';
  98. }
  99. }