Completed
Push — master ( 5cd775...78bef9 )
by Jonathan
14:29 queued 04:27
created

Domain::getTablePrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Models;
4
5
use Illuminate\Database\Eloquent\SoftDeletes;
6
use Illuminate\Support\Collection;
7
use Cviebrock\EloquentSluggable\Sluggable;
0 ignored issues
show
Bug introduced by
The type Cviebrock\EloquentSluggable\Sluggable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Nicolaslopezj\Searchable\SearchableTrait;
0 ignored issues
show
Bug introduced by
The type Nicolaslopezj\Searchable\SearchableTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Gzero\EloquentTree\Model\Tree;
0 ignored issues
show
Bug introduced by
The type Gzero\EloquentTree\Model\Tree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Uccello\Core\Support\Traits\RelatedlistTrait;
11
12
class Domain extends Tree
13
{
14
    use SoftDeletes;
15
    use Sluggable;
16
    use SearchableTrait;
17
    use RelatedlistTrait;
18
19
    protected $tablePrefix;
20
21
    /**
22
     * The accessors to append to the model's array form.
23
     *
24
     * @var array
25
     */
26
    protected $appends = [
27
        'recordLabel'
28
    ];
29
30
    /**
31
     * The table associated with the model.
32
     *
33
     * @var string
34
     */
35
    protected $table = 'domains';
36
37
    /**
38
     * The attributes that should be mutated to dates.
39
     *
40
     * @var array
41
     */
42
    protected $dates = [ 'deleted_at' ];
43
44
    /**
45
     * The attributes that should be casted to native types.
46
     *
47
     * @var array
48
     */
49
    protected $casts = [
50
        'data' => 'object',
51
    ];
52
53
    /**
54
     * The attributes that are mass assignable.
55
     *
56
     * @var array
57
     */
58
    protected $fillable = [
59
        'name',
60
        'description',
61
        'parent_id',
62
    ];
63
64
    /**
65
     * Searchable rules.
66
     * See https://github.com/nicolaslopezj/searchable
67
     *
68
     * @var array
69
     */
70
    protected $searchable = [
71
        'columns' => [
72
            'name' => 1
73
        ]
74
    ];
75
76
    /**
77
     * Return the sluggable configuration array for this model.
78
     *
79
     * @return array
80
     */
81
    public function sluggable()
82
    {
83
        return [
84
            'slug' => [
85
                'source' => 'name',
86
                'onUpdate' => true,
87
                'includeTrashed' => false
88
            ]
89
        ];
90
    }
91
92
    public function __construct(array $attributes = [ ])
93
    {
94
        parent::__construct($attributes);
95
96
        // Init table prefix
97
        $this->initTablePrefix();
98
99
        // Init table name
100
        $this->initTableName();
101
102
        $this->addTreeEvents(); // Adding tree events
103
    }
104
105
    public function getTablePrefix()
106
    {
107
        return $this->tablePrefix;
108
    }
109
110
    protected function initTablePrefix()
111
    {
112
        $this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_');
113
    }
114
115
    protected function initTableName()
116
    {
117
        if ($this->table)
118
        {
119
            $this->table = $this->tablePrefix.$this->table;
120
        }
121
    }
122
123
    public function privileges()
124
    {
125
        return $this->hasMany(Privilege::class);
126
    }
127
128
    public function users()
129
    {
130
        return $this->hasMany(User::class);
131
    }
132
133
    public function roles()
134
    {
135
        return $this->hasMany(Role::class);
136
    }
137
138
    public function profiles()
139
    {
140
        return $this->hasMany(Profile::class);
141
    }
142
143
    public function modules()
144
    {
145
        return $this->belongsToMany(Module::class, $this->tablePrefix.'domains_modules');
146
    }
147
148
    public function menus()
149
    {
150
        return $this->hasMany(Menu::class);
151
    }
152
153
    /**
154
     * Returns record label
155
     *
156
     * @return string
157
     */
158
    public function getRecordLabelAttribute() : string
159
    {
160
        return $this->name;
161
    }
162
163
    /**
164
     * Returns all admin modules activated in the domain
165
     *
166
     * @return array
167
     */
168
    protected function getAdminModulesAttribute() : array
169
    {
170
        $modules = [ ];
171
172
        foreach ($this->modules()->get() as $module) {
173
            if ($module->isAdminModule()) {
174
                $modules[ ] = $module;
175
            }
176
        }
177
178
        return $modules;
179
    }
180
181
    /**
182
     * Returns all not admin modules activated in the domain
183
     *
184
     * @return array
185
     */
186
    protected function getNotAdminModulesAttribute() : array
187
    {
188
        $modules = [ ];
189
190
        foreach ($this->modules()->get() as $module) {
191
            if (!$module->isAdminModule()) {
192
                $modules[ ] = $module;
193
            }
194
        }
195
196
        return $modules;
197
    }
198
199
    /**
200
     * Return main menu
201
     * Priority:
202
     * 1. User menu
203
     * 2. Domain menu
204
     * 3. Default menu
205
     *
206
     * @return \Uccello\Core\Models\Menu|null
207
     */
208
    public function getMainMenuAttribute()
209
    {
210
        $userMenu = auth()->user()->menus()->where('type', 'main')->where('domain_id', $this->id)->first();
211
        $domainMenu = $this->menus()->where('type', 'main')->whereNull('user_id')->first();
212
        $defaultMenu = Menu::where('type', 'main')->whereNull('domain_id')->whereNull('user_id')->first();
213
214
        if (!is_null($userMenu)) {
215
            return $userMenu;
216
        } elseif (!is_null($domainMenu)) {
217
            return $domainMenu;
218
        } elseif (!is_null($defaultMenu)) {
219
            return $defaultMenu;
220
        } else {
221
            return null;
222
        }
223
    }
224
225
    /**
226
     * Return admin menu
227
     * Priority:
228
     * 1. User menu
229
     * 2. Domain menu
230
     * 3. Default menu
231
     *
232
     * @return \Uccello\Core\Models\Menu|null
233
     */
234
    public function getAdminMenuAttribute()
235
    {
236
        $userMenu = auth()->user()->menus()->where('type', 'admin')->where('domain_id', $this->id)->first();
237
        $domainMenu = $this->menus()->where('type', 'admin')->whereNull('user_id')->first();
238
        $defaultMenu = Menu::where('type', 'admin')->whereNull('domain_id')->whereNull('user_id')->first();
239
240
        if (!is_null($userMenu)) {
241
            return $userMenu;
242
        } elseif (!is_null($domainMenu)) {
243
            return $domainMenu;
244
        } elseif (!is_null($defaultMenu)) {
245
            return $defaultMenu;
246
        } else {
247
            return null;
248
        }
249
    }
250
}
251