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