Themes.pm 2.2 KB

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