Themes.pm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 get_dir {
  15. state $tdir = '';
  16. return $tdir if $tdir;
  17. my $conf = Trog::Config::get();
  18. my $theme = $conf->param('general.theme') || '';
  19. if( $theme ) {
  20. my $themedir = "www/themes/$theme";
  21. $tdir = $themedir if -d $themedir;
  22. }
  23. Trog::Log::INFO("Loading Theme '$theme' from '$tdir'");
  24. return $tdir;
  25. };
  26. sub td {
  27. my $dir = get_dir();
  28. return $dir ? "/$dir" : '';
  29. }
  30. sub template_dir ( $template, $content_type, $is_component = 0, $is_dir = 0 ) {
  31. my $ct = $Trog::Vars::byct{$content_type};
  32. my ( $mtd, $mtemp ) = ( get_dir() . "/templates/$ct", "$template_dir/$ct" );
  33. if ($is_component) {
  34. $mtd .= "/components";
  35. $mtemp .= "/components";
  36. }
  37. if ($is_dir) {
  38. return $mtd && -d "$mtd/$template" ? $mtd : $mtemp;
  39. }
  40. return $mtd && -f "$mtd/$template" ? $mtd : $mtemp;
  41. }
  42. # Pick appropriate dir based on whether theme override exists
  43. sub _dir_for_resource ($resource) {
  44. my $theme_dir = get_dir();
  45. return $theme_dir && -f "$theme_dir/$resource" ? $theme_dir : '';
  46. }
  47. sub themed ($resource) {
  48. return _dir_for_resource("$resource") . "/$resource";
  49. }
  50. # For style we want to load *both* style files and have the override come later.
  51. sub themed_style ($resource) {
  52. my @styles = ("/styles/$resource");
  53. my $styled = _dir_for_resource("styles/$resource");
  54. $styled =~ s/^www\///;
  55. push( @styles, "/$styled/styles/$resource" ) if $styled;
  56. return @styles;
  57. }
  58. sub themed_script ($resource) {
  59. return _dir_for_resource("scripts/$resource") . "/scripts/$resource";
  60. }
  61. sub themed_template ($resource) {
  62. return _dir_for_resource("templates/$resource") . "/templates/$resource";
  63. }
  64. sub templates_in_dir ( $path, $ct, $is_component = 0 ) {
  65. $path = template_dir( $path, $ct, $is_component, 1 ) . "/$path";
  66. my $forms = [];
  67. return $forms unless -d $path;
  68. opendir( my $dh, $path );
  69. while ( my $form = readdir($dh) ) {
  70. push( @$forms, $form ) if -f "$path/$form" && $form =~ m/.*\.tx$/;
  71. }
  72. close($dh);
  73. return $forms;
  74. }
  75. 1;