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