xref: /webtrees/app/Module/UserFavoritesModule.php (revision bd44f43fad3efd8fd7f063363bfcb5a97080fa7d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\GedcomRecord;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Tree;
23use Fisharebest\Webtrees\User;
24use stdClass;
25use Symfony\Component\HttpFoundation\RedirectResponse;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * Class UserFavoritesModule
30 */
31class UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
32{
33    /**
34     * How should this module be labelled on tabs, menus, etc.?
35     *
36     * @return string
37     */
38    public function getTitle()
39    {
40        return /* I18N: Name of a module */
41            I18N::translate('Favorites');
42    }
43
44    /**
45     * A sentence describing what this module does.
46     *
47     * @return string
48     */
49    public function getDescription()
50    {
51        return /* I18N: Description of the “Favorites” module */
52            I18N::translate('Display and manage a user’s favorite pages.');
53    }
54
55    /**
56     * Generate the HTML content of this block.
57     *
58     * @param Tree     $tree
59     * @param int      $block_id
60     * @param bool     $template
61     * @param string[] $cfg
62     *
63     * @return string
64     */
65    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
66    {
67        $content = view('modules/user_favorites/favorites', [
68            'block_id'  => $block_id,
69            'favorites' => $this->getFavorites($tree, Auth::user()),
70            'tree'      => $tree,
71        ]);
72
73        if ($template) {
74            return view('modules/block-template', [
75                'block'      => str_replace('_', '-', $this->getName()),
76                'id'         => $block_id,
77                'config_url' => '',
78                'title'      => $this->getTitle(),
79                'content'    => $content,
80            ]);
81        } else {
82            return $content;
83        }
84    }
85
86    /**
87     * Should this block load asynchronously using AJAX?
88     *
89     * Simple blocks are faster in-line, more comples ones
90     * can be loaded later.
91     *
92     * @return bool
93     */
94    public function loadAjax(): bool
95    {
96        return false;
97    }
98
99    /**
100     * Can this block be shown on the user’s home page?
101     *
102     * @return bool
103     */
104    public function isUserBlock(): bool
105    {
106        return true;
107    }
108
109    /**
110     * Can this block be shown on the tree’s home page?
111     *
112     * @return bool
113     */
114    public function isGedcomBlock(): bool
115    {
116        return false;
117    }
118
119    /**
120     * An HTML form to edit block settings
121     *
122     * @param Tree $tree
123     * @param int  $block_id
124     *
125     * @return void
126     */
127    public function configureBlock(Tree $tree, int $block_id)
128    {
129    }
130
131    /**
132     * Get the favorites for a user
133     *
134     * @param Tree $tree
135     * @param User $user
136     *
137     * @return stdClass[]
138     */
139    public function getFavorites(Tree $tree, User $user)
140    {
141        $favorites =
142            Database::prepare(
143                "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
144                " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id")
145                ->execute([
146                    'tree_id' => $tree->getTreeId(),
147                    'user_id' => $user->getUserId(),
148                ])
149                ->fetchAll();
150
151        foreach ($favorites as $favorite) {
152            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
153        }
154
155        return $favorites;
156    }
157
158    /**
159     * @param Request $request
160     * @param Tree    $tree
161     * @param User    $user
162     *
163     * @return RedirectResponse
164     */
165    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
166    {
167        $note  = $request->get('note', '');
168        $title = $request->get('title', '');
169        $url   = $request->get('url', '');
170        $xref  = $request->get('xref', '');
171
172        if (Auth::check()) {
173            if ($url !== '') {
174                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
175            } else {
176                $this->addRecordFavorite($tree, $user, $xref, $note);
177            }
178        }
179
180        $url = route('user-page', ['ged' => $tree->getName()]);
181
182        return new RedirectResponse($url);
183    }
184
185    /**
186     * @param Request $request
187     * @param Tree    $tree
188     * @param User    $user
189     *
190     * @return RedirectResponse
191     */
192    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
193    {
194        $favorite_id = (int)$request->get('favorite_id');
195
196        if (Auth::check()) {
197            Database::prepare(
198                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id"
199            )->execute([
200                'favorite_id' => $favorite_id,
201                'user_id'     => $user->getUserId(),
202            ]);
203        }
204
205        $url = route('user-page', ['ged' => $tree->getName()]);
206
207        return new RedirectResponse($url);
208    }
209
210    /**
211     * @param Tree   $tree
212     * @param User   $user
213     * @param string $url
214     * @param string $title
215     * @param string $note
216     */
217    private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note)
218    {
219        $favorite = Database::prepare(
220            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url"
221        )->execute([
222            'gedcom_id' => $tree->getTreeId(),
223            'user_id'   => $user->getUserId(),
224            'url'       => $url,
225        ])->fetchOneRow();
226
227        if ($favorite === null) {
228            Database::prepare(
229                "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
230            )->execute([
231                'gedcom_id' => $tree->getTreeId(),
232                'user_id'   => $user->getUserId(),
233                'url'       => $url,
234                'note'      => $note,
235                'title'     => $title,
236            ]);
237        } else {
238            Database::prepare(
239                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
240            )->execute([
241                'note'        => $note,
242                'title'       => $title,
243                'favorite_id' => $favorite->favorite_id,
244            ]);
245        }
246    }
247
248    /**
249     * @param Tree   $tree
250     * @param User   $user
251     * @param string $xref
252     * @param string $note
253     */
254    private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note)
255    {
256        $favorite = Database::prepare(
257            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref"
258        )->execute([
259            'gedcom_id' => $tree->getTreeId(),
260            'user_id'   => $user->getUserId(),
261            'xref'      => $xref,
262        ])->fetchOneRow();
263
264        if ($favorite === null) {
265            Database::prepare(
266                "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)"
267            )->execute([
268                'gedcom_id' => $tree->getTreeId(),
269                'user_id'   => $user->getUserId(),
270                'xref'      => $xref,
271                'note'      => $note,
272            ]);
273        } else {
274            Database::prepare(
275                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
276            )->execute([
277                'note'        => $note,
278                'favorite_id' => $favorite->favorite_id,
279            ]);
280        }
281    }
282}
283