1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is the model class for table "contacts". |
9
|
|
|
* |
10
|
|
|
* @property int $id |
11
|
|
|
* @property int $default |
12
|
|
|
* @property string $title |
13
|
|
|
* @property string $address |
14
|
|
|
* @property string $email |
15
|
|
|
* @property string $phone |
16
|
|
|
* @property string $metaKeys |
17
|
|
|
* @property string $metaDescription |
18
|
|
|
* @property string $created_at |
19
|
|
|
* @property string $updated_at |
20
|
|
|
* @property string $mapQ |
21
|
|
|
* @property int $mapZoom |
22
|
|
|
* |
23
|
|
|
* @property ContactSocial[] $contactSocial |
24
|
|
|
* @property Social[] $social |
25
|
|
|
* |
26
|
|
|
* @package app\models |
27
|
|
|
*/ |
28
|
|
|
class Contact extends ActiveRecord |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public static function tableName() |
34
|
|
|
{ |
35
|
|
|
return 'contacts'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function rules() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
[ |
45
|
|
|
[ |
46
|
|
|
'title', |
47
|
|
|
'metaKeys', |
48
|
|
|
'metaDescription' |
49
|
|
|
], |
50
|
|
|
'required' |
51
|
|
|
], |
52
|
|
|
[ |
53
|
|
|
[ |
54
|
|
|
'title', |
55
|
|
|
'metaKeys', |
56
|
|
|
'address', |
57
|
|
|
'email', |
58
|
|
|
'phone', |
59
|
|
|
'mapQ', |
60
|
|
|
], |
61
|
|
|
'string', |
62
|
|
|
'max' => 128 |
63
|
|
|
], |
64
|
|
|
[ |
65
|
|
|
'metaDescription', |
66
|
|
|
'string', |
67
|
|
|
'max' => 255 |
68
|
|
|
], |
69
|
|
|
[ |
70
|
|
|
[ |
71
|
|
|
'default', |
72
|
|
|
'mapZoom' |
73
|
|
|
], |
74
|
|
|
'integer' |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
'title', |
78
|
|
|
'unique', |
79
|
|
|
'skipOnError' => true, |
80
|
|
|
'targetClass' => static::class, |
81
|
|
|
'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
'created_at', |
86
|
|
|
'updated_at' |
87
|
|
|
], |
88
|
|
|
'safe' |
89
|
|
|
], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function attributeLabels() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'id' => 'ID', |
100
|
|
|
'default' => Yii::t('app', 'Default'), |
101
|
|
|
'title' => Yii::t('contacts', 'Title'), |
102
|
|
|
'address' => Yii::t('contacts', 'Address'), |
103
|
|
|
'email' => Yii::t('contacts', 'Email'), |
104
|
|
|
'phone' => Yii::t('contacts', 'Phone'), |
105
|
|
|
'metaKeys' => Yii::t('app', 'Meta keys'), |
106
|
|
|
'metaDescription' => Yii::t('app', 'Meta description'), |
107
|
|
|
'created_at' => Yii::t('app', 'Created date'), |
108
|
|
|
'updated_at' => Yii::t('app', 'Updated date'), |
109
|
|
|
'mapQ' => Yii::t('contacts', 'Map place'), |
110
|
|
|
'mapZoom' => Yii::t('contacts', 'Map zoom'), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the default contacts record. |
116
|
|
|
* |
117
|
|
|
* @return null|\yii\db\ActiveRecord |
118
|
|
|
*/ |
119
|
|
|
public static function getDefaultContacts() |
120
|
|
|
{ |
121
|
|
|
return static::find() |
122
|
|
|
->where([ |
123
|
|
|
'default' => 1 |
124
|
|
|
]) |
125
|
|
|
->one(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Reset the default contacts record. |
130
|
|
|
* |
131
|
|
|
* @param boolean $insert |
132
|
|
|
* |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function beforeSave($insert) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
if ($this->default == 1) { |
138
|
|
|
|
139
|
|
|
$default = static::findOne([ |
140
|
|
|
'default' => 1, |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
if (null !== $default) { |
144
|
|
|
$default->default = 0; |
145
|
|
|
$default->save(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return parent::beforeSave($insert); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return \yii\db\ActiveQuery |
154
|
|
|
*/ |
155
|
|
|
public function getContactSocial() |
156
|
|
|
{ |
157
|
|
|
return $this->hasMany(ContactSocial::class, [ |
158
|
|
|
'contacts_id' => 'id' |
159
|
|
|
]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return \yii\db\ActiveQuery |
164
|
|
|
*/ |
165
|
|
|
public function getSocial() |
166
|
|
|
{ |
167
|
|
|
return $this->hasMany(Social::class, [ |
168
|
|
|
'id' => 'social_id' |
169
|
|
|
])->viaTable('contacts_social', [ |
170
|
|
|
'contacts_id' => 'id' |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.