xref: /webtrees/app/Module/UserFavoritesModule.php (revision d72b284a0846ca045e548a1c77ad11813bcbab92)
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 */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
208c2e8227SGreg Roach
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
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;
35a4439c01SGreg Roachuse stdClass;
368c2e8227SGreg Roach
378c2e8227SGreg Roach/**
388c2e8227SGreg Roach * Class UserFavoritesModule
398c2e8227SGreg Roach */
4037eb8894SGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
41c1010edaSGreg Roach{
4249a243cbSGreg Roach    use ModuleBlockTrait;
4349a243cbSGreg Roach
44a4439c01SGreg Roach    /**
450cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
46a4439c01SGreg Roach     *
47a4439c01SGreg Roach     * @return string
48a4439c01SGreg Roach     */
4949a243cbSGreg Roach    public function title(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Name of a module */
52bbb76c12SGreg Roach        return I18N::translate('Favorites');
53a4439c01SGreg Roach    }
54a4439c01SGreg Roach
55a4439c01SGreg Roach    /**
56a4439c01SGreg Roach     * A sentence describing what this module does.
57a4439c01SGreg Roach     *
58a4439c01SGreg Roach     * @return string
59a4439c01SGreg Roach     */
6049a243cbSGreg Roach    public function description(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
63bbb76c12SGreg Roach        return I18N::translate('Display and manage a user’s favorite pages.');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6676692c8bSGreg Roach    /**
67a4439c01SGreg Roach     * Generate the HTML content of this block.
68a4439c01SGreg Roach     *
69a4439c01SGreg Roach     * @param Tree     $tree
70a4439c01SGreg Roach     * @param int      $block_id
713caaa4d2SGreg Roach     * @param string   $context
723caaa4d2SGreg Roach     * @param string[] $config
73a4439c01SGreg Roach     *
74a4439c01SGreg Roach     * @return string
75a4439c01SGreg Roach     */
763caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
77c1010edaSGreg Roach    {
788ec20abdSGreg Roach        $content = view('modules/favorites/favorites', [
79a4439c01SGreg Roach            'block_id'    => $block_id,
808ec20abdSGreg Roach            'can_edit'    => true,
81a4439c01SGreg Roach            'favorites'   => $this->getFavorites($tree, Auth::user()),
828ec20abdSGreg Roach            'module_name' => $this->name(),
83a4439c01SGreg Roach            'tree'        => $tree,
84a4439c01SGreg Roach        ]);
85a4439c01SGreg Roach
863caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
87147e99aaSGreg Roach            return view('modules/block-template', [
881e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
89a4439c01SGreg Roach                'id'         => $block_id,
90a4439c01SGreg Roach                'config_url' => '',
9149a243cbSGreg Roach                'title'      => $this->title(),
92a4439c01SGreg Roach                'content'    => $content,
93a4439c01SGreg Roach            ]);
94a4439c01SGreg Roach        }
95b2ce94c6SRico Sonntag
96b2ce94c6SRico Sonntag        return $content;
97a4439c01SGreg Roach    }
98a4439c01SGreg Roach
99a4439c01SGreg Roach    /**
100a4439c01SGreg Roach     * Should this block load asynchronously using AJAX?
1013caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
102a4439c01SGreg Roach     *
103a4439c01SGreg Roach     * @return bool
104a4439c01SGreg Roach     */
105c1010edaSGreg Roach    public function loadAjax(): bool
106c1010edaSGreg Roach    {
107a4439c01SGreg Roach        return false;
108a4439c01SGreg Roach    }
109a4439c01SGreg Roach
110a4439c01SGreg Roach    /**
11176692c8bSGreg Roach     * Can this block be shown on the user’s home page?
11276692c8bSGreg Roach     *
11376692c8bSGreg Roach     * @return bool
11476692c8bSGreg Roach     */
115c1010edaSGreg Roach    public function isUserBlock(): bool
116c1010edaSGreg Roach    {
1178c2e8227SGreg Roach        return true;
1188c2e8227SGreg Roach    }
1198c2e8227SGreg Roach
12076692c8bSGreg Roach    /**
12176692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
12276692c8bSGreg Roach     *
12376692c8bSGreg Roach     * @return bool
12476692c8bSGreg Roach     */
12563276d8fSGreg Roach    public function isTreeBlock(): bool
126c1010edaSGreg Roach    {
1278c2e8227SGreg Roach        return false;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
1308c2e8227SGreg Roach    /**
131a4439c01SGreg Roach     * Get the favorites for a user
1328c2e8227SGreg Roach     *
1339807cf72SGreg Roach     * @param Tree          $tree
134e5a6b4d4SGreg Roach     * @param UserInterface $user
1358c2e8227SGreg Roach     *
136a4439c01SGreg Roach     * @return stdClass[]
1378c2e8227SGreg Roach     */
138e5a6b4d4SGreg Roach    public function getFavorites(Tree $tree, UserInterface $user): array
139c1010edaSGreg Roach    {
1400587a5b3SGreg Roach        return DB::table('favorite')
1410587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
142895230eeSGreg Roach            ->where('user_id', '=', $user->id())
1430587a5b3SGreg Roach            ->get()
1440b5fd0a6SGreg Roach            ->map(static function (stdClass $row) use ($tree): stdClass {
1450587a5b3SGreg Roach                if ($row->xref !== null) {
1460587a5b3SGreg Roach                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
14725cd7ed9SGreg Roach                } else {
1480587a5b3SGreg Roach                    $row->record = null;
1499807cf72SGreg Roach                }
1509807cf72SGreg Roach
1510587a5b3SGreg Roach                return $row;
1520587a5b3SGreg Roach            })
1530587a5b3SGreg Roach            ->all();
1548c2e8227SGreg Roach    }
1558c2e8227SGreg Roach
15676692c8bSGreg Roach    /**
1576ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
15876692c8bSGreg Roach     *
1596ccdf4f0SGreg Roach     * @return ResponseInterface
16076692c8bSGreg Roach     */
16157ab2231SGreg Roach    public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface
162c1010edaSGreg Roach    {
16357ab2231SGreg Roach        $tree   = $request->getAttribute('tree');
16457ab2231SGreg Roach        $user   = $request->getAttribute('user');
165eb235819SGreg Roach        $params = $request->getParsedBody();
166eb235819SGreg Roach
167eb235819SGreg Roach        $note  = $params['note'];
168eb235819SGreg Roach        $title = $params['title'];
169eb235819SGreg Roach        $url   = $params['url'];
170eb235819SGreg Roach        $type  = $params['type'];
171eb235819SGreg Roach        $xref  = $params[$type . '-xref'] ?? '';
1728840c547SGreg Roach
1738ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
17425cd7ed9SGreg Roach
175a4439c01SGreg Roach        if (Auth::check()) {
1768ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
177a4439c01SGreg Roach                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
1785c007fdfSGreg Roach            }
1795c007fdfSGreg Roach
1808ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
18125cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $user, $record, $note);
182a4439c01SGreg Roach            }
1838c2e8227SGreg Roach        }
1848840c547SGreg Roach
185*d72b284aSGreg Roach        $url = route('user-page', ['tree' => $tree->name()]);
186a4439c01SGreg Roach
1876ccdf4f0SGreg Roach        return redirect($url);
188a4439c01SGreg Roach    }
189a4439c01SGreg Roach
190a4439c01SGreg Roach    /**
1916ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
192a4439c01SGreg Roach     *
1936ccdf4f0SGreg Roach     * @return ResponseInterface
194a4439c01SGreg Roach     */
19557ab2231SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface
196c1010edaSGreg Roach    {
19757ab2231SGreg Roach        $tree        = $request->getAttribute('tree');
19857ab2231SGreg Roach        $user        = $request->getAttribute('user');
199c50a90beSGreg Roach        $favorite_id = $request->getQueryParams()['favorite_id'];
200a4439c01SGreg Roach
2018ec20abdSGreg Roach        if (Auth::check()) {
2020587a5b3SGreg Roach            DB::table('favorite')
2030587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
204895230eeSGreg Roach                ->where('user_id', '=', $user->id())
2050587a5b3SGreg Roach                ->delete();
2068ec20abdSGreg Roach        }
207a4439c01SGreg Roach
208*d72b284aSGreg Roach        $url = route('user-page', ['tree' => $tree->name()]);
209a4439c01SGreg Roach
2106ccdf4f0SGreg Roach        return redirect($url);
211a4439c01SGreg Roach    }
212a4439c01SGreg Roach
213a4439c01SGreg Roach    /**
214a4439c01SGreg Roach     * @param Tree          $tree
215e5a6b4d4SGreg Roach     * @param UserInterface $user
216a4439c01SGreg Roach     * @param string        $url
217a4439c01SGreg Roach     * @param string        $title
218a4439c01SGreg Roach     * @param string        $note
21918d7a90dSGreg Roach     *
22018d7a90dSGreg Roach     * @return void
221a4439c01SGreg Roach     */
222e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void
223c1010edaSGreg Roach    {
2240587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
22572cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
226895230eeSGreg Roach            'user_id'   => $user->id(),
227a4439c01SGreg Roach            'url'       => $url,
2280587a5b3SGreg Roach        ], [
2290587a5b3SGreg Roach            'favorite_type' => 'URL',
230a4439c01SGreg Roach            'note'          => $note,
231a4439c01SGreg Roach            'title'         => $title,
232a4439c01SGreg Roach        ]);
233a4439c01SGreg Roach    }
234a4439c01SGreg Roach
235a4439c01SGreg Roach    /**
236a4439c01SGreg Roach     * @param Tree          $tree
237e5a6b4d4SGreg Roach     * @param UserInterface $user
23825cd7ed9SGreg Roach     * @param GedcomRecord  $record
239a4439c01SGreg Roach     * @param string        $note
24018d7a90dSGreg Roach     *
24118d7a90dSGreg Roach     * @return void
242a4439c01SGreg Roach     */
243e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void
244c1010edaSGreg Roach    {
2450587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
24672cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
247895230eeSGreg Roach            'user_id'   => $user->id(),
24825cd7ed9SGreg Roach            'xref'      => $record->xref(),
2490587a5b3SGreg Roach        ], [
25025cd7ed9SGreg Roach            'favorite_type' => $record::RECORD_TYPE,
251a4439c01SGreg Roach            'note'          => $note,
252a4439c01SGreg Roach        ]);
2538c2e8227SGreg Roach    }
2548ec20abdSGreg Roach
2558ec20abdSGreg Roach    /**
2568ec20abdSGreg Roach     * @param string $type
2578ec20abdSGreg Roach     * @param string $xref
2588ec20abdSGreg Roach     * @param Tree   $tree
2598ec20abdSGreg Roach     *
2608ec20abdSGreg Roach     * @return GedcomRecord|null
2618ec20abdSGreg Roach     */
2628ec20abdSGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
2638ec20abdSGreg Roach    {
2648ec20abdSGreg Roach        switch ($type) {
2658ec20abdSGreg Roach            case 'indi':
2668ec20abdSGreg Roach                return Individual::getInstance($xref, $tree);
2678ec20abdSGreg Roach
2688ec20abdSGreg Roach            case 'fam':
2698ec20abdSGreg Roach                return Family::getInstance($xref, $tree);
2708ec20abdSGreg Roach
2718ec20abdSGreg Roach            case 'sour':
2728ec20abdSGreg Roach                return Source::getInstance($xref, $tree);
2738ec20abdSGreg Roach
2748ec20abdSGreg Roach            case 'repo':
2758ec20abdSGreg Roach                return Repository::getInstance($xref, $tree);
2768ec20abdSGreg Roach
2778ec20abdSGreg Roach            case 'obje':
2788ec20abdSGreg Roach                return Media::getInstance($xref, $tree);
2798ec20abdSGreg Roach
2808ec20abdSGreg Roach            default:
2818ec20abdSGreg Roach                return null;
2828ec20abdSGreg Roach        }
2838ec20abdSGreg Roach    }
2848c2e8227SGreg Roach}
285