1 | <?php |
||
2 | |||
3 | namespace MySociety\TheyWorkForYou; |
||
4 | |||
5 | /** |
||
6 | * Template Renderer |
||
7 | * |
||
8 | * Prepares variables for inclusion in a template, as well as handling variables |
||
9 | * for use in header and footer. |
||
10 | */ |
||
11 | |||
12 | class Renderer { |
||
13 | /** |
||
14 | * Output Page |
||
15 | * |
||
16 | * Assembles a completed page from template and sends it to output. |
||
17 | * |
||
18 | * @param string $template The name of the template file to load. |
||
19 | * @param array $data An associative array of data to be made available to the template. |
||
20 | */ |
||
21 | |||
22 | public static function output($template, $data = [], $template_only = false) { |
||
23 | |||
24 | global $page_errors; |
||
25 | //////////////////////////////////////////////////////////// |
||
26 | // Find the user's country. Used by header, so a safe bit to do regardless. |
||
27 | if (preg_match('#^[A-Z]{2}$#i', get_http_var('country'))) { |
||
28 | $data['country'] = strtoupper(get_http_var('country')); |
||
29 | } else { |
||
30 | $data['country'] = Utility\Gaze::getCountryByIp($_SERVER["REMOTE_ADDR"]); |
||
31 | } |
||
32 | |||
33 | //////////////////////////////////////////////////////////// |
||
34 | // Get the page data |
||
35 | global $DATA, $this_page, $THEUSER; |
||
36 | |||
37 | $header = new Renderer\Header(); |
||
38 | $data = array_merge($header->data, $data); |
||
39 | |||
40 | $user = new Renderer\User(); |
||
41 | $data = array_merge($user->data, $data); |
||
42 | |||
43 | if (isset($page_errors)) { |
||
44 | $data['page_errors'] = $page_errors; |
||
45 | } |
||
46 | |||
47 | //////////////////////////////////////////////////////////// |
||
48 | // Search URL |
||
49 | |||
50 | $SEARCH = new Url('search'); |
||
51 | $SEARCH->reset(); |
||
52 | $data['search_url'] = $SEARCH->generate(); |
||
53 | |||
54 | //////////////////////////////////////////////////////////// |
||
55 | // Search URL |
||
56 | // Footer Links |
||
57 | |||
58 | $footer = new Renderer\Footer(); |
||
59 | $data['footer_links'] = $footer->data; |
||
60 | |||
61 | # banner text |
||
62 | $announcement_manager = new Model\AnnouncementManagement(); |
||
63 | $data['random_banner'] = $announcement_manager->get_random_valid_banner(); |
||
64 | $data = self::addCommonURLs($data); |
||
65 | |||
66 | //////////////////////////////////////////////////////////// |
||
67 | // Unpack the data we've been passed so it's available for use in the templates. |
||
68 | |||
69 | extract($data); |
||
70 | |||
71 | //////////////////////////////////////////////////////////// |
||
72 | // Require the templates and output |
||
73 | |||
74 | if ($template_only) { |
||
75 | require_once INCLUDESPATH . 'easyparliament/templates/html/' . $template . '.php'; |
||
76 | } else { |
||
77 | header('Content-Type: text/html; charset=utf-8'); |
||
78 | require_once INCLUDESPATH . 'easyparliament/templates/html/header.php'; |
||
79 | require_once INCLUDESPATH . 'easyparliament/templates/html/' . $template . '.php'; |
||
80 | require_once INCLUDESPATH . 'easyparliament/templates/html/footer.php'; |
||
81 | exit; |
||
0 ignored issues
–
show
|
|||
82 | } |
||
83 | } |
||
84 | |||
85 | private static function addCommonURLs($data) { |
||
86 | $urls = []; |
||
87 | if (isset($data['urls'])) { |
||
88 | $urls = $data['urls']; |
||
89 | } |
||
90 | |||
91 | $common_urls = ['search', 'alert']; |
||
92 | |||
93 | foreach ($common_urls as $path) { |
||
94 | if (!isset($urls[$path])) { |
||
95 | $url = new Url($path); |
||
96 | $urls[$path] = $url->generate(); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | $data['urls'] = $urls; |
||
101 | return $data; |
||
102 | } |
||
103 | |||
104 | } |
||
105 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.