Completed
Branch develop-3.0 (48060b)
by Mohamed
02:47
created

Tag::updater()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model;
13
14
use Illuminate\Database\Eloquent\Collection;
15
use Tinyissue\Extensions\Auth\LoggedUser;
16
use URL;
17
18
/**
19
 * Tag is model class for tags.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property int $id
24
 * @property int $parent_id
25
 * @property string $name
26
 * @property string $fullname
27
 * @property string $bgcolor
28
 * @property bool $group
29
 * @property Tag $parent
30
 * @property int $role_limit
31
 * @property int $message_limit
32
 * @property int $readonly
33
 * @property Tag $parent
34
 * @property Collection $tags
35
 * @property Collection $issues
36
 * @property Collection $projects
37
 *
38
 * @method  Collection getGroupWithTags()
39
 * @method  array getGroupsDropdown()
40
 * @method  Collection getGroups()
41
 * @method  Collection getStatusTags()
42
 * @method  Collection getTypeTags()
43
 * @method  Collection getResolutionTags()
44
 * @method  int countNumberOfTags()
45
 * @method  $this groupOnly()
46
 * @method  $this notGroup()
47
 * @method  $this ofType($type)
48
 * @method  $this accessibleToUser(User $user)
49
 * @method  $this accessibleToLoggedUser()
50
 */
51
class Tag extends ModelAbstract
52
{
53
    use TagRelations, TagScopes, LoggedUser;
54
55
    /**
56
     * Core tag: Open.
57
     *
58
     * @var string
59
     */
60
    const STATUS_OPEN = 'open';
61
62
    /**
63
     * Core tag: Closed.
64
     *
65
     * @var string
66
     */
67
    const STATUS_CLOSED = 'closed';
68
69
    /**
70
     * Core tag group: Status.
71
     *
72
     * @var string
73
     */
74
    const GROUP_STATUS = 'status';
75
76
    /**
77
     * Core tag group: Type.
78
     *
79
     * @var string
80
     */
81
    const GROUP_TYPE = 'type';
82
83
    /**
84
     * Core tag group: Resolution.
85
     *
86
     * @var string
87
     */
88
    const GROUP_RESOLUTION = 'resolution';
89
90
    /**
91
     * Timestamp enabled.
92
     *
93
     * @var bool
94
     */
95
    public $timestamps = true;
96
97
    /**
98
     * List of allowed columns to be used in $this->fill().
99
     *
100
     * @var array
101
     */
102
    public $fillable = ['parent_id', 'name', 'bgcolor', 'group', 'role_limit', 'message_limit', 'readonly'];
103
104
    /**
105
     * Name of database table.
106
     *
107
     * @var string
108
     */
109
    protected $table = 'tags';
110
111
    /**
112
     * @param User|null $user
113
     *
114
     * @return \Tinyissue\Repository\Tag\Updater
115
     */
116
    public function updater(User $user = null)
117
    {
118
        return parent::updater($user);
119
    }
120
121
    /**
122
     * Generate a URL for the tag.
123
     *
124
     * @param string $url
125
     *
126
     * @return mixed
127
     */
128
    public function to($url)
129
    {
130
        return URL::to('administration/tag/' . $this->id . (($url) ? '/' . $url : ''));
131
    }
132
133
    /**
134
     * Returns tag full name with prefix group name and ":" in between.
135
     *
136
     * @return string
137
     */
138
    public function getFullNameAttribute()
139
    {
140
        return ucwords($this->attributes['name']);
141
    }
142
143
    /**
144
     * Whether or not the current user can view this tag.
145
     *
146
     * @return bool
147
     */
148
    public function canView()
149
    {
150
        return $this->getLoggedUser()->role_id >= $this->role_limit;
151
    }
152
153
    /**
154
     * Whether or not the tag to mark issue as ready only.
155
     *
156
     * @param User $user
157
     *
158
     * @return bool
159
     */
160
    public function isReadOnly(User $user = null)
161
    {
162
        if (is_null($user)) {
163
            return (boolean) $this->readonly;
164
        }
165
166
        return (boolean) $this->readonly && $user->role_id <= $this->readonly;
167
    }
168
169
    /**
170
     * Return an array of tag details.
171
     *
172
     * @return array
173
     */
174
    public function toShortArray()
175
    {
176
        return [
177
            'id'            => $this->id,
178
            'name'          => $this->fullname,
179
            'bgcolor'       => $this->bgcolor,
180
            'message_limit' => $this->message_limit,
181
            'group'         => $this->parent->fullname,
182
        ];
183
    }
184
185
    /**
186
     * Returns an array of core groups.
187
     *
188
     * @return array
189
     */
190
    public static function getCoreGroups()
191
    {
192
        return [
193
            self::GROUP_STATUS,
194
            self::GROUP_TYPE,
195
            self::GROUP_RESOLUTION,
196
        ];
197
    }
198
199
    /**
200
     * Whether or not the user is allowed to receive messages that contains the current tag.
201
     *
202
     * @param User $user
203
     *
204
     * @return bool
205
     */
206
    public function allowMessagesToUser(User $user)
207
    {
208
        if (!$this->message_limit || $this->message_limit <= $user->role_id) {
209
            return true;
210
        }
211
212
        return false;
213
    }
214
}
215