xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
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;
218c2e8227SGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
238ec20abdSGreg Roachuse Fisharebest\Webtrees\Family;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
268ec20abdSGreg Roachuse Fisharebest\Webtrees\Individual;
278ec20abdSGreg Roachuse Fisharebest\Webtrees\Media;
288ec20abdSGreg Roachuse Fisharebest\Webtrees\Repository;
298ec20abdSGreg Roachuse Fisharebest\Webtrees\Source;
309807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
310587a5b3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
321e7a7a28SGreg Roachuse Illuminate\Support\Str;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
346ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
359807cf72SGreg Roachuse stdClass;
368c2e8227SGreg Roach
374ea62551SGreg Roachuse function assert;
384ea62551SGreg Roach
398c2e8227SGreg Roach/**
408c2e8227SGreg Roach * Class FamilyTreeFavoritesModule
418c2e8227SGreg Roach */
4237eb8894SGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleBlockTrait;
4549a243cbSGreg Roach
4676692c8bSGreg Roach    /**
470cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4876692c8bSGreg Roach     *
4976692c8bSGreg Roach     * @return string
5076692c8bSGreg Roach     */
5149a243cbSGreg Roach    public function title(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Name of a module */
54bbb76c12SGreg Roach        return I18N::translate('Favorites');
558c2e8227SGreg Roach    }
568c2e8227SGreg 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 “Favorites” module */
65bbb76c12SGreg Roach        return I18N::translate('Display and manage a family tree’s favorite pages.');
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    {
808ec20abdSGreg Roach        $content = view('modules/favorites/favorites', [
81a4439c01SGreg Roach            'block_id'    => $block_id,
828ec20abdSGreg Roach            'can_edit'    => Auth::isManager($tree),
83a4439c01SGreg Roach            'favorites'   => $this->getFavorites($tree),
848ec20abdSGreg Roach            'module_name' => $this->name(),
85e490cd80SGreg Roach            'tree'        => $tree,
869807cf72SGreg Roach        ]);
879807cf72SGreg Roach
883caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
89147e99aaSGreg Roach            return view('modules/block-template', [
901e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
919c6524dcSGreg Roach                'id'         => $block_id,
929c6524dcSGreg Roach                'config_url' => '',
9349a243cbSGreg Roach                'title'      => $this->title(),
949c6524dcSGreg Roach                'content'    => $content,
959c6524dcSGreg Roach            ]);
968c2e8227SGreg Roach        }
97b2ce94c6SRico Sonntag
98b2ce94c6SRico Sonntag        return $content;
998c2e8227SGreg Roach    }
1008c2e8227SGreg Roach
10176692c8bSGreg Roach    /**
10276692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
1033caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
10476692c8bSGreg Roach     *
10576692c8bSGreg Roach     * @return bool
10676692c8bSGreg Roach     */
107c1010edaSGreg Roach    public function loadAjax(): bool
108c1010edaSGreg Roach    {
1098c2e8227SGreg Roach        return false;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
11276692c8bSGreg Roach    /**
11376692c8bSGreg Roach     * Can this block be shown on the user’s home page?
11476692c8bSGreg Roach     *
11576692c8bSGreg Roach     * @return bool
11676692c8bSGreg Roach     */
117c1010edaSGreg Roach    public function isUserBlock(): bool
118c1010edaSGreg Roach    {
1198c2e8227SGreg Roach        return false;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
12276692c8bSGreg Roach    /**
12376692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
12476692c8bSGreg Roach     *
12576692c8bSGreg Roach     * @return bool
12676692c8bSGreg Roach     */
12763276d8fSGreg Roach    public function isTreeBlock(): bool
128c1010edaSGreg Roach    {
1298c2e8227SGreg Roach        return true;
1308c2e8227SGreg Roach    }
1318c2e8227SGreg Roach
13276692c8bSGreg Roach    /**
1338ec20abdSGreg Roach     * Get the favorites for a family tree
1348c2e8227SGreg Roach     *
1359807cf72SGreg Roach     * @param Tree $tree
1368c2e8227SGreg Roach     *
1379807cf72SGreg Roach     * @return stdClass[]
1388c2e8227SGreg Roach     */
1398f53f488SRico Sonntag    public function getFavorites(Tree $tree): array
140c1010edaSGreg Roach    {
1410587a5b3SGreg Roach        return DB::table('favorite')
1420587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1430587a5b3SGreg Roach            ->whereNull('user_id')
1440587a5b3SGreg Roach            ->get()
1450b5fd0a6SGreg Roach            ->map(static function (stdClass $row) use ($tree): stdClass {
1460587a5b3SGreg Roach                if ($row->xref !== null) {
1470587a5b3SGreg Roach                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
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    {
16457ab2231SGreg Roach        $tree = $request->getAttribute('tree');
1654ea62551SGreg Roach        assert($tree instanceof Tree);
1664ea62551SGreg Roach
16757ab2231SGreg Roach        $user   = $request->getAttribute('user');
168*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
169eb235819SGreg Roach
170eb235819SGreg Roach        $note  = $params['note'];
171eb235819SGreg Roach        $title = $params['title'];
172eb235819SGreg Roach        $url   = $params['url'];
173eb235819SGreg Roach        $type  = $params['type'];
174eb235819SGreg Roach        $xref  = $params[$type . '-xref'] ?? '';
175a4439c01SGreg Roach
1768ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
17725cd7ed9SGreg Roach
178a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
1798ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
180a4439c01SGreg Roach                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
1815c007fdfSGreg Roach            }
1825c007fdfSGreg Roach
1838ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
18425cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $record, $note);
185a4439c01SGreg Roach            }
186a4439c01SGreg Roach        }
187a4439c01SGreg Roach
188d72b284aSGreg Roach        $url = route('tree-page', ['tree' => $tree->name()]);
189a4439c01SGreg Roach
1906ccdf4f0SGreg Roach        return redirect($url);
191a4439c01SGreg Roach    }
192a4439c01SGreg Roach
193a4439c01SGreg Roach    /**
1946ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
195a4439c01SGreg Roach     *
1966ccdf4f0SGreg Roach     * @return ResponseInterface
197a4439c01SGreg Roach     */
19857ab2231SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface
199c1010edaSGreg Roach    {
20057ab2231SGreg Roach        $tree = $request->getAttribute('tree');
2014ea62551SGreg Roach        assert($tree instanceof Tree);
2024ea62551SGreg Roach
20357ab2231SGreg Roach        $user        = $request->getAttribute('user');
204eb235819SGreg Roach        $favorite_id = $request->getQueryParams()['favorite_id'];
205a4439c01SGreg Roach
206a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
2070587a5b3SGreg Roach            DB::table('favorite')
2080587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
2090587a5b3SGreg Roach                ->whereNull('user_id')
2100587a5b3SGreg Roach                ->delete();
211a4439c01SGreg Roach        }
212a4439c01SGreg Roach
213d72b284aSGreg Roach        $url = route('tree-page', ['tree' => $tree->name()]);
214a4439c01SGreg Roach
2156ccdf4f0SGreg Roach        return redirect($url);
216a4439c01SGreg Roach    }
217a4439c01SGreg Roach
218a4439c01SGreg Roach    /**
219a4439c01SGreg Roach     * @param Tree   $tree
220a4439c01SGreg Roach     * @param string $url
221a4439c01SGreg Roach     * @param string $title
222a4439c01SGreg Roach     * @param string $note
22318d7a90dSGreg Roach     *
22418d7a90dSGreg Roach     * @return void
225a4439c01SGreg Roach     */
226e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note): void
227c1010edaSGreg Roach    {
2280587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
22972cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2300587a5b3SGreg Roach            'user_id'   => null,
231a4439c01SGreg Roach            'url'       => $url,
2320587a5b3SGreg Roach        ], [
2330587a5b3SGreg Roach            'favorite_type' => 'URL',
234a4439c01SGreg Roach            'note'          => $note,
235a4439c01SGreg Roach            'title'         => $title,
236a4439c01SGreg Roach        ]);
237a4439c01SGreg Roach    }
238a4439c01SGreg Roach
239a4439c01SGreg Roach    /**
240a4439c01SGreg Roach     * @param Tree         $tree
24125cd7ed9SGreg Roach     * @param GedcomRecord $record
242a4439c01SGreg Roach     * @param string       $note
24318d7a90dSGreg Roach     *
24418d7a90dSGreg Roach     * @return void
245a4439c01SGreg Roach     */
246e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note): void
247c1010edaSGreg Roach    {
2480587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
24972cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2500587a5b3SGreg Roach            'user_id'   => null,
25125cd7ed9SGreg Roach            'xref'      => $record->xref(),
2520587a5b3SGreg Roach        ], [
25325cd7ed9SGreg Roach            'favorite_type' => $record::RECORD_TYPE,
254a4439c01SGreg Roach            'note'          => $note,
255a4439c01SGreg Roach        ]);
256a4439c01SGreg Roach    }
2578ec20abdSGreg Roach
2588ec20abdSGreg Roach    /**
2598ec20abdSGreg Roach     * @param string $type
2608ec20abdSGreg Roach     * @param string $xref
2618ec20abdSGreg Roach     * @param Tree   $tree
2628ec20abdSGreg Roach     *
2638ec20abdSGreg Roach     * @return GedcomRecord|null
2648ec20abdSGreg Roach     */
2658ec20abdSGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
2668ec20abdSGreg Roach    {
2678ec20abdSGreg Roach        switch ($type) {
2688ec20abdSGreg Roach            case 'indi':
2698ec20abdSGreg Roach                return Individual::getInstance($xref, $tree);
2708ec20abdSGreg Roach
2718ec20abdSGreg Roach            case 'fam':
2728ec20abdSGreg Roach                return Family::getInstance($xref, $tree);
2738ec20abdSGreg Roach
2748ec20abdSGreg Roach            case 'sour':
2758ec20abdSGreg Roach                return Source::getInstance($xref, $tree);
2768ec20abdSGreg Roach
2778ec20abdSGreg Roach            case 'repo':
2788ec20abdSGreg Roach                return Repository::getInstance($xref, $tree);
2798ec20abdSGreg Roach
2808ec20abdSGreg Roach            case 'obje':
2818ec20abdSGreg Roach                return Media::getInstance($xref, $tree);
2828ec20abdSGreg Roach
2838ec20abdSGreg Roach            default:
2848ec20abdSGreg Roach                return null;
2858ec20abdSGreg Roach        }
2868ec20abdSGreg Roach    }
2878c2e8227SGreg Roach}
288