Passed
Push — analysis-z4nkaD ( 2af593 )
by Philippe
11:48 queued 27s
created

Module::scopeWithoutPageSpecific()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\Modules;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Thinktomorrow\Chief\Pages\Page;
8
use Illuminate\Database\Eloquent\Model;
9
use Spatie\MediaLibrary\HasMedia\HasMedia;
10
use Thinktomorrow\Chief\Management\Managers;
11
use Illuminate\Database\Eloquent\SoftDeletes;
12
use Thinktomorrow\Chief\Relations\ActsAsChild;
13
use Thinktomorrow\Chief\Snippets\WithSnippets;
14
use Thinktomorrow\Chief\Fields\Types\HtmlField;
15
use Thinktomorrow\Chief\Fields\Types\InputField;
16
use Thinktomorrow\Chief\Management\ManagedModel;
17
use Thinktomorrow\Chief\Relations\ActingAsChild;
18
use Thinktomorrow\AssetLibrary\Traits\AssetTrait;
19
use Thinktomorrow\Chief\Concerns\Viewable\Viewable;
20
use Thinktomorrow\Chief\Concerns\Morphable\Morphable;
21
use Thinktomorrow\Chief\FlatReferences\FlatReference;
22
use Thinktomorrow\Chief\Concerns\Translatable\Translatable;
23
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract;
24
use Astrotomic\Translatable\Translatable as BaseTranslatable;
25
use Thinktomorrow\Chief\Concerns\Morphable\MorphableContract;
26
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract;
27
28
class Module extends Model implements ManagedModel, TranslatableContract, HasMedia, ActsAsChild, MorphableContract, ViewableContract
29
{
30
    use Morphable,
0 ignored issues
show
introduced by
The trait Thinktomorrow\Chief\Concerns\Viewable\Viewable requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $content, $viewKey
Loading history...
Bug introduced by
The trait Thinktomorrow\Chief\Concerns\Morphable\Morphable requires the property $morph_key which is not provided by Thinktomorrow\Chief\Modules\Module.
Loading history...
introduced by
The trait Thinktomorrow\AssetLibrary\Traits\AssetTrait requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $assets, $pivot, $fallbackPath, $each, $locale, $mediaConversionRegistrations, $fallbackUrl, $media, $type, $collection_name
Loading history...
31
        AssetTrait,
32
        Translatable,
33
        BaseTranslatable,
34
        SoftDeletes,
35
        ActingAsChild,
36
        WithSnippets,
37
        Viewable;
38
39
    // Explicitly mention the translation model so on inheritance the child class uses the proper default translation model
40
    protected $translationModel = ModuleTranslation::class;
41
    protected $translationForeignKey = 'module_id';
42
    protected $translatedAttributes = [
43
        'title', 'content'
44
    ];
45
46
    public $useTranslationFallback = true;
47
    public $table = "modules";
48
    protected $guarded = [];
49
    protected $with = ['translations'];
50
51
    protected $baseViewPath;
52
53
    public function __construct(array $attributes = [])
54
    {
55
        $this->constructWithSnippets();
56
57
        if (!isset($this->baseViewPath)) {
58
            $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.modules', 'modules');
59
        }
60
61
        parent::__construct($attributes);
62
    }
63
64
    public static function managedModelKey(): string
65
    {
66
        if (isset(static::$managedModelKey)) {
67
            return static::$managedModelKey;
0 ignored issues
show
Bug introduced by
The property managedModelKey does not seem to exist on Thinktomorrow\Chief\Modules\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...
68
        }
69
70
        throw new \Exception('Missing required static property \'managedModelKey\' on ' . static::class. '.');
71
    }
72
73
    /**
74
     * Enlist all available managed modules for creation.
75
     * @return Collection of ManagedModelDetails
76
     */
77
    public static function availableForCreate(): Collection
78
    {
79
        $managers = app(Managers::class)->findByTag('module')->filter(function ($manager) {
80
            return $manager->can('create');
81
        })->map(function ($manager) {
82
            return $manager->details();
83
        });
84
85
        return $managers;
86
    }
87
88
    /**
89
     * Return true if there is at least one registered module
90
     */
91
    public static function atLeastOneRegistered(): bool
92
    {
93
        return app(Managers::class)->hasRegistered('module');
94
    }
95
96
    public function page()
97
    {
98
        return $this->belongsTo(Page::class, 'page_id');
99
    }
100
101
    /**
102
     * The page specific ones are the text modules
103
     * which are added via the page builder
104
     *
105
     * @param $query
106
     */
107
    public function scopeWithoutPageSpecific($query)
108
    {
109
        $query->whereNull('page_id');
110
    }
111
112
    public function isPageSpecific(): bool
113
    {
114
        return !is_null($this->page_id);
115
    }
116
117
    /**
118
     * Each page / Module model can expose some custom fields. Add here the list of fields defined as name => Field where Field
119
     * is an instance of \Thinktomorrow\Chief\Fields\Types\Field
120
     *
121
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
122
     * @return array
123
     */
124
    public function customFields()
125
    {
126
        return [];
127
    }
128
129
    /**
130
     * Each module model can expose the managed translatable fields. These should be included as attributes just like the regular
131
     * translatable attributes. This method allows for easy installation of the form fields in chief.
132
     *
133
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
134
     * @return array
135
     */
136
    final public static function translatableFields($key = null)
137
    {
138
        $translatableFields = array_merge(static::defaultTranslatableFields(), static::customTranslatableFields());
139
140
        return $key ? array_pluck($translatableFields, $key) : $translatableFields;
0 ignored issues
show
introduced by
$key is of type null, thus it always evaluated to false.
Loading history...
Bug introduced by
The function array_pluck was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
        return $key ? /** @scrutinizer ignore-call */ array_pluck($translatableFields, $key) : $translatableFields;
Loading history...
141
    }
142
143
    /**
144
     * The custom addition of fields for a module model.
145
     *
146
     * To add a field, you should:
147
     * 1. override this method with your own and return the comprised list of fields.
148
     * 2. Setup the proper migrations and add the new field to the translatable values of the collection.
149
     *
150
     * @return array
151
     */
152
    public static function customTranslatableFields(): array
153
    {
154
        return [];
155
    }
156
157
    /**
158
     * The default set of fields for a module model.
159
     *
160
     * If you wish to remove any of these fields, you should:
161
     * 1. override this method with your own and return the comprised list of fields.
162
     * 2. Provide a migration to remove the column from database and remove the fields from the translatable values of the model.
163
     *
164
     * @return array
165
     */
166
    public static function defaultTranslatableFields(): array
167
    {
168
        return [
169
            InputField::make('title')->label('titel'),
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\InputField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
            InputField::make('title')->/** @scrutinizer ignore-call */ label('titel'),
Loading history...
170
            HtmlField::make('content')->label('Inhoud'),
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\HtmlField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
            HtmlField::make('content')->/** @scrutinizer ignore-call */ label('Inhoud'),
Loading history...
171
        ];
172
    }
173
174
    public function mediaUrls($type = null, $size = 'full'): Collection
175
    {
176
        return $this->getAllFiles($type)->map->getFileUrl($size);
177
    }
178
179
    public function mediaUrl($type = null, $size = 'full'): ?string
180
    {
181
        return $this->mediaUrls($type, $size)->first();
182
    }
183
184
    public static function mediaFields($key = null)
185
    {
186
        $types = [
187
//            MediaType::BACKGROUND => [
188
//                'type' => MediaType::BACKGROUND,
189
//                'label' => 'Achtergrond afbeelding',
190
//                'description' => '',
191
//            ]
192
        ];
193
194
        return $key ? array_pluck($types, $key) : $types;
0 ignored issues
show
Bug introduced by
The function array_pluck was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
        return $key ? /** @scrutinizer ignore-call */ array_pluck($types, $key) : $types;
Loading history...
195
    }
196
197
    public static function findBySlug($slug)
198
    {
199
        return static::where('slug', $slug)->first();
200
    }
201
202
    public function flatReference(): FlatReference
203
    {
204
        return new FlatReference(static::class, $this->id);
205
    }
206
207
    public function flatReferenceLabel(): string
208
    {
209
        return $this->slug ?? '';
210
    }
211
212
    public function flatReferenceGroup(): string
213
    {
214
        $classKey = get_class($this);
215
        $labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : Str::singular($classKey);
0 ignored issues
show
Bug introduced by
The property labelSingular does not seem to exist on Thinktomorrow\Chief\Modules\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...
216
217
        return $labelSingular;
218
    }
219
}
220