xref: /webtrees/app/Module/UserJournalModule.php (revision 1afd8fb8ec6b95d0719e0a266a25e8ef0190c467)
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 stdClass;
26use Symfony\Component\HttpFoundation\RedirectResponse;
27use Symfony\Component\HttpFoundation\Request;
28use Symfony\Component\HttpFoundation\Response;
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     * How should this module be identified in the control panel, etc.?
40     *
41     * @return string
42     */
43    public function title(): string
44    {
45        /* I18N: Name of a module */
46        return I18N::translate('Journal');
47    }
48
49    /**
50     * A sentence describing what this module does.
51     *
52     * @return string
53     */
54    public function description(): string
55    {
56        /* I18N: Description of the “Journal” module */
57        return I18N::translate('A private area to record notes or keep a journal.');
58    }
59
60    /**
61     * Generate the HTML content of this block.
62     *
63     * @param Tree     $tree
64     * @param int      $block_id
65     * @param string   $ctype
66     * @param string[] $cfg
67     *
68     * @return string
69     */
70    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
71    {
72        $articles = DB::table('news')
73            ->where('user_id', '=', Auth::id())
74            ->orderByDesc('updated')
75            ->get()
76            ->map(function (stdClass $row): stdClass {
77                $row->updated = Carbon::make($row->updated);
78
79                return $row;
80            });
81
82        $content = view('modules/user_blog/list', [
83            'articles' => $articles,
84            'block_id' => $block_id,
85            'limit'    => 5,
86        ]);
87
88        if ($ctype !== '') {
89            return view('modules/block-template', [
90                'block'      => str_replace('_', '-', $this->name()),
91                'id'         => $block_id,
92                'config_url' => '',
93                'title'      => $this->title(),
94                'content'    => $content,
95            ]);
96        }
97
98        return $content;
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 Request $request
123     * @param int     $block_id
124     *
125     * @return void
126     */
127    public function saveBlockConfiguration(Request $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 Request $request
145     *
146     * @return Response
147     */
148    public function getEditJournalAction(Request $request): Response
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 Request $request
180     * @param Tree    $tree
181     *
182     * @return RedirectResponse
183     */
184    public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse
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 new RedirectResponse($url);
215    }
216
217    /**
218     * @param Request $request
219     * @param Tree    $tree
220     *
221     * @return RedirectResponse
222     */
223    public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse
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 new RedirectResponse($url);
237    }
238}
239