xref: /webtrees/app/Module/UserFavoritesModule.php (revision eb235819d2dcd26d677ffd48caefaff86620ce99)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
198c2e8227SGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
228ec20abdSGreg Roachuse Fisharebest\Webtrees\Family;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
258ec20abdSGreg Roachuse Fisharebest\Webtrees\Individual;
268ec20abdSGreg Roachuse Fisharebest\Webtrees\Media;
278ec20abdSGreg Roachuse Fisharebest\Webtrees\Repository;
288ec20abdSGreg Roachuse Fisharebest\Webtrees\Source;
299807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
300587a5b3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
311e7a7a28SGreg Roachuse Illuminate\Support\Str;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34a4439c01SGreg Roachuse stdClass;
358c2e8227SGreg 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
705f2ae573SGreg Roach     * @param string   $ctype
71a4439c01SGreg Roach     * @param string[] $cfg
72a4439c01SGreg Roach     *
73a4439c01SGreg Roach     * @return string
74a4439c01SGreg Roach     */
755f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): 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
856a8879feSGreg Roach        if ($ctype !== '') {
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?
100a4439c01SGreg Roach     * Simple blocks are faster in-line, more comples ones
101a4439c01SGreg Roach     * 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    /**
131a45f9889SGreg Roach     * Update the configuration for a block.
132a45f9889SGreg Roach     *
1336ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
134a45f9889SGreg Roach     * @param int     $block_id
135a45f9889SGreg Roach     *
136a45f9889SGreg Roach     * @return void
137a45f9889SGreg Roach     */
1386ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
139a45f9889SGreg Roach    {
140a45f9889SGreg Roach    }
141a45f9889SGreg Roach
142a45f9889SGreg Roach    /**
143a4439c01SGreg Roach     * An HTML form to edit block settings
144a4439c01SGreg Roach     *
145a4439c01SGreg Roach     * @param Tree $tree
146a4439c01SGreg Roach     * @param int  $block_id
147a4439c01SGreg Roach     *
148a4439c01SGreg Roach     * @return void
149a4439c01SGreg Roach     */
150e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
151c1010edaSGreg Roach    {
152a4439c01SGreg Roach    }
153a4439c01SGreg Roach
154a4439c01SGreg Roach    /**
155a4439c01SGreg Roach     * Get the favorites for a user
1568c2e8227SGreg Roach     *
1579807cf72SGreg Roach     * @param Tree          $tree
158e5a6b4d4SGreg Roach     * @param UserInterface $user
1598c2e8227SGreg Roach     *
160a4439c01SGreg Roach     * @return stdClass[]
1618c2e8227SGreg Roach     */
162e5a6b4d4SGreg Roach    public function getFavorites(Tree $tree, UserInterface $user): array
163c1010edaSGreg Roach    {
1640587a5b3SGreg Roach        return DB::table('favorite')
1650587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
166895230eeSGreg Roach            ->where('user_id', '=', $user->id())
1670587a5b3SGreg Roach            ->get()
1680b5fd0a6SGreg Roach            ->map(static function (stdClass $row) use ($tree): stdClass {
1690587a5b3SGreg Roach                if ($row->xref !== null) {
1700587a5b3SGreg Roach                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
17125cd7ed9SGreg Roach                } else {
1720587a5b3SGreg Roach                    $row->record = null;
1739807cf72SGreg Roach                }
1749807cf72SGreg Roach
1750587a5b3SGreg Roach                return $row;
1760587a5b3SGreg Roach            })
1770587a5b3SGreg Roach            ->all();
1788c2e8227SGreg Roach    }
1798c2e8227SGreg Roach
18076692c8bSGreg Roach    /**
1816ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
182b6db7c1fSGreg Roach     * @param Tree                   $tree
183e5a6b4d4SGreg Roach     * @param UserInterface          $user
18476692c8bSGreg Roach     *
1856ccdf4f0SGreg Roach     * @return ResponseInterface
18676692c8bSGreg Roach     */
1876ccdf4f0SGreg Roach    public function postAddFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
188c1010edaSGreg Roach    {
189*eb235819SGreg Roach        $params = $request->getParsedBody();
190*eb235819SGreg Roach
191*eb235819SGreg Roach        $note  = $params['note'];
192*eb235819SGreg Roach        $title = $params['title'];
193*eb235819SGreg Roach        $url   = $params['url'];
194*eb235819SGreg Roach        $type  = $params['type'];
195*eb235819SGreg Roach        $xref  = $params[$type . '-xref'] ?? '';
1968840c547SGreg Roach
1978ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
19825cd7ed9SGreg Roach
199a4439c01SGreg Roach        if (Auth::check()) {
2008ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
201a4439c01SGreg Roach                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
2025c007fdfSGreg Roach            }
2035c007fdfSGreg Roach
2048ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
20525cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $user, $record, $note);
206a4439c01SGreg Roach            }
2078c2e8227SGreg Roach        }
2088840c547SGreg Roach
209aa6f03bbSGreg Roach        $url = route('user-page', ['ged' => $tree->name()]);
210a4439c01SGreg Roach
2116ccdf4f0SGreg Roach        return redirect($url);
212a4439c01SGreg Roach    }
213a4439c01SGreg Roach
214a4439c01SGreg Roach    /**
2156ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
216b6db7c1fSGreg Roach     * @param Tree                   $tree
217e5a6b4d4SGreg Roach     * @param UserInterface          $user
218a4439c01SGreg Roach     *
2196ccdf4f0SGreg Roach     * @return ResponseInterface
220a4439c01SGreg Roach     */
2216ccdf4f0SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
222c1010edaSGreg Roach    {
223c50a90beSGreg Roach        $favorite_id = $request->getQueryParams()['favorite_id'];
224a4439c01SGreg Roach
2258ec20abdSGreg Roach        if (Auth::check()) {
2260587a5b3SGreg Roach            DB::table('favorite')
2270587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
228895230eeSGreg Roach                ->where('user_id', '=', $user->id())
2290587a5b3SGreg Roach                ->delete();
2308ec20abdSGreg Roach        }
231a4439c01SGreg Roach
232aa6f03bbSGreg Roach        $url = route('user-page', ['ged' => $tree->name()]);
233a4439c01SGreg Roach
2346ccdf4f0SGreg Roach        return redirect($url);
235a4439c01SGreg Roach    }
236a4439c01SGreg Roach
237a4439c01SGreg Roach    /**
238a4439c01SGreg Roach     * @param Tree          $tree
239e5a6b4d4SGreg Roach     * @param UserInterface $user
240a4439c01SGreg Roach     * @param string        $url
241a4439c01SGreg Roach     * @param string        $title
242a4439c01SGreg Roach     * @param string        $note
24318d7a90dSGreg Roach     *
24418d7a90dSGreg Roach     * @return void
245a4439c01SGreg Roach     */
246e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void
247c1010edaSGreg Roach    {
2480587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
24972cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
250895230eeSGreg Roach            'user_id'   => $user->id(),
251a4439c01SGreg Roach            'url'       => $url,
2520587a5b3SGreg Roach        ], [
2530587a5b3SGreg Roach            'favorite_type' => 'URL',
254a4439c01SGreg Roach            'note'          => $note,
255a4439c01SGreg Roach            'title'         => $title,
256a4439c01SGreg Roach        ]);
257a4439c01SGreg Roach    }
258a4439c01SGreg Roach
259a4439c01SGreg Roach    /**
260a4439c01SGreg Roach     * @param Tree          $tree
261e5a6b4d4SGreg Roach     * @param UserInterface $user
26225cd7ed9SGreg Roach     * @param GedcomRecord  $record
263a4439c01SGreg Roach     * @param string        $note
26418d7a90dSGreg Roach     *
26518d7a90dSGreg Roach     * @return void
266a4439c01SGreg Roach     */
267e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void
268c1010edaSGreg Roach    {
2690587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
27072cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
271895230eeSGreg Roach            'user_id'   => $user->id(),
27225cd7ed9SGreg Roach            'xref'      => $record->xref(),
2730587a5b3SGreg Roach        ], [
27425cd7ed9SGreg Roach            'favorite_type' => $record::RECORD_TYPE,
275a4439c01SGreg Roach            'note'          => $note,
276a4439c01SGreg Roach        ]);
2778c2e8227SGreg Roach    }
2788ec20abdSGreg Roach
2798ec20abdSGreg Roach    /**
2808ec20abdSGreg Roach     * @param string $type
2818ec20abdSGreg Roach     * @param string $xref
2828ec20abdSGreg Roach     * @param Tree   $tree
2838ec20abdSGreg Roach     *
2848ec20abdSGreg Roach     * @return GedcomRecord|null
2858ec20abdSGreg Roach     */
2868ec20abdSGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
2878ec20abdSGreg Roach    {
2888ec20abdSGreg Roach        switch ($type) {
2898ec20abdSGreg Roach            case 'indi':
2908ec20abdSGreg Roach                return Individual::getInstance($xref, $tree);
2918ec20abdSGreg Roach
2928ec20abdSGreg Roach            case 'fam':
2938ec20abdSGreg Roach                return Family::getInstance($xref, $tree);
2948ec20abdSGreg Roach
2958ec20abdSGreg Roach            case 'sour':
2968ec20abdSGreg Roach                return Source::getInstance($xref, $tree);
2978ec20abdSGreg Roach
2988ec20abdSGreg Roach            case 'repo':
2998ec20abdSGreg Roach                return Repository::getInstance($xref, $tree);
3008ec20abdSGreg Roach
3018ec20abdSGreg Roach            case 'obje':
3028ec20abdSGreg Roach                return Media::getInstance($xref, $tree);
3038ec20abdSGreg Roach
3048ec20abdSGreg Roach            default:
3058ec20abdSGreg Roach                return null;
3068ec20abdSGreg Roach        }
3078ec20abdSGreg Roach    }
3088c2e8227SGreg Roach}
309