UserHelper   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 79
dl 0
loc 171
rs 10
c 3
b 0
f 1
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
B generateCss() 0 23 8
A linkExternalHomepage() 0 14 3
A banned() 0 8 2
A getAvatar() 0 52 3
A linkToUserProfile() 0 27 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\View\Helper;
14
15
use App\Model\Entity\User;
16
use Cake\View\Helper\HtmlHelper;
17
use Cake\View\Helper\UrlHelper;
18
use Identicon\Identicon;
19
use Saito\RememberTrait;
20
use Saito\User\CurrentUser\CurrentUserInterface;
21
use Saito\User\ForumsUserInterface;
22
use Stopwatch\Lib\Stopwatch;
23
24
/**
25
 * Class UserHelper
26
 *
27
 * @package App\View\Helper
28
 * @property HtmlHelper $Html
29
 * @property UrlHelper $Url
30
 */
31
class UserHelper extends AppHelper
32
{
33
    use RememberTrait;
34
35
    public $helpers = ['Html', 'Url'];
36
37
    /**
38
     * banned
39
     *
40
     * @param bool $isBanned banned
41
     * @return string
42
     */
43
    public function banned($isBanned)
44
    {
45
        $out = '';
46
        if ($isBanned) {
47
            $out = '<i class="fa fa-ban fa-lg"></i>';
48
        }
49
50
        return $out;
51
    }
52
53
    /**
54
     * generates CSS from user-preferences
55
     *
56
     * @param array $User user
57
     * @return string
58
     */
59
    public function generateCss(array $User)
60
    {
61
        $styles = [];
62
63
        // colors
64
        $cNew = $User['user_color_new_postings'];
65
        $cOld = $User['user_color_old_postings'];
66
        $cAct = $User['user_color_actual_posting'];
67
68
        $aMetatags = ['', ':link', ':visited', ':hover', ':active'];
69
        foreach ($aMetatags as $aMetatag) {
70
            if (!empty($cOld) && $cOld !== '#') {
71
                $styles[] = ".et-root .et$aMetatag, .et-reply .et$aMetatag	{ color: $cOld; }";
72
            }
73
            if (!empty($cNew) && $cNew !== '#') {
74
                $styles[] = ".et-new .et$aMetatag { color: $cNew; }";
75
            }
76
            if (!empty($cAct) && $cAct !== '#') {
77
                $styles[] = ".et-current .et$aMetatag { color: $cAct; }";
78
            }
79
        }
80
81
        return '<style type="text/css">' . implode(" ", $styles) . '</style>';
82
    }
83
84
    /**
85
     * Creates link to user's external (non-Saito) homepage
86
     *
87
     * @param string $url user provided URL-string
88
     * @return string link or escaped string
89
     */
90
    public function linkExternalHomepage(string $url): string
91
    {
92
        $link = $url;
93
94
        if (substr($link, 0, 4) == 'www.') {
95
            $link = 'http://' . $link;
96
        }
97
        if (substr($link, 0, 4) == 'http') {
98
            $text = '<i class="fa fa-home fa-lg"></i>';
99
100
            return $this->Html->link($text, $link, ['escape' => false]);
101
        }
102
103
        return h($url);
104
    }
105
106
    /**
107
     * Link to user-profile
108
     *
109
     * @param User|ForumsUserInterface $user user
110
     * @param bool|CurrentUserInterface $link link
111
     * @param array $options options
112
     * @return string
113
     */
114
    public function linkToUserProfile($user, $link = true, array $options = []): string
115
    {
116
        $options += [
117
            'title' => $user->get('username'),
118
            'escape' => true,
119
        ];
120
        $id = $user->get('id');
121
122
        $name = $options['title'];
123
        unset($options['title']);
124
125
        if (empty($id)) {
126
            // removed user
127
            $html = $name;
128
        } elseif (
129
            ($link === true)
130
            || ($link instanceof CurrentUserInterface && $link->isLoggedIn())
131
        ) {
132
            return $this->Html->link($name, '/users/view/' . $id, $options);
133
        } else {
134
            $html = $name;
135
        }
136
        if ($options['escape']) {
137
            $html = h($html);
138
        }
139
140
        return $html;
141
    }
142
143
    /**
144
     * Get image avatar for user
145
     *
146
     * @param User|ForumsUserInterface $user User
147
     * @param array $options options
148
     * @return string HTML
149
     */
150
    public function getAvatar($user, array $options = [])
151
    {
152
        $getAvatar = function () use ($user, $options) {
153
            Stopwatch::start('UserHelper::getAvatar()');
154
            $defaults = [
155
                'class' => 'avatar-image',
156
                'link' => [
157
                    'class' => 'avatar-link',
158
                    'escape' => false,
159
                ],
160
                'size' => 50,
161
                'style' => '',
162
                'tag' => 'span',
163
            ];
164
            $options = array_replace_recursive($defaults, $options);
165
            $size = $options['size'];
166
167
            $avatar = $user->get('avatar');
168
            if ($avatar) {
169
                $userId = $user->get('id');
170
                $url = "useruploads/users/avatar/{$userId}/square_{$avatar}";
171
                $imgUri = $this->Url->assetUrl($url);
172
            } else {
173
                $name = $user->get('username');
174
                $hdpi = 2 * $size;
175
                $imgUri = (new Identicon())->getImageDataUri($name, $hdpi);
176
            }
177
178
            $style = "background-image: url({$imgUri});" . $options['style'];
179
180
            $html = $this->Html->tag(
181
                $options['tag'],
182
                '',
183
                [
184
                    'class' => $options['class'],
185
                    'style' => $style,
186
                ]
187
            );
188
189
            if ($options['link'] !== false) {
190
                $options['link']['title'] = $html;
191
                $html = $this->linkToUserProfile($user, true, $options['link']);
192
            }
193
            Stopwatch::end('UserHelper::getAvatar()');
194
195
            return $html;
196
        };
197
198
        $name = $user->get('username');
199
        $hash = 'avatar.' . md5($name . serialize($options));
200
201
        return $this->remember($hash, $getAvatar);
202
    }
203
}
204