Themes.pm 2.1 KB

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