xref: /webtrees/app/Module/UserJournalModule.php (revision c0d396ead092b2de2bd331757c79810fd256aad8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Tree;
22use Symfony\Component\HttpFoundation\RedirectResponse;
23use Symfony\Component\HttpFoundation\Request;
24use Symfony\Component\HttpFoundation\Response;
25use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
26
27/**
28 * Class UserJournalModule
29 */
30class UserJournalModule extends AbstractModule implements ModuleBlockInterface
31{
32    /**
33     * Create a new module.
34     *
35     * @param string $directory Where is this module installed
36     */
37    public function __construct($directory)
38    {
39        parent::__construct($directory);
40
41        // Create/update the database tables.
42        Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3);
43    }
44
45    /**
46     * How should this module be labelled on tabs, menus, etc.?
47     *
48     * @return string
49     */
50    public function getTitle()
51    {
52        return /* I18N: Name of a module */
53            I18N::translate('Journal');
54    }
55
56    /**
57     * A sentence describing what this module does.
58     *
59     * @return string
60     */
61    public function getDescription()
62    {
63        return /* I18N: Description of the “Journal” module */
64            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 bool     $template
73     * @param string[] $cfg
74     *
75     * @return string
76     */
77    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
78    {
79        $articles = Database::prepare(
80            "SELECT news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) + :offset AS updated, subject, body FROM `##news` WHERE user_id = :user_id ORDER BY updated DESC"
81        )->execute([
82            'offset'  => WT_TIMESTAMP_OFFSET,
83            'user_id' => Auth::id(),
84        ])->fetchAll();
85
86        $content = view('modules/user_blog/list', [
87            'articles' => $articles,
88            'block_id' => $block_id,
89            'limit'    => 5,
90        ]);
91
92        if ($template) {
93            return view('modules/block-template', [
94                'block'      => str_replace('_', '-', $this->getName()),
95                'id'         => $block_id,
96                'config_url' => '',
97                'title'      => $this->getTitle(),
98                'content'    => $content,
99            ]);
100        } else {
101            return $content;
102        }
103    }
104
105    /** {@inheritdoc} */
106    public function loadAjax(): bool
107    {
108        return false;
109    }
110
111    /** {@inheritdoc} */
112    public function isUserBlock(): bool
113    {
114        return true;
115    }
116
117    /** {@inheritdoc} */
118    public function isGedcomBlock(): bool
119    {
120        return false;
121    }
122
123    /**
124     * An HTML form to edit block settings
125     *
126     * @param Tree $tree
127     * @param int  $block_id
128     *
129     * @return void
130     */
131    public function configureBlock(Tree $tree, int $block_id)
132    {
133    }
134
135    /**
136     * @param Request $request
137     *
138     * @return Response
139     */
140    public function getEditJournalAction(Request $request): Response
141    {
142        if (!Auth::check()) {
143            throw new AccessDeniedHttpException;
144        }
145
146        $news_id = $request->get('news_id');
147
148        if ($news_id > 0) {
149            $row = Database::prepare(
150                "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id"
151            )->execute([
152                'news_id' => $news_id,
153                'user_id' => Auth::id(),
154            ])->fetchOneRow();
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            Database::prepare(
190                "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" .
191                " WHERE news_id = :news_id AND user_id = :user_id"
192            )->execute([
193                'subject' => $subject,
194                'body'    => $body,
195                'news_id' => $news_id,
196                'user_id' => Auth::id(),
197            ]);
198        } else {
199            Database::prepare(
200                "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)"
201            )->execute([
202                'body'    => $body,
203                'subject' => $subject,
204                'user_id' => Auth::id(),
205            ]);
206        }
207
208        $url = route('user-page', [
209            'ged' => $tree->getName(),
210        ]);
211
212        return new RedirectResponse($url);
213    }
214
215    /**
216     * @param Request $request
217     * @param Tree    $tree
218     *
219     * @return RedirectResponse
220     */
221    public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse
222    {
223        $news_id = $request->get('news_id');
224
225        if (!Auth::check()) {
226            throw new AccessDeniedHttpException;
227        }
228
229        Database::prepare(
230            "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id"
231        )->execute([
232            'news_id' => $news_id,
233            'user_id' => Auth::id(),
234        ]);
235
236        $url = route('user-page', [
237            'ged' => $tree->getName(),
238        ]);
239
240        return new RedirectResponse($url);
241    }
242}
243