xref: /webtrees/app/Module/UserFavoritesModule.php (revision 12c79f746ea1903d66aea1fe0412eda6149a5532)
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     * Update the configuration for a block.
121     *
122     * @param Request $request
123     * @param int     $block_id
124     *
125     * @return void
126     */
127    public function saveBlockConfiguration(Request $request, int $block_id)
128    {
129    }
130
131    /**
132     * An HTML form to edit block settings
133     *
134     * @param Tree $tree
135     * @param int  $block_id
136     *
137     * @return void
138     */
139    public function editBlockConfiguration(Tree $tree, int $block_id)
140    {
141    }
142
143    /**
144     * Get the favorites for a user
145     *
146     * @param Tree $tree
147     * @param User $user
148     *
149     * @return stdClass[]
150     */
151    public function getFavorites(Tree $tree, User $user)
152    {
153        $favorites = Database::prepare(
154            "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
155            " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id"
156        )->execute([
157            'tree_id' => $tree->getTreeId(),
158            'user_id' => $user->getUserId(),
159        ])->fetchAll();
160
161        foreach ($favorites as $favorite) {
162            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
163        }
164
165        return $favorites;
166    }
167
168    /**
169     * @param Request $request
170     * @param Tree    $tree
171     * @param User    $user
172     *
173     * @return RedirectResponse
174     */
175    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
176    {
177        $note  = $request->get('note', '');
178        $title = $request->get('title', '');
179        $url   = $request->get('url', '');
180        $xref  = $request->get('xref', '');
181
182        if (Auth::check()) {
183            if ($url !== '') {
184                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
185            } else {
186                $this->addRecordFavorite($tree, $user, $xref, $note);
187            }
188        }
189
190        $url = route('user-page', ['ged' => $tree->getName()]);
191
192        return new RedirectResponse($url);
193    }
194
195    /**
196     * @param Request $request
197     * @param Tree    $tree
198     * @param User    $user
199     *
200     * @return RedirectResponse
201     */
202    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
203    {
204        $favorite_id = (int)$request->get('favorite_id');
205
206        if (Auth::check()) {
207            Database::prepare(
208                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id"
209            )->execute([
210                'favorite_id' => $favorite_id,
211                'user_id'     => $user->getUserId(),
212            ]);
213        }
214
215        $url = route('user-page', ['ged' => $tree->getName()]);
216
217        return new RedirectResponse($url);
218    }
219
220    /**
221     * @param Tree   $tree
222     * @param User   $user
223     * @param string $url
224     * @param string $title
225     * @param string $note
226     */
227    private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note)
228    {
229        $favorite = Database::prepare(
230            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url"
231        )->execute([
232            'gedcom_id' => $tree->getTreeId(),
233            'user_id'   => $user->getUserId(),
234            'url'       => $url,
235        ])->fetchOneRow();
236
237        if ($favorite === null) {
238            Database::prepare(
239                "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
240            )->execute([
241                'gedcom_id' => $tree->getTreeId(),
242                'user_id'   => $user->getUserId(),
243                'url'       => $url,
244                'note'      => $note,
245                'title'     => $title,
246            ]);
247        } else {
248            Database::prepare(
249                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
250            )->execute([
251                'note'        => $note,
252                'title'       => $title,
253                'favorite_id' => $favorite->favorite_id,
254            ]);
255        }
256    }
257
258    /**
259     * @param Tree   $tree
260     * @param User   $user
261     * @param string $xref
262     * @param string $note
263     */
264    private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note)
265    {
266        $favorite = Database::prepare(
267            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref"
268        )->execute([
269            'gedcom_id' => $tree->getTreeId(),
270            'user_id'   => $user->getUserId(),
271            'xref'      => $xref,
272        ])->fetchOneRow();
273
274        if ($favorite === null) {
275            Database::prepare(
276                "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)"
277            )->execute([
278                'gedcom_id' => $tree->getTreeId(),
279                'user_id'   => $user->getUserId(),
280                'xref'      => $xref,
281                'note'      => $note,
282            ]);
283        } else {
284            Database::prepare(
285                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
286            )->execute([
287                'note'        => $note,
288                'favorite_id' => $favorite->favorite_id,
289            ]);
290        }
291    }
292}
293