Issues (357)

classes/Renderer/User.php (2 issues)

1
<?php
2
3
namespace MySociety\TheyWorkForYou\Renderer;
4
5
/**
6
 * User data for headers
7
 */
8
9
class User
10
{
11
    public $data;
12
13
    private $user;
14
    private $pagedata;
15
    private $page;
16
    private $returl;
17
18
    public function __construct() {
19
        global $THEUSER, $DATA, $this_page;
20
21
        $this->user = $THEUSER;
22
        $this->pagedata = $DATA;
23
        $this->page = $this_page;
24
        $this->data = array();
25
        $this->setupNavLinks();
26
    }
27
28
    private function setupNavLinks() {
29
        $this->data['user_nav_links'] = array();
30
31
        // We may want to send the user back to this current page after they've
32
        // joined, logged out or logged in. So we put the URL in $returl.
33
        $URL = new \MySociety\TheyWorkForYou\Url($this->page);
34
        $this->returl = $URL->generate('none');
35
36
        //user logged in
37
        if ($this->user->isloggedin()) {
38
            $this->addLoggedInLinks();
39
        } else {
40
            $this->addLoggedOutLinks();
41
        }
42
    }
43
44
    private function AddLangSwitcher(){
45
        if (preg_match('#^(senedd|wales|ms(?!p))#', $this->page)) {
0 ignored issues
show
$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

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