Passed
Pull Request — master (#1962)
by
unknown
04:44
created

User::addLoggedOutLinks()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 39
rs 9.2728
1
<?php
2
3
namespace MySociety\TheyWorkForYou\Renderer;
4
5
/**
6
 * User data for headers
7
 */
8
9
class User {
10
    public $data;
11
12
    private $user;
13
    private $pagedata;
14
    private $page;
15
    private $returl;
16
17
    public function __construct() {
18
        global $THEUSER, $DATA, $this_page;
19
20
        $this->user = $THEUSER;
21
        $this->pagedata = $DATA;
22
        $this->page = $this_page;
23
        $this->data = [];
24
        $this->setupNavLinks();
25
    }
26
27
    private function setupNavLinks() {
28
        $this->data['user_nav_links'] = [];
29
30
        // We may want to send the user back to this current page after they've
31
        // joined, logged out or logged in. So we put the URL in $returl.
32
        $URL = new \MySociety\TheyWorkForYou\Url($this->page);
33
        $this->returl = $URL->generate('none');
34
35
        //user logged in
36
        if ($this->user->isloggedin()) {
37
            $this->addLoggedInLinks();
38
        } else {
39
            $this->addLoggedOutLinks();
40
        }
41
    }
42
43
    private function AddLangSwitcher() {
44
        if (preg_match('#^(senedd|wales|ms(?!p))#', $this->page)) {
0 ignored issues
show
Bug introduced by
$this->page of type mixed is incompatible with the type string expected by parameter $subject of preg_match(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        if (preg_match('#^(senedd|wales|ms(?!p))#', /** @scrutinizer ignore-type */ $this->page)) {
Loading history...
45
            $href = $_SERVER['REQUEST_URI'];
46
            if (LANGUAGE == 'cy') {
0 ignored issues
show
introduced by
The condition MySociety\TheyWorkForYou...nderer\LANGUAGE == 'cy' is always false.
Loading history...
47
                $text = 'English';
48
                $href = "//" . DOMAIN . $href;
49
            } else {
50
                $text = 'Cymraeg';
51
                if (strpos(DOMAIN, 'www') !== false) {
52
                    $href = "//" . str_replace('www.', 'cy.', DOMAIN) . $href;
53
                } else {
54
                    $href = "//cy." . DOMAIN . $href;
55
                }
56
            }
57
            $this->data['user_nav_links'][] = [
58
                'href' => $href,
59
                'classes' => '',
60
                'title' => '',
61
                'text' => $text,
62
            ];
63
        }
64
    }
65
66
    private function addLoggedInLinks() {
67
        // The 'Edit details' link.
68
        $menudata   = $this->pagedata->page_metadata('userviewself', 'menu');
69
        $edittitle  = $menudata['title'];
70
        $EDITURL    = new \MySociety\TheyWorkForYou\Url('userviewself');
71
        if ($this->page == 'userviewself' || $this->page == 'useredit') {
0 ignored issues
show
introduced by
The condition $this->page == 'useredit' is always false.
Loading history...
72
            $editclass = 'on';
73
        } else {
74
            $editclass = '';
75
        }
76
77
        // The 'Log out' link.
78
        $menudata   = $this->pagedata->page_metadata('userlogout', 'menu');
79
        $logouttext = $menudata['text'];
80
        $logouttitle = $menudata['title'];
81
82
        $LOGOUTURL  = new \MySociety\TheyWorkForYou\Url('userlogout');
83
        if ($this->page != 'userlogout') {
0 ignored issues
show
introduced by
The condition $this->page != 'userlogout' is always true.
Loading history...
84
            $LOGOUTURL->insert(["ret" => $this->returl]);
85
            $logoutclass = '';
86
        } else {
87
            $logoutclass = 'on';
88
        }
89
90
        $username = $this->user->firstname() . ' ' . $this->user->lastname();
91
92
        $this->addRepLinks();
93
94
        $this->data['user_nav_links'][] = [
95
            'href'    => $EDITURL->generate(),
96
            'title'   => $edittitle,
97
            'classes' => $editclass,
98
            'text'    => _htmlentities($username),
99
        ];
100
101
        $this->data['user_nav_links'][] = [
102
            'href'    => $LOGOUTURL->generate(),
103
            'title'   => $logouttitle,
104
            'classes' => $logoutclass,
105
            'text'    => $logouttext,
106
        ];
107
108
        $this->addContactLink();
109
        $this->addDonateLink();
110
        $this->AddLangSwitcher();
111
    }
112
113
    private function addLoggedOutLinks() {
114
        // The 'Join' link.
115
116
117
        // The 'Log in' link.
118
        $menudata = $this->pagedata->page_metadata('userlogin', 'menu');
119
        $logintext = $menudata['text'];
120
        $logintitle = $menudata['title'];
121
122
        $LOGINURL = new \MySociety\TheyWorkForYou\Url('userlogin');
123
        if ($this->page != 'userlogin') {
0 ignored issues
show
introduced by
The condition $this->page != 'userlogin' is always true.
Loading history...
124
            if ($this->page != "userlogout" &&
125
                $this->page != "userpassword" &&
0 ignored issues
show
introduced by
The condition $this->page != 'userpassword' is always true.
Loading history...
126
                $this->page != 'userjoin') {
0 ignored issues
show
introduced by
The condition $this->page != 'userjoin' is always true.
Loading history...
127
                // We don't do this on the logout page, because then the user
128
                // will return straight to the logout page and be logged out
129
                // immediately!
130
                // And it's also silly if we're sent back to Change Password.
131
                // And the join page.
132
                $LOGINURL->insert(["ret" => $this->returl]);
133
            }
134
            $loginclass = '';
135
        } else {
136
            $loginclass = 'on';
137
        }
138
139
        $this->data['user_nav_links'][] = [
140
            'href'    => $LOGINURL->generate(),
141
            'title'   => $logintitle,
142
            'classes' => $loginclass,
143
            'text'    => $logintext,
144
        ];
145
146
147
148
        $this->addRepLinks();
149
        $this->addContactLink();
150
        $this->addDonateLink();
151
        $this->AddLangSwitcher();
152
    }
153
154
    // add links to your MP etc if postcode set
155
    private function addRepLinks() {
156
        if ($this->user->postcode_is_set()) {
157
158
            $areas = \MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituencies($this->user->postcode());
159
            $items = ['yourmp'];
160
            if (isset($areas['SPC'])) {
161
                $items[] = 'yourmsp';
162
            } elseif (isset($areas['NIE'])) {
163
                $items[] = 'yourmla';
164
            } elseif (isset($areas['WAC'])) {
165
                $items[] = 'yourms';
166
            }
167
168
            foreach ($items as $item) {
169
                $menudata   = $this->pagedata->page_metadata($item, 'menu');
170
                $logintext  = $menudata['text'];
171
                $URL = new \MySociety\TheyWorkForYou\Url($item);
172
                $this->data['user_nav_links'][] = [
173
                    'href'    => $URL->generate(),
174
                    'title'   => '',
175
                    'classes' => '',
176
                    'text'    => $logintext,
177
                ];
178
            }
179
        }
180
181
    }
182
183
    private function addContactLink() {
184
        $menudata = $this->pagedata->page_metadata('contact', 'menu');
185
        $text = $menudata['text'];
186
        $title = $menudata['title'];
187
        $url = new \MySociety\TheyWorkForYou\Url('contact');
188
        $this->data['user_nav_links'][] = [
189
            'href'    => $url->generate(),
190
            'title'   => $title,
191
            'classes' => '',
192
            'text'    => $text,
193
        ];
194
    }
195
196
    private function addDonateLink() {
197
        if (LANGUAGE == 'cy') {
0 ignored issues
show
introduced by
The condition MySociety\TheyWorkForYou...nderer\LANGUAGE == 'cy' is always false.
Loading history...
198
            return;
199
        }
200
        $menudata = $this->pagedata->page_metadata('donate', 'menu');
201
        $text = $menudata['text'];
202
        $title = $menudata['title'];
203
        $url = new \MySociety\TheyWorkForYou\Url('donate');
204
        $this->data['user_nav_links'][] = [
205
            'href'    => $url->generate(),
206
            'title'   => $title,
207
            'classes' => 'donate-button',
208
            'text'    => $text,
209
        ];
210
    }
211
212
}
213