Completed
Push — master ( 63bf48...9f04eb )
by Abdelrahman
01:16 queued 11s
created

Form::deactivate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Forms\Models;
6
7
use Spatie\Sluggable\SlugOptions;
8
use Rinvex\Support\Traits\HasSlug;
9
use Illuminate\Database\Eloquent\Model;
10
use Rinvex\Cacheable\CacheableEloquent;
11
use Rinvex\Support\Traits\HasTranslations;
12
use Rinvex\Support\Traits\ValidatingTrait;
13
use Illuminate\Database\Eloquent\Relations\HasMany;
14
use Illuminate\Database\Eloquent\Relations\MorphTo;
15
16
/**
17
 * Rinvex\Forms\Models\Form.
18
 *
19
 * @property int                                                                             $id
20
 * @property int                                                                             $entity_id
21
 * @property string                                                                          $entity_type
22
 * @property string                                                                          $slug
23
 * @property string                                                                          $name
24
 * @property string                                                                          $description
25
 * @property array                                                                           $content
26
 * @property array                                                                           $actions
27
 * @property array                                                                           $submission
28
 * @property bool                                                                         $is_active
29
 * @property bool                                                                         $is_public
30
 * @property \Carbon\Carbon|null                                                             $created_at
31
 * @property \Carbon\Carbon|null                                                             $updated_at
32
 * @property \Carbon\Carbon|null                                                             $deleted_at
33
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent                              $entity
34
 *
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereActions($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereContent($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereCreatedAt($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereDescription($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereEntityId($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereEntityType($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereIsActive($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereIsPublic($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereName($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereSlug($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereSubmission($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereUpdatedAt($value)
47
 * @mixin \Eloquent
48
 */
49
class Form extends Model
50
{
51
    use HasSlug;
52
    use ValidatingTrait;
53
    use HasTranslations;
54
    use CacheableEloquent;
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected $fillable = [
60
        'entity_id',
61
        'entity_type',
62
        'slug',
63
        'name',
64
        'description',
65
        'content',
66
        'actions',
67
        'submission',
68
        'is_active',
69
        'is_public',
70
    ];
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected $casts = [
76
        'entity_id' => 'integer',
77
        'entity_type' => 'string',
78
        'slug' => 'string',
79
        'name' => 'string',
80
        'description' => 'string',
81
        'content' => 'json',
82
        'actions' => 'json',
83
        'submission' => 'json',
84
        'is_active' => 'boolean',
85
        'is_public' => 'boolean',
86
        'deleted_at' => 'datetime',
87
    ];
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected $observables = [
93
        'validating',
94
        'validated',
95
    ];
96
97
    /**
98
     * The attributes that are translatable.
99
     *
100
     * @var array
101
     */
102
    public $translatable = [
103
        'name',
104
        'description',
105
    ];
106
107
    /**
108
     * The default rules that the model will validate against.
109
     *
110
     * @var array
111
     */
112
    protected $rules = [
113
        'entity_id' => 'nullable|integer',
114
        'entity_type' => 'nullable|string|strip_tags|max:150',
115
        'slug' => 'required|string|strip_tags|max:150',
116
        'name' => 'required|string|strip_tags|max:150',
117
        'description' => 'nullable|string|max:10000',
118
        'content' => 'required|array',
119
        'actions' => 'nullable|array',
120
        'submission' => 'nullable|array',
121
        'is_active' => 'sometimes|boolean',
122
        'is_public' => 'sometimes|boolean',
123
    ];
124
125
    /**
126
     * Whether the model should throw a
127
     * ValidationException if it fails validation.
128
     *
129
     * @var bool
130
     */
131
    protected $throwValidationExceptions = true;
132
133
    /**
134
     * Create a new Eloquent model instance.
135
     *
136
     * @param array $attributes
137
     */
138
    public function __construct(array $attributes = [])
139
    {
140
        parent::__construct($attributes);
141
142
        $this->setTable(config('rinvex.forms.tables.forms'));
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    protected static function boot()
149
    {
150
        parent::boot();
151
152
        static::deleted(function (self $model) {
153
            $model->responses()->delete();
154
        });
155
    }
156
157
    /**
158
     * Get the options for generating the slug.
159
     *
160
     * @return \Spatie\Sluggable\SlugOptions
161
     */
162
    public function getSlugOptions(): SlugOptions
163
    {
164
        return SlugOptions::create()
165
                          ->doNotGenerateSlugsOnUpdate()
166
                          ->generateSlugsFrom('name')
167
                          ->saveSlugsTo('slug');
168
    }
169
170
    /**
171
     * The form may have many responses.
172
     *
173
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
174
     */
175
    public function responses(): HasMany
176
    {
177
        return $this->hasMany(config('rinvex.forms.models.form_response'), 'form_id', 'id');
178
    }
179
180
    /**
181
     * Get the owner model of the form.
182
     *
183
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
184
     */
185
    public function entity(): MorphTo
186
    {
187
        return $this->morphTo('entity', 'entity_type', 'entity_id', 'id');
188
    }
189
190
    /**
191
     * Activate the form.
192
     *
193
     * @return $this
194
     */
195
    public function activate()
196
    {
197
        $this->update(['is_active' => true]);
198
199
        return $this;
200
    }
201
202
    /**
203
     * Deactivate the form.
204
     *
205
     * @return $this
206
     */
207
    public function deactivate()
208
    {
209
        $this->update(['is_active' => false]);
210
211
        return $this;
212
    }
213
}
214