xref: /webtrees/app/Module/UserJournalModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
254348fc02SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
268e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
28d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
2950d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService;
30fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
31b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
320dc38e08SGreg Roachuse Illuminate\Database\Query\Expression;
331e7a7a28SGreg Roachuse Illuminate\Support\Str;
346ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
36f3874e19SGreg Roach
374348fc02SGreg Roachuse function redirect;
388c2e8227SGreg Roach
398c2e8227SGreg Roach/**
408c2e8227SGreg Roach * Class UserJournalModule
418c2e8227SGreg Roach */
4237eb8894SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleBlockTrait;
4549a243cbSGreg Roach
4643f2f523SGreg Roach    private HtmlService $html_service;
4750d6f48cSGreg Roach
4850d6f48cSGreg Roach    /**
4950d6f48cSGreg Roach     * @param HtmlService $html_service
5050d6f48cSGreg Roach     */
519e18e23bSGreg Roach    public function __construct(HtmlService $html_service)
5250d6f48cSGreg Roach    {
5350d6f48cSGreg Roach        $this->html_service = $html_service;
5450d6f48cSGreg Roach    }
5550d6f48cSGreg Roach
5649a243cbSGreg Roach    public function description(): string
57c1010edaSGreg Roach    {
58bbb76c12SGreg Roach        /* I18N: Description of the “Journal” module */
59bbb76c12SGreg Roach        return I18N::translate('A private area to record notes or keep a journal.');
608c2e8227SGreg Roach    }
618c2e8227SGreg Roach
6276692c8bSGreg Roach    /**
6376692c8bSGreg Roach     * Generate the HTML content of this block.
6476692c8bSGreg Roach     *
65e490cd80SGreg Roach     * @param Tree                 $tree
6676692c8bSGreg Roach     * @param int                  $block_id
673caaa4d2SGreg Roach     * @param string               $context
6876d39c55SGreg Roach     * @param array<string,string> $config
6976692c8bSGreg Roach     *
7076692c8bSGreg Roach     * @return string
7176692c8bSGreg Roach     */
723caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
73c1010edaSGreg Roach    {
74ec589cf2SGreg Roach        $articles = DB::table('news')
75ec589cf2SGreg Roach            ->where('user_id', '=', Auth::id())
76ec589cf2SGreg Roach            ->orderByDesc('updated')
77ca52a408SGreg Roach            ->get()
78f70bcff5SGreg Roach            ->map(static function (object $row): object {
79d97083feSGreg Roach                $row->updated = Registry::timestampFactory()->fromString($row->updated);
80ca52a408SGreg Roach
81ca52a408SGreg Roach                return $row;
82ca52a408SGreg Roach            });
83ec589cf2SGreg Roach
84147e99aaSGreg Roach        $content = view('modules/user_blog/list', [
85fe8a65d1SGreg Roach            'articles' => $articles,
86fe8a65d1SGreg Roach            'block_id' => $block_id,
87fe8a65d1SGreg Roach            'limit'    => 5,
885f1b387cSGreg Roach            'tree'     => $tree,
89fe8a65d1SGreg Roach        ]);
908c2e8227SGreg Roach
913caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
92147e99aaSGreg Roach            return view('modules/block-template', [
931e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
949c6524dcSGreg Roach                'id'         => $block_id,
959c6524dcSGreg Roach                'config_url' => '',
9649a243cbSGreg Roach                'title'      => $this->title(),
979c6524dcSGreg Roach                'content'    => $content,
989c6524dcSGreg Roach            ]);
998c2e8227SGreg Roach        }
100b2ce94c6SRico Sonntag
101b2ce94c6SRico Sonntag        return $content;
1028c2e8227SGreg Roach    }
1038c2e8227SGreg Roach
1046ccdf4f0SGreg Roach    /**
1056ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1066ccdf4f0SGreg Roach     *
1076ccdf4f0SGreg Roach     * @return string
1086ccdf4f0SGreg Roach     */
1096ccdf4f0SGreg Roach    public function title(): string
1106ccdf4f0SGreg Roach    {
1116ccdf4f0SGreg Roach        /* I18N: Name of a module */
1126ccdf4f0SGreg Roach        return I18N::translate('Journal');
1136ccdf4f0SGreg Roach    }
1146ccdf4f0SGreg Roach
11550d6f48cSGreg Roach    /**
11650d6f48cSGreg Roach     * Should this block load asynchronously using AJAX?
11750d6f48cSGreg Roach     *
1183caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
11950d6f48cSGreg Roach     *
12050d6f48cSGreg Roach     * @return bool
12150d6f48cSGreg Roach     */
122c1010edaSGreg Roach    public function loadAjax(): bool
123c1010edaSGreg Roach    {
1248c2e8227SGreg Roach        return false;
1258c2e8227SGreg Roach    }
1268c2e8227SGreg Roach
12750d6f48cSGreg Roach    /**
12850d6f48cSGreg Roach     * Can this block be shown on the user’s home page?
12950d6f48cSGreg Roach     *
13050d6f48cSGreg Roach     * @return bool
13150d6f48cSGreg Roach     */
132c1010edaSGreg Roach    public function isUserBlock(): bool
133c1010edaSGreg Roach    {
1348c2e8227SGreg Roach        return true;
1358c2e8227SGreg Roach    }
1368c2e8227SGreg Roach
13750d6f48cSGreg Roach    /**
13850d6f48cSGreg Roach     * Can this block be shown on the tree’s home page?
13950d6f48cSGreg Roach     *
14050d6f48cSGreg Roach     * @return bool
14150d6f48cSGreg Roach     */
14263276d8fSGreg Roach    public function isTreeBlock(): bool
143c1010edaSGreg Roach    {
1448c2e8227SGreg Roach        return false;
1458c2e8227SGreg Roach    }
1468c2e8227SGreg Roach
14776692c8bSGreg Roach    /**
1486ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
149fe8a65d1SGreg Roach     *
1506ccdf4f0SGreg Roach     * @return ResponseInterface
151fe8a65d1SGreg Roach     */
1526ccdf4f0SGreg Roach    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
153c1010edaSGreg Roach    {
154b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
1555f1b387cSGreg Roach
156fe8a65d1SGreg Roach        if (!Auth::check()) {
157d501c45dSGreg Roach            throw new HttpAccessDeniedException();
158fe8a65d1SGreg Roach        }
159fe8a65d1SGreg Roach
160748dbe15SGreg Roach        $news_id = Validator::queryParams($request)->integer('news_id', 0);
161fe8a65d1SGreg Roach
162748dbe15SGreg Roach        if ($news_id !== 0) {
163ec589cf2SGreg Roach            $row = DB::table('news')
164ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
165ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
166ec589cf2SGreg Roach                ->first();
1674348fc02SGreg Roach
1684348fc02SGreg Roach            // Record was deleted before we could read it?
169e61121aaSGreg Roach            if ($row === null) {
1704566681eSGreg Roach                throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'news_id:' . $news_id));
1714348fc02SGreg Roach            }
172fe8a65d1SGreg Roach        } else {
1734348fc02SGreg Roach            $row = (object)['body' => '', 'subject' => ''];
174fe8a65d1SGreg Roach        }
175fe8a65d1SGreg Roach
176fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
177fe8a65d1SGreg Roach
178147e99aaSGreg Roach        return $this->viewResponse('modules/user_blog/edit', [
179fe8a65d1SGreg Roach            'body'    => $row->body,
180fe8a65d1SGreg Roach            'news_id' => $news_id,
181fe8a65d1SGreg Roach            'subject' => $row->subject,
182fe8a65d1SGreg Roach            'title'   => $title,
1835f1b387cSGreg Roach            'tree'    => $tree,
184fe8a65d1SGreg Roach        ]);
185fe8a65d1SGreg Roach    }
186fe8a65d1SGreg Roach
187fe8a65d1SGreg Roach    /**
1886ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
189fe8a65d1SGreg Roach     *
1906ccdf4f0SGreg Roach     * @return ResponseInterface
191fe8a65d1SGreg Roach     */
19257ab2231SGreg Roach    public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface
193c1010edaSGreg Roach    {
194b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
19557ab2231SGreg Roach
196fe8a65d1SGreg Roach        if (!Auth::check()) {
197d501c45dSGreg Roach            throw new HttpAccessDeniedException();
198fe8a65d1SGreg Roach        }
199fe8a65d1SGreg Roach
200748dbe15SGreg Roach        $news_id = Validator::queryParams($request)->integer('news_id', 0);
2016623e4dbSGreg Roach        $subject = Validator::parsedBody($request)->string('subject');
2026623e4dbSGreg Roach        $body    = Validator::parsedBody($request)->string('body');
203fe8a65d1SGreg Roach
20450d6f48cSGreg Roach        $subject = $this->html_service->sanitize($subject);
20550d6f48cSGreg Roach        $body    = $this->html_service->sanitize($body);
20650d6f48cSGreg Roach
207748dbe15SGreg Roach        if ($news_id !== 0) {
208ec589cf2SGreg Roach            DB::table('news')
209ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
210ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
211ec589cf2SGreg Roach                ->update([
212fe8a65d1SGreg Roach                    'body'    => $body,
213ec589cf2SGreg Roach                    'subject' => $subject,
2140dc38e08SGreg Roach                    'updated' => new Expression('updated'), // See issue #3208
215fe8a65d1SGreg Roach                ]);
216fe8a65d1SGreg Roach        } else {
217ec589cf2SGreg Roach            DB::table('news')->insert([
218fe8a65d1SGreg Roach                'body'    => $body,
219fe8a65d1SGreg Roach                'subject' => $subject,
220fe8a65d1SGreg Roach                'user_id' => Auth::id(),
221fe8a65d1SGreg Roach            ]);
222fe8a65d1SGreg Roach        }
223fe8a65d1SGreg Roach
2248e0e1b25SGreg Roach        $url = route(UserPage::class, ['tree' => $tree->name()]);
225fe8a65d1SGreg Roach
2266ccdf4f0SGreg Roach        return redirect($url);
227fe8a65d1SGreg Roach    }
228fe8a65d1SGreg Roach
229fe8a65d1SGreg Roach    /**
2306ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
231fe8a65d1SGreg Roach     *
2326ccdf4f0SGreg Roach     * @return ResponseInterface
233fe8a65d1SGreg Roach     */
23457ab2231SGreg Roach    public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface
235c1010edaSGreg Roach    {
236b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
237748dbe15SGreg Roach        $news_id = Validator::queryParams($request)->integer('news_id');
238fe8a65d1SGreg Roach
239ec589cf2SGreg Roach        DB::table('news')
240ec589cf2SGreg Roach            ->where('news_id', '=', $news_id)
241ec589cf2SGreg Roach            ->where('user_id', '=', Auth::id())
242ec589cf2SGreg Roach            ->delete();
243fe8a65d1SGreg Roach
2448e0e1b25SGreg Roach        $url = route(UserPage::class, ['tree' => $tree->name()]);
245fe8a65d1SGreg Roach
2466ccdf4f0SGreg Roach        return redirect($url);
247fe8a65d1SGreg Roach    }
2488c2e8227SGreg Roach}
249