FormFieldUpdate::labels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Admin\Profile;
4
5
use Apps\ActiveRecord\ProfileField;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Model;
8
9
/**
10
 * Class FormFieldUpdate. Update additional field business logic model
11
 * @package Apps\Model\Admin\Profile
12
 */
13
class FormFieldUpdate extends Model
14
{
15
    public $type;
16
    public $name;
17
    public $reg_exp;
18
    public $reg_cond;
19
20
    private $_record;
21
22
    /**
23
     * FormFieldUpdate constructor. Pass profile field record inside
24
     * @param ProfileField $record
25
     */
26
    public function __construct(ProfileField $record)
27
    {
28
        $this->_record = $record;
29
        parent::__construct();
30
    }
31
32
    /**
33
     * Set defaults values
34
     */
35
    public function before()
36
    {
37
        foreach ($this->_record->toArray() as $property => $value) {
38
            if (property_exists($this, $property)) {
39
                $this->{$property} = $value;
40
            }
41
        }
42
    }
43
44
    /**
45
     * Forum display labels
46
     * @return array
47
     */
48
    public function labels(): array
49
    {
50
        return [
51
            'name' => __('Title'),
52
            'type' => __('Type'),
53
            'reg_exp' => __('Validation regexp'),
54
            'reg_cond' => __('Validation direction')
55
        ];
56
    }
57
58
    /**
59
     * Validation rules
60
     * @return array
61
     */
62
    public function rules(): array
63
    {
64
        $rules = [
65
            [['type', 'reg_exp', 'reg_cond'], 'required'],
66
            ['reg_cond', 'boolean']
67
        ];
68
69
        foreach (App::$Properties->get('languages') as $lang) {
70
            $rules[] = ['name.' . $lang, 'required'];
71
            $rules[] = ['name.' . $lang, 'length_max', 50];
72
        }
73
74
        return $rules;
75
    }
76
77
    /**
78
     * Save data to db
79
     */
80
    public function save()
81
    {
82
        $this->_record->name = $this->name;
83
        $this->_record->reg_exp = $this->reg_exp;
84
        $this->_record->reg_cond = $this->reg_cond;
85
        $this->_record->type = $this->type;
86
        $this->_record->save();
87
    }
88
89
    /**
90
     * Delete record from db
91
     * @throws \Exception
92
     */
93
    public function delete()
94
    {
95
        $this->_record->delete();
96
    }
97
}
98