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
<?php

/**
 * Nested Controller for Translatable Objects
 * @author bschmitt
 */

// URL Rule for translated websites
Director::addRules(2, array(
    '$URLLocale/$URLSegment/$Action/$ID/$OtherID' => 'TranslatedNestedController'
));


class TranslatedNestedController extends ModelAsController {

    public $locale;
    public $defaultLocale = "en_GB";

    public function init() {

        // Debug::dump($this->URLParams);
        // Debug::dump($_GET);

        if(!Translatable::is_enabled()) FormResponse::add('statusMessage("'._t('TranslatedModelAsController','Please enable Translatable').'","bad");');

        $this->setLocale($this->request->param('URLLocale'));

        parent::init();
    }

    public function setLocale($locale) {

        Translatable::set_current_locale($locale);
        $this->locale = $locale;
    }

    // converts 3-letter region code to locale
    public function convertRegionToLocale($region) {

        return (key_exists($region, $this->region_locale_mapping)) ? $this->region_locale_mapping[$region] : $this->defaultLocale;
    }

    /**
     * @return ContentController
     */
    public function getNestedController() {
        $request = $this->request;

        $URLSegment = ($request->param('URLSegment')) ? $request->param('URLSegment') : "home";

        if(!$URLSegment) {
            throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
        }

        // Find page by link, regardless of current locale settings

        //-- Translatable::disable_locale_filter();

        $sitetree = DataObject::get_one(
                'SiteTree',
                sprintf(
                '"URLSegment" = \'%s\' %s',
                Convert::raw2sql($URLSegment),
                (SiteTree::nested_urls() ? 'AND "ParentID" = 0' : null)

                )
        );

        //-- Translatable::enable_locale_filter(); // option 1
        //-- $sitetree = $sitetree->getTranslation($this->Locale); // option 2

        if(!$sitetree) {
            // If a root page has been renamed, redirect to the new location.
            // See ContentController->handleRequest() for similiar logic.
            $redirect = self::find_old_page($URLSegment);
            if($redirect = self::find_old_page($URLSegment)) {
                $params = $request->getVars();
                if(isset($params['url'])) unset($params['url']);
                $this->response = new SS_HTTPResponse();
                $this->response->redirect(
                        Controller::join_links(
                        $redirect->Link(
                        Controller::join_links(
                        $request->param('Action'),
                        $request->param('ID'),
                        $request->param('OtherID')
                        )
                        ),
                        // Needs to be in separate join links to avoid urlencoding
                        ($params) ? '?' . http_build_query($params) : null
                        ),
                        301
                );

                return $this->response;
            }

            if($response = ErrorPage::response_for(404)) {
                return $response;
            } else {
                $this->httpError(404, 'The requested page could not be found.');
            }
        }

        // Enforce current locale setting to the loaded SiteTree object
        if($sitetree->Locale) Translatable::set_current_locale($sitetree->Locale);

        if(isset($_REQUEST['debug'])) {
            Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}");
        }

        return self::controller_for($sitetree, $this->request->param('Action'));
    }


}
?>