xref: /webtrees/app/Module/UserFavoritesModule.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;
218c2e8227SGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
246f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
268e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2809482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
299807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
311e7a7a28SGreg Roachuse Illuminate\Support\Str;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
348c2e8227SGreg Roach
358c2e8227SGreg Roach/**
368c2e8227SGreg Roach * Class UserFavoritesModule
378c2e8227SGreg Roach */
3837eb8894SGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
39c1010edaSGreg Roach{
4049a243cbSGreg Roach    use ModuleBlockTrait;
4149a243cbSGreg Roach
42a4439c01SGreg Roach    /**
430cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
44a4439c01SGreg Roach     *
45a4439c01SGreg Roach     * @return string
46a4439c01SGreg Roach     */
4749a243cbSGreg Roach    public function title(): string
48c1010edaSGreg Roach    {
49bbb76c12SGreg Roach        /* I18N: Name of a module */
50bbb76c12SGreg Roach        return I18N::translate('Favorites');
51a4439c01SGreg Roach    }
52a4439c01SGreg Roach
5349a243cbSGreg Roach    public function description(): string
54c1010edaSGreg Roach    {
55bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
56bbb76c12SGreg Roach        return I18N::translate('Display and manage a user’s favorite pages.');
578c2e8227SGreg Roach    }
588c2e8227SGreg Roach
5976692c8bSGreg Roach    /**
60a4439c01SGreg Roach     * Generate the HTML content of this block.
61a4439c01SGreg Roach     *
62a4439c01SGreg Roach     * @param Tree                 $tree
63a4439c01SGreg Roach     * @param int                  $block_id
643caaa4d2SGreg Roach     * @param string               $context
6576d39c55SGreg Roach     * @param array<string,string> $config
66a4439c01SGreg Roach     *
67a4439c01SGreg Roach     * @return string
68a4439c01SGreg Roach     */
693caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
70c1010edaSGreg Roach    {
718ec20abdSGreg Roach        $content = view('modules/favorites/favorites', [
72a4439c01SGreg Roach            'block_id'    => $block_id,
738ec20abdSGreg Roach            'can_edit'    => true,
74a4439c01SGreg Roach            'favorites'   => $this->getFavorites($tree, Auth::user()),
758ec20abdSGreg Roach            'module_name' => $this->name(),
76a4439c01SGreg Roach            'tree'        => $tree,
77a4439c01SGreg Roach        ]);
78a4439c01SGreg Roach
793caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
80147e99aaSGreg Roach            return view('modules/block-template', [
811e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
82a4439c01SGreg Roach                'id'         => $block_id,
83a4439c01SGreg Roach                'config_url' => '',
8449a243cbSGreg Roach                'title'      => $this->title(),
85a4439c01SGreg Roach                'content'    => $content,
86a4439c01SGreg Roach            ]);
87a4439c01SGreg Roach        }
88b2ce94c6SRico Sonntag
89b2ce94c6SRico Sonntag        return $content;
90a4439c01SGreg Roach    }
91a4439c01SGreg Roach
92a4439c01SGreg Roach    /**
93a4439c01SGreg Roach     * Should this block load asynchronously using AJAX?
943caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
95a4439c01SGreg Roach     *
96a4439c01SGreg Roach     * @return bool
97a4439c01SGreg Roach     */
98c1010edaSGreg Roach    public function loadAjax(): bool
99c1010edaSGreg Roach    {
100a4439c01SGreg Roach        return false;
101a4439c01SGreg Roach    }
102a4439c01SGreg Roach
103a4439c01SGreg Roach    /**
10476692c8bSGreg Roach     * Can this block be shown on the user’s home page?
10576692c8bSGreg Roach     *
10676692c8bSGreg Roach     * @return bool
10776692c8bSGreg Roach     */
108c1010edaSGreg Roach    public function isUserBlock(): bool
109c1010edaSGreg Roach    {
1108c2e8227SGreg Roach        return true;
1118c2e8227SGreg Roach    }
1128c2e8227SGreg Roach
11376692c8bSGreg Roach    /**
11476692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
11576692c8bSGreg Roach     *
11676692c8bSGreg Roach     * @return bool
11776692c8bSGreg Roach     */
11863276d8fSGreg Roach    public function isTreeBlock(): bool
119c1010edaSGreg Roach    {
1208c2e8227SGreg Roach        return false;
1218c2e8227SGreg Roach    }
1228c2e8227SGreg Roach
1238c2e8227SGreg Roach    /**
124a4439c01SGreg Roach     * Get the favorites for a user
1258c2e8227SGreg Roach     *
1269807cf72SGreg Roach     * @param Tree          $tree
127e5a6b4d4SGreg Roach     * @param UserInterface $user
1288c2e8227SGreg Roach     *
129f70bcff5SGreg Roach     * @return array<object>
1308c2e8227SGreg Roach     */
131e5a6b4d4SGreg Roach    public function getFavorites(Tree $tree, UserInterface $user): array
132c1010edaSGreg Roach    {
1330587a5b3SGreg Roach        return DB::table('favorite')
1340587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
135895230eeSGreg Roach            ->where('user_id', '=', $user->id())
1360587a5b3SGreg Roach            ->get()
137f70bcff5SGreg Roach            ->map(static function (object $row) use ($tree): object {
1380587a5b3SGreg Roach                if ($row->xref !== null) {
1396b9cb339SGreg Roach                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
14072fc3a1aSGreg Roach
14172fc3a1aSGreg Roach                    if ($row->record instanceof GedcomRecord && !$row->record->canShowName()) {
14272fc3a1aSGreg Roach                        $row->record = null;
1434da82d42SGreg Roach                        $row->note   = null;
14472fc3a1aSGreg Roach                    }
14525cd7ed9SGreg Roach                } else {
1460587a5b3SGreg Roach                    $row->record = null;
1479807cf72SGreg Roach                }
1489807cf72SGreg Roach
1490587a5b3SGreg Roach                return $row;
1500587a5b3SGreg Roach            })
1510587a5b3SGreg Roach            ->all();
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
15476692c8bSGreg Roach    /**
1556ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
15676692c8bSGreg Roach     *
1576ccdf4f0SGreg Roach     * @return ResponseInterface
15876692c8bSGreg Roach     */
15957ab2231SGreg Roach    public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface
160c1010edaSGreg Roach    {
161b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
162b55cbc6bSGreg Roach        $user   = Validator::attributes($request)->user();
1639f0bdfcdSGreg Roach        $note   = Validator::parsedBody($request)->string('note');
1649f0bdfcdSGreg Roach        $title  = Validator::parsedBody($request)->string('title');
1659f0bdfcdSGreg Roach        $url    = Validator::parsedBody($request)->string('url');
1669f0bdfcdSGreg Roach        $type   = Validator::parsedBody($request)->string('type');
1679f0bdfcdSGreg Roach        $xref   = Validator::parsedBody($request)->string($type . '-xref', '');
1688ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
16925cd7ed9SGreg Roach
170a4439c01SGreg Roach        if (Auth::check()) {
1718ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
172a4439c01SGreg Roach                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
1735c007fdfSGreg Roach            }
1745c007fdfSGreg Roach
1758ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
17625cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $user, $record, $note);
177a4439c01SGreg Roach            }
1788c2e8227SGreg Roach        }
1798840c547SGreg Roach
1808e0e1b25SGreg Roach        $url = route(UserPage::class, ['tree' => $tree->name()]);
181a4439c01SGreg Roach
1826ccdf4f0SGreg Roach        return redirect($url);
183a4439c01SGreg Roach    }
184a4439c01SGreg Roach
185a4439c01SGreg Roach    /**
1866ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
187a4439c01SGreg Roach     *
1886ccdf4f0SGreg Roach     * @return ResponseInterface
189a4439c01SGreg Roach     */
19057ab2231SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface
191c1010edaSGreg Roach    {
192b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
193b55cbc6bSGreg Roach        $user        = Validator::attributes($request)->user();
194b55cbc6bSGreg Roach        $favorite_id = Validator::queryParams($request)->integer('favorite_id');
195a4439c01SGreg Roach
1968ec20abdSGreg Roach        if (Auth::check()) {
1970587a5b3SGreg Roach            DB::table('favorite')
1980587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
199895230eeSGreg Roach                ->where('user_id', '=', $user->id())
2000587a5b3SGreg Roach                ->delete();
2018ec20abdSGreg Roach        }
202a4439c01SGreg Roach
2038e0e1b25SGreg Roach        $url = route(UserPage::class, ['tree' => $tree->name()]);
204a4439c01SGreg Roach
2056ccdf4f0SGreg Roach        return redirect($url);
206a4439c01SGreg Roach    }
207a4439c01SGreg Roach
208a4439c01SGreg Roach    /**
209a4439c01SGreg Roach     * @param Tree          $tree
210e5a6b4d4SGreg Roach     * @param UserInterface $user
211a4439c01SGreg Roach     * @param string        $url
212a4439c01SGreg Roach     * @param string        $title
213a4439c01SGreg Roach     * @param string        $note
21418d7a90dSGreg Roach     *
21518d7a90dSGreg Roach     * @return void
216a4439c01SGreg Roach     */
217e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void
218c1010edaSGreg Roach    {
2190587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
22072cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
221895230eeSGreg Roach            'user_id'   => $user->id(),
222a4439c01SGreg Roach            'url'       => $url,
2230587a5b3SGreg Roach        ], [
2240587a5b3SGreg Roach            'favorite_type' => 'URL',
225a4439c01SGreg Roach            'note'          => $note,
226a4439c01SGreg Roach            'title'         => $title,
227a4439c01SGreg Roach        ]);
228a4439c01SGreg Roach    }
229a4439c01SGreg Roach
230a4439c01SGreg Roach    /**
231a4439c01SGreg Roach     * @param Tree          $tree
232e5a6b4d4SGreg Roach     * @param UserInterface $user
23325cd7ed9SGreg Roach     * @param GedcomRecord  $record
234a4439c01SGreg Roach     * @param string        $note
23518d7a90dSGreg Roach     *
23618d7a90dSGreg Roach     * @return void
237a4439c01SGreg Roach     */
238e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void
239c1010edaSGreg Roach    {
2400587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
24172cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
242895230eeSGreg Roach            'user_id'   => $user->id(),
24325cd7ed9SGreg Roach            'xref'      => $record->xref(),
2440587a5b3SGreg Roach        ], [
24502467d32SGreg Roach            'favorite_type' => $record->tag(),
246a4439c01SGreg Roach            'note'          => $note,
247a4439c01SGreg Roach        ]);
2488c2e8227SGreg Roach    }
2498ec20abdSGreg Roach
250*1ff45046SGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): GedcomRecord|null
2518ec20abdSGreg Roach    {
2528ec20abdSGreg Roach        switch ($type) {
2538ec20abdSGreg Roach            case 'indi':
2546b9cb339SGreg Roach                return Registry::individualFactory()->make($xref, $tree);
2558ec20abdSGreg Roach
2568ec20abdSGreg Roach            case 'fam':
2576b9cb339SGreg Roach                return Registry::familyFactory()->make($xref, $tree);
2588ec20abdSGreg Roach
2598ec20abdSGreg Roach            case 'sour':
2606b9cb339SGreg Roach                return Registry::sourceFactory()->make($xref, $tree);
2618ec20abdSGreg Roach
2628ec20abdSGreg Roach            case 'repo':
2636b9cb339SGreg Roach                return Registry::repositoryFactory()->make($xref, $tree);
2648ec20abdSGreg Roach
2658ec20abdSGreg Roach            case 'obje':
2666b9cb339SGreg Roach                return Registry::mediaFactory()->make($xref, $tree);
2678ec20abdSGreg Roach
2688ec20abdSGreg Roach            default:
2698ec20abdSGreg Roach                return null;
2708ec20abdSGreg Roach        }
2718ec20abdSGreg Roach    }
2728c2e8227SGreg Roach}
273