xref: /webtrees/app/Module/FamilyTreeFavoritesModule.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;
236f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2709482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
289807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
338c2e8227SGreg Roach
348c2e8227SGreg Roach/**
358c2e8227SGreg Roach * Class FamilyTreeFavoritesModule
368c2e8227SGreg Roach */
3737eb8894SGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface
38c1010edaSGreg Roach{
3949a243cbSGreg Roach    use ModuleBlockTrait;
4049a243cbSGreg Roach
4176692c8bSGreg Roach    /**
420cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4376692c8bSGreg Roach     *
4476692c8bSGreg Roach     * @return string
4576692c8bSGreg Roach     */
4649a243cbSGreg Roach    public function title(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Name of a module */
49bbb76c12SGreg Roach        return I18N::translate('Favorites');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
5249a243cbSGreg Roach    public function description(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
55bbb76c12SGreg Roach        return I18N::translate('Display and manage a family tree’s favorite pages.');
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
5876692c8bSGreg Roach    /**
5976692c8bSGreg Roach     * Generate the HTML content of this block.
6076692c8bSGreg Roach     *
61e490cd80SGreg Roach     * @param Tree                 $tree
6276692c8bSGreg Roach     * @param int                  $block_id
633caaa4d2SGreg Roach     * @param string               $context
6476d39c55SGreg Roach     * @param array<string,string> $config
6576692c8bSGreg Roach     *
6676692c8bSGreg Roach     * @return string
6776692c8bSGreg Roach     */
683caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
69c1010edaSGreg Roach    {
708ec20abdSGreg Roach        $content = view('modules/favorites/favorites', [
71a4439c01SGreg Roach            'block_id'    => $block_id,
728ec20abdSGreg Roach            'can_edit'    => Auth::isManager($tree),
73a4439c01SGreg Roach            'favorites'   => $this->getFavorites($tree),
748ec20abdSGreg Roach            'module_name' => $this->name(),
75e490cd80SGreg Roach            'tree'        => $tree,
769807cf72SGreg Roach        ]);
779807cf72SGreg Roach
783caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
79147e99aaSGreg Roach            return view('modules/block-template', [
801e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
819c6524dcSGreg Roach                'id'         => $block_id,
829c6524dcSGreg Roach                'config_url' => '',
8349a243cbSGreg Roach                'title'      => $this->title(),
849c6524dcSGreg Roach                'content'    => $content,
859c6524dcSGreg Roach            ]);
868c2e8227SGreg Roach        }
87b2ce94c6SRico Sonntag
88b2ce94c6SRico Sonntag        return $content;
898c2e8227SGreg Roach    }
908c2e8227SGreg Roach
9176692c8bSGreg Roach    /**
9276692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
933caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
9476692c8bSGreg Roach     *
9576692c8bSGreg Roach     * @return bool
9676692c8bSGreg Roach     */
97c1010edaSGreg Roach    public function loadAjax(): bool
98c1010edaSGreg Roach    {
998c2e8227SGreg Roach        return false;
1008c2e8227SGreg Roach    }
1018c2e8227SGreg Roach
10276692c8bSGreg Roach    /**
10376692c8bSGreg Roach     * Can this block be shown on the user’s home page?
10476692c8bSGreg Roach     *
10576692c8bSGreg Roach     * @return bool
10676692c8bSGreg Roach     */
107c1010edaSGreg Roach    public function isUserBlock(): bool
108c1010edaSGreg Roach    {
1098c2e8227SGreg Roach        return false;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
11276692c8bSGreg Roach    /**
11376692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
11476692c8bSGreg Roach     *
11576692c8bSGreg Roach     * @return bool
11676692c8bSGreg Roach     */
11763276d8fSGreg Roach    public function isTreeBlock(): bool
118c1010edaSGreg Roach    {
1198c2e8227SGreg Roach        return true;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
12276692c8bSGreg Roach    /**
1238ec20abdSGreg Roach     * Get the favorites for a family tree
1248c2e8227SGreg Roach     *
1259807cf72SGreg Roach     * @param Tree $tree
1268c2e8227SGreg Roach     *
127f70bcff5SGreg Roach     * @return array<object>
1288c2e8227SGreg Roach     */
1298f53f488SRico Sonntag    public function getFavorites(Tree $tree): array
130c1010edaSGreg Roach    {
1310587a5b3SGreg Roach        return DB::table('favorite')
1320587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1330587a5b3SGreg Roach            ->whereNull('user_id')
1340587a5b3SGreg Roach            ->get()
135f70bcff5SGreg Roach            ->map(static function (object $row) use ($tree): object {
1360587a5b3SGreg Roach                if ($row->xref !== null) {
1376b9cb339SGreg Roach                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
13872fc3a1aSGreg Roach
13972fc3a1aSGreg Roach                    if ($row->record instanceof GedcomRecord && !$row->record->canShowName()) {
14072fc3a1aSGreg Roach                        $row->record = null;
1414da82d42SGreg Roach                        $row->note   = null;
14272fc3a1aSGreg Roach                    }
14325cd7ed9SGreg Roach                } else {
1440587a5b3SGreg Roach                    $row->record = null;
1459807cf72SGreg Roach                }
1469807cf72SGreg Roach
1470587a5b3SGreg Roach                return $row;
1480587a5b3SGreg Roach            })
1490587a5b3SGreg Roach            ->all();
1508c2e8227SGreg Roach    }
151a4439c01SGreg Roach
152a4439c01SGreg Roach    /**
1536ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
154a4439c01SGreg Roach     *
1556ccdf4f0SGreg Roach     * @return ResponseInterface
156a4439c01SGreg Roach     */
15757ab2231SGreg Roach    public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface
158c1010edaSGreg Roach    {
159b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
160b55cbc6bSGreg Roach        $user   = Validator::attributes($request)->user();
1619f0bdfcdSGreg Roach        $note   = Validator::parsedBody($request)->string('note');
1629f0bdfcdSGreg Roach        $title  = Validator::parsedBody($request)->string('title');
1639f0bdfcdSGreg Roach        $url    = Validator::parsedBody($request)->string('url');
1649f0bdfcdSGreg Roach        $type   = Validator::parsedBody($request)->string('type');
1659f0bdfcdSGreg Roach        $xref   = Validator::parsedBody($request)->string($type . '-xref', '');
1668ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
16725cd7ed9SGreg Roach
168a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
1698ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
170a4439c01SGreg Roach                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
1715c007fdfSGreg Roach            }
1725c007fdfSGreg Roach
1738ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
17425cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $record, $note);
175a4439c01SGreg Roach            }
176a4439c01SGreg Roach        }
177a4439c01SGreg Roach
1788e0e1b25SGreg Roach        $url = route(TreePage::class, ['tree' => $tree->name()]);
179a4439c01SGreg Roach
1806ccdf4f0SGreg Roach        return redirect($url);
181a4439c01SGreg Roach    }
182a4439c01SGreg Roach
183a4439c01SGreg Roach    /**
1846ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
185a4439c01SGreg Roach     *
1866ccdf4f0SGreg Roach     * @return ResponseInterface
187a4439c01SGreg Roach     */
18857ab2231SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface
189c1010edaSGreg Roach    {
190b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
191b55cbc6bSGreg Roach        $user        = Validator::attributes($request)->user();
192748dbe15SGreg Roach        $favorite_id = Validator::queryParams($request)->integer('favorite_id');
193a4439c01SGreg Roach
194a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
1950587a5b3SGreg Roach            DB::table('favorite')
1960587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
1970587a5b3SGreg Roach                ->whereNull('user_id')
1980587a5b3SGreg Roach                ->delete();
199a4439c01SGreg Roach        }
200a4439c01SGreg Roach
2018e0e1b25SGreg Roach        $url = route(TreePage::class, ['tree' => $tree->name()]);
202a4439c01SGreg Roach
2036ccdf4f0SGreg Roach        return redirect($url);
204a4439c01SGreg Roach    }
205a4439c01SGreg Roach
206a4439c01SGreg Roach    /**
207a4439c01SGreg Roach     * @param Tree   $tree
208a4439c01SGreg Roach     * @param string $url
209a4439c01SGreg Roach     * @param string $title
210a4439c01SGreg Roach     * @param string $note
21118d7a90dSGreg Roach     *
21218d7a90dSGreg Roach     * @return void
213a4439c01SGreg Roach     */
214e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note): void
215c1010edaSGreg Roach    {
2160587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
21772cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2180587a5b3SGreg Roach            'user_id'   => null,
219a4439c01SGreg Roach            'url'       => $url,
2200587a5b3SGreg Roach        ], [
2210587a5b3SGreg Roach            'favorite_type' => 'URL',
222a4439c01SGreg Roach            'note'          => $note,
223a4439c01SGreg Roach            'title'         => $title,
224a4439c01SGreg Roach        ]);
225a4439c01SGreg Roach    }
226a4439c01SGreg Roach
227a4439c01SGreg Roach    /**
228a4439c01SGreg Roach     * @param Tree         $tree
22925cd7ed9SGreg Roach     * @param GedcomRecord $record
230a4439c01SGreg Roach     * @param string       $note
23118d7a90dSGreg Roach     *
23218d7a90dSGreg Roach     * @return void
233a4439c01SGreg Roach     */
234e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note): void
235c1010edaSGreg Roach    {
2360587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
23772cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2380587a5b3SGreg Roach            'user_id'   => null,
23925cd7ed9SGreg Roach            'xref'      => $record->xref(),
2400587a5b3SGreg Roach        ], [
24102467d32SGreg Roach            'favorite_type' => $record->tag(),
242a4439c01SGreg Roach            'note'          => $note,
243a4439c01SGreg Roach        ]);
244a4439c01SGreg Roach    }
2458ec20abdSGreg Roach
246*1ff45046SGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): GedcomRecord|null
2478ec20abdSGreg Roach    {
2488ec20abdSGreg Roach        switch ($type) {
2498ec20abdSGreg Roach            case 'indi':
2506b9cb339SGreg Roach                return Registry::individualFactory()->make($xref, $tree);
2518ec20abdSGreg Roach
2528ec20abdSGreg Roach            case 'fam':
2536b9cb339SGreg Roach                return Registry::familyFactory()->make($xref, $tree);
2548ec20abdSGreg Roach
2558ec20abdSGreg Roach            case 'sour':
2566b9cb339SGreg Roach                return Registry::sourceFactory()->make($xref, $tree);
2578ec20abdSGreg Roach
2588ec20abdSGreg Roach            case 'repo':
2596b9cb339SGreg Roach                return Registry::repositoryFactory()->make($xref, $tree);
2608ec20abdSGreg Roach
2618ec20abdSGreg Roach            case 'obje':
2626b9cb339SGreg Roach                return Registry::mediaFactory()->make($xref, $tree);
2638ec20abdSGreg Roach
2648ec20abdSGreg Roach            default:
2658ec20abdSGreg Roach                return null;
2668ec20abdSGreg Roach        }
2678ec20abdSGreg Roach    }
2688c2e8227SGreg Roach}
269