Passed
Push — main ( 936852...10149e )
by Thierry
15:09
created

Session::addList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.7998
1
<?php
2
3
namespace App\Ajax\Web\Planning;
4
5
use App\Ajax\CallableClass;
6
use Siak\Tontine\Exception\MessageException;
7
use Siak\Tontine\Model\Session as SessionModel;
8
use Siak\Tontine\Service\Planning\SessionService;
9
use Siak\Tontine\Service\Tontine\MemberService;
10
use Siak\Tontine\Service\Tontine\TontineService;
11
use Siak\Tontine\Validation\Planning\SessionValidator;
12
13
use function Jaxon\jq;
14
use function Jaxon\pm;
15
use function array_filter;
16
use function array_map;
17
use function array_unique;
18
use function collect;
19
use function count;
20
use function explode;
21
use function trans;
22
use function trim;
23
24
/**
25
 * @databag planning
26
 */
27
class Session extends CallableClass
28
{
29
    /**
30
     * @var SessionValidator
31
     */
32
    protected SessionValidator $validator;
33
34
    public function __construct(private TontineService $tontineService,
35
        private MemberService $memberService, private SessionService $sessionService)
36
    {}
37
38
    /**
39
     * @exclude
40
     */
41
    public function show()
42
    {
43
        return $this->home();
44
    }
45
46
    public function home()
47
    {
48
        $round = $this->tenantService->round();
49
        $html = $this->render('pages.planning.session.home', ['round' => $round]);
50
        $this->response->html('section-title', trans('tontine.menus.planning'));
51
        $this->response->html('content-home-sessions', $html);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type null; however, parameter $sData of Jaxon\Response\Response::html() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->response->html('content-home-sessions', /** @scrutinizer ignore-type */ $html);
Loading history...
52
53
        if(!$round)
54
        {
55
            return $this->response;
56
        }
57
58
        $this->jq('#btn-sessions-refresh')->click($this->rq()->home());
59
        $this->jq('#btn-sessions-add')->click($this->rq()->add());
60
        $this->jq('#btn-sessions-add-list')->click($this->rq()->addList());
61
62
        return $this->page();
63
    }
64
65
    public function page(int $pageNumber = 0)
66
    {
67
        $sessionCount = $this->sessionService->getSessionCount();
68
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $sessionCount,
69
            'planning', 'session.page');
70
        $sessions = $this->sessionService->getSessions($pageNumber);
71
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $sessionCount);
72
73
        $statuses = [
74
            SessionModel::STATUS_PENDING => trans('tontine.session.status.pending'),
75
            SessionModel::STATUS_OPENED => trans('tontine.session.status.opened'),
76
            SessionModel::STATUS_CLOSED => trans('tontine.session.status.closed'),
77
        ];
78
79
        $html = $this->render('pages.planning.session.page')
80
            ->with('sessions', $sessions)
81
            ->with('statuses', $statuses)
82
            ->with('pagination', $pagination);
83
        $this->response->html('content-page-sessions', $html);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type null; however, parameter $sData of Jaxon\Response\Response::html() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $this->response->html('content-page-sessions', /** @scrutinizer ignore-type */ $html);
Loading history...
84
85
        $sessionId = jq()->parent()->attr('data-session-id')->toInt();
86
        $this->jq('.btn-session-edit')->click($this->rq()->edit($sessionId));
87
        $this->jq('.btn-session-venue')->click($this->rq()->editVenue($sessionId));
88
        $this->jq('.btn-session-delete')->click($this->rq()->delete($sessionId)
89
            ->confirm(trans('tontine.session.questions.delete')));
90
91
        return $this->response;
92
    }
93
94
    public function add()
95
    {
96
        $title = trans('tontine.session.titles.add');
97
        $content = $this->render('pages.planning.session.add')
98
            ->with('members', $this->memberService->getMemberList()->prepend('', 0));
99
        $buttons = [[
100
            'title' => trans('common.actions.cancel'),
101
            'class' => 'btn btn-tertiary',
102
            'click' => 'close',
103
        ],[
104
            'title' => trans('common.actions.save'),
105
            'class' => 'btn btn-primary',
106
            'click' => $this->rq()->create(pm()->form('session-form')),
107
        ]];
108
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
109
110
        return $this->response;
111
    }
112
113
    /**
114
     * @di $validator
115
     */
116
    public function create(array $formValues)
117
    {
118
        $values = $this->validator->validateItem($formValues);
119
        $this->sessionService->createSession($values);
120
        $this->dialog->hide();
121
        $this->notify->success(trans('tontine.session.messages.created'), trans('common.titles.success'));
122
123
        return $this->page();
124
    }
125
126
    public function addList()
127
    {
128
        $title = trans('tontine.session.titles.add-list');
129
        $content = $this->render('pages.planning.session.list');
130
        $buttons = [[
131
            'title' => trans('common.actions.cancel'),
132
            'class' => 'btn btn-tertiary',
133
            'click' => 'close',
134
        ],[
135
            'title' => trans('common.actions.year'),
136
            'class' => 'btn btn-primary',
137
            'click' => $this->rq()->years(),
138
        ],[
139
            'title' => trans('common.actions.save'),
140
            'class' => 'btn btn-primary',
141
            'click' => $this->rq()->createList(pm()->form('session-list')),
142
        ]];
143
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
144
145
        return $this->response;
146
    }
147
148
    public function years()
149
    {
150
        $sessions = $this->sessionService->getYearSessions();
151
        $html = collect($sessions)->map(function($session) {
0 ignored issues
show
Bug introduced by
$sessions of type array|stdClass[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
        $html = collect(/** @scrutinizer ignore-type */ $sessions)->map(function($session) {
Loading history...
152
            return $session->title . ';' . $session->date;
153
        })->join("\n");
154
        $this->response->html('new-sessions-list', $html);
155
156
        return $this->response;
157
    }
158
159
    /**
160
     * @param string $sessions
161
     *
162
     * @return array
163
     */
164
    private function parseSessionList(string $sessions): array
165
    {
166
        $sessions = array_map(function($value) {
167
            if(!($value = trim($value, " \t\n\r;")))
168
            {
169
                return [];
170
            }
171
            $values = explode(";", $value);
172
            if(count($values) !== 2)
173
            {
174
                return []; // Todo: throw an exception
175
            }
176
            return [
177
                'title' => trim($values[0]),
178
                'date' => trim($values[1]),
179
                'start' => '00:00',
180
                'end' => '00:00',
181
                'host_id' => 0,
182
            ];
183
        }, explode("\n", trim($sessions, " \t\n\r;")));
184
        // Filter empty lines.
185
        $sessions = array_filter($sessions, fn($session) => count($session) > 0);
186
187
        // Check uniqueness of session dates
188
        $sessionDates = array_unique(array_map(fn($session) => $session['date'], $sessions));
189
        if(count($sessions) !== count($sessionDates))
190
        {
191
            throw new MessageException(trans('tontine.session.errors.date_dup'));
192
        }
193
194
        return $this->validator->validateList($sessions);
195
    }
196
197
    /**
198
     * @di $validator
199
     */
200
    public function createList(array $formValues)
201
    {
202
        $values = $this->parseSessionList($formValues['sessions'] ?? '');
203
204
        $this->sessionService->createSessions($values);
205
        $this->dialog->hide();
206
        $this->notify->success(trans('tontine.session.messages.created'), trans('common.titles.success'));
207
208
        return $this->home();
209
    }
210
211
    public function edit(int $sessionId)
212
    {
213
        $session = $this->sessionService->getSession($sessionId);
214
        $title = trans('tontine.session.titles.edit');
215
        $content = $this->render('pages.planning.session.edit')
216
            ->with('session', $session)
217
            ->with('members', $this->memberService->getMemberList()->prepend('', 0));
218
        $buttons = [[
219
            'title' => trans('common.actions.cancel'),
220
            'class' => 'btn btn-tertiary',
221
            'click' => 'close',
222
        ],[
223
            'title' => trans('common.actions.save'),
224
            'class' => 'btn btn-primary',
225
            'click' => $this->rq()->update($session->id, pm()->form('session-form')),
226
        ]];
227
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
228
229
        return $this->response;
230
    }
231
232
    /**
233
     * @di $validator
234
     */
235
    public function update(int $sessionId, array $formValues)
236
    {
237
        $formValues['id'] = $sessionId;
238
        $values = $this->validator->validateItem($formValues);
239
        $session = $this->sessionService->getSession($sessionId);
240
241
        $this->sessionService->updateSession($session, $values);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of Siak\Tontine\Service\Pla...ervice::updateSession() does only seem to accept Siak\Tontine\Model\Session, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

241
        $this->sessionService->updateSession(/** @scrutinizer ignore-type */ $session, $values);
Loading history...
242
        $this->dialog->hide();
243
        $this->notify->success(trans('tontine.session.messages.updated'), trans('common.titles.success'));
244
245
        return  $this->page();
246
    }
247
248
    public function editVenue(int $sessionId)
249
    {
250
        $session = $this->sessionService->getSession($sessionId);
251
252
        $venue = $session->venue ?? ($session->host ? $session->host->address : '');
253
        $title = trans('tontine.session.titles.venue');
254
        $content = $this->render('pages.planning.session.venue')
255
            ->with('session', $session)->with('venue', $venue);
256
        $buttons = [[
257
            'title' => trans('common.actions.cancel'),
258
            'class' => 'btn btn-tertiary',
259
            'click' => 'close',
260
        ],[
261
            'title' => trans('common.actions.save'),
262
            'class' => 'btn btn-primary',
263
            'click' => $this->rq()->saveVenue($session->id, pm()->form('session-form')),
264
        ]];
265
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

265
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
266
267
        return $this->response;
268
    }
269
270
    /**
271
     * @di $validator
272
     */
273
    public function saveVenue(int $sessionId, array $formValues)
274
    {
275
        $values = $this->validator->validateVenue($formValues);
276
        $session = $this->sessionService->getSession($sessionId);
277
278
        $this->sessionService->saveSessionVenue($session, $values);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of Siak\Tontine\Service\Pla...ice::saveSessionVenue() does only seem to accept Siak\Tontine\Model\Session, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

278
        $this->sessionService->saveSessionVenue(/** @scrutinizer ignore-type */ $session, $values);
Loading history...
279
        $this->dialog->hide();
280
        $this->notify->success(trans('tontine.session.messages.updated'), trans('common.titles.success'));
281
282
        return $this->page();
283
    }
284
285
    public function delete(int $sessionId)
286
    {
287
        $session = $this->sessionService->getSession($sessionId);
288
        $this->sessionService->deleteSession($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of Siak\Tontine\Service\Pla...ervice::deleteSession() does only seem to accept Siak\Tontine\Model\Session, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
        $this->sessionService->deleteSession(/** @scrutinizer ignore-type */ $session);
Loading history...
289
        $this->notify->success(trans('tontine.session.messages.deleted'), trans('common.titles.success'));
290
291
        return $this->page();
292
    }
293
}
294