xref: /webtrees/app/Module/UserJournalModule.php (revision d72b284a0846ca045e548a1c77ad11813bcbab92)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2450d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService;
25fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
26ec589cf2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30ca52a408SGreg Roachuse stdClass;
31fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
328c2e8227SGreg Roach
338c2e8227SGreg Roach/**
348c2e8227SGreg Roach * Class UserJournalModule
358c2e8227SGreg Roach */
3637eb8894SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleBlockTrait;
3949a243cbSGreg Roach
4050d6f48cSGreg Roach    /** @var HtmlService */
4150d6f48cSGreg Roach    private $html_service;
4250d6f48cSGreg Roach
4350d6f48cSGreg Roach    /**
4450d6f48cSGreg Roach     * HtmlBlockModule bootstrap.
4550d6f48cSGreg Roach     *
4650d6f48cSGreg Roach     * @param HtmlService $html_service
4750d6f48cSGreg Roach     */
4850d6f48cSGreg Roach    public function boot(HtmlService $html_service)
4950d6f48cSGreg Roach    {
5050d6f48cSGreg Roach        $this->html_service = $html_service;
5150d6f48cSGreg Roach    }
5250d6f48cSGreg Roach
5376692c8bSGreg Roach    /**
5476692c8bSGreg Roach     * A sentence describing what this module does.
5576692c8bSGreg Roach     *
5676692c8bSGreg Roach     * @return string
5776692c8bSGreg Roach     */
5849a243cbSGreg Roach    public function description(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Description of the “Journal” module */
61bbb76c12SGreg Roach        return I18N::translate('A private area to record notes or keep a journal.');
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
6476692c8bSGreg Roach    /**
6576692c8bSGreg Roach     * Generate the HTML content of this block.
6676692c8bSGreg Roach     *
67e490cd80SGreg Roach     * @param Tree     $tree
6876692c8bSGreg Roach     * @param int      $block_id
693caaa4d2SGreg Roach     * @param string   $context
703caaa4d2SGreg Roach     * @param string[] $config
7176692c8bSGreg Roach     *
7276692c8bSGreg Roach     * @return string
7376692c8bSGreg Roach     */
743caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
75c1010edaSGreg Roach    {
76ec589cf2SGreg Roach        $articles = DB::table('news')
77ec589cf2SGreg Roach            ->where('user_id', '=', Auth::id())
78ec589cf2SGreg Roach            ->orderByDesc('updated')
79ca52a408SGreg Roach            ->get()
800b5fd0a6SGreg Roach            ->map(static function (stdClass $row): stdClass {
814459dc9aSGreg Roach                $row->updated = Carbon::make($row->updated);
82ca52a408SGreg Roach
83ca52a408SGreg Roach                return $row;
84ca52a408SGreg Roach            });
85ec589cf2SGreg Roach
86147e99aaSGreg Roach        $content = view('modules/user_blog/list', [
87fe8a65d1SGreg Roach            'articles' => $articles,
88fe8a65d1SGreg Roach            'block_id' => $block_id,
89fe8a65d1SGreg Roach            'limit'    => 5,
90fe8a65d1SGreg Roach        ]);
918c2e8227SGreg Roach
923caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
93147e99aaSGreg Roach            return view('modules/block-template', [
941e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
959c6524dcSGreg Roach                'id'         => $block_id,
969c6524dcSGreg Roach                'config_url' => '',
9749a243cbSGreg Roach                'title'      => $this->title(),
989c6524dcSGreg Roach                'content'    => $content,
999c6524dcSGreg Roach            ]);
1008c2e8227SGreg Roach        }
101b2ce94c6SRico Sonntag
102b2ce94c6SRico Sonntag        return $content;
1038c2e8227SGreg Roach    }
1048c2e8227SGreg Roach
1056ccdf4f0SGreg Roach    /**
1066ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1076ccdf4f0SGreg Roach     *
1086ccdf4f0SGreg Roach     * @return string
1096ccdf4f0SGreg Roach     */
1106ccdf4f0SGreg Roach    public function title(): string
1116ccdf4f0SGreg Roach    {
1126ccdf4f0SGreg Roach        /* I18N: Name of a module */
1136ccdf4f0SGreg Roach        return I18N::translate('Journal');
1146ccdf4f0SGreg Roach    }
1156ccdf4f0SGreg Roach
11650d6f48cSGreg Roach    /**
11750d6f48cSGreg Roach     * Should this block load asynchronously using AJAX?
11850d6f48cSGreg Roach     *
1193caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
12050d6f48cSGreg Roach     *
12150d6f48cSGreg Roach     * @return bool
12250d6f48cSGreg Roach     */
123c1010edaSGreg Roach    public function loadAjax(): bool
124c1010edaSGreg Roach    {
1258c2e8227SGreg Roach        return false;
1268c2e8227SGreg Roach    }
1278c2e8227SGreg Roach
12850d6f48cSGreg Roach    /**
12950d6f48cSGreg Roach     * Can this block be shown on the user’s home page?
13050d6f48cSGreg Roach     *
13150d6f48cSGreg Roach     * @return bool
13250d6f48cSGreg Roach     */
133c1010edaSGreg Roach    public function isUserBlock(): bool
134c1010edaSGreg Roach    {
1358c2e8227SGreg Roach        return true;
1368c2e8227SGreg Roach    }
1378c2e8227SGreg Roach
13850d6f48cSGreg Roach    /**
13950d6f48cSGreg Roach     * Can this block be shown on the tree’s home page?
14050d6f48cSGreg Roach     *
14150d6f48cSGreg Roach     * @return bool
14250d6f48cSGreg Roach     */
14363276d8fSGreg Roach    public function isTreeBlock(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return false;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
14876692c8bSGreg Roach    /**
1496ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
150fe8a65d1SGreg Roach     *
1516ccdf4f0SGreg Roach     * @return ResponseInterface
152fe8a65d1SGreg Roach     */
1536ccdf4f0SGreg Roach    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
154c1010edaSGreg Roach    {
155fe8a65d1SGreg Roach        if (!Auth::check()) {
15659f2f229SGreg Roach            throw new AccessDeniedHttpException();
157fe8a65d1SGreg Roach        }
158fe8a65d1SGreg Roach
159c9e6b699SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
160fe8a65d1SGreg Roach
161c9e6b699SGreg Roach        if ($news_id !== '') {
162ec589cf2SGreg Roach            $row = DB::table('news')
163ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
164ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
165ec589cf2SGreg Roach                ->first();
166fe8a65d1SGreg Roach        } else {
167fe8a65d1SGreg Roach            $row = (object) [
168fe8a65d1SGreg Roach                'body'    => '',
169fe8a65d1SGreg Roach                'subject' => '',
170fe8a65d1SGreg Roach            ];
171fe8a65d1SGreg Roach        }
172fe8a65d1SGreg Roach
173fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
174fe8a65d1SGreg Roach
175147e99aaSGreg Roach        return $this->viewResponse('modules/user_blog/edit', [
176fe8a65d1SGreg Roach            'body'    => $row->body,
177fe8a65d1SGreg Roach            'news_id' => $news_id,
178fe8a65d1SGreg Roach            'subject' => $row->subject,
179fe8a65d1SGreg Roach            'title'   => $title,
180fe8a65d1SGreg Roach        ]);
181fe8a65d1SGreg Roach    }
182fe8a65d1SGreg Roach
183fe8a65d1SGreg Roach    /**
1846ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
185fe8a65d1SGreg Roach     *
1866ccdf4f0SGreg Roach     * @return ResponseInterface
187fe8a65d1SGreg Roach     */
18857ab2231SGreg Roach    public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface
189c1010edaSGreg Roach    {
19057ab2231SGreg Roach        $tree = $request->getAttribute('tree');
19157ab2231SGreg Roach
192fe8a65d1SGreg Roach        if (!Auth::check()) {
19359f2f229SGreg Roach            throw new AccessDeniedHttpException();
194fe8a65d1SGreg Roach        }
195fe8a65d1SGreg Roach
196c9e6b699SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
197c9e6b699SGreg Roach        $subject = $request->getParsedBody()['subject'];
198c9e6b699SGreg Roach        $body    = $request->getParsedBody()['body'];
199fe8a65d1SGreg Roach
20050d6f48cSGreg Roach        $subject = $this->html_service->sanitize($subject);
20150d6f48cSGreg Roach        $body    = $this->html_service->sanitize($body);
20250d6f48cSGreg Roach
203c9e6b699SGreg Roach        if ($news_id !== '') {
204ec589cf2SGreg Roach            DB::table('news')
205ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
206ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
207ec589cf2SGreg Roach                ->update([
208fe8a65d1SGreg Roach                    'body'    => $body,
209ec589cf2SGreg Roach                    'subject' => $subject,
210fe8a65d1SGreg Roach                ]);
211fe8a65d1SGreg Roach        } else {
212ec589cf2SGreg Roach            DB::table('news')->insert([
213fe8a65d1SGreg Roach                'body'    => $body,
214fe8a65d1SGreg Roach                'subject' => $subject,
215fe8a65d1SGreg Roach                'user_id' => Auth::id(),
216fe8a65d1SGreg Roach            ]);
217fe8a65d1SGreg Roach        }
218fe8a65d1SGreg Roach
219*d72b284aSGreg Roach        $url = route('user-page', ['tree' => $tree->name()]);
220fe8a65d1SGreg Roach
2216ccdf4f0SGreg Roach        return redirect($url);
222fe8a65d1SGreg Roach    }
223fe8a65d1SGreg Roach
224fe8a65d1SGreg Roach    /**
2256ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
226fe8a65d1SGreg Roach     *
2276ccdf4f0SGreg Roach     * @return ResponseInterface
228fe8a65d1SGreg Roach     */
22957ab2231SGreg Roach    public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface
230c1010edaSGreg Roach    {
23157ab2231SGreg Roach        $tree    = $request->getAttribute('tree');
232c9e6b699SGreg Roach        $news_id = $request->getQueryParams()['news_id'];
233fe8a65d1SGreg Roach
234ec589cf2SGreg Roach        DB::table('news')
235ec589cf2SGreg Roach            ->where('news_id', '=', $news_id)
236ec589cf2SGreg Roach            ->where('user_id', '=', Auth::id())
237ec589cf2SGreg Roach            ->delete();
238fe8a65d1SGreg Roach
239*d72b284aSGreg Roach        $url = route('user-page', ['tree' => $tree->name()]);
240fe8a65d1SGreg Roach
2416ccdf4f0SGreg Roach        return redirect($url);
242fe8a65d1SGreg Roach    }
2438c2e8227SGreg Roach}
244