xref: /webtrees/app/Module/UserJournalModule.php (revision 982e6c5587f167a514f0634a1e3d44fc2835f003)
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 */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2550d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService;
26fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
27ec589cf2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
281e7a7a28SGreg Roachuse Illuminate\Support\Str;
295229eadeSGreg Roachuse InvalidArgumentException;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32ca52a408SGreg Roachuse stdClass;
33fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
34f3874e19SGreg Roach
355229eadeSGreg Roachuse function assert;
368c2e8227SGreg Roach
378c2e8227SGreg Roach/**
388c2e8227SGreg Roach * Class UserJournalModule
398c2e8227SGreg Roach */
4037eb8894SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface
41c1010edaSGreg Roach{
4249a243cbSGreg Roach    use ModuleBlockTrait;
4349a243cbSGreg Roach
4450d6f48cSGreg Roach    /** @var HtmlService */
4550d6f48cSGreg Roach    private $html_service;
4650d6f48cSGreg Roach
4750d6f48cSGreg Roach    /**
4850d6f48cSGreg Roach     * HtmlBlockModule bootstrap.
4950d6f48cSGreg Roach     *
5050d6f48cSGreg Roach     * @param HtmlService $html_service
5150d6f48cSGreg Roach     */
52*982e6c55SGreg Roach    public function boot(HtmlService $html_service): void
5350d6f48cSGreg Roach    {
5450d6f48cSGreg Roach        $this->html_service = $html_service;
5550d6f48cSGreg Roach    }
5650d6f48cSGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * A sentence describing what this module does.
5976692c8bSGreg Roach     *
6076692c8bSGreg Roach     * @return string
6176692c8bSGreg Roach     */
6249a243cbSGreg Roach    public function description(): string
63c1010edaSGreg Roach    {
64bbb76c12SGreg Roach        /* I18N: Description of the “Journal” module */
65bbb76c12SGreg Roach        return I18N::translate('A private area to record notes or keep a journal.');
668c2e8227SGreg Roach    }
678c2e8227SGreg Roach
6876692c8bSGreg Roach    /**
6976692c8bSGreg Roach     * Generate the HTML content of this block.
7076692c8bSGreg Roach     *
71e490cd80SGreg Roach     * @param Tree     $tree
7276692c8bSGreg Roach     * @param int      $block_id
733caaa4d2SGreg Roach     * @param string   $context
743caaa4d2SGreg Roach     * @param string[] $config
7576692c8bSGreg Roach     *
7676692c8bSGreg Roach     * @return string
7776692c8bSGreg Roach     */
783caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
79c1010edaSGreg Roach    {
80ec589cf2SGreg Roach        $articles = DB::table('news')
81ec589cf2SGreg Roach            ->where('user_id', '=', Auth::id())
82ec589cf2SGreg Roach            ->orderByDesc('updated')
83ca52a408SGreg Roach            ->get()
840b5fd0a6SGreg Roach            ->map(static function (stdClass $row): stdClass {
854459dc9aSGreg Roach                $row->updated = Carbon::make($row->updated);
86ca52a408SGreg Roach
87ca52a408SGreg Roach                return $row;
88ca52a408SGreg Roach            });
89ec589cf2SGreg Roach
90147e99aaSGreg Roach        $content = view('modules/user_blog/list', [
91fe8a65d1SGreg Roach            'articles' => $articles,
92fe8a65d1SGreg Roach            'block_id' => $block_id,
93fe8a65d1SGreg Roach            'limit'    => 5,
94fe8a65d1SGreg Roach        ]);
958c2e8227SGreg Roach
963caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
97147e99aaSGreg Roach            return view('modules/block-template', [
981e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
999c6524dcSGreg Roach                'id'         => $block_id,
1009c6524dcSGreg Roach                'config_url' => '',
10149a243cbSGreg Roach                'title'      => $this->title(),
1029c6524dcSGreg Roach                'content'    => $content,
1039c6524dcSGreg Roach            ]);
1048c2e8227SGreg Roach        }
105b2ce94c6SRico Sonntag
106b2ce94c6SRico Sonntag        return $content;
1078c2e8227SGreg Roach    }
1088c2e8227SGreg Roach
1096ccdf4f0SGreg Roach    /**
1106ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1116ccdf4f0SGreg Roach     *
1126ccdf4f0SGreg Roach     * @return string
1136ccdf4f0SGreg Roach     */
1146ccdf4f0SGreg Roach    public function title(): string
1156ccdf4f0SGreg Roach    {
1166ccdf4f0SGreg Roach        /* I18N: Name of a module */
1176ccdf4f0SGreg Roach        return I18N::translate('Journal');
1186ccdf4f0SGreg Roach    }
1196ccdf4f0SGreg Roach
12050d6f48cSGreg Roach    /**
12150d6f48cSGreg Roach     * Should this block load asynchronously using AJAX?
12250d6f48cSGreg Roach     *
1233caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
12450d6f48cSGreg Roach     *
12550d6f48cSGreg Roach     * @return bool
12650d6f48cSGreg Roach     */
127c1010edaSGreg Roach    public function loadAjax(): bool
128c1010edaSGreg Roach    {
1298c2e8227SGreg Roach        return false;
1308c2e8227SGreg Roach    }
1318c2e8227SGreg Roach
13250d6f48cSGreg Roach    /**
13350d6f48cSGreg Roach     * Can this block be shown on the user’s home page?
13450d6f48cSGreg Roach     *
13550d6f48cSGreg Roach     * @return bool
13650d6f48cSGreg Roach     */
137c1010edaSGreg Roach    public function isUserBlock(): bool
138c1010edaSGreg Roach    {
1398c2e8227SGreg Roach        return true;
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
14250d6f48cSGreg Roach    /**
14350d6f48cSGreg Roach     * Can this block be shown on the tree’s home page?
14450d6f48cSGreg Roach     *
14550d6f48cSGreg Roach     * @return bool
14650d6f48cSGreg Roach     */
14763276d8fSGreg Roach    public function isTreeBlock(): bool
148c1010edaSGreg Roach    {
1498c2e8227SGreg Roach        return false;
1508c2e8227SGreg Roach    }
1518c2e8227SGreg Roach
15276692c8bSGreg Roach    /**
1536ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
154fe8a65d1SGreg Roach     *
1556ccdf4f0SGreg Roach     * @return ResponseInterface
156fe8a65d1SGreg Roach     */
1576ccdf4f0SGreg Roach    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
158c1010edaSGreg Roach    {
159fe8a65d1SGreg Roach        if (!Auth::check()) {
16059f2f229SGreg Roach            throw new AccessDeniedHttpException();
161fe8a65d1SGreg Roach        }
162fe8a65d1SGreg Roach
163c9e6b699SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
164fe8a65d1SGreg Roach
165c9e6b699SGreg Roach        if ($news_id !== '') {
166ec589cf2SGreg Roach            $row = DB::table('news')
167ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
168ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
169ec589cf2SGreg Roach                ->first();
170fe8a65d1SGreg Roach        } else {
171fe8a65d1SGreg Roach            $row = (object) [
172fe8a65d1SGreg Roach                'body'    => '',
173fe8a65d1SGreg Roach                'subject' => '',
174fe8a65d1SGreg Roach            ];
175fe8a65d1SGreg Roach        }
176fe8a65d1SGreg Roach
177fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
178fe8a65d1SGreg Roach
179147e99aaSGreg Roach        return $this->viewResponse('modules/user_blog/edit', [
180fe8a65d1SGreg Roach            'body'    => $row->body,
181fe8a65d1SGreg Roach            'news_id' => $news_id,
182fe8a65d1SGreg Roach            'subject' => $row->subject,
183fe8a65d1SGreg Roach            'title'   => $title,
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    {
19457ab2231SGreg Roach        $tree = $request->getAttribute('tree');
1955229eadeSGreg Roach        assert($tree instanceof Tree, new InvalidArgumentException());
19657ab2231SGreg Roach
197fe8a65d1SGreg Roach        if (!Auth::check()) {
19859f2f229SGreg Roach            throw new AccessDeniedHttpException();
199fe8a65d1SGreg Roach        }
200fe8a65d1SGreg Roach
201c9e6b699SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
202c9e6b699SGreg Roach        $subject = $request->getParsedBody()['subject'];
203c9e6b699SGreg Roach        $body    = $request->getParsedBody()['body'];
204fe8a65d1SGreg Roach
20550d6f48cSGreg Roach        $subject = $this->html_service->sanitize($subject);
20650d6f48cSGreg Roach        $body    = $this->html_service->sanitize($body);
20750d6f48cSGreg Roach
208c9e6b699SGreg Roach        if ($news_id !== '') {
209ec589cf2SGreg Roach            DB::table('news')
210ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
211ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
212ec589cf2SGreg Roach                ->update([
213fe8a65d1SGreg Roach                    'body'    => $body,
214ec589cf2SGreg Roach                    'subject' => $subject,
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
224d72b284aSGreg Roach        $url = route('user-page', ['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    {
23657ab2231SGreg Roach        $tree    = $request->getAttribute('tree');
237c9e6b699SGreg Roach        $news_id = $request->getQueryParams()['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
244d72b284aSGreg Roach        $url = route('user-page', ['tree' => $tree->name()]);
245fe8a65d1SGreg Roach
2466ccdf4f0SGreg Roach        return redirect($url);
247fe8a65d1SGreg Roach    }
2488c2e8227SGreg Roach}
249