Themes.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package Trog::Themes;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use FindBin::libs;
  7. use Trog::Vars;
  8. use Trog::Config;
  9. use Trog::Log;
  10. =head1 Trog::Themes
  11. Utility functions for getting themed paths.
  12. =cut
  13. our $template_dir = 'www/templates';
  14. sub routes {
  15. my $rdir = get_dir();
  16. return () unless -f "$rdir/routes.pm";
  17. local $@;
  18. eval { require "$rdir/routes.pm"; 1; } or do {
  19. die "Could not load Theme routing package: $@";
  20. };
  21. return %Theme::routes;
  22. }
  23. sub get_dir {
  24. state $tdir = '';
  25. return $tdir if $tdir;
  26. my $conf = Trog::Config::get();
  27. my $theme = $conf->param('general.theme') || '';
  28. if( $theme ) {
  29. my $themedir = "www/themes/$theme";
  30. $tdir = $themedir if -d $themedir;
  31. }
  32. Trog::Log::INFO("Loading Theme '$theme' from '$tdir'");
  33. return $tdir;
  34. };
  35. sub td {
  36. my $dir = get_dir();
  37. return $dir ? "/$dir" : '';
  38. }
  39. sub template_dir ( $template, $content_type, $is_component = 0, $is_dir = 0 ) {
  40. my $ct = $Trog::Vars::byct{$content_type};
  41. my ( $mtd, $mtemp ) = ( get_dir() . "/templates/$ct", "$template_dir/$ct" );
  42. if ($is_component) {
  43. $mtd .= "/components";
  44. $mtemp .= "/components";
  45. }
  46. if ($is_dir) {
  47. return $mtd && -d "$mtd/$template" ? $mtd : $mtemp;
  48. }
  49. return $mtd && -f "$mtd/$template" ? $mtd : $mtemp;
  50. }
  51. # Pick appropriate dir based on whether theme override exists
  52. sub _dir_for_resource ($resource) {
  53. my $theme_dir = get_dir();
  54. return $theme_dir && -f "$theme_dir/$resource" ? $theme_dir : '';
  55. }
  56. sub themed ($resource) {
  57. return _dir_for_resource("$resource") . "/$resource";
  58. }
  59. # For style we want to load *both* style files and have the override come later.
  60. sub themed_style ($resource) {
  61. my @styles = ("/styles/$resource");
  62. my $styled = _dir_for_resource("styles/$resource");
  63. $styled =~ s/^www\///;
  64. push( @styles, "/$styled/styles/$resource" ) if $styled;
  65. return @styles;
  66. }
  67. sub themed_script ($resource) {
  68. return _dir_for_resource("scripts/$resource") . "/scripts/$resource";
  69. }
  70. sub themed_template ($resource) {
  71. return _dir_for_resource("templates/$resource") . "/templates/$resource";
  72. }
  73. sub templates_in_dir ( $path, $ct, $is_component = 0 ) {
  74. $path = template_dir( $path, $ct, $is_component, 1 ) . "/$path";
  75. my $forms = [];
  76. return $forms unless -d $path;
  77. opendir( my $dh, $path );
  78. while ( my $form = readdir($dh) ) {
  79. push( @$forms, $form ) if -f "$path/$form" && $form =~ m/.*\.tx$/;
  80. }
  81. close($dh);
  82. return $forms;
  83. }
  84. 1;