eb7e82fb
Administrator
29.02.16
|
1
2
|
<?php
|
38a6e1dd
Yarik
test
|
3
|
namespace common\models;
|
eb7e82fb
Administrator
29.02.16
|
4
|
|
38a6e1dd
Yarik
test
|
5
|
use Yii;
|
eb7e82fb
Administrator
29.02.16
|
6
|
|
eb7e82fb
Administrator
29.02.16
|
7
|
/**
|
38a6e1dd
Yarik
test
|
8
9
10
11
12
13
14
15
16
17
18
|
* This is the model class for table "job".
* @property integer $job_id
* @property string $name
* @property string $link
* @property string $date_start
* @property string $date_end
* @property string $position
* @property integer $user_id
* @property integer $total_count
* @property integer $complete_count
* @property integer $current
|
eb7e82fb
Administrator
29.02.16
|
19
|
*/
|
38a6e1dd
Yarik
test
|
20
|
class Job extends \yii\db\ActiveRecord
|
eb7e82fb
Administrator
29.02.16
|
21
|
{
|
9fd076e8
Administrator
01.03.16
|
22
|
|
38a6e1dd
Yarik
test
|
23
24
25
26
27
28
29
|
/**
* @inheritdoc
*/
public static function tableName()
{
return 'job';
}
|
9fd076e8
Administrator
01.03.16
|
30
|
|
38a6e1dd
Yarik
test
|
31
32
33
34
35
36
|
/**
* @inheritdoc
*/
public function behaviors()
{
return [
|
9fd076e8
Administrator
01.03.16
|
37
|
|
38a6e1dd
Yarik
test
|
38
|
];
|
9fd076e8
Administrator
01.03.16
|
39
40
|
}
|
38a6e1dd
Yarik
test
|
41
42
43
|
public function beforeSave($insert)
{
$this->date_start = \Yii::$app->formatter->asDatetime($this->date_start, 'Y-MM-d HH:mm:ss');
|
9fd076e8
Administrator
01.03.16
|
44
|
|
38a6e1dd
Yarik
test
|
45
46
47
|
if($this->date_end) {
$this->date_end = \Yii::$app->formatter->asDatetime($this->date_end, 'Y-MM-d HH:mm:ss');
}
|
eb7e82fb
Administrator
29.02.16
|
48
|
|
38a6e1dd
Yarik
test
|
49
50
|
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
|
eb7e82fb
Administrator
29.02.16
|
51
|
|
38a6e1dd
Yarik
test
|
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[ 'name' ],
'required',
],
[
[
'date_start',
'date_end',
],
'safe',
],
[
[
'user_id',
|
38a6e1dd
Yarik
test
|
72
73
74
75
76
77
78
79
80
81
82
83
84
|
'current',
],
'integer',
],
[
[
'name',
'link',
'position',
],
'string',
'max' => 255,
],
|
4b85f536
Yarik
test
|
85
86
87
88
89
90
91
|
[
[
'link',
],
'match',
'pattern' => '/^(?:https?:\/\/)?(?:w{3}\.)?'.preg_quote($_SERVER['HTTP_HOST']).'\/company\/\w+\/\d+$/i',
],
|
5d627502
Yarik
test
|
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
[
[
'total_count',
'complete_count',
],
'integer',
'min' => 0,
],
[
[
'complete_count',
],
'compare',
'compareAttribute' => 'total_count',
'operator' => '<=',
]
|
38a6e1dd
Yarik
test
|
108
|
];
|
eb7e82fb
Administrator
29.02.16
|
109
110
|
}
|
38a6e1dd
Yarik
test
|
111
112
|
public function getExpTime()
{
|
4b85f536
Yarik
test
|
113
114
115
116
117
|
if(!empty($this->date_start) && !empty($this->date_end)) {
$result = '';
$start = new \DateTime($this->date_start);
$end = new \DateTime($this->date_end);
$interval = $end->diff($start);
|
fd384984
Yarik
test
|
118
119
120
121
122
123
124
|
if($interval->y > 0) {
if($interval->y == 1) {
$result .= $interval->y.' год';
} elseif($interval->y > 1 && $interval->y <= 4) {
$result .= $interval->y.' года';
} else {
$result .= $interval->y.' лет';
|
4b85f536
Yarik
test
|
125
126
|
}
}
|
fd384984
Yarik
test
|
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
if($interval->m > 0) {
if(!empty($result)) {
$result .= ', ';
}
if($interval->m == 1) {
$result .= $interval->m.' месяц';
} elseif($interval->m > 1 && $interval->m <= 4) {
$result .= $interval->m.' месяца';
} else {
$result .= $interval->m.' месяцев';
}
}
if(empty($result) && $interval->d > 0) {
$result = 'Меньше месяца';
}
|
4b85f536
Yarik
test
|
142
|
} elseif(!empty($this->date_start)) {
|
fd384984
Yarik
test
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
$result = '';
$start = new \DateTime($this->date_start);
$end = new \DateTime();
$interval = $end->diff($start);
if($interval->y > 0) {
if($interval->y == 1) {
$result .= $interval->y.' год';
} elseif($interval->y > 1 && $interval->y <= 4) {
$result .= $interval->y.' года';
} else {
$result .= $interval->y.' лет';
}
}
if($interval->m > 0) {
if(!empty($result)) {
$result .= ', ';
}
if($interval->m == 1) {
$result .= $interval->m.' месяц';
} elseif($interval->m > 1 && $interval->m <= 4) {
$result .= $interval->m.' месяца';
} else {
$result .= $interval->m.' месяцев';
}
}
if(empty($result) && $interval->d > 0) {
$result = 'Меньше месяца';
}
|
38a6e1dd
Yarik
test
|
171
|
} else {
|
4b85f536
Yarik
test
|
172
|
return false;
|
38a6e1dd
Yarik
test
|
173
|
}
|
fd384984
Yarik
test
|
174
|
return $result;
|
4b85f536
Yarik
test
|
175
176
177
178
179
180
181
182
183
184
|
// if($this->date_end && $this->date_start) {
// $date = new \DateTime(date('Y-m-d H:i:s', $this->date_start));
// return \Yii::$app->formatter->asRelativeTime($date->diff(new \DateTime(date('Y-m-d H:i:s', $this->date_end))));
// } elseif($this->date_start) {
// $now = new \DateTime();
// $date = new \DateTime(date('Y-m-d H:i:s', strtotime($this->date_start)));
// return \Yii::$app->formatter->asRelativeTime($date->diff(new \DateTime()));
// } else {
// return 'неизвестна дата начала';
// }
|
38a6e1dd
Yarik
test
|
185
|
}
|
eb7e82fb
Administrator
29.02.16
|
186
|
|
06ec2844
Administrator
28.03.16
|
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'job_id' => Yii::t('app', 'job_id'),
'name' => Yii::t('app', 'name'),
'link' => Yii::t('app', 'link'),
'date_start' => Yii::t('app', 'date_start'),
'date_end' => Yii::t('app', 'date_end'),
'position' => Yii::t('app', 'position'),
'user_id' => Yii::t('app', 'user_id'),
'total_count' => Yii::t('app', 'total_count'),
'complete_count' => Yii::t('app', 'complete_count'),
'current' => Yii::t('app', 'current'),
];
|
eb7e82fb
Administrator
29.02.16
|
205
|
}
|
06ec2844
Administrator
28.03.16
|
206
|
}
|