xref: /webtrees/app/Module/UserFavoritesModule.php (revision f70bcff589628705e50ed72a6fc6cbad68558677)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2709482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
289807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
290587a5b3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
338c2e8227SGreg Roach
344ea62551SGreg Roachuse function assert;
354ea62551SGreg Roach
368c2e8227SGreg Roach/**
378c2e8227SGreg Roach * Class UserFavoritesModule
388c2e8227SGreg Roach */
3937eb8894SGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
40c1010edaSGreg Roach{
4149a243cbSGreg Roach    use ModuleBlockTrait;
4249a243cbSGreg Roach
43a4439c01SGreg Roach    /**
440cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
45a4439c01SGreg Roach     *
46a4439c01SGreg Roach     * @return string
47a4439c01SGreg Roach     */
4849a243cbSGreg Roach    public function title(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Name of a module */
51bbb76c12SGreg Roach        return I18N::translate('Favorites');
52a4439c01SGreg Roach    }
53a4439c01SGreg Roach
54a4439c01SGreg Roach    /**
55a4439c01SGreg Roach     * A sentence describing what this module does.
56a4439c01SGreg Roach     *
57a4439c01SGreg Roach     * @return string
58a4439c01SGreg Roach     */
5949a243cbSGreg Roach    public function description(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
62bbb76c12SGreg Roach        return I18N::translate('Display and manage a user’s favorite pages.');
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
6576692c8bSGreg Roach    /**
66a4439c01SGreg Roach     * Generate the HTML content of this block.
67a4439c01SGreg Roach     *
68a4439c01SGreg Roach     * @param Tree          $tree
69a4439c01SGreg Roach     * @param int           $block_id
703caaa4d2SGreg Roach     * @param string        $context
7109482a55SGreg Roach     * @param array<string> $config
72a4439c01SGreg Roach     *
73a4439c01SGreg Roach     * @return string
74a4439c01SGreg Roach     */
753caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
76c1010edaSGreg Roach    {
778ec20abdSGreg Roach        $content = view('modules/favorites/favorites', [
78a4439c01SGreg Roach            'block_id'    => $block_id,
798ec20abdSGreg Roach            'can_edit'    => true,
80a4439c01SGreg Roach            'favorites'   => $this->getFavorites($tree, Auth::user()),
818ec20abdSGreg Roach            'module_name' => $this->name(),
82a4439c01SGreg Roach            'tree'        => $tree,
83a4439c01SGreg Roach        ]);
84a4439c01SGreg Roach
853caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
86147e99aaSGreg Roach            return view('modules/block-template', [
871e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
88a4439c01SGreg Roach                'id'         => $block_id,
89a4439c01SGreg Roach                'config_url' => '',
9049a243cbSGreg Roach                'title'      => $this->title(),
91a4439c01SGreg Roach                'content'    => $content,
92a4439c01SGreg Roach            ]);
93a4439c01SGreg Roach        }
94b2ce94c6SRico Sonntag
95b2ce94c6SRico Sonntag        return $content;
96a4439c01SGreg Roach    }
97a4439c01SGreg Roach
98a4439c01SGreg Roach    /**
99a4439c01SGreg Roach     * Should this block load asynchronously using AJAX?
1003caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
101a4439c01SGreg Roach     *
102a4439c01SGreg Roach     * @return bool
103a4439c01SGreg Roach     */
104c1010edaSGreg Roach    public function loadAjax(): bool
105c1010edaSGreg Roach    {
106a4439c01SGreg Roach        return false;
107a4439c01SGreg Roach    }
108a4439c01SGreg Roach
109a4439c01SGreg Roach    /**
11076692c8bSGreg Roach     * Can this block be shown on the user’s home page?
11176692c8bSGreg Roach     *
11276692c8bSGreg Roach     * @return bool
11376692c8bSGreg Roach     */
114c1010edaSGreg Roach    public function isUserBlock(): bool
115c1010edaSGreg Roach    {
1168c2e8227SGreg Roach        return true;
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
11976692c8bSGreg Roach    /**
12076692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
12176692c8bSGreg Roach     *
12276692c8bSGreg Roach     * @return bool
12376692c8bSGreg Roach     */
12463276d8fSGreg Roach    public function isTreeBlock(): bool
125c1010edaSGreg Roach    {
1268c2e8227SGreg Roach        return false;
1278c2e8227SGreg Roach    }
1288c2e8227SGreg Roach
1298c2e8227SGreg Roach    /**
130a4439c01SGreg Roach     * Get the favorites for a user
1318c2e8227SGreg Roach     *
1329807cf72SGreg Roach     * @param Tree          $tree
133e5a6b4d4SGreg Roach     * @param UserInterface $user
1348c2e8227SGreg Roach     *
135*f70bcff5SGreg Roach     * @return array<object>
1368c2e8227SGreg Roach     */
137e5a6b4d4SGreg Roach    public function getFavorites(Tree $tree, UserInterface $user): array
138c1010edaSGreg Roach    {
1390587a5b3SGreg Roach        return DB::table('favorite')
1400587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
141895230eeSGreg Roach            ->where('user_id', '=', $user->id())
1420587a5b3SGreg Roach            ->get()
143*f70bcff5SGreg Roach            ->map(static function (object $row) use ($tree): object {
1440587a5b3SGreg Roach                if ($row->xref !== null) {
1456b9cb339SGreg Roach                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
14672fc3a1aSGreg Roach
14772fc3a1aSGreg Roach                    if ($row->record instanceof GedcomRecord && !$row->record->canShowName()) {
14872fc3a1aSGreg Roach                        $row->record = null;
1494da82d42SGreg Roach                        $row->note   = null;
15072fc3a1aSGreg Roach                    }
15125cd7ed9SGreg Roach                } else {
1520587a5b3SGreg Roach                    $row->record = null;
1539807cf72SGreg Roach                }
1549807cf72SGreg Roach
1550587a5b3SGreg Roach                return $row;
1560587a5b3SGreg Roach            })
1570587a5b3SGreg Roach            ->all();
1588c2e8227SGreg Roach    }
1598c2e8227SGreg Roach
16076692c8bSGreg Roach    /**
1616ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
16276692c8bSGreg Roach     *
1636ccdf4f0SGreg Roach     * @return ResponseInterface
16476692c8bSGreg Roach     */
16557ab2231SGreg Roach    public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface
166c1010edaSGreg Roach    {
16757ab2231SGreg Roach        $tree = $request->getAttribute('tree');
1684ea62551SGreg Roach        assert($tree instanceof Tree);
1694ea62551SGreg Roach
17057ab2231SGreg Roach        $user   = $request->getAttribute('user');
171b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
172eb235819SGreg Roach
173eb235819SGreg Roach        $note  = $params['note'];
174eb235819SGreg Roach        $title = $params['title'];
175eb235819SGreg Roach        $url   = $params['url'];
176eb235819SGreg Roach        $type  = $params['type'];
177eb235819SGreg Roach        $xref  = $params[$type . '-xref'] ?? '';
1788840c547SGreg Roach
1798ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
18025cd7ed9SGreg Roach
181a4439c01SGreg Roach        if (Auth::check()) {
1828ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
183a4439c01SGreg Roach                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
1845c007fdfSGreg Roach            }
1855c007fdfSGreg Roach
1868ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
18725cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $user, $record, $note);
188a4439c01SGreg Roach            }
1898c2e8227SGreg Roach        }
1908840c547SGreg Roach
1918e0e1b25SGreg Roach        $url = route(UserPage::class, ['tree' => $tree->name()]);
192a4439c01SGreg Roach
1936ccdf4f0SGreg Roach        return redirect($url);
194a4439c01SGreg Roach    }
195a4439c01SGreg Roach
196a4439c01SGreg Roach    /**
1976ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
198a4439c01SGreg Roach     *
1996ccdf4f0SGreg Roach     * @return ResponseInterface
200a4439c01SGreg Roach     */
20157ab2231SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface
202c1010edaSGreg Roach    {
20357ab2231SGreg Roach        $tree = $request->getAttribute('tree');
2044ea62551SGreg Roach        assert($tree instanceof Tree);
2054ea62551SGreg Roach
20657ab2231SGreg Roach        $user        = $request->getAttribute('user');
207c50a90beSGreg Roach        $favorite_id = $request->getQueryParams()['favorite_id'];
208a4439c01SGreg Roach
2098ec20abdSGreg Roach        if (Auth::check()) {
2100587a5b3SGreg Roach            DB::table('favorite')
2110587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
212895230eeSGreg Roach                ->where('user_id', '=', $user->id())
2130587a5b3SGreg Roach                ->delete();
2148ec20abdSGreg Roach        }
215a4439c01SGreg Roach
2168e0e1b25SGreg Roach        $url = route(UserPage::class, ['tree' => $tree->name()]);
217a4439c01SGreg Roach
2186ccdf4f0SGreg Roach        return redirect($url);
219a4439c01SGreg Roach    }
220a4439c01SGreg Roach
221a4439c01SGreg Roach    /**
222a4439c01SGreg Roach     * @param Tree          $tree
223e5a6b4d4SGreg Roach     * @param UserInterface $user
224a4439c01SGreg Roach     * @param string        $url
225a4439c01SGreg Roach     * @param string        $title
226a4439c01SGreg Roach     * @param string        $note
22718d7a90dSGreg Roach     *
22818d7a90dSGreg Roach     * @return void
229a4439c01SGreg Roach     */
230e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void
231c1010edaSGreg Roach    {
2320587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
23372cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
234895230eeSGreg Roach            'user_id'   => $user->id(),
235a4439c01SGreg Roach            'url'       => $url,
2360587a5b3SGreg Roach        ], [
2370587a5b3SGreg Roach            'favorite_type' => 'URL',
238a4439c01SGreg Roach            'note'          => $note,
239a4439c01SGreg Roach            'title'         => $title,
240a4439c01SGreg Roach        ]);
241a4439c01SGreg Roach    }
242a4439c01SGreg Roach
243a4439c01SGreg Roach    /**
244a4439c01SGreg Roach     * @param Tree          $tree
245e5a6b4d4SGreg Roach     * @param UserInterface $user
24625cd7ed9SGreg Roach     * @param GedcomRecord  $record
247a4439c01SGreg Roach     * @param string        $note
24818d7a90dSGreg Roach     *
24918d7a90dSGreg Roach     * @return void
250a4439c01SGreg Roach     */
251e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void
252c1010edaSGreg Roach    {
2530587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
25472cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
255895230eeSGreg Roach            'user_id'   => $user->id(),
25625cd7ed9SGreg Roach            'xref'      => $record->xref(),
2570587a5b3SGreg Roach        ], [
25802467d32SGreg Roach            'favorite_type' => $record->tag(),
259a4439c01SGreg Roach            'note'          => $note,
260a4439c01SGreg Roach        ]);
2618c2e8227SGreg Roach    }
2628ec20abdSGreg Roach
2638ec20abdSGreg Roach    /**
2648ec20abdSGreg Roach     * @param string $type
2658ec20abdSGreg Roach     * @param string $xref
2668ec20abdSGreg Roach     * @param Tree   $tree
2678ec20abdSGreg Roach     *
2688ec20abdSGreg Roach     * @return GedcomRecord|null
2698ec20abdSGreg Roach     */
2708ec20abdSGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
2718ec20abdSGreg Roach    {
2728ec20abdSGreg Roach        switch ($type) {
2738ec20abdSGreg Roach            case 'indi':
2746b9cb339SGreg Roach                return Registry::individualFactory()->make($xref, $tree);
2758ec20abdSGreg Roach
2768ec20abdSGreg Roach            case 'fam':
2776b9cb339SGreg Roach                return Registry::familyFactory()->make($xref, $tree);
2788ec20abdSGreg Roach
2798ec20abdSGreg Roach            case 'sour':
2806b9cb339SGreg Roach                return Registry::sourceFactory()->make($xref, $tree);
2818ec20abdSGreg Roach
2828ec20abdSGreg Roach            case 'repo':
2836b9cb339SGreg Roach                return Registry::repositoryFactory()->make($xref, $tree);
2848ec20abdSGreg Roach
2858ec20abdSGreg Roach            case 'obje':
2866b9cb339SGreg Roach                return Registry::mediaFactory()->make($xref, $tree);
2878ec20abdSGreg Roach
2888ec20abdSGreg Roach            default:
2898ec20abdSGreg Roach                return null;
2908ec20abdSGreg Roach        }
2918ec20abdSGreg Roach    }
2928c2e8227SGreg Roach}
293