Touren application
[infodrom.org/touren.infodrom.org] / controller / indexcontroller.class.php
1 <?php
2
3 class IndexController extends ControllerBase implements ControllerInterface
4 {
5     public function allowUnauthenticated()
6     {
7         return ['login'];
8     }
9
10     public function getNavigation()
11     {
12         if (empty($_SESSION['userid']))
13             return [];
14
15         $list = [];
16         $list[] = ['url' => $this->app->getBaseURL(),
17                    'title' => 'Home'];
18         $list[] = ['url' => $this->app->getBaseURL() . 'index/settings',
19                    'title' => 'Einstellungen'];
20         if ($this->app->isAdmin()) {
21             $list[] = ['url' => $this->app->getBaseURL() . 'tour/newmember',
22                        'title' => 'Neuer Biker'];
23             $list[] = ['url' => $this->app->getBaseURL() . 'tour/new',
24                        'title' => 'Neue Tour'];
25         }
26         $list[] = ['url' => $this->app->getBaseURL() . 'account/logout',
27                    'title' => 'Logout'];
28
29         return $list;
30     }
31
32     public function indexAction($request, $response)
33     {
34         $base = $this->app->getBaseURL();
35         $sql = <<<EOS
36             SELECT tour.name, start_date,
37               tour_status.key = 'plan' AS plan,
38               tour_status.key = 'cancel' AS cancel,
39               '{$base}' || 'tour/' || tour.key || '/index' AS url
40             FROM tour
41             JOIN tour_member ON tour_id = tour.id
42             JOIN tour_status ON tour_status_id = tour_status.id
43             LEFT JOIN tour_date ON tour_date_id = tour_date.id
44             WHERE member_id = {$_SESSION['userid']}
45             ORDER BY year DESC, start_date DESC, plan DESC
46 EOS;
47         $list = $this->db->fetchObjectList($sql);
48
49         $response->setData(Template::render('page/list', ['list' => $list]));
50     }
51
52     public function loginAction($request, $response)
53     {
54         $response->setData(Template::render('page/login',
55                                             [
56                                              'action' =>  $this->app->getBaseURL().'account/login'
57                                              ]));
58     }
59
60     public function settingsAction($request, $response)
61     {
62         $user = new Sys_User($_SESSION['userid']);
63
64         $form = new Form('settings');
65         $form->setTitle('Einstellungen');
66         $form->add(new FormElement('text', ['name' => 'email',
67                                             'title' => 'Mail-Adresse',
68                                             'help' => 'Nach Änderung muß das Passwort neu gesetzt werden',
69                                             'value' => $_SESSION['email']]));
70         $form->add(new FormElement('text', ['name' => 'mobile',
71                                             'title' => 'Mobiltelefon',
72                                             'help' => 'Nur sichtbar für Tour-Mitglieder',
73                                             'placeholder' => '0150-1234567',
74                                             'value' => $user->get('mobile')]));
75         $form->add(new FormElement('checkbox', ['name' => 'single_room',
76                                                 'title' => 'Einzelzimmer gewünscht',
77                                                 'help' => 'Normalerweise teilen wir uns zu zweit ein Doppelzimmer',
78                                                 'checked' => $user->get('single_room')]));
79
80         $response->setData($form->toString());
81     }
82
83     public function ajaxSettings($request, $response, $data)
84     {
85         $ok = $this->db->update('sys_user',
86                                 ['email' => $data['email'],
87                                  'mobile' => $data['mobile'],
88                                  'single_room' => isset($data['single_room']) && $data['single_room'] == '1'],
89                                 'id='.$_SESSION['userid']);
90
91         if (!$ok) {
92             return $response->setError('Fehler beim Speichern');
93         }
94
95         $_SESSION['email'] = $data['email'];
96     }
97 }