xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision d11be7027e34e3121be11cc025421873364403f9)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5*d11be702SGreg 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;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
248e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2609482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
279807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
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
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
5276692c8bSGreg Roach    /**
5376692c8bSGreg Roach     * A sentence describing what this module does.
5476692c8bSGreg Roach     *
5576692c8bSGreg Roach     * @return string
5676692c8bSGreg Roach     */
5749a243cbSGreg Roach    public function description(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
60bbb76c12SGreg Roach        return I18N::translate('Display and manage a family tree’s favorite pages.');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
6376692c8bSGreg Roach    /**
6476692c8bSGreg Roach     * Generate the HTML content of this block.
6576692c8bSGreg Roach     *
66e490cd80SGreg Roach     * @param Tree                 $tree
6776692c8bSGreg Roach     * @param int                  $block_id
683caaa4d2SGreg Roach     * @param string               $context
6976d39c55SGreg Roach     * @param array<string,string> $config
7076692c8bSGreg Roach     *
7176692c8bSGreg Roach     * @return string
7276692c8bSGreg Roach     */
733caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
74c1010edaSGreg Roach    {
758ec20abdSGreg Roach        $content = view('modules/favorites/favorites', [
76a4439c01SGreg Roach            'block_id'    => $block_id,
778ec20abdSGreg Roach            'can_edit'    => Auth::isManager($tree),
78a4439c01SGreg Roach            'favorites'   => $this->getFavorites($tree),
798ec20abdSGreg Roach            'module_name' => $this->name(),
80e490cd80SGreg Roach            'tree'        => $tree,
819807cf72SGreg Roach        ]);
829807cf72SGreg Roach
833caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
84147e99aaSGreg Roach            return view('modules/block-template', [
851e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
869c6524dcSGreg Roach                'id'         => $block_id,
879c6524dcSGreg Roach                'config_url' => '',
8849a243cbSGreg Roach                'title'      => $this->title(),
899c6524dcSGreg Roach                'content'    => $content,
909c6524dcSGreg Roach            ]);
918c2e8227SGreg Roach        }
92b2ce94c6SRico Sonntag
93b2ce94c6SRico Sonntag        return $content;
948c2e8227SGreg Roach    }
958c2e8227SGreg Roach
9676692c8bSGreg Roach    /**
9776692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
983caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
9976692c8bSGreg Roach     *
10076692c8bSGreg Roach     * @return bool
10176692c8bSGreg Roach     */
102c1010edaSGreg Roach    public function loadAjax(): bool
103c1010edaSGreg Roach    {
1048c2e8227SGreg Roach        return false;
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
10776692c8bSGreg Roach    /**
10876692c8bSGreg Roach     * Can this block be shown on the user’s home page?
10976692c8bSGreg Roach     *
11076692c8bSGreg Roach     * @return bool
11176692c8bSGreg Roach     */
112c1010edaSGreg Roach    public function isUserBlock(): bool
113c1010edaSGreg Roach    {
1148c2e8227SGreg Roach        return false;
1158c2e8227SGreg Roach    }
1168c2e8227SGreg Roach
11776692c8bSGreg Roach    /**
11876692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
11976692c8bSGreg Roach     *
12076692c8bSGreg Roach     * @return bool
12176692c8bSGreg Roach     */
12263276d8fSGreg Roach    public function isTreeBlock(): bool
123c1010edaSGreg Roach    {
1248c2e8227SGreg Roach        return true;
1258c2e8227SGreg Roach    }
1268c2e8227SGreg Roach
12776692c8bSGreg Roach    /**
1288ec20abdSGreg Roach     * Get the favorites for a family tree
1298c2e8227SGreg Roach     *
1309807cf72SGreg Roach     * @param Tree $tree
1318c2e8227SGreg Roach     *
132f70bcff5SGreg Roach     * @return array<object>
1338c2e8227SGreg Roach     */
1348f53f488SRico Sonntag    public function getFavorites(Tree $tree): array
135c1010edaSGreg Roach    {
1360587a5b3SGreg Roach        return DB::table('favorite')
1370587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1380587a5b3SGreg Roach            ->whereNull('user_id')
1390587a5b3SGreg Roach            ->get()
140f70bcff5SGreg Roach            ->map(static function (object $row) use ($tree): object {
1410587a5b3SGreg Roach                if ($row->xref !== null) {
1426b9cb339SGreg Roach                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
14372fc3a1aSGreg Roach
14472fc3a1aSGreg Roach                    if ($row->record instanceof GedcomRecord && !$row->record->canShowName()) {
14572fc3a1aSGreg Roach                        $row->record = null;
1464da82d42SGreg Roach                        $row->note   = null;
14772fc3a1aSGreg Roach                    }
14825cd7ed9SGreg Roach                } else {
1490587a5b3SGreg Roach                    $row->record = null;
1509807cf72SGreg Roach                }
1519807cf72SGreg Roach
1520587a5b3SGreg Roach                return $row;
1530587a5b3SGreg Roach            })
1540587a5b3SGreg Roach            ->all();
1558c2e8227SGreg Roach    }
156a4439c01SGreg Roach
157a4439c01SGreg Roach    /**
1586ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
159a4439c01SGreg Roach     *
1606ccdf4f0SGreg Roach     * @return ResponseInterface
161a4439c01SGreg Roach     */
16257ab2231SGreg Roach    public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface
163c1010edaSGreg Roach    {
164b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
165b55cbc6bSGreg Roach        $user   = Validator::attributes($request)->user();
1669f0bdfcdSGreg Roach        $note   = Validator::parsedBody($request)->string('note');
1679f0bdfcdSGreg Roach        $title  = Validator::parsedBody($request)->string('title');
1689f0bdfcdSGreg Roach        $url    = Validator::parsedBody($request)->string('url');
1699f0bdfcdSGreg Roach        $type   = Validator::parsedBody($request)->string('type');
1709f0bdfcdSGreg Roach        $xref   = Validator::parsedBody($request)->string($type . '-xref', '');
1718ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
17225cd7ed9SGreg Roach
173a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
1748ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
175a4439c01SGreg Roach                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
1765c007fdfSGreg Roach            }
1775c007fdfSGreg Roach
1788ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
17925cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $record, $note);
180a4439c01SGreg Roach            }
181a4439c01SGreg Roach        }
182a4439c01SGreg Roach
1838e0e1b25SGreg Roach        $url = route(TreePage::class, ['tree' => $tree->name()]);
184a4439c01SGreg Roach
1856ccdf4f0SGreg Roach        return redirect($url);
186a4439c01SGreg Roach    }
187a4439c01SGreg Roach
188a4439c01SGreg Roach    /**
1896ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
190a4439c01SGreg Roach     *
1916ccdf4f0SGreg Roach     * @return ResponseInterface
192a4439c01SGreg Roach     */
19357ab2231SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface
194c1010edaSGreg Roach    {
195b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
196b55cbc6bSGreg Roach        $user        = Validator::attributes($request)->user();
197748dbe15SGreg Roach        $favorite_id = Validator::queryParams($request)->integer('favorite_id');
198a4439c01SGreg Roach
199a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
2000587a5b3SGreg Roach            DB::table('favorite')
2010587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
2020587a5b3SGreg Roach                ->whereNull('user_id')
2030587a5b3SGreg Roach                ->delete();
204a4439c01SGreg Roach        }
205a4439c01SGreg Roach
2068e0e1b25SGreg Roach        $url = route(TreePage::class, ['tree' => $tree->name()]);
207a4439c01SGreg Roach
2086ccdf4f0SGreg Roach        return redirect($url);
209a4439c01SGreg Roach    }
210a4439c01SGreg Roach
211a4439c01SGreg Roach    /**
212a4439c01SGreg Roach     * @param Tree   $tree
213a4439c01SGreg Roach     * @param string $url
214a4439c01SGreg Roach     * @param string $title
215a4439c01SGreg Roach     * @param string $note
21618d7a90dSGreg Roach     *
21718d7a90dSGreg Roach     * @return void
218a4439c01SGreg Roach     */
219e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note): void
220c1010edaSGreg Roach    {
2210587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
22272cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2230587a5b3SGreg Roach            'user_id'   => null,
224a4439c01SGreg Roach            'url'       => $url,
2250587a5b3SGreg Roach        ], [
2260587a5b3SGreg Roach            'favorite_type' => 'URL',
227a4439c01SGreg Roach            'note'          => $note,
228a4439c01SGreg Roach            'title'         => $title,
229a4439c01SGreg Roach        ]);
230a4439c01SGreg Roach    }
231a4439c01SGreg Roach
232a4439c01SGreg Roach    /**
233a4439c01SGreg Roach     * @param Tree         $tree
23425cd7ed9SGreg Roach     * @param GedcomRecord $record
235a4439c01SGreg Roach     * @param string       $note
23618d7a90dSGreg Roach     *
23718d7a90dSGreg Roach     * @return void
238a4439c01SGreg Roach     */
239e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note): void
240c1010edaSGreg Roach    {
2410587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
24272cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2430587a5b3SGreg Roach            'user_id'   => null,
24425cd7ed9SGreg Roach            'xref'      => $record->xref(),
2450587a5b3SGreg Roach        ], [
24602467d32SGreg Roach            'favorite_type' => $record->tag(),
247a4439c01SGreg Roach            'note'          => $note,
248a4439c01SGreg Roach        ]);
249a4439c01SGreg Roach    }
2508ec20abdSGreg Roach
2518ec20abdSGreg Roach    /**
2528ec20abdSGreg Roach     * @param string $type
2538ec20abdSGreg Roach     * @param string $xref
2548ec20abdSGreg Roach     * @param Tree   $tree
2558ec20abdSGreg Roach     *
2568ec20abdSGreg Roach     * @return GedcomRecord|null
2578ec20abdSGreg Roach     */
2588ec20abdSGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
2598ec20abdSGreg Roach    {
2608ec20abdSGreg Roach        switch ($type) {
2618ec20abdSGreg Roach            case 'indi':
2626b9cb339SGreg Roach                return Registry::individualFactory()->make($xref, $tree);
2638ec20abdSGreg Roach
2648ec20abdSGreg Roach            case 'fam':
2656b9cb339SGreg Roach                return Registry::familyFactory()->make($xref, $tree);
2668ec20abdSGreg Roach
2678ec20abdSGreg Roach            case 'sour':
2686b9cb339SGreg Roach                return Registry::sourceFactory()->make($xref, $tree);
2698ec20abdSGreg Roach
2708ec20abdSGreg Roach            case 'repo':
2716b9cb339SGreg Roach                return Registry::repositoryFactory()->make($xref, $tree);
2728ec20abdSGreg Roach
2738ec20abdSGreg Roach            case 'obje':
2746b9cb339SGreg Roach                return Registry::mediaFactory()->make($xref, $tree);
2758ec20abdSGreg Roach
2768ec20abdSGreg Roach            default:
2778ec20abdSGreg Roach                return null;
2788ec20abdSGreg Roach        }
2798ec20abdSGreg Roach    }
2808c2e8227SGreg Roach}
281