xref: /webtrees/app/Module/UserFavoritesModule.php (revision b6db7c1f82484c632c75d5225ce767d398ddc27f)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
229807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
23a6afca4cSGreg Roachuse Fisharebest\Webtrees\User;
24a4439c01SGreg Roachuse stdClass;
25a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
268840c547SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class UserFavoritesModule
308c2e8227SGreg Roach */
31c1010edaSGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
33a4439c01SGreg Roach    /**
34a4439c01SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
35a4439c01SGreg Roach     *
36a4439c01SGreg Roach     * @return string
37a4439c01SGreg Roach     */
38c1010edaSGreg Roach    public function getTitle()
39c1010edaSGreg Roach    {
40c1010edaSGreg Roach        return /* I18N: Name of a module */
41c1010edaSGreg Roach            I18N::translate('Favorites');
42a4439c01SGreg Roach    }
43a4439c01SGreg Roach
44a4439c01SGreg Roach    /**
45a4439c01SGreg Roach     * A sentence describing what this module does.
46a4439c01SGreg Roach     *
47a4439c01SGreg Roach     * @return string
48a4439c01SGreg Roach     */
49c1010edaSGreg Roach    public function getDescription()
50c1010edaSGreg Roach    {
51c1010edaSGreg Roach        return /* I18N: Description of the “Favorites” module */
52c1010edaSGreg Roach            I18N::translate('Display and manage a user’s favorite pages.');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
5576692c8bSGreg Roach    /**
56a4439c01SGreg Roach     * Generate the HTML content of this block.
57a4439c01SGreg Roach     *
58a4439c01SGreg Roach     * @param Tree     $tree
59a4439c01SGreg Roach     * @param int      $block_id
60a4439c01SGreg Roach     * @param bool     $template
61a4439c01SGreg Roach     * @param string[] $cfg
62a4439c01SGreg Roach     *
63a4439c01SGreg Roach     * @return string
64a4439c01SGreg Roach     */
65c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
66c1010edaSGreg Roach    {
67a4439c01SGreg Roach        $content = view('modules/user_favorites/favorites', [
68a4439c01SGreg Roach            'block_id'  => $block_id,
69a4439c01SGreg Roach            'favorites' => $this->getFavorites($tree, Auth::user()),
70a4439c01SGreg Roach            'tree'      => $tree,
71a4439c01SGreg Roach        ]);
72a4439c01SGreg Roach
73a4439c01SGreg Roach        if ($template) {
74147e99aaSGreg Roach            return view('modules/block-template', [
75a4439c01SGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
76a4439c01SGreg Roach                'id'         => $block_id,
77a4439c01SGreg Roach                'config_url' => '',
78a4439c01SGreg Roach                'title'      => $this->getTitle(),
79a4439c01SGreg Roach                'content'    => $content,
80a4439c01SGreg Roach            ]);
81a4439c01SGreg Roach        } else {
82a4439c01SGreg Roach            return $content;
83a4439c01SGreg Roach        }
84a4439c01SGreg Roach    }
85a4439c01SGreg Roach
86a4439c01SGreg Roach    /**
87a4439c01SGreg Roach     * Should this block load asynchronously using AJAX?
88a4439c01SGreg Roach     *
89a4439c01SGreg Roach     * Simple blocks are faster in-line, more comples ones
90a4439c01SGreg Roach     * can be loaded later.
91a4439c01SGreg Roach     *
92a4439c01SGreg Roach     * @return bool
93a4439c01SGreg Roach     */
94c1010edaSGreg Roach    public function loadAjax(): bool
95c1010edaSGreg Roach    {
96a4439c01SGreg Roach        return false;
97a4439c01SGreg Roach    }
98a4439c01SGreg Roach
99a4439c01SGreg Roach    /**
10076692c8bSGreg Roach     * Can this block be shown on the user’s home page?
10176692c8bSGreg Roach     *
10276692c8bSGreg Roach     * @return bool
10376692c8bSGreg Roach     */
104c1010edaSGreg Roach    public function isUserBlock(): bool
105c1010edaSGreg Roach    {
1068c2e8227SGreg Roach        return true;
1078c2e8227SGreg Roach    }
1088c2e8227SGreg Roach
10976692c8bSGreg Roach    /**
11076692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
11176692c8bSGreg Roach     *
11276692c8bSGreg Roach     * @return bool
11376692c8bSGreg Roach     */
114c1010edaSGreg Roach    public function isGedcomBlock(): bool
115c1010edaSGreg Roach    {
1168c2e8227SGreg Roach        return false;
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach    /**
120a4439c01SGreg Roach     * An HTML form to edit block settings
121a4439c01SGreg Roach     *
122a4439c01SGreg Roach     * @param Tree $tree
123a4439c01SGreg Roach     * @param int  $block_id
124a4439c01SGreg Roach     *
125a4439c01SGreg Roach     * @return void
126a4439c01SGreg Roach     */
127c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
128c1010edaSGreg Roach    {
129a4439c01SGreg Roach    }
130a4439c01SGreg Roach
131a4439c01SGreg Roach    /**
132a4439c01SGreg Roach     * Get the favorites for a user
1338c2e8227SGreg Roach     *
1349807cf72SGreg Roach     * @param Tree $tree
135a6afca4cSGreg Roach     * @param User $user
1368c2e8227SGreg Roach     *
137a4439c01SGreg Roach     * @return stdClass[]
1388c2e8227SGreg Roach     */
139c1010edaSGreg Roach    public function getFavorites(Tree $tree, User $user)
140c1010edaSGreg Roach    {
1419807cf72SGreg Roach        $favorites =
1428c2e8227SGreg Roach            Database::prepare(
143e5588fb0SGreg Roach                "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
144a4439c01SGreg Roach                " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id")
1459807cf72SGreg Roach                ->execute([
1469807cf72SGreg Roach                    'tree_id' => $tree->getTreeId(),
147a6afca4cSGreg Roach                    'user_id' => $user->getUserId(),
1489807cf72SGreg Roach                ])
1499807cf72SGreg Roach                ->fetchAll();
1509807cf72SGreg Roach
1519807cf72SGreg Roach        foreach ($favorites as $favorite) {
1529807cf72SGreg Roach            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
1539807cf72SGreg Roach        }
1549807cf72SGreg Roach
1559807cf72SGreg Roach        return $favorites;
1568c2e8227SGreg Roach    }
1578c2e8227SGreg Roach
15876692c8bSGreg Roach    /**
1598840c547SGreg Roach     * @param Request $request
160*b6db7c1fSGreg Roach     * @param Tree    $tree
161*b6db7c1fSGreg Roach     * @param User    $user
16276692c8bSGreg Roach     *
163a4439c01SGreg Roach     * @return RedirectResponse
16476692c8bSGreg Roach     */
165*b6db7c1fSGreg Roach    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
166c1010edaSGreg Roach    {
167a4439c01SGreg Roach        $note  = $request->get('note', '');
168a4439c01SGreg Roach        $title = $request->get('title', '');
169a4439c01SGreg Roach        $url   = $request->get('url', '');
170a4439c01SGreg Roach        $xref  = $request->get('xref', '');
1718840c547SGreg Roach
172a4439c01SGreg Roach        if (Auth::check()) {
173a4439c01SGreg Roach            if ($url !== '') {
174a4439c01SGreg Roach                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
175a4439c01SGreg Roach            } else {
176a4439c01SGreg Roach                $this->addRecordFavorite($tree, $user, $xref, $note);
177a4439c01SGreg Roach            }
1788c2e8227SGreg Roach        }
1798840c547SGreg Roach
180a4439c01SGreg Roach        $url = route('user-page', ['ged' => $tree->getName()]);
181a4439c01SGreg Roach
182a4439c01SGreg Roach        return new RedirectResponse($url);
183a4439c01SGreg Roach    }
184a4439c01SGreg Roach
185a4439c01SGreg Roach    /**
186a4439c01SGreg Roach     * @param Request $request
187*b6db7c1fSGreg Roach     * @param Tree    $tree
188*b6db7c1fSGreg Roach     * @param User    $user
189a4439c01SGreg Roach     *
190a4439c01SGreg Roach     * @return RedirectResponse
191a4439c01SGreg Roach     */
192*b6db7c1fSGreg Roach    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
193c1010edaSGreg Roach    {
194a4439c01SGreg Roach        $favorite_id = (int)$request->get('favorite_id');
195a4439c01SGreg Roach
196a4439c01SGreg Roach        if (Auth::check()) {
197a4439c01SGreg Roach            Database::prepare(
198a4439c01SGreg Roach                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id"
199a4439c01SGreg Roach            )->execute([
200a4439c01SGreg Roach                'favorite_id' => $favorite_id,
201a4439c01SGreg Roach                'user_id'     => $user->getUserId(),
202a4439c01SGreg Roach            ]);
203a4439c01SGreg Roach        }
204a4439c01SGreg Roach
205a4439c01SGreg Roach        $url = route('user-page', ['ged' => $tree->getName()]);
206a4439c01SGreg Roach
207a4439c01SGreg Roach        return new RedirectResponse($url);
208a4439c01SGreg Roach    }
209a4439c01SGreg Roach
210a4439c01SGreg Roach    /**
211a4439c01SGreg Roach     * @param Tree   $tree
212a4439c01SGreg Roach     * @param User   $user
213a4439c01SGreg Roach     * @param string $url
214a4439c01SGreg Roach     * @param string $title
215a4439c01SGreg Roach     * @param string $note
216a4439c01SGreg Roach     */
217c1010edaSGreg Roach    private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note)
218c1010edaSGreg Roach    {
219a4439c01SGreg Roach        $favorite = Database::prepare(
220a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url"
221a4439c01SGreg Roach        )->execute([
222a4439c01SGreg Roach            'gedcom_id' => $tree->getTreeId(),
223a4439c01SGreg Roach            'user_id'   => $user->getUserId(),
224a4439c01SGreg Roach            'url'       => $url,
225a4439c01SGreg Roach        ])->fetchOneRow();
226a4439c01SGreg Roach
227a4439c01SGreg Roach        if ($favorite === null) {
228a4439c01SGreg Roach            Database::prepare(
229a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
230a4439c01SGreg Roach            )->execute([
231a4439c01SGreg Roach                'gedcom_id' => $tree->getTreeId(),
232a4439c01SGreg Roach                'user_id'   => $user->getUserId(),
233a4439c01SGreg Roach                'url'       => $url,
234a4439c01SGreg Roach                'note'      => $note,
235a4439c01SGreg Roach                'title'     => $title,
236a4439c01SGreg Roach            ]);
237a4439c01SGreg Roach        } else {
238a4439c01SGreg Roach            Database::prepare(
239a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
240a4439c01SGreg Roach            )->execute([
241a4439c01SGreg Roach                'note'        => $note,
242a4439c01SGreg Roach                'title'       => $title,
243a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
244a4439c01SGreg Roach            ]);
245a4439c01SGreg Roach        }
246a4439c01SGreg Roach    }
247a4439c01SGreg Roach
248a4439c01SGreg Roach    /**
249a4439c01SGreg Roach     * @param Tree   $tree
250a4439c01SGreg Roach     * @param User   $user
251a4439c01SGreg Roach     * @param string $xref
252a4439c01SGreg Roach     * @param string $note
253a4439c01SGreg Roach     */
254c1010edaSGreg Roach    private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note)
255c1010edaSGreg Roach    {
256a4439c01SGreg Roach        $favorite = Database::prepare(
257a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref"
258a4439c01SGreg Roach        )->execute([
259a4439c01SGreg Roach            'gedcom_id' => $tree->getTreeId(),
260a4439c01SGreg Roach            'user_id'   => $user->getUserId(),
261a4439c01SGreg Roach            'xref'      => $xref,
262a4439c01SGreg Roach        ])->fetchOneRow();
263a4439c01SGreg Roach
264a4439c01SGreg Roach        if ($favorite === null) {
265a4439c01SGreg Roach            Database::prepare(
266a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)"
267a4439c01SGreg Roach            )->execute([
268a4439c01SGreg Roach                'gedcom_id' => $tree->getTreeId(),
269a4439c01SGreg Roach                'user_id'   => $user->getUserId(),
270a4439c01SGreg Roach                'xref'      => $xref,
271a4439c01SGreg Roach                'note'      => $note,
272a4439c01SGreg Roach            ]);
273a4439c01SGreg Roach        } else {
274a4439c01SGreg Roach            Database::prepare(
275a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
276a4439c01SGreg Roach            )->execute([
277a4439c01SGreg Roach                'note'        => $note,
278a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
279a4439c01SGreg Roach            ]);
280a4439c01SGreg Roach        }
2818c2e8227SGreg Roach    }
2828c2e8227SGreg Roach}
283