xref: /webtrees/app/Module/UserJournalModule.php (revision fd6c8c786edaa7cf7534ebe606cd8d58eec364cb)
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(): string
51    {
52        /* I18N: Name of a module */
53        return I18N::translate('Journal');
54    }
55
56    /**
57     * A sentence describing what this module does.
58     *
59     * @return string
60     */
61    public function getDescription(): 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 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        }
101
102        return $content;
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     * Update the configuration for a block.
125     *
126     * @param Request $request
127     * @param int     $block_id
128     *
129     * @return void
130     */
131    public function saveBlockConfiguration(Request $request, int $block_id)
132    {
133    }
134
135    /**
136     * An HTML form to edit block settings
137     *
138     * @param Tree $tree
139     * @param int  $block_id
140     *
141     * @return void
142     */
143    public function editBlockConfiguration(Tree $tree, int $block_id)
144    {
145    }
146
147    /**
148     * @param Request $request
149     *
150     * @return Response
151     */
152    public function getEditJournalAction(Request $request): Response
153    {
154        if (!Auth::check()) {
155            throw new AccessDeniedHttpException();
156        }
157
158        $news_id = $request->get('news_id');
159
160        if ($news_id > 0) {
161            $row = Database::prepare(
162                "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id"
163            )->execute([
164                'news_id' => $news_id,
165                'user_id' => Auth::id(),
166            ])->fetchOneRow();
167        } else {
168            $row = (object) [
169                'body'    => '',
170                'subject' => '',
171            ];
172        }
173
174        $title = I18N::translate('Add/edit a journal/news entry');
175
176        return $this->viewResponse('modules/user_blog/edit', [
177            'body'    => $row->body,
178            'news_id' => $news_id,
179            'subject' => $row->subject,
180            'title'   => $title,
181        ]);
182    }
183
184    /**
185     * @param Request $request
186     * @param Tree    $tree
187     *
188     * @return RedirectResponse
189     */
190    public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse
191    {
192        if (!Auth::check()) {
193            throw new AccessDeniedHttpException();
194        }
195
196        $news_id = $request->get('news_id');
197        $subject = $request->get('subject');
198        $body    = $request->get('body');
199
200        if ($news_id > 0) {
201            Database::prepare(
202                "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" .
203                " WHERE news_id = :news_id AND user_id = :user_id"
204            )->execute([
205                'subject' => $subject,
206                'body'    => $body,
207                'news_id' => $news_id,
208                'user_id' => Auth::id(),
209            ]);
210        } else {
211            Database::prepare(
212                "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)"
213            )->execute([
214                'body'    => $body,
215                'subject' => $subject,
216                'user_id' => Auth::id(),
217            ]);
218        }
219
220        $url = route('user-page', [
221            'ged' => $tree->getName(),
222        ]);
223
224        return new RedirectResponse($url);
225    }
226
227    /**
228     * @param Request $request
229     * @param Tree    $tree
230     *
231     * @return RedirectResponse
232     */
233    public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse
234    {
235        $news_id = $request->get('news_id');
236
237        if (!Auth::check()) {
238            throw new AccessDeniedHttpException();
239        }
240
241        Database::prepare(
242            "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id"
243        )->execute([
244            'news_id' => $news_id,
245            'user_id' => Auth::id(),
246        ]);
247
248        $url = route('user-page', [
249            'ged' => $tree->getName(),
250        ]);
251
252        return new RedirectResponse($url);
253    }
254}
255