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