Passed
Push — master ( 4e39d4...a81abc )
by Greg
06:05
created

UserJournalModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Carbon;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Services\HtmlService;
26
use Fisharebest\Webtrees\Tree;
27
use Illuminate\Database\Capsule\Manager as DB;
28
use Illuminate\Support\Str;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use stdClass;
32
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
33
34
use function assert;
35
36
/**
37
 * Class UserJournalModule
38
 */
39
class UserJournalModule extends AbstractModule implements ModuleBlockInterface
40
{
41
    use ModuleBlockTrait;
42
43
    /** @var HtmlService */
44
    private $html_service;
45
46
    /**
47
     * HtmlBlockModule constructor.
48
     *
49
     * @param HtmlService $html_service
50
     */
51
    public function __construct(HtmlService $html_service)
52
    {
53
        $this->html_service = $html_service;
54
    }
55
56
    /**
57
     * A sentence describing what this module does.
58
     *
59
     * @return string
60
     */
61
    public function description(): string
62
    {
63
        /* I18N: Description of the “Journal” module */
64
        return I18N::translate('A private area to record notes or keep a journal.');
65
    }
66
67
    /**
68
     * Generate the HTML content of this block.
69
     *
70
     * @param Tree     $tree
71
     * @param int      $block_id
72
     * @param string   $context
73
     * @param string[] $config
74
     *
75
     * @return string
76
     */
77
    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
78
    {
79
        $articles = DB::table('news')
80
            ->where('user_id', '=', Auth::id())
81
            ->orderByDesc('updated')
82
            ->get()
83
            ->map(static function (stdClass $row): stdClass {
84
                $row->updated = Carbon::make($row->updated);
85
86
                return $row;
87
            });
88
89
        $content = view('modules/user_blog/list', [
90
            'articles' => $articles,
91
            'block_id' => $block_id,
92
            'limit'    => 5,
93
        ]);
94
95
        if ($context !== self::CONTEXT_EMBED) {
96
            return view('modules/block-template', [
97
                'block'      => Str::kebab($this->name()),
98
                'id'         => $block_id,
99
                'config_url' => '',
100
                'title'      => $this->title(),
101
                'content'    => $content,
102
            ]);
103
        }
104
105
        return $content;
106
    }
107
108
    /**
109
     * How should this module be identified in the control panel, etc.?
110
     *
111
     * @return string
112
     */
113
    public function title(): string
114
    {
115
        /* I18N: Name of a module */
116
        return I18N::translate('Journal');
117
    }
118
119
    /**
120
     * Should this block load asynchronously using AJAX?
121
     *
122
     * Simple blocks are faster in-line, more complex ones can be loaded later.
123
     *
124
     * @return bool
125
     */
126
    public function loadAjax(): bool
127
    {
128
        return false;
129
    }
130
131
    /**
132
     * Can this block be shown on the user’s home page?
133
     *
134
     * @return bool
135
     */
136
    public function isUserBlock(): bool
137
    {
138
        return true;
139
    }
140
141
    /**
142
     * Can this block be shown on the tree’s home page?
143
     *
144
     * @return bool
145
     */
146
    public function isTreeBlock(): bool
147
    {
148
        return false;
149
    }
150
151
    /**
152
     * @param ServerRequestInterface $request
153
     *
154
     * @return ResponseInterface
155
     */
156
    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
157
    {
158
        if (!Auth::check()) {
159
            throw new AccessDeniedHttpException();
160
        }
161
162
        $news_id = $request->getQueryParams()['news_id'] ?? '';
163
164
        if ($news_id !== '') {
165
            $row = DB::table('news')
166
                ->where('news_id', '=', $news_id)
167
                ->where('user_id', '=', Auth::id())
168
                ->first();
169
        } else {
170
            $row = (object) [
171
                'body'    => '',
172
                'subject' => '',
173
            ];
174
        }
175
176
        $title = I18N::translate('Add/edit a journal/news entry');
177
178
        return $this->viewResponse('modules/user_blog/edit', [
179
            'body'    => $row->body,
180
            'news_id' => $news_id,
181
            'subject' => $row->subject,
182
            'title'   => $title,
183
        ]);
184
    }
185
186
    /**
187
     * @param ServerRequestInterface $request
188
     *
189
     * @return ResponseInterface
190
     */
191
    public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface
192
    {
193
        $tree = $request->getAttribute('tree');
194
        assert($tree instanceof Tree);
195
196
        if (!Auth::check()) {
197
            throw new AccessDeniedHttpException();
198
        }
199
200
        $news_id = $request->getQueryParams()['news_id'] ?? '';
201
        $subject = $request->getParsedBody()['subject'];
202
        $body    = $request->getParsedBody()['body'];
203
204
        $subject = $this->html_service->sanitize($subject);
205
        $body    = $this->html_service->sanitize($body);
206
207
        if ($news_id !== '') {
208
            DB::table('news')
209
                ->where('news_id', '=', $news_id)
210
                ->where('user_id', '=', Auth::id())
211
                ->update([
212
                    'body'    => $body,
213
                    'subject' => $subject,
214
                ]);
215
        } else {
216
            DB::table('news')->insert([
217
                'body'    => $body,
218
                'subject' => $subject,
219
                'user_id' => Auth::id(),
220
            ]);
221
        }
222
223
        $url = route('user-page', ['tree' => $tree->name()]);
224
225
        return redirect($url);
226
    }
227
228
    /**
229
     * @param ServerRequestInterface $request
230
     *
231
     * @return ResponseInterface
232
     */
233
    public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface
234
    {
235
        $tree    = $request->getAttribute('tree');
236
        $news_id = $request->getQueryParams()['news_id'];
237
238
        DB::table('news')
239
            ->where('news_id', '=', $news_id)
240
            ->where('user_id', '=', Auth::id())
241
            ->delete();
242
243
        $url = route('user-page', ['tree' => $tree->name()]);
244
245
        return redirect($url);
246
    }
247
}
248