summaryrefslogtreecommitdiff
path: root/app/frontend/controls/LanguageSelect.php
blob: 162eb193f9cdcb1027814e2d912efd9484000a75 (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
<?php

Prado::using('Application.web.FacadeTemplateControl');

Prado::using('Application.user.DbUser');
Prado::using('Application.facades.UserFacade');

Prado::using('Application.dto.LanguageDTO');

class LanguageSelect extends FacadeTemplateControl {

    public function getUserToChange() {
        return $this->getControlState('user');
    }

    public function setUserToChange(DbUser $user) {
        if ($user->IsGuest && !$this->Page->IsCallBack) {
            throw new TInvalidDataValueException(
                Prado::localize(
                    'Language preference change impossible for guest user'
                )
            );
        }
        $this->setControlState('user', $user);
    }

    public function onPreRender($param) {
        parent::onPreRender($param);
        $this->Languages->DataSource = $this->_getLanguages();
        $this->Languages->DataValueField = 'Name';
        $this->Languages->DataTextField = 'Label';
        $this->Languages->dataBind();
        $this->Languages->setSelectedValue(
            $this->Application->Globalization->getCulture()
        );
    }

    public function saveLang($sender, $param) {
        $this->getFacade()->setLanguagePreference(
            $this->UserToChange,
            $this->Languages->SelectedValue
        );
        $this->Response->redirect(
            $this->Service->constructUrl(
                $this->Service->getRequestedPage()->PagePath
            )
        );
    }

    private function _getLanguages() {
        $langs = array_map(
            function($lang) {
                return new LanguageDTO($lang);
            },
            $this->Application->Globalization->getAllowedCultures()
        );
        usort($langs, ['LanguageDTO', '__compare']);
        return $langs;
    }

}

?>