Passed
Push — ft/module-slug-rename ( e4fdb9 )
by Philippe
20:43
created

Module::available()   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 Thinktomorrow\Chief\Modules;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\Chief\Concerns\Viewable\Viewable;
7
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract;
8
use Thinktomorrow\Chief\Management\ManagedModel;
9
use Thinktomorrow\Chief\Pages\Page;
10
use Illuminate\Database\Eloquent\Model;
11
use Spatie\MediaLibrary\HasMedia\HasMedia;
12
use Thinktomorrow\Chief\Management\Managers;
13
use Illuminate\Database\Eloquent\SoftDeletes;
14
use Thinktomorrow\Chief\Relations\ActsAsChild;
15
use Thinktomorrow\Chief\Snippets\WithSnippets;
16
use Thinktomorrow\Chief\Fields\Types\HtmlField;
17
use Thinktomorrow\Chief\Fields\Types\InputField;
18
use Thinktomorrow\Chief\Relations\ActingAsChild;
19
use Thinktomorrow\AssetLibrary\Traits\AssetTrait;
20
use Thinktomorrow\Chief\Concerns\Morphable\Morphable;
21
use Thinktomorrow\Chief\FlatReferences\FlatReference;
22
use Dimsav\Translatable\Translatable as BaseTranslatable;
23
use Thinktomorrow\Chief\Concerns\Translatable\Translatable;
24
use Thinktomorrow\Chief\Concerns\Morphable\MorphableContract;
25
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract;
26
27
class Module extends Model implements ManagedModel, TranslatableContract, HasMedia, ActsAsChild, MorphableContract, ViewableContract
28
{
29
    use Morphable,
0 ignored issues
show
introduced by
The trait Dimsav\Translatable\Translatable requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $translations, $localeKey
Loading history...
Bug introduced by
The trait Thinktomorrow\Chief\Conc...anslatable\Translatable requires the property $translations which is not provided by Thinktomorrow\Chief\Modules\Module.
Loading history...
introduced by
The trait Thinktomorrow\Chief\Concerns\Morphable\Morphable requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $translations, $morph_key
Loading history...
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...
introduced by
The trait Thinktomorrow\AssetLibrary\Traits\AssetTrait requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $assets, $pivot, $each, $locale, $mediaConversionRegistrations, $media, $type, $collection_name
Loading history...
30
        AssetTrait,
31
        Translatable,
32
        BaseTranslatable,
33
        SoftDeletes,
34
        ActingAsChild,
35
        WithSnippets,
36
        Viewable;
37
38
    // Explicitly mention the translation model so on inheritance the child class uses the proper default translation model
39
    protected $translationModel = ModuleTranslation::class;
40
    protected $translationForeignKey = 'module_id';
41
    protected $translatedAttributes = [
42
        'title', 'content'
43
    ];
44
45
    public $useTranslationFallback = true;
46
    public $table = "modules";
47
    protected $guarded = [];
48
    protected $with = ['translations'];
49
50
    protected $baseViewPath;
51
52
    public function __construct(array $attributes = [])
53
    {
54
        $this->constructWithSnippets();
55
56
        if (!isset($this->baseViewPath)) {
57
            $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.modules', 'modules');
58
        }
59
60
        parent::__construct($attributes);
61
    }
62
63
    public static function managedModelKey(): string
64
    {
65
        if (isset(static::$managedModelKey)) {
66
            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...
67
        }
68
69
        throw new \Exception('Missing required static property \'managedModelKey\' on ' . static::class. '.');
70
    }
71
72
    /**
73
     * Enlist all available managed modules.
74
     * @return Collection of ManagedModelDetails
75
     */
76
    public static function available(): Collection
77
    {
78
        return app(Managers::class)->findDetailsByTag('module');
79
    }
80
81
    public function page()
82
    {
83
        return $this->belongsTo(Page::class, 'page_id');
84
    }
85
86
    /**
87
     * The page specific ones are the text modules
88
     * which are added via the page builder
89
     *
90
     * @param $query
91
     */
92
    public function scopeWithoutPageSpecific($query)
93
    {
94
        $query->whereNull('page_id');
95
    }
96
97
    public function isPageSpecific(): bool
98
    {
99
        return !is_null($this->page_id);
100
    }
101
102
    /**
103
     * Each page / Module model can expose some custom fields. Add here the list of fields defined as name => Field where Field
104
     * is an instance of \Thinktomorrow\Chief\Fields\Types\Field
105
     *
106
     * @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...
107
     * @return array
108
     */
109
    public function customFields()
110
    {
111
        return [];
112
    }
113
114
    /**
115
     * Each module model can expose the managed translatable fields. These should be included as attributes just like the regular
116
     * translatable attributes. This method allows for easy installation of the form fields in chief.
117
     *
118
     * @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...
119
     * @return array
120
     */
121
    final public static function translatableFields($key = null)
122
    {
123
        $translatableFields = array_merge(static::defaultTranslatableFields(), static::customTranslatableFields());
124
125
        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...
Deprecated Code introduced by
The function array_pluck() has been deprecated: Arr::pluck() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

125
        return $key ? /** @scrutinizer ignore-deprecated */ array_pluck($translatableFields, $key) : $translatableFields;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
126
    }
127
128
    /**
129
     * The custom addition of fields for a module model.
130
     *
131
     * To add a field, you should:
132
     * 1. override this method with your own and return the comprised list of fields.
133
     * 2. Setup the proper migrations and add the new field to the translatable values of the collection.
134
     *
135
     * @return array
136
     */
137
    public static function customTranslatableFields(): array
138
    {
139
        return [];
140
    }
141
142
    /**
143
     * The default set of fields for a module model.
144
     *
145
     * If you wish to remove any of these fields, you should:
146
     * 1. override this method with your own and return the comprised list of fields.
147
     * 2. Provide a migration to remove the column from database and remove the fields from the translatable values of the model.
148
     *
149
     * @return array
150
     */
151
    public static function defaultTranslatableFields(): array
152
    {
153
        return [
154
            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

154
            InputField::make('title')->/** @scrutinizer ignore-call */ label('titel'),
Loading history...
155
            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

155
            HtmlField::make('content')->/** @scrutinizer ignore-call */ label('Inhoud'),
Loading history...
156
        ];
157
    }
158
159
    public function mediaUrls($type = null, $size = 'full'): Collection
160
    {
161
        return $this->getAllFiles($type)->map->getFileUrl($size);
162
    }
163
164
    public function mediaUrl($type = null, $size = 'full'): ?string
165
    {
166
        return $this->mediaUrls($type, $size)->first();
167
    }
168
169
    public static function mediaFields($key = null)
170
    {
171
        $types = [
172
//            MediaType::BACKGROUND => [
173
//                'type' => MediaType::BACKGROUND,
174
//                'label' => 'Achtergrond afbeelding',
175
//                'description' => '',
176
//            ]
177
        ];
178
179
        return $key ? array_pluck($types, $key) : $types;
0 ignored issues
show
Deprecated Code introduced by
The function array_pluck() has been deprecated: Arr::pluck() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

179
        return $key ? /** @scrutinizer ignore-deprecated */ array_pluck($types, $key) : $types;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
180
    }
181
182
    public static function findByInternalTitle($internal_title)
183
    {
184
        return static::where('internal_title', $internal_title)->first();
185
    }
186
187
    public function flatReference(): FlatReference
188
    {
189
        return new FlatReference(static::class, $this->id);
190
    }
191
192
    public function flatReferenceLabel(): string
193
    {
194
        return $this->internal_title ?? '';
195
    }
196
197
    public function flatReferenceGroup(): string
198
    {
199
        $classKey = get_class($this);
200
        $labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : str_singular($classKey);
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

200
        $labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : /** @scrutinizer ignore-deprecated */ str_singular($classKey);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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...
201
202
        return $labelSingular;
203
    }
204
}
205