summaryrefslogtreecommitdiff
path: root/app/frontend/controls/TimezoneSelect.php
blob: 5c198a400c6f4533db148f98ba212965323531f2 (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
<?php

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

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

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

class TimezoneSelect 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(
                    'Timezone preference change impossible for guest user'
                )
            );
        }
        $this->setControlState('user', $user);
    }

    public function onPreRender($param) {
        parent::onPreRender($param);
        $this->Timezones->DataSource = $this->_getTimezones();
        $this->Timezones->DataValueField = 'Name';
        $this->Timezones->DataTextField = 'Label';
        $this->Timezones->dataBind();
        $this->Timezones->setSelectedValue(
            $this->getFacade()->getTimezonePreference($this->UserToChange)->Name
        );
    }

    public function saveTimezone($sender, $param) {
        $this->getFacade()->setTimezonePreference(
            $this->UserToChange,
            $this->Timezones->SelectedValue
        );
    }

    private function _getTimezones() {
        $timezones = array_map(
            function(string $tz) {
                return new TimezoneDTO($tz);
            },
            DateTimeZone::listIdentifiers()
        );
        usort($timezones, ['TimezoneDTO', '__compare']);
        return $timezones;
    }

}

?>