This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.php
100 lines (81 loc) · 2.65 KB
/
Board.php
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
<?php
class Board extends Eloquent
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'boards';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [];
protected $fillable = ['type', 'code', 'description'];
# Covert code to id
public static function unified2Id($list)
{
foreach ($list as $key => $value) {
if (!is_numeric($value)) {
$board = Board::where('code', $value)->first();
if ($board != null) {
$list[$key] = $board->id;
} else {
unset($list[$key]);
}
}
}
return $list;
}
# If board is using, return relative record id, or return false
public function getUsingStatus($from = '', $end = '')
{
if (empty($from) or empty($end)) {
$from = date("Y-m-d");
$end = date("Y-m-d");
}
if ($this->type == 'stairs') {
if (Auth::check()) {
$record = ApplyRecord::whereInterDateRange($from, $end)
->where('board_id', $this->id)
->where('user_id', Auth::id())
->first();
return is_null($record) ? false : $record->id;
}
return false;
}
$record = ApplyRecord::whereInterDateRange($from, $end)
->where('board_id', '=', $this->id)
->orderBy('post_from', 'desc')
->first();
return is_null($record) ? false : $record->id;
}
public function scopeWhereCode($query, $code)
{
return $query->where('code', $code);
}
# Fetch board(s) which is being used
public function scopeWhereIsUsing($query, $from = '', $end = '')
{
if (empty($from) or empty($end)) {
$from = date("Y-m-d");
$end = date("Y-m-d");
}
$records = ApplyRecord::whereInterDateRange($from, $end);
$using_boards = array_unique($records->lists('board_id'));
return $query->whereIn('id', $using_boards);
}
# Fetch board(s) which is not being used
public function scopeWhereIsEmpty($query, $from = '', $end = '')
{
if (empty($from) or empty($end)) {
$from = date("Y-m-d");
$end = date("Y-m-d");
}
$records = ApplyRecord::whereInterDateRange($from, $end);
$using_boards = array_unique($records->lists('board_id'));
return $query->whereNotIn('id', $using_boards);
}
}