UserPresenter::avatar()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php namespace NukaCode\Users\Presenters;
2
3
use NukaCode\Core\Presenters\BasePresenter;
4
5
class UserPresenter extends BasePresenter {
6
7
    /********************************************************************
8
     * Model attributes
9
     *******************************************************************/
10
11
    /**
12
     * Always uppercase a user's username
13
     *
14
     * @return string
15
     */
16
    public function username()
17
    {
18
        return ucwords($this->entity->username);
19
    }
20
21
    public function emailLink()
22
    {
23
        $preference = $this->getPreferenceValueByKeyName('SHOW_EMAIL');
24
25
        if ($preference == 'yes') {
26
            return \HTML::mailto($this->email, $this->email);
27
        }
28
29
        return null;
30
    }
31
32
    /********************************************************************
33
     * Preferences
34
     *******************************************************************/
35
36
    public function alertLocation()
37
    {
38
        $preference = $this->getPreferenceValueByKeyName('ALERT_LOCATION');
39
40
        switch ($preference) {
41
            case 'top-left':
42
                return 'messenger-on-top messenger-on-left';
43
            case 'top':
44
                return 'messenger-on-top';
45
            case 'top-right':
46
                return 'messenger-on-top messenger-on-right';
47
            case 'bottom-right':
48
                return 'messenger-on-bottom messenger-on-right';
49
            case 'bottom':
50
                return 'messenger-on-bottom';
51
            case 'bottom-left':
52
                return 'messenger-on-bottom messenger-on-left';
53
        }
54
    }
55
56
    public function popover()
57
    {
58
        return $this->getPreferenceValueByKeyName('POPOVER_TYPE');
59
    }
60
61
    public function image()
62
    {
63
        $avatarPreference = $this->getPreferenceValueByKeyName('AVATAR');
64
65
        if ($avatarPreference != 'none') {
66
            return $this->{$avatarPreference};
67
        } else {
68
            return '/img/no_user.png';
69
        }
70
    }
71
72
    public function avatar()
73
    {
74
        // get the users avatar if it exists
75
        if (file_exists(public_path() .'/img/avatars/User/'. \Str::studly($this->username) .'.png')) {
76
            return '/img/avatars/User/'. \Str::studly($this->username) .'.png';
77
        }
78
79
        // If no other image is set, use the default
80
        return '/img/no_user.png';
81
    }
82
83
    /**
84
     * Check for an avatar uploaded to the site, resort to gravatar if none exists, resort to no user image if no gravatar exists
85
     *
86
     * @return string
87
     */
88
    public function gravatar()
89
    {
90
        // Check for valid gravatar
91
        $gravCheck = 'http://www.gravatar.com/avatar/'. md5( strtolower( trim( $this->email ) ) ) .'.png?d=404';
92
        $response  = get_headers($gravCheck);
93
94
        // If a valid gravatar URL is found, use the gravatar image
95
        if ($response[0] != "HTTP/1.0 404 Not Found"){
96
            return 'http://www.gravatar.com/avatar/'. md5( strtolower( trim( $this->email ) ) ) .'.png?s=200&d=blank';
97
        }
98
99
        // If no other image is set, use the default
100
        return '/img/no_user.png';
101
    }
102
103
    public function onlyGravatar()
104
    {
105
        return 'http://www.gravatar.com/avatar/'. md5( strtolower( trim( $this->email ) ) ) .'.png';
106
    }
107
108
    /********************************************************************
109
     * New attributes
110
     *******************************************************************/
111
112
    public function profile()
113
    {
114
        return \HTML::link('/user/view/'. $this->id, $this->username);
115
    }
116
117
    public function roleList()
118
    {
119
        if ($this->roles->count() > 0) {
120
            return implode('<br />', $this->roles()->orderBy('group')->orderBy('priority')->get()->fullName->toArray());
121
        }
122
123
        return 'None';
124
    }
125
126
    /**
127
     * Get the number of posts from this user
128
     *
129
     */
130
    public function postsCount()
131
    {
132
        $postsCount   = $this->posts->count();
133
        $repliesCount = $this->replies->count();
134
135
        return $postsCount + $repliesCount;
136
    }
137
138
    /**
139
     * Get the user's css file
140
     *
141
     */
142
    public function theme()
143
    {
144
        return public_path() .'/css/master3/users/'. \Str::studly($this->username) .'.css';
145
    }
146
147
    /**
148
     * Get the user's css file for the laravel style method
149
     *
150
     */
151
    public function themeStyle()
152
    {
153
        return '/css/master3/users/'. \Str::studly($this->username) .'.css';
154
    }
155
156
    /**
157
     * Combine the user's first and last name to produce a full name
158
     *
159
     * @return string
160
     */
161
    public function fullName()
162
    {
163
        return $this->firstName .' '. $this->lastName;
164
    }
165
166
    /**
167
     * Make the join date easier to read
168
     *
169
     * @return string
170
     */
171
    public function joinDate()
172
    {
173
        return $this->createdAtReadable();
174
    }
175
176
    /**
177
     * Make the last active date easier to read
178
     *
179
     * @return string
180
     */
181
    public function lastActiveReadable()
182
    {
183
        return ($this->lastActive == '0000-00-00 00:00:00' || $this->lastActive == null ? 'Never' : date('F jS, Y \a\t h:ia', strtotime($this->lastActive)));
184
    }
185
186 View Code Duplication
    public function online($flip = false)
187
    {
188
        if ($this->lastActive >= date('Y-m-d H:i:s', strtotime('-15 minutes'))) {
189
            $status = ['<i class="text-success fa fa-circle"></i>', 'Online'];
190
        } else {
191
            $status = ['<i class="text-error fa fa-circle"></i>', 'Offline'];
192
        }
193
194
        if ($flip) {
195
            return implode('&nbsp;&nbsp;', array_reverse($status));
196
        }
197
198
        return implode('&nbsp;&nbsp;', $status);
199
    }
200
201 View Code Duplication
    public function onlineMaterialize($flip = false)
202
    {
203
        if ($this->lastActive >= date('Y-m-d H:i:s', strtotime('-15 minutes'))) {
204
            $status = ['<i class="green-text fa fa-circle"></i>', 'Online'];
205
        } else {
206
            $status = ['<i class="red-text fa fa-circle"></i>', 'Offline'];
207
        }
208
209
        if ($flip) {
210
            return implode('&nbsp;&nbsp;', array_reverse($status));
211
        }
212
213
        return implode('&nbsp;&nbsp;', $status);
214
    }
215
}
216