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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

/**
 * Description of TranslatedModelAsController
 * Modified Controller for multilingual URL Syntax
 * @author bschmitt
 */
class TranslatedModelAsController extends ModelAsController {

    private $locale;
    private $page;

    public function init() {

        // Hack: I don't know why Versioned::reading_stage is not set when using this controller - 
        // which causes that all pages got displayed in draft mode before this was added
        if(isset($_REQUEST["stage"])) {
            $_SESSION["currentStage"] = $_REQUEST["stage"];
        } else {
            $_SESSION["currentStage"] = "Live";
            Versioned::reading_stage("Live");
        }

        $this->checkUrl();
    }

    public function setPage($page) {

        $this->page = $page;
    }

    public function setLocale($locale) {

        $this->locale = $this->checkLocale($locale);

        // set locale within translatable
        Translatable::set_current_locale($this->locale);
    }

    public static function getLocale() {

        return self::$locale;
    }

    // selects the language and country from given url segment
    public function checkUrl() {

        // url segment is empty
        if(empty($this->URLParams['URLSegment1']) && empty($this->URLParams['URLSegment2'])) {

            $this->setLocale($this->getProbableLocale());
            $this->setPage("home");
        }

        // locale/ region and page is set
        if(!empty($this->URLParams['URLSegment1']) && !empty($this->URLParams['URLSegment2'])) {

            $this->setLocale($this->URLParams['URLSegment1']);
            $this->setPage($this->URLParams['URLSegment2']);
        }
    }

    // selects valid locale from url string
    public function checkLocale($str) {

        // language and country : en_US
        if(preg_match('/(^[a-z]{2})_([A-Z]{2})$/', $str, $matches)) {

            return $str;
        }

        // language and country : en-us
        if(preg_match('/(^[a-zA-Z]{2})-([a-zA-Z]{2})$/', $str, $matches)) {

            return strtolower($matches[1])."_".strtoupper($matches[2]);
        }

        return $this->getProbableLocale();
    }

    // selects most probale locale for the user
    public function getProbableLocale() {

        // should do ip check etc
        // e.g. http://aktuell.de.selfhtml.org/artikel/php/httpsprache/index.htm

        return Translatable::default_locale();
    }

    // retuns the output controller for the pages
    public function getNestedController() {

        if($this->page) {

            $SQL_URLSegment = Convert::raw2sql($this->page);
            $child = SiteTree::get_by_url($SQL_URLSegment);

            if(!$child) {          

                $child = $this->findOldPage($SQL_URLSegment);

                if($child) {

                    $url = Controller::join_links(
                        Director::baseURL(),
                        $this->locale,
                        $this->page,
                        (isset($this->urlParams['Action'])) ? $this->urlParams['Action'] : null,
                        (isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null,
                        (isset($this->urlParams['OtherID'])) ? $this->urlParams['OtherID'] : null
                    );

                    $response = new HTTPResponse();
                    $response->redirect($url, 301);
                    return $response;

                }

                $child = $this->get404Page();
            }

            if($child) {

                if(isset($_REQUEST['debug'])) Debug::message("Using record #$child->ID of type $child->class with URL {$this->urlParams['URLSegment']}");

                // set language
                if($child->Locale) Translatable::set_current_locale($child->Locale);

                $controllerClass = "{$child->class}_Controller";

                if($this->urlParams['Action'] && ClassInfo::exists($controllerClass.'_'.$this->urlParams['Action'])) {
                    $controllerClass = $controllerClass.'_'.$this->urlParams['Action'];
                }

                if(ClassInfo::exists($controllerClass)) {
                    $controller = new $controllerClass($child);
                } else {
                    $controller = $child;
                }

                // set locale to page controller
                $controller->setLocale($this->locale);

                return $controller;

            } else {
                return new HTTPResponse("The requested page couldn't be found.",404);
            }

        }

        else {
            user_error("ModelAsController not geting a URLSegment.  It looks like the site isn't redirecting to home", E_USER_ERROR);
        }
    }

}
?>