Issues (1313)

models/Contact.php (1 issue)

Severity
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord};
7
8
/**
9
 * This is the model class for table "contacts".
10
 *
11
 * @property int $id
12
 * @property int $default
13
 * @property string $created_at
14
 * @property string $updated_at
15
 * @property string $mapQ
16
 * @property int $mapZoom
17
 *
18
 * @property ContactLanguage[] $contactsLanguages
19
 * @property Language[] $languages
20
 * @property ContactSocial[] $contactSocial
21
 * @property Social[] $social
22
 *
23
 * @package app\models
24
 */
25
class Contact extends ActiveRecord
26
{
27
    use MultilanguageTrait;
0 ignored issues
show
The trait Itstructure\AdminModule\models\MultilanguageTrait requires some properties which are not provided by app\models\Contact: $language, $shortName
Loading history...
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public static function tableName()
33
    {
34
        return 'contacts';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function rules()
41
    {
42
        return [
43
            [
44
                [
45
                    'default',
46
                    'mapZoom'
47
                ],
48
                'integer'
49
            ],
50
            [
51
                'mapQ',
52
                'string',
53
                'max' => 128
54
            ],
55
            [
56
                [
57
                    'created_at',
58
                    'updated_at'
59
                ],
60
                'safe'
61
            ],
62
        ];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function attributeLabels()
69
    {
70
        return [
71
            'id' => 'ID',
72
            'default' => Yii::t('app', 'Default'),
73
            'created_at' => Yii::t('app', 'Created date'),
74
            'updated_at' => Yii::t('app', 'Updated date'),
75
            'mapQ' => Yii::t('contacts', 'Map place'),
76
            'mapZoom' => Yii::t('contacts', 'Map zoom'),
77
        ];
78
    }
79
80
    /**
81
     * Returns the default contacts record.
82
     *
83
     * @return array|null|\yii\db\ActiveRecord
84
     */
85
    public static function getDefaultContacts()
86
    {
87
        return static::find()
88
            ->where([
89
                'default' => 1
90
            ])
91
            ->one();
92
    }
93
94
    /**
95
     * Reset the default contacts record.
96
     *
97
     * @param boolean $insert
98
     *
99
     * @return mixed
100
     */
101
    public function beforeSave($insert)
102
    {
103
        if ($this->default == 1) {
104
105
            $default = static::findOne([
106
                'default' => 1,
107
            ]);
108
109
            if (null !== $default) {
110
                $default->default = 0;
111
                $default->save();
112
            }
113
        }
114
115
        return parent::beforeSave($insert);
116
    }
117
118
    /**
119
     * @return \yii\db\ActiveQuery
120
     */
121
    public function getContactsLanguages()
122
    {
123
        return $this->hasMany(ContactLanguage::class, [
124
            'contacts_id' => 'id'
125
        ]);
126
    }
127
128
    /**
129
     * @return \yii\db\ActiveQuery
130
     */
131
    public function getLanguages()
132
    {
133
        return $this->hasMany(Language::class, [
134
            'id' => 'language_id'
135
        ])->viaTable('contacts_language', [
136
            'contacts_id' => 'id'
137
        ]);
138
    }
139
140
    /**
141
     * @return \yii\db\ActiveQuery
142
     */
143
    public function getContactSocial()
144
    {
145
        return $this->hasMany(ContactSocial::class, [
146
            'contacts_id' => 'id'
147
        ]);
148
    }
149
150
    /**
151
     * @return \yii\db\ActiveQuery
152
     */
153
    public function getSocial()
154
    {
155
        return $this->hasMany(Social::class, [
156
            'id' => 'social_id'
157
        ])->viaTable('contacts_social', [
158
            'contacts_id' => 'id'
159
        ]);
160
    }
161
}
162