Completed
Push — master ( 5b5792...2b84bd )
by Schlaefer
03:33 queued 11s
created

UserHelper::contact()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Saito - The Threaded Web Forum
4
 *
5
 * @copyright Copyright (c) the Saito Project Developers 2015
6
 * @link https://github.com/Schlaefer/Saito
7
 * @license http://opensource.org/licenses/MIT
8
 */
9
10
namespace App\View\Helper;
11
12
use App\Model\Entity\User;
13
use Cake\ORM\Entity;
14
use Identicon\Identicon;
15
use Saito\RememberTrait;
16
use Saito\User\ForumsUserInterface;
17
use Saito\User\SaitoUser;
18
use Stopwatch\Lib\Stopwatch;
19
20
/**
21
 * Class UserHelper
22
 *
23
 * @package App\View\Helper
24
 */
25
class UserHelper extends AppHelper
26
{
27
    use RememberTrait;
28
29
    public $helpers = ['Html', 'Url'];
30
31
    /**
32
     * banned
33
     *
34
     * @param bool $isBanned banned
35
     * @return string
36
     */
37
    public function banned($isBanned)
38
    {
39
        $out = '';
40
        if ($isBanned) {
41
            $out = '<i class="fa fa-ban fa-lg"></i>';
42
        }
43
44
        return $out;
45
    }
46
47
    /**
48
     * generates CSS from user-preferences
49
     *
50
     * @param array $User user
51
     * @return string
52
     */
53
    public function generateCss(array $User)
54
    {
55
        $styles = [];
56
57
        // colors
58
        $cNew = $User['user_color_new_postings'];
59
        $cOld = $User['user_color_old_postings'];
60
        $cAct = $User['user_color_actual_posting'];
61
62
        $aMetatags = ['', ':link', ':visited', ':hover', ':active'];
63
        foreach ($aMetatags as $aMetatag) {
64
            if (!empty($cOld) && $cOld !== '#') {
65
                $styles[] = ".et-root .et$aMetatag, .et-reply .et$aMetatag	{ color: $cOld; }";
66
            }
67
            if (!empty($cNew) && $cNew !== '#') {
68
                $styles[] = ".et-new .et$aMetatag { color: $cNew; }";
69
            }
70
            if (!empty($cAct) && $cAct !== '#') {
71
                $styles[] = ".et-current .et$aMetatag { color: $cAct; }";
72
            }
73
        }
74
75
        return '<style type="text/css">' . implode(" ", $styles) . '</style>';
76
    }
77
78
    /**
79
     * Translates user types
80
     *
81
     * @param string $type type
82
     * @return mixed
83
     */
84 View Code Duplication
    public function type($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        // write out all __() strings for l10n
87
        switch ($type) {
88
            case 'user':
89
                return __('user.type.user');
90
            case 'mod':
91
                return __('user.type.mod');
92
            case 'admin':
93
                return __('user.type.admin');
94
        }
95
    }
96
97
    /**
98
     * Creates link to user's external (non-Saito) homepage
99
     *
100
     * @param string $url user provided URL-string
101
     * @return string link or escaped string
102
     */
103
    public function linkExternalHomepage(string $url): string
104
    {
105
        $link = $url;
106
107
        if (substr($link, 0, 4) == 'www.') {
108
            $link = 'http://' . $link;
109
        }
110
        if (substr($link, 0, 4) == 'http') {
111
            $text = '<i class="fa fa-home fa-lg"></i>';
112
113
            return $this->Html->link($text, $link, ['escape' => false]);
114
        }
115
116
        return h($url);
117
    }
118
119
    /**
120
     * Link to user-profile
121
     *
122
     * @param User|ForumsUserInterface $user user
123
     * @param bool $link link
124
     * @param array $options options
125
     * @return string
126
     */
127
    public function linkToUserProfile($user, $link = true, array $options = [])
128
    {
129
        $options += [
130
            'title' => $user->get('username'),
131
            'escape' => true
132
        ];
133
        $id = $user->get('id');
134
135
        $name = $options['title'];
136
        unset($options['title']);
137
138
        if (empty($id)) {
139
            // removed user
140
            $html = $name;
141
        } elseif ($link || ($link instanceof ForumsUserInterface && $link->isLoggedIn())
142
        ) {
143
            return $this->Html->link($name, '/users/view/' . $id, $options);
144
        } else {
145
            $html = $name;
146
        }
147
        if ($options['escape']) {
148
            $html = h($html);
149
        }
150
151
        return $html;
152
    }
153
154
    /**
155
     * Get image avatar for user
156
     *
157
     * @param User|ForumsUserInterface $user User
158
     * @param array $options options
159
     * @return string HTML
160
     */
161
    public function getAvatar($user, array $options = [])
162
    {
163
        $getAvatar = function () use ($user, $options) {
164
            Stopwatch::start('UserHelper::getAvatar()');
165
            $defaults = [
166
                'class' => 'avatar-image',
167
                'link' => [
168
                    'class' => 'avatar-link',
169
                    'escape' => false
170
                ],
171
                'size' => 50,
172
                'style' => '',
173
                'tag' => 'span'
174
            ];
175
            $options = array_replace_recursive($defaults, $options);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $options, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
176
            $size = $options['size'];
177
178
            $avatar = $user->get('avatar');
179
            if ($avatar) {
180
                $userId = $user->get('id');
181
                $url = "useruploads/users/avatar/{$userId}/square_{$avatar}";
182
                $imgUri = $this->Url->assetUrl($url);
183
            } else {
184
                $name = $user->get('username');
185
                $hdpi = 2 * $size;
186
                $imgUri = (new Identicon)->getImageDataUri($name, $hdpi);
187
            }
188
189
            $style = "background-image: url({$imgUri});" . $options['style'];
190
191
            $html = $this->Html->tag(
192
                $options['tag'],
193
                '',
194
                [
195
                    'class' => $options['class'],
196
                    'style' => $style,
197
                ]
198
            );
199
200
            if ($options['link'] !== false) {
201
                $options['link']['title'] = $html;
202
                $html = $this->linkToUserProfile($user, true, $options['link']);
203
            }
204
            Stopwatch::end('UserHelper::getAvatar()');
205
206
            return $html;
207
        };
208
209
        $name = $user->get('username');
210
        $hash = 'avatar.' . md5($name . serialize($options));
211
212
        return $this->remember($hash, $getAvatar);
213
    }
214
}
215