|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Ajax\Web\Planning; |
|
4
|
|
|
|
|
5
|
|
|
use App\Ajax\CallableClass; |
|
6
|
|
|
use App\Ajax\Web\Planning\Pool; |
|
7
|
|
|
use Siak\Tontine\Model\Pool as PoolModel; |
|
8
|
|
|
use Siak\Tontine\Model\Session as SessionModel; |
|
9
|
|
|
use Siak\Tontine\Service\Planning\SessionService; |
|
10
|
|
|
use Siak\Tontine\Service\Planning\PoolService; |
|
11
|
|
|
use Siak\Tontine\Validation\Planning\PoolRoundValidator; |
|
12
|
|
|
|
|
13
|
|
|
use function Jaxon\pm; |
|
14
|
|
|
use function trans; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @databag pool.round |
|
18
|
|
|
* @before getPool |
|
19
|
|
|
*/ |
|
20
|
|
|
class PoolRound extends CallableClass |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var PoolRoundValidator |
|
24
|
|
|
*/ |
|
25
|
|
|
protected PoolRoundValidator $validator; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var PoolModel|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected ?PoolModel $pool = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The constructor |
|
34
|
|
|
* |
|
35
|
|
|
* @param SessionService $sessionService |
|
36
|
|
|
* @param PoolService $poolService |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(private SessionService $sessionService, |
|
39
|
|
|
private PoolService $poolService) |
|
40
|
|
|
{} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getPool() |
|
46
|
|
|
{ |
|
47
|
|
|
$poolId = $this->target()->method() === 'home' ? $this->target()->args()[0] : |
|
48
|
|
|
(int)$this->bag('pool.round')->get('pool.id'); |
|
49
|
|
|
$this->pool = $this->poolService->getPool($poolId); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function showTitles() |
|
53
|
|
|
{ |
|
54
|
|
|
$html = $this->render('pages.planning.pool.round.actions', [ |
|
55
|
|
|
'pool' => $this->pool, |
|
56
|
|
|
]); |
|
57
|
|
|
$this->response->html('pool-round-actions', $html); |
|
|
|
|
|
|
58
|
|
|
$this->jq('#btn-pool-round-back')->click($this->cl(Pool::class)->rq()->home()); |
|
59
|
|
|
$this->jq('#btn-pool-round-save')->click($this->rq()->saveRound(pm()->form('round-form'))); |
|
60
|
|
|
if($this->pool->pool_round) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->jq('#btn-pool-round-delete')->click($this->rq()->deleteRound() |
|
63
|
|
|
->confirm(trans('tontine.pool_round.questions.delete'))); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$defaultLabel = trans('tontine.pool_round.labels.default'); |
|
67
|
|
|
$startSession = !$this->pool->pool_round ? $defaultLabel : $this->pool->start_date; |
|
68
|
|
|
$endSession = !$this->pool->pool_round ? $defaultLabel : $this->pool->end_date; |
|
69
|
|
|
$this->response->html('pool-round-start-session-title', |
|
70
|
|
|
trans('tontine.pool_round.titles.start_session', ['session' => $startSession])); |
|
71
|
|
|
$this->response->html('pool-round-end-session-title', |
|
72
|
|
|
trans('tontine.pool_round.titles.end_session', ['session' => $endSession])); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getSessionPageNumber(SessionModel $session): int |
|
76
|
|
|
{ |
|
77
|
|
|
$sessionCount = $this->sessionService->getTontineSessionCount($session, true, false); |
|
78
|
|
|
return (int)($sessionCount / $this->tenantService->getLimit()) + 1; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function home(int $poolId) |
|
82
|
|
|
{ |
|
83
|
|
|
if(!$this->pool) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->response; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$html = $this->render('pages.planning.pool.round.home', [ |
|
89
|
|
|
'pool' => $this->pool, |
|
90
|
|
|
]); |
|
91
|
|
|
$this->response->html('content-home', $html); |
|
|
|
|
|
|
92
|
|
|
$this->showTitles(); |
|
93
|
|
|
$this->jq('#btn-show-start-session-page')->click($this->rq()->showStartSessionPage()); |
|
94
|
|
|
$this->jq('#btn-show-end-session-page')->click($this->rq()->showEndSessionPage()); |
|
95
|
|
|
|
|
96
|
|
|
$this->bag('pool.round')->set('pool.id', $poolId); |
|
97
|
|
|
|
|
98
|
|
|
$startPageNumber = 1; |
|
99
|
|
|
$endPageNumber = 1; |
|
100
|
|
|
if($this->pool->pool_round !== null) |
|
101
|
|
|
{ |
|
102
|
|
|
// Go to the pages of the round start and end sessions. |
|
103
|
|
|
$startPageNumber = $this->getSessionPageNumber($this->pool->pool_round->start_session); |
|
104
|
|
|
$endPageNumber = $this->getSessionPageNumber($this->pool->pool_round->end_session); |
|
105
|
|
|
} |
|
106
|
|
|
$this->showStartSession($startPageNumber); |
|
107
|
|
|
$this->showEndSession($endPageNumber); |
|
108
|
|
|
|
|
109
|
|
|
return $this->response; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @di $validator |
|
114
|
|
|
*/ |
|
115
|
|
|
public function saveRound(array $formValues) |
|
116
|
|
|
{ |
|
117
|
|
|
$values = $this->validator->validateItem($formValues); |
|
118
|
|
|
$this->poolService->saveRound($this->pool, $values); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
// Reload the pool |
|
121
|
|
|
$this->getPool(); |
|
122
|
|
|
$this->showTitles(); |
|
123
|
|
|
$this->notify->info(trans('tontine.pool_round.messages.saved')); |
|
124
|
|
|
|
|
125
|
|
|
return $this->response; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function deleteRound() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->poolService->deleteRound($this->pool); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
// Reload the pool |
|
133
|
|
|
$this->getPool(); |
|
134
|
|
|
$this->home($this->pool->id); |
|
135
|
|
|
$this->notify->info(trans('tontine.pool_round.messages.deleted')); |
|
136
|
|
|
|
|
137
|
|
|
return $this->response; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function showSession($request, int $sessionId, int $pageNumber, string $field) |
|
141
|
|
|
{ |
|
142
|
|
|
$sessionCount = $this->sessionService->getTontineSessionCount(); |
|
143
|
|
|
[$pageNumber, $perPage] = $this->pageNumber($pageNumber, $sessionCount, |
|
144
|
|
|
'pool.round', "session.$field.page"); |
|
145
|
|
|
$sessions = $this->sessionService->getTontineSessions($pageNumber, false); |
|
146
|
|
|
$pagination = $request->paginate($pageNumber, $perPage, $sessionCount); |
|
147
|
|
|
|
|
148
|
|
|
$html = $this->render('pages.planning.pool.round.sessions', [ |
|
149
|
|
|
'field' => $field, |
|
150
|
|
|
'sessions' => $sessions, |
|
151
|
|
|
'sessionId' => $sessionId, |
|
152
|
|
|
'pagination' => $pagination, |
|
153
|
|
|
]); |
|
154
|
|
|
$this->response->html("pool-round-sessions-$field", $html); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
return $this->response; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function showStartSession(int $pageNumber = 0) |
|
160
|
|
|
{ |
|
161
|
|
|
$sessionId = $this->pool->pool_round ? $this->pool->pool_round->start_session_id : 0; |
|
162
|
|
|
$request = $this->rq()->showStartSession(); |
|
163
|
|
|
return $this->showSession($request, $sessionId, $pageNumber, 'start'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function showEndSession(int $pageNumber = 0) |
|
167
|
|
|
{ |
|
168
|
|
|
$sessionId = $this->pool->pool_round ? $this->pool->pool_round->end_session_id : 0; |
|
169
|
|
|
$request = $this->rq()->showEndSession(); |
|
170
|
|
|
return $this->showSession($request, $sessionId, $pageNumber, 'end'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function showStartSessionPage() |
|
174
|
|
|
{ |
|
175
|
|
|
if(!$this->pool->pool_round) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->response; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$sessionId = $this->pool->pool_round->start_session_id; |
|
181
|
|
|
$pageNumber = $this->getSessionPageNumber($this->pool->pool_round->start_session); |
|
182
|
|
|
$request = $this->rq()->showStartSession(); |
|
183
|
|
|
return $this->showSession($request, $sessionId, $pageNumber, 'start'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function showEndSessionPage() |
|
187
|
|
|
{ |
|
188
|
|
|
if(!$this->pool->pool_round) |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->response; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$sessionId = $this->pool->pool_round->end_session_id; |
|
194
|
|
|
$pageNumber = $this->getSessionPageNumber($this->pool->pool_round->end_session); |
|
195
|
|
|
$request = $this->rq()->showEndSession(); |
|
196
|
|
|
return $this->showSession($request, $sessionId, $pageNumber, 'end'); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|