xref: /webtrees/app/Module/UserFavoritesModule.php (revision 342dcecd8628deacd49d86f3247fd77e64bf33c3)
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        /* I18N: Name of a module */
41        return 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        /* I18N: Description of the “Favorites” module */
52        return 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 = Database::prepare(
142            "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
143            " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id"
144        )->execute([
145            'tree_id' => $tree->getTreeId(),
146            'user_id' => $user->getUserId(),
147        ])->fetchAll();
148
149        foreach ($favorites as $favorite) {
150            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
151        }
152
153        return $favorites;
154    }
155
156    /**
157     * @param Request $request
158     * @param Tree    $tree
159     * @param User    $user
160     *
161     * @return RedirectResponse
162     */
163    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
164    {
165        $note  = $request->get('note', '');
166        $title = $request->get('title', '');
167        $url   = $request->get('url', '');
168        $xref  = $request->get('xref', '');
169
170        if (Auth::check()) {
171            if ($url !== '') {
172                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
173            } else {
174                $this->addRecordFavorite($tree, $user, $xref, $note);
175            }
176        }
177
178        $url = route('user-page', ['ged' => $tree->getName()]);
179
180        return new RedirectResponse($url);
181    }
182
183    /**
184     * @param Request $request
185     * @param Tree    $tree
186     * @param User    $user
187     *
188     * @return RedirectResponse
189     */
190    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
191    {
192        $favorite_id = (int)$request->get('favorite_id');
193
194        if (Auth::check()) {
195            Database::prepare(
196                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id"
197            )->execute([
198                'favorite_id' => $favorite_id,
199                'user_id'     => $user->getUserId(),
200            ]);
201        }
202
203        $url = route('user-page', ['ged' => $tree->getName()]);
204
205        return new RedirectResponse($url);
206    }
207
208    /**
209     * @param Tree   $tree
210     * @param User   $user
211     * @param string $url
212     * @param string $title
213     * @param string $note
214     */
215    private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note)
216    {
217        $favorite = Database::prepare(
218            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url"
219        )->execute([
220            'gedcom_id' => $tree->getTreeId(),
221            'user_id'   => $user->getUserId(),
222            'url'       => $url,
223        ])->fetchOneRow();
224
225        if ($favorite === null) {
226            Database::prepare(
227                "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
228            )->execute([
229                'gedcom_id' => $tree->getTreeId(),
230                'user_id'   => $user->getUserId(),
231                'url'       => $url,
232                'note'      => $note,
233                'title'     => $title,
234            ]);
235        } else {
236            Database::prepare(
237                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
238            )->execute([
239                'note'        => $note,
240                'title'       => $title,
241                'favorite_id' => $favorite->favorite_id,
242            ]);
243        }
244    }
245
246    /**
247     * @param Tree   $tree
248     * @param User   $user
249     * @param string $xref
250     * @param string $note
251     */
252    private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note)
253    {
254        $favorite = Database::prepare(
255            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref"
256        )->execute([
257            'gedcom_id' => $tree->getTreeId(),
258            'user_id'   => $user->getUserId(),
259            'xref'      => $xref,
260        ])->fetchOneRow();
261
262        if ($favorite === null) {
263            Database::prepare(
264                "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)"
265            )->execute([
266                'gedcom_id' => $tree->getTreeId(),
267                'user_id'   => $user->getUserId(),
268                'xref'      => $xref,
269                'note'      => $note,
270            ]);
271        } else {
272            Database::prepare(
273                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
274            )->execute([
275                'note'        => $note,
276                'favorite_id' => $favorite->favorite_id,
277            ]);
278        }
279    }
280}
281