Completed
Branch feature/currentUserRefactoring (c13c1d)
by Schlaefer
09:08
created

UserHelper::linkToUserProfile()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
nc 5
nop 3
dl 0
loc 26
rs 9.1111
c 1
b 0
f 0
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 (($link === true)
129
            || ($link instanceof CurrentUserInterface && $link->isLoggedIn())
130
        ) {
131
            return $this->Html->link($name, '/users/view/' . $id, $options);
132
        } else {
133
            $html = $name;
134
        }
135
        if ($options['escape']) {
136
            $html = h($html);
137
        }
138
139
        return $html;
140
    }
141
142
    /**
143
     * Get image avatar for user
144
     *
145
     * @param User|ForumsUserInterface $user User
146
     * @param array $options options
147
     * @return string HTML
148
     */
149
    public function getAvatar($user, array $options = [])
150
    {
151
        $getAvatar = function () use ($user, $options) {
152
            Stopwatch::start('UserHelper::getAvatar()');
153
            $defaults = [
154
                'class' => 'avatar-image',
155
                'link' => [
156
                    'class' => 'avatar-link',
157
                    'escape' => false
158
                ],
159
                'size' => 50,
160
                'style' => '',
161
                'tag' => 'span'
162
            ];
163
            $options = array_replace_recursive($defaults, $options);
164
            $size = $options['size'];
165
166
            $avatar = $user->get('avatar');
167
            if ($avatar) {
168
                $userId = $user->get('id');
169
                $url = "useruploads/users/avatar/{$userId}/square_{$avatar}";
170
                $imgUri = $this->Url->assetUrl($url);
171
            } else {
172
                $name = $user->get('username');
173
                $hdpi = 2 * $size;
174
                $imgUri = (new Identicon)->getImageDataUri($name, $hdpi);
175
            }
176
177
            $style = "background-image: url({$imgUri});" . $options['style'];
178
179
            $html = $this->Html->tag(
180
                $options['tag'],
181
                '',
182
                [
183
                    'class' => $options['class'],
184
                    'style' => $style,
185
                ]
186
            );
187
188
            if ($options['link'] !== false) {
189
                $options['link']['title'] = $html;
190
                $html = $this->linkToUserProfile($user, true, $options['link']);
191
            }
192
            Stopwatch::end('UserHelper::getAvatar()');
193
194
            return $html;
195
        };
196
197
        $name = $user->get('username');
198
        $hash = 'avatar.' . md5($name . serialize($options));
199
200
        return $this->remember($hash, $getAvatar);
201
    }
202
}
203