xref: /webtrees/app/Module/UserFavoritesModule.php (revision 72cf66d48ef1f917238d9b0939a8aa33f257e274)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
198c2e8227SGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
249807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
25a6afca4cSGreg Roachuse Fisharebest\Webtrees\User;
26a4439c01SGreg Roachuse stdClass;
27a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
288840c547SGreg Roachuse Symfony\Component\HttpFoundation\Request;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class UserFavoritesModule
328c2e8227SGreg Roach */
33c1010edaSGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
35a4439c01SGreg Roach    /**
36a4439c01SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
37a4439c01SGreg Roach     *
38a4439c01SGreg Roach     * @return string
39a4439c01SGreg Roach     */
408f53f488SRico Sonntag    public function getTitle(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module */
43bbb76c12SGreg Roach        return I18N::translate('Favorites');
44a4439c01SGreg Roach    }
45a4439c01SGreg Roach
46a4439c01SGreg Roach    /**
47a4439c01SGreg Roach     * A sentence describing what this module does.
48a4439c01SGreg Roach     *
49a4439c01SGreg Roach     * @return string
50a4439c01SGreg Roach     */
518f53f488SRico Sonntag    public function getDescription(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
54bbb76c12SGreg Roach        return I18N::translate('Display and manage a user’s favorite pages.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5776692c8bSGreg Roach    /**
58a4439c01SGreg Roach     * Generate the HTML content of this block.
59a4439c01SGreg Roach     *
60a4439c01SGreg Roach     * @param Tree     $tree
61a4439c01SGreg Roach     * @param int      $block_id
62a4439c01SGreg Roach     * @param bool     $template
63a4439c01SGreg Roach     * @param string[] $cfg
64a4439c01SGreg Roach     *
65a4439c01SGreg Roach     * @return string
66a4439c01SGreg Roach     */
67c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
68c1010edaSGreg Roach    {
69a4439c01SGreg Roach        $content = view('modules/user_favorites/favorites', [
70a4439c01SGreg Roach            'block_id'  => $block_id,
71a4439c01SGreg Roach            'favorites' => $this->getFavorites($tree, Auth::user()),
72a4439c01SGreg Roach            'tree'      => $tree,
73a4439c01SGreg Roach        ]);
74a4439c01SGreg Roach
75a4439c01SGreg Roach        if ($template) {
76147e99aaSGreg Roach            return view('modules/block-template', [
77a4439c01SGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
78a4439c01SGreg Roach                'id'         => $block_id,
79a4439c01SGreg Roach                'config_url' => '',
80a4439c01SGreg Roach                'title'      => $this->getTitle(),
81a4439c01SGreg Roach                'content'    => $content,
82a4439c01SGreg Roach            ]);
83a4439c01SGreg Roach        }
84b2ce94c6SRico Sonntag
85b2ce94c6SRico Sonntag        return $content;
86a4439c01SGreg Roach    }
87a4439c01SGreg Roach
88a4439c01SGreg Roach    /**
89a4439c01SGreg Roach     * Should this block load asynchronously using AJAX?
90a4439c01SGreg Roach     *
91a4439c01SGreg Roach     * Simple blocks are faster in-line, more comples ones
92a4439c01SGreg Roach     * can be loaded later.
93a4439c01SGreg Roach     *
94a4439c01SGreg Roach     * @return bool
95a4439c01SGreg Roach     */
96c1010edaSGreg Roach    public function loadAjax(): bool
97c1010edaSGreg Roach    {
98a4439c01SGreg Roach        return false;
99a4439c01SGreg Roach    }
100a4439c01SGreg Roach
101a4439c01SGreg Roach    /**
10276692c8bSGreg Roach     * Can this block be shown on the user’s home page?
10376692c8bSGreg Roach     *
10476692c8bSGreg Roach     * @return bool
10576692c8bSGreg Roach     */
106c1010edaSGreg Roach    public function isUserBlock(): bool
107c1010edaSGreg Roach    {
1088c2e8227SGreg Roach        return true;
1098c2e8227SGreg Roach    }
1108c2e8227SGreg Roach
11176692c8bSGreg Roach    /**
11276692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
11376692c8bSGreg Roach     *
11476692c8bSGreg Roach     * @return bool
11576692c8bSGreg Roach     */
116c1010edaSGreg Roach    public function isGedcomBlock(): bool
117c1010edaSGreg Roach    {
1188c2e8227SGreg Roach        return false;
1198c2e8227SGreg Roach    }
1208c2e8227SGreg Roach
1218c2e8227SGreg Roach    /**
122a45f9889SGreg Roach     * Update the configuration for a block.
123a45f9889SGreg Roach     *
124a45f9889SGreg Roach     * @param Request $request
125a45f9889SGreg Roach     * @param int     $block_id
126a45f9889SGreg Roach     *
127a45f9889SGreg Roach     * @return void
128a45f9889SGreg Roach     */
129a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
130a45f9889SGreg Roach    {
131a45f9889SGreg Roach    }
132a45f9889SGreg Roach
133a45f9889SGreg Roach    /**
134a4439c01SGreg Roach     * An HTML form to edit block settings
135a4439c01SGreg Roach     *
136a4439c01SGreg Roach     * @param Tree $tree
137a4439c01SGreg Roach     * @param int  $block_id
138a4439c01SGreg Roach     *
139a4439c01SGreg Roach     * @return void
140a4439c01SGreg Roach     */
141a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
142c1010edaSGreg Roach    {
143a4439c01SGreg Roach    }
144a4439c01SGreg Roach
145a4439c01SGreg Roach    /**
146a4439c01SGreg Roach     * Get the favorites for a user
1478c2e8227SGreg Roach     *
1489807cf72SGreg Roach     * @param Tree $tree
149a6afca4cSGreg Roach     * @param User $user
1508c2e8227SGreg Roach     *
151a4439c01SGreg Roach     * @return stdClass[]
1528c2e8227SGreg Roach     */
1538f53f488SRico Sonntag    public function getFavorites(Tree $tree, User $user): array
154c1010edaSGreg Roach    {
155bdb3725aSGreg Roach        $favorites = Database::prepare(
156e5588fb0SGreg Roach            "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
157bdb3725aSGreg Roach            " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id"
158bdb3725aSGreg Roach        )->execute([
159*72cf66d4SGreg Roach            'tree_id' => $tree->id(),
160a6afca4cSGreg Roach            'user_id' => $user->getUserId(),
161bdb3725aSGreg Roach        ])->fetchAll();
1629807cf72SGreg Roach
1639807cf72SGreg Roach        foreach ($favorites as $favorite) {
1649807cf72SGreg Roach            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
1659807cf72SGreg Roach        }
1669807cf72SGreg Roach
1679807cf72SGreg Roach        return $favorites;
1688c2e8227SGreg Roach    }
1698c2e8227SGreg Roach
17076692c8bSGreg Roach    /**
1718840c547SGreg Roach     * @param Request $request
172b6db7c1fSGreg Roach     * @param Tree    $tree
173b6db7c1fSGreg Roach     * @param User    $user
17476692c8bSGreg Roach     *
175a4439c01SGreg Roach     * @return RedirectResponse
17676692c8bSGreg Roach     */
177b6db7c1fSGreg Roach    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
178c1010edaSGreg Roach    {
179a4439c01SGreg Roach        $note  = $request->get('note', '');
180a4439c01SGreg Roach        $title = $request->get('title', '');
181a4439c01SGreg Roach        $url   = $request->get('url', '');
182a4439c01SGreg Roach        $xref  = $request->get('xref', '');
1838840c547SGreg Roach
184a4439c01SGreg Roach        if (Auth::check()) {
185a4439c01SGreg Roach            if ($url !== '') {
186a4439c01SGreg Roach                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
187a4439c01SGreg Roach            } else {
188a4439c01SGreg Roach                $this->addRecordFavorite($tree, $user, $xref, $note);
189a4439c01SGreg Roach            }
1908c2e8227SGreg Roach        }
1918840c547SGreg Roach
192a4439c01SGreg Roach        $url = route('user-page', ['ged' => $tree->getName()]);
193a4439c01SGreg Roach
194a4439c01SGreg Roach        return new RedirectResponse($url);
195a4439c01SGreg Roach    }
196a4439c01SGreg Roach
197a4439c01SGreg Roach    /**
198a4439c01SGreg Roach     * @param Request $request
199b6db7c1fSGreg Roach     * @param Tree    $tree
200b6db7c1fSGreg Roach     * @param User    $user
201a4439c01SGreg Roach     *
202a4439c01SGreg Roach     * @return RedirectResponse
203a4439c01SGreg Roach     */
204b6db7c1fSGreg Roach    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
205c1010edaSGreg Roach    {
206a4439c01SGreg Roach        $favorite_id = (int) $request->get('favorite_id');
207a4439c01SGreg Roach
208a4439c01SGreg Roach        if (Auth::check()) {
209a4439c01SGreg Roach            Database::prepare(
210a4439c01SGreg Roach                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id"
211a4439c01SGreg Roach            )->execute([
212a4439c01SGreg Roach                'favorite_id' => $favorite_id,
213a4439c01SGreg Roach                'user_id'     => $user->getUserId(),
214a4439c01SGreg Roach            ]);
215a4439c01SGreg Roach        }
216a4439c01SGreg Roach
217a4439c01SGreg Roach        $url = route('user-page', ['ged' => $tree->getName()]);
218a4439c01SGreg Roach
219a4439c01SGreg Roach        return new RedirectResponse($url);
220a4439c01SGreg Roach    }
221a4439c01SGreg Roach
222a4439c01SGreg Roach    /**
223a4439c01SGreg Roach     * @param Tree   $tree
224a4439c01SGreg Roach     * @param User   $user
225a4439c01SGreg Roach     * @param string $url
226a4439c01SGreg Roach     * @param string $title
227a4439c01SGreg Roach     * @param string $note
22818d7a90dSGreg Roach     *
22918d7a90dSGreg Roach     * @return void
230a4439c01SGreg Roach     */
231c1010edaSGreg Roach    private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note)
232c1010edaSGreg Roach    {
233a4439c01SGreg Roach        $favorite = Database::prepare(
234a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url"
235a4439c01SGreg Roach        )->execute([
236*72cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
237a4439c01SGreg Roach            'user_id'   => $user->getUserId(),
238a4439c01SGreg Roach            'url'       => $url,
239a4439c01SGreg Roach        ])->fetchOneRow();
240a4439c01SGreg Roach
241a4439c01SGreg Roach        if ($favorite === null) {
242a4439c01SGreg Roach            Database::prepare(
243a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
244a4439c01SGreg Roach            )->execute([
245*72cf66d4SGreg Roach                'gedcom_id' => $tree->id(),
246a4439c01SGreg Roach                'user_id'   => $user->getUserId(),
247a4439c01SGreg Roach                'url'       => $url,
248a4439c01SGreg Roach                'note'      => $note,
249a4439c01SGreg Roach                'title'     => $title,
250a4439c01SGreg Roach            ]);
251a4439c01SGreg Roach        } else {
252a4439c01SGreg Roach            Database::prepare(
253a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
254a4439c01SGreg Roach            )->execute([
255a4439c01SGreg Roach                'note'        => $note,
256a4439c01SGreg Roach                'title'       => $title,
257a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
258a4439c01SGreg Roach            ]);
259a4439c01SGreg Roach        }
260a4439c01SGreg Roach    }
261a4439c01SGreg Roach
262a4439c01SGreg Roach    /**
263a4439c01SGreg Roach     * @param Tree   $tree
264a4439c01SGreg Roach     * @param User   $user
265a4439c01SGreg Roach     * @param string $xref
266a4439c01SGreg Roach     * @param string $note
26718d7a90dSGreg Roach     *
26818d7a90dSGreg Roach     * @return void
269a4439c01SGreg Roach     */
270c1010edaSGreg Roach    private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note)
271c1010edaSGreg Roach    {
272a4439c01SGreg Roach        $favorite = Database::prepare(
273a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref"
274a4439c01SGreg Roach        )->execute([
275*72cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
276a4439c01SGreg Roach            'user_id'   => $user->getUserId(),
277a4439c01SGreg Roach            'xref'      => $xref,
278a4439c01SGreg Roach        ])->fetchOneRow();
279a4439c01SGreg Roach
280a4439c01SGreg Roach        if ($favorite === null) {
281a4439c01SGreg Roach            Database::prepare(
282a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)"
283a4439c01SGreg Roach            )->execute([
284*72cf66d4SGreg Roach                'gedcom_id' => $tree->id(),
285a4439c01SGreg Roach                'user_id'   => $user->getUserId(),
286a4439c01SGreg Roach                'xref'      => $xref,
287a4439c01SGreg Roach                'note'      => $note,
288a4439c01SGreg Roach            ]);
289a4439c01SGreg Roach        } else {
290a4439c01SGreg Roach            Database::prepare(
291a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
292a4439c01SGreg Roach            )->execute([
293a4439c01SGreg Roach                'note'        => $note,
294a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
295a4439c01SGreg Roach            ]);
296a4439c01SGreg Roach        }
2978c2e8227SGreg Roach    }
2988c2e8227SGreg Roach}
299