forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.php
More file actions
executable file
·362 lines (294 loc) · 8.77 KB
/
Project.php
File metadata and controls
executable file
·362 lines (294 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
namespace app\models;
use app\widgets\LinkPager;
use Yii;
use yii\data\Pagination;
/**
* This is the model class for table "doc_project".
*
* @property int $id
* @property string $encode_id 加密id
* @property string $title 项目名称
* @property string $remark 项目描述
* @property int $sort 项目排序
* @property int $type 项目类型
* @property int $status 项目状态
* @property int $creater_id 创建者id
* @property int $updater_id 更新者id
* @property string $created_at 创建时间
* @property string $updated_at 更新时间
*/
class Project extends Model
{
const PUBLIC_TYPE = 10;
const AUTH_TYPE = 20;
const PRIVATE_TYPE = 30;
/**
* 绑定数据表
*/
public static function tableName()
{
return '{{%project}}';
}
/**
* 验证规则
*/
public function rules()
{
return [
[['encode_id', 'title', 'status', 'creater_id'], 'required'],
[['type', 'sort'], 'filter', 'filter' => 'intval'], //此规则必须,否则就算模型里该字段没有修改,也会出现在脏属性里
[['encode_id'], 'string', 'max' => 50],
[['title', 'remark'], 'string', 'max' => 250],
[['encode_id'], 'unique'],
[['created_at', 'updated_at'], 'safe'],
];
}
/**
* 字段字典
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'encode_id' => '加密id',
'title' => '项目名称',
'remark' => '项目描述',
'sort' => '项目排序',
'allow_search' => '搜索状态',
'status' => '项目状态',
'creater_id' => '创建者id',
'updater_id' => '更新者id',
'created_at' => '创建时间',
'updated_at' => '更新时间',
];
}
/**
* 项目所有类型标签
* @return array
*/
public function getTypeLabels()
{
return [
self::PUBLIC_TYPE => '公开项目',
// self::AUTH_TYPE => '授权项目',
self::PRIVATE_TYPE => '私有项目',
];
}
/**
* 获取当前项目类型标签
* @return mixed
*/
public function getTypeLabel()
{
return $this->getTypeLabels()[$this->type];
}
/**
* 获取项目环境
* @return \yii\db\ActiveQuery
*/
public function getEnvs()
{
$filter = ['status' => Env::ACTIVE_STATUS];
$order = [
'sort' =>SORT_DESC,
'id' => SORT_ASC
];
return $this->hasMany(Env::className(), ['project_id' => 'id'])->where($filter)->orderBy($order);
}
/**
* 获取项目模块
* @return \yii\db\ActiveQuery
*/
public function getModules()
{
$filter = [
'status' => Module::ACTIVE_STATUS
];
$order = [
'sort' =>SORT_DESC,
'id' => SORT_DESC
];
return $this->hasMany(Module::className(), ['project_id' => 'id'])->where($filter)->orderBy($order);
}
/**
* 获取项目接口
* @return \yii\db\ActiveQuery
*/
public function getApis()
{
$filter = [
'status' => Api::ACTIVE_STATUS
];
$order = [
'sort' =>SORT_DESC,
'id' => SORT_DESC
];
return $this->hasMany(Api::className(), ['project_id' => 'id'])->where($filter)->orderBy($order);
}
/**
* 获取项目成员
* @return \yii\db\ActiveQuery
*/
public function getMembers()
{
$order = ['id' => SORT_DESC];
return $this->hasMany(Member::className(), ['project_id' => 'id'])->orderBy($order);
}
/**
* 获取非项目成员(不包括自己)
* @return \yii\db\ActiveQuery
*/
public function getNotMembers($param = [])
{
$member_ids = $this->getMembers()->select('user_id')->column();
$query = Account::find();
$query->andFilterWhere(['status' => Account::ACTIVE_STATUS]);
$query->andFilterWhere(['not in', 'id', $member_ids])
->andFilterWhere(['<>', 'id', $this->creater_id]);
$query->andFilterWhere([
'or',
['like','name', $param['name']],
['like','email', $param['name']],
]);
$sql = $query->createCommand()->getRawSql();
return $query->all();
}
/**
* 判断是否是项目创建者
* @return bool
*/
public function isCreater($user_id = 0)
{
$user_id = (int)$user_id ? $user_id : Yii::$app->user->identity->id;
return $this->creater_id == $user_id ? true : false;
}
/**
* 判断是否是项目参与者
* @return bool
*/
public function isJoiner($user_id = 0)
{
$user_id = (int)$user_id ? $user_id : Yii::$app->user->identity->id;
$query = Member::find()->where(['project_id' => $this->id, 'user_id' => $user_id]);
return $query->exists() ? true : false;
}
/**
* 判断是否是公开项目
* @return bool
*/
public function isPublic()
{
return $this->type == self::PUBLIC_TYPE ? true : false;
}
/**
* 判断是否是私有项目
* @return bool
*/
public function isPrivate()
{
return $this->type == self::PRIVATE_TYPE ? true : false;
}
/**
* 获取项目地址
* @return string
*/
public function getUrl($scheme = false)
{
return url('home/project/show', ['id' => $this->encode_id], $scheme);
}
/**
* 获取项目身份
* @param int $user_id
* @return string
*/
public function getIdentity($user_id = 0)
{
if($this->isCreater($user_id)){
return '创建者';
}
if($this->isJoiner($user_id)){
return '项目成员';
}
return '普通游客';
}
/**
* 检测是否有操作权限
* @param $rule
* @return bool
*/
public function hasRule($type, $rule, $user_id = 0)
{
$user_id = (int)$user_id ? $user_id : Yii::$app->user->identity->id;
$account = Account::findModel($user_id);
// 系统管理员拥有一切权限
if($account->isAdmin){
return true;
}
// 项目创建者拥有所有权限
if($this->isCreater($user_id)){
return true;
}
$member = Member::findOne(['project_id' => $this->id, 'user_id' => $user_id]);
if(!$member->id){
return false;
}
if($member->hasRule($type, $rule)){
return true;
}
return false;
}
/**
* 项目搜索
* @param array $params
* @return $this
* @throws \Exception
*/
public function search($params = [])
{
$this->params = array2object($params);
if(!$this->params->orderBy){
$this->params->orderBy = 'id desc';
}
$query = self::find()->joinWith('creater');
$query->andFilterWhere([
'{{%project}}.creater_id' => $this->params->creater_id,
]);
$this->params->status && $query->andFilterWhere([
'{{%project}}.status' => $this->params->status,
]);
$this->params->type && $query->andFilterWhere(['in', '{{%project}}.type', $this->params->type]);
$query->andFilterWhere(['like', '{{%project}}.title', $this->params->title]);
$this->params->start_date && $query->andFilterWhere(['>=', '{{%project}}.created_at', $this->params->start_date . ' 00:00:00']);
$this->params->end_date && $query->andFilterWhere(['<=', '{{%project}}.created_at', $this->params->end_date . ' 23:59:59']);
$query->andFilterWhere([
'or',
['like','{{%user}}.name', $this->params->user->name],
['like','{{%user}}.email', $this->params->user->name],
]);
$this->count = $query->count();
$pagination = new Pagination([
'pageSizeParam' => false,
'totalCount' => $this->count,
'pageSize' => $this->pageSize,
'validatePage' => false,
]);
$this->models = $query
->offset($pagination->offset)
->limit($pagination->limit)
->orderBy($this->params->orderBy)
->all();
$this->sql = $query->createCommand()->getRawSql();
// dump($this->sql);
$this->pages = LinkPager::widget([
'pagination' => $pagination,
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
'hideOnSinglePage' => true,
'maxButtonCount' => 5,
]);
return $this;
}
}