aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Cobrand.pm
blob: ad21820d757d33fd6ca6e1c4c0ba89b905956551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: evdb@mysociety.org. WWW: http://www.mysociety.org

package FixMyStreet::Cobrand;

use strict;
use warnings;

use FixMyStreet;
use Carp;

use Module::Pluggable
  sub_name    => '_cobrands',
  search_path => ['FixMyStreet::Cobrand'],
  require     => 1;

my @ALL_COBRAND_CLASSES = __PACKAGE__->_cobrands;

=head2 get_allowed_cobrands

Return an array reference of allowed cobrand subdomains

=cut

sub get_allowed_cobrands {
    my $allowed_cobrand_string = FixMyStreet->config('ALLOWED_COBRANDS');
    my @allowed_cobrands = split( /\|/, $allowed_cobrand_string );
    return \@allowed_cobrands;
}

=head2 available_cobrand_classes

    @available_cobrand_classes =
      FixMyStreet::Cobrand->available_cobrand_classes();

Return an array of all the classes that were found and that have monikers that
match the values from get_allowed_cobrands.

=cut

sub available_cobrand_classes {
    my $class = shift;

    my %allowed = map { $_ => 1 } @{ $class->get_allowed_cobrands };
    my @avail = grep { $allowed{ $_->moniker } } @ALL_COBRAND_CLASSES;

    return @avail;
}

=head2 get_class_for_host

    $cobrand_class = FixMyStreet::Cobrand->get_class_for_host( $host );

Given a host determine which cobrand we should be using. 

=cut

sub get_class_for_host {
    my $class = shift;
    my $host  = shift;

    foreach my $avail ( $class->available_cobrand_classes ) {
        my $moniker = $avail->moniker;
        return $avail if $host =~ m{$moniker};
    }

    # if none match then use the default
    return 'FixMyStreet::Cobrand::Default';
}

=head2 get_class_for_moniker

    $cobrand_class = FixMyStreet::Cobrand->get_class_for_moniker( $moniker );

Given a moniker determine which cobrand we should be using. 

=cut

sub get_class_for_moniker {
    my $class   = shift;
    my $moniker = shift;

    foreach my $avail ( $class->available_cobrand_classes ) {
        return $avail if $moniker eq $avail->moniker;
    }

    # Special case for old blank cobrand entries in fixmystreet.com.
    return 'FixMyStreet::Cobrand::FixMyStreet' if $moniker eq '';

    # if none match then use the default
    return 'FixMyStreet::Cobrand::Default';
}

1;