Completed
Push — master ( 32dcc3...b50c12 )
by Jonathan
14:17 queued 06:25
created

Module::widgets()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Uccello\Core\Models;
4
5
use Uccello\Core\Database\Eloquent\Model;
6
7
class Module extends Model
8
{
9
    /**
10
     * The table associated with the model.
11
     *
12
     * @var string
13
     */
14
    protected $table = 'modules';
15
16
    /**
17
     * The attributes that should be casted to native types.
18
     *
19
     * @var array
20
     */
21
    protected $casts = [
22
        'data' => 'object',
23
    ];
24
25
    /**
26
     * The attributes that are mass assignable.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = [
31
        'name',
32
        'icon',
33
        'model_class',
34
        'data'
35
    ];
36
37
    protected function initTablePrefix()
38
    {
39
        $this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_');
40
    }
41
42
    public function permissions()
43
    {
44
        return $this->hasMany(Permission::class);
45
    }
46
47
    public function domains()
48
    {
49
        return $this->belongsToMany(Domain::class, $this->tablePrefix.'domains_modules');
50
    }
51
52
    public function tabs()
53
    {
54
        return $this->hasMany(Tab::class)->orderBy('sequence');
55
    }
56
57
    public function blocks()
58
    {
59
        return $this->hasMany(Block::class)->orderBy('sequence');
60
    }
61
62
    public function fields()
63
    {
64
        return $this->hasMany(Field::class);
65
    }
66
67
    public function filters()
68
    {
69
        return $this->hasMany(Filter::class);
70
    }
71
72
    public function relatedlists()
73
    {
74
        return $this->hasMany(Relatedlist::class, 'module_id')->orderBy('sequence');
75
    }
76
77
    public function links()
78
    {
79
        return $this->hasMany(Link::class, 'module_id')->orderBy('sequence');
80
    }
81
82
    public function detailLinks()
83
    {
84
        return $this->hasMany(Link::class, 'module_id')->where('type', 'detail')->orderBy('sequence');
85
    }
86
87
    public function detailActionLinks()
88
    {
89
        return $this->hasMany(Link::class, 'module_id')->where('type', 'detail.action')->orderBy('sequence');
90
    }
91
92
    public function widgets()
93
    {
94
        return $this->belongsToMany(Widget::class, $this->tablePrefix.'modules_widgets')->withPivot('data');
95
    }
96
97
    /**
98
     * Returns module package name
99
     *
100
     * @return string|null
101
     */
102
    public function getPackageAttribute() : ?string
103
    {
104
        $package = ''; // For modules created directory in the host application
105
106
        // Get only package name if defined (Format: vendor/package)
107
        if (isset($this->data->package))
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
108
        {
109
            $packageData = explode('/', $this->data->package);
110
            $package = array_pop($packageData);
111
        }
112
113
        return $package;
114
    }
115
116
    /**
117
     * Return all module links to display in the menu
118
     *
119
     * @return array
120
     */
121
    public function getMenuLinksAttribute() : array
122
    {
123
        $menuLinks = [ ];
124
125
        //TODO: Adds capability needed
126
127
        if (isset($this->data->menu)) {
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
128
            // One route
129
            if (is_string($this->data->menu)) {
130
                $link = new \StdClass;
131
                $link->label = $this->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
132
                $link->route = $this->data->menu;
133
                $link->icon = $this->icon;
134
                $menuLinks[ ] = $link;
135
            }
136
            // Several routes
137
            elseif (is_array($this->data->menu)) {
138
                foreach ($this->data->menu as $link) {
139
                    if (empty($link->icon)) {
140
                        $link->icon = $this->icon;
141
                    }
142
                    $menuLinks[ ] = $link;
143
                }
144
            }
145
            // No route wanted
146
            elseif ($this->data->menu === false) {
147
                // Nothing to do
148
            }
149
        }
150
        // No route defined, add it automaticaly
151
        else {
152
            $link = new \StdClass;
153
            $link->label = $this->name;
154
            $link->route = 'uccello.list';
155
            $link->icon = $this->icon;
156
            $menuLinks[ ] = $link;
157
        }
158
159
        return $menuLinks;
160
    }
161
162
    /**
163
     * Searches in the module a field by name.
164
     *
165
     * @param string $name
166
     * @return Field|null
167
     */
168
    public function getField($name) : ?Field
169
    {
170
        return $this->fields()->where('name', $name)->first();
171
    }
172
173
    /**
174
     * Checks if the module is active on a domain.
175
     *
176
     * @param Domain $domain
177
     * @return boolean
178
     */
179
    public function isActiveOnDomain(Domain $domain) : bool
180
    {
181
        $isActive = false;
182
183
        foreach ($this->domains as $domainActive) {
184
            if ($domainActive->id === $domain->id) {
185
                $isActive = true;
186
                break;
187
            }
188
        }
189
190
        return $isActive;
191
    }
192
193
    /**
194
     * Checks if the module is for administration.
195
     *
196
     * @return boolean
197
     */
198
    public function isAdminModule() : bool
199
    {
200
        return $this->data->admin ?? false;
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
201
    }
202
203
    /**
204
     * Check if the module is mandatory.
205
     *
206
     * @return boolean
207
     */
208
    public function isMandatory() : bool
209
    {
210
        return $this->data->mandatory ?? false;
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
211
    }
212
}
213