xref: /webtrees/app/Module/UserJournalModule.php (revision 4947e7089adb08f24bf1eef67994a106a8df60aa)
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\I18N;
22use Fisharebest\Webtrees\Tree;
23use Illuminate\Database\Capsule\Manager as DB;
24use Symfony\Component\HttpFoundation\RedirectResponse;
25use Symfony\Component\HttpFoundation\Request;
26use Symfony\Component\HttpFoundation\Response;
27use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
28
29/**
30 * Class UserJournalModule
31 */
32class UserJournalModule extends AbstractModule implements ModuleBlockInterface
33{
34    use ModuleBlockTrait;
35
36    /**
37     * How should this module be labelled on tabs, menus, etc.?
38     *
39     * @return string
40     */
41    public function title(): string
42    {
43        /* I18N: Name of a module */
44        return I18N::translate('Journal');
45    }
46
47    /**
48     * A sentence describing what this module does.
49     *
50     * @return string
51     */
52    public function description(): string
53    {
54        /* I18N: Description of the “Journal” module */
55        return I18N::translate('A private area to record notes or keep a journal.');
56    }
57
58    /**
59     * Generate the HTML content of this block.
60     *
61     * @param Tree     $tree
62     * @param int      $block_id
63     * @param string   $ctype
64     * @param string[] $cfg
65     *
66     * @return string
67     */
68    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
69    {
70        $articles = DB::table('news')
71            ->where('user_id', '=', Auth::id())
72            ->orderByDesc('updated')
73            ->select(['news_id', 'user_id', 'gedcom_id', DB::raw('UNIX_TIMESTAMP(updated) AS updated'), 'subject', 'body'])
74            ->get();
75
76        $content = view('modules/user_blog/list', [
77            'articles' => $articles,
78            'block_id' => $block_id,
79            'limit'    => 5,
80        ]);
81
82        if ($ctype !== '') {
83            return view('modules/block-template', [
84                'block'      => str_replace('_', '-', $this->name()),
85                'id'         => $block_id,
86                'config_url' => '',
87                'title'      => $this->title(),
88                'content'    => $content,
89            ]);
90        }
91
92        return $content;
93    }
94
95    /** {@inheritdoc} */
96    public function loadAjax(): bool
97    {
98        return false;
99    }
100
101    /** {@inheritdoc} */
102    public function isUserBlock(): bool
103    {
104        return true;
105    }
106
107    /** {@inheritdoc} */
108    public function isTreeBlock(): bool
109    {
110        return false;
111    }
112
113    /**
114     * Update the configuration for a block.
115     *
116     * @param Request $request
117     * @param int     $block_id
118     *
119     * @return void
120     */
121    public function saveBlockConfiguration(Request $request, int $block_id)
122    {
123    }
124
125    /**
126     * An HTML form to edit block settings
127     *
128     * @param Tree $tree
129     * @param int  $block_id
130     *
131     * @return void
132     */
133    public function editBlockConfiguration(Tree $tree, int $block_id)
134    {
135    }
136
137    /**
138     * @param Request $request
139     *
140     * @return Response
141     */
142    public function getEditJournalAction(Request $request): Response
143    {
144        if (!Auth::check()) {
145            throw new AccessDeniedHttpException();
146        }
147
148        $news_id = $request->get('news_id');
149
150        if ($news_id > 0) {
151            $row = DB::table('news')
152                ->where('news_id', '=', $news_id)
153                ->where('user_id', '=', Auth::id())
154                ->first();
155        } else {
156            $row = (object) [
157                'body'    => '',
158                'subject' => '',
159            ];
160        }
161
162        $title = I18N::translate('Add/edit a journal/news entry');
163
164        return $this->viewResponse('modules/user_blog/edit', [
165            'body'    => $row->body,
166            'news_id' => $news_id,
167            'subject' => $row->subject,
168            'title'   => $title,
169        ]);
170    }
171
172    /**
173     * @param Request $request
174     * @param Tree    $tree
175     *
176     * @return RedirectResponse
177     */
178    public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse
179    {
180        if (!Auth::check()) {
181            throw new AccessDeniedHttpException();
182        }
183
184        $news_id = $request->get('news_id');
185        $subject = $request->get('subject');
186        $body    = $request->get('body');
187
188        if ($news_id > 0) {
189            DB::table('news')
190                ->where('news_id', '=', $news_id)
191                ->where('user_id', '=', Auth::id())
192                ->update([
193                    'body'    => $body,
194                    'subject' => $subject,
195                ]);
196        } else {
197            DB::table('news')->insert([
198                'body'    => $body,
199                'subject' => $subject,
200                'user_id' => Auth::id(),
201            ]);
202        }
203
204        $url = route('user-page', [
205            'ged' => $tree->name(),
206        ]);
207
208        return new RedirectResponse($url);
209    }
210
211    /**
212     * @param Request $request
213     * @param Tree    $tree
214     *
215     * @return RedirectResponse
216     */
217    public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse
218    {
219        $news_id = $request->get('news_id');
220
221        DB::table('news')
222            ->where('news_id', '=', $news_id)
223            ->where('user_id', '=', Auth::id())
224            ->delete();
225
226        $url = route('user-page', [
227            'ged' => $tree->name(),
228        ]);
229
230        return new RedirectResponse($url);
231    }
232}
233