/
Nation.php
169 lines (157 loc) · 6.03 KB
/
Nation.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
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
<?php
/**
* Copyright (C) MUNPANEL
* This file is part of MUNPANEL System.
*
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
*
* Developed by Adam Yi <xuan@yiad.am>
*/
namespace App;
use Illuminate\Database\Eloquent\Model;
class Nation extends Model
{
protected $table='nations';
protected $fillable = ['committee_id', 'name', 'conpetence', 'veto_power', 'attendance', 'locked'];
public function committee()
{
return $this->belongsTo('App\Committee');
}
public function delegates()
{
return $this->hasMany('App\Delegate');
}
public function assignedDelegates()
{
return $this->belongsToMany('App\Delegate', 'delegate_nation', 'nation_id', 'delegate_id');
}
public function nationgroups()
{
return $this->belongstoMany('App\Nationgroup', 'nationgroup_nation');
}
public function delegateScope($withBizCard = false)
{
$prefix = '';
$scope = '';
if (isset($this->delegates))
{
$delegates = $this->delegates;
foreach($delegates as $delegate)
{
$scope .= $prefix;
// if ($withBizCard) $scope .= '<a href="'.mp_url('/delBizCard.modal/'.$delegate->user->id).'" class="details-modal" data-toggle="ajaxModal">';
$scope .= $delegate->reg->name();
// if ($withBizCard) $scope .= '</a>';
if ($withBizCard) $scope .= '<a style="cursor: pointer;" class="details-popover" data-html="1" data-placement="right" data-trigger="click" data-original-title="'.$delegate->reg->name().'" data-toggle="popover" data-content="'.view('delegateBizCard', ['delegate' => $delegate]).'"><i class="fa fa-phone-square fa-fw"></i></a>';
$prefix = ', ';
}
}
if ($scope != '')
return $scope;
return '无';
}
public function assignedDelegateScope($withBizCard = false)
{
$prefix = '';
$scope = '';
$delegates = $this->assignedDelegates;
if ($delegates->count() > 0)
{
foreach($delegates as $delegate)
{
$scope .= $prefix;
// if ($withBizCard) $scope .= '<a href="'.mp_url('/delBizCard.modal/'.$delegate->user->id).'" class="details-modal" data-toggle="ajaxModal">';
$scope .= $delegate->reg->name();
// if ($withBizCard) $scope .= '</a>';
if ($withBizCard) $scope .= '<a style="cursor: pointer;" class="details-popover" data-html="1" data-placement="right" data-trigger="click" data-original-title="'.$delegate->reg->name().'" data-toggle="popover" data-content="'.view('delegateBizCard', ['delegate' => $delegate]).'"><i class="fa fa-phone-square fa-fw"></i></a>';
$prefix = ', ';
}
}
if ($scope != '')
return $scope;
return '无';
}
public function scopeNationGroup($useShortName = false, $maxDisplay = 0)
{
$prefix = '';
$scope = '';
$i = 0;
$n = $this->nationgroups->count();
if ($n == 0) return '无';
if (isset($this->nationgroups))
{
$nationgroups = $this->nationgroups;
foreach($nationgroups as $nationgroup)
{
if ($useShortName)
$scope .= $prefix . $nationgroup->name;
else
$scope .= $prefix . $nationgroup->display_name;
if ($maxDisplay > 0 && ++$i >= $maxDisplay) break;
$prefix = ', ';
}
}
if ($maxDisplay > 0 && $n > $maxDisplay) $scope .= "等 ".$n." 个";
return $scope;
}
public function displayName($remark = true, $committee = 0)
{
$info = '';
if ($remark)
$info .= $this->remark;
if ($committee == 1) {
if ($info != '')
$info .= ', ';
$info .= $this->committee->name;
}
else if ($committee == 2) {
if ($info != '')
$info .= ', ';
$info .= $this->committee->display_name;
}
if ($info != '')
return $this->name . ' (' . $info . ')';
return $this->name;
}
// deprecated due to change of logic.
public function setLock($lock = true)
{
if ($lock)//Set lock of delegates first before calling this function. This design helps avoiding bugs.
{
$this->locked = true;
$this->save();
$delegates = $this->assignedDelegates;
foreach ($delegates as $delegate)
{
if (!$delegate->seat_locked)
{
$this->assignedDelegates()->detach($delegate);
$sms = '感谢您报名'.Reg::currentConference()->name.',由于其他代表的席位锁定,系统自动更新了您的可选席位列表,敬请留意。';
if ($delegate->nation_id == $this->id)
{
$delegate->nation_id = null;
$delegate->save();
$sms = '感谢您参加'.Reg::currentConference()->name.',很遗憾您之前选择的席位已被他人锁定。请您重新登录系统选择席位,感谢您的理解与支持。';
}
$delegate->reg->user->sendSMS($sms);
}
}
}
else
{
$this->locked = false;
$this->save();
$delegates = $this->assignedDelegates;
foreach ($delegates as $delegate)
{
if ($delegate->seat_locked)
{
$delegate->seat_locked = false;
$delegate->save();
$delegate->reg->user->sendSMS('感谢您报名'.Reg::currentConference()->name.',您的席位锁定状态已被取消。您现可登录系统修改您的席位选择,感谢。');
}
}
}
}
}