xref: /webtrees/app/Module/FamilyTreeFavoritesModule.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 FamilyTreeFavoritesModule
30 */
31class FamilyTreeFavoritesModule 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 family tree’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/gedcom_favorites/favorites', [
68            'block_id'   => $block_id,
69            'favorites'  => $this->getFavorites($tree),
70            'is_manager' => Auth::isManager($tree),
71            'tree'       => $tree,
72        ]);
73
74        if ($template) {
75            return view('modules/block-template', [
76                'block'      => str_replace('_', '-', $this->getName()),
77                'id'         => $block_id,
78                'config_url' => '',
79                'title'      => $this->getTitle(),
80                'content'    => $content,
81            ]);
82        } else {
83            return $content;
84        }
85    }
86
87    /**
88     * Should this block load asynchronously using AJAX?
89     *
90     * Simple blocks are faster in-line, more comples ones
91     * can be loaded later.
92     *
93     * @return bool
94     */
95    public function loadAjax(): bool
96    {
97        return false;
98    }
99
100    /**
101     * Can this block be shown on the user’s home page?
102     *
103     * @return bool
104     */
105    public function isUserBlock(): bool
106    {
107        return false;
108    }
109
110    /**
111     * Can this block be shown on the tree’s home page?
112     *
113     * @return bool
114     */
115    public function isGedcomBlock(): bool
116    {
117        return true;
118    }
119
120    /**
121     * An HTML form to edit block settings
122     *
123     * @param Tree $tree
124     * @param int  $block_id
125     *
126     * @return void
127     */
128    public function configureBlock(Tree $tree, int $block_id)
129    {
130    }
131
132    /**
133     * Get favorites for a family tree
134     *
135     * @param Tree $tree
136     *
137     * @return stdClass[]
138     */
139    public function getFavorites(Tree $tree)
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 IS NULL")
145                ->execute([
146                    'tree_id' => $tree->getTreeId(),
147                ])
148                ->fetchAll();
149
150        foreach ($favorites as $favorite) {
151            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
152        }
153
154        return $favorites;
155    }
156
157    /**
158     * @param Request $request
159     * @param Tree    $tree
160     * @param User    $user
161     *
162     * @return RedirectResponse
163     */
164    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
165    {
166        $note  = $request->get('note', '');
167        $title = $request->get('title', '');
168        $url   = $request->get('url', '');
169        $xref  = $request->get('xref', '');
170
171        if (Auth::isManager($tree, $user)) {
172            if ($url !== '') {
173                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
174            } else {
175                $this->addRecordFavorite($tree, $xref, $note);
176            }
177        }
178
179        $url = route('tree-page', ['ged' => $tree->getName()]);
180
181        return new RedirectResponse($url);
182    }
183
184    /**
185     * @param Request $request
186     * @param Tree    $tree
187     * @param User    $user
188     *
189     * @return RedirectResponse
190     */
191    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
192    {
193        $favorite_id = (int)$request->get('favorite_id');
194
195        if (Auth::isManager($tree, $user)) {
196            Database::prepare(
197                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND gedcom_id = :tree_id"
198            )->execute([
199                'favorite_id' => $favorite_id,
200                'tree_id'     => $tree->getTreeId(),
201            ]);
202        }
203
204        $url = route('tree-page', ['ged' => $tree->getName()]);
205
206        return new RedirectResponse($url);
207    }
208
209    /**
210     * @param Tree   $tree
211     * @param string $url
212     * @param string $title
213     * @param string $note
214     */
215    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note)
216    {
217        $favorite = Database::prepare(
218            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND url = :url"
219        )->execute([
220            'gedcom_id' => $tree->getTreeId(),
221            'url'       => $url,
222        ])->fetchOneRow();
223
224        if ($favorite === null) {
225            Database::prepare(
226                "INSERT INTO `##favorite` (gedcom_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
227            )->execute([
228                'gedcom_id' => $tree->getTreeId(),
229                'url'       => $url,
230                'note'      => $note,
231                'title'     => $title,
232            ]);
233        } else {
234            Database::prepare(
235                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
236            )->execute([
237                'note'        => $note,
238                'title'       => $title,
239                'favorite_id' => $favorite->favorite_id,
240            ]);
241        }
242    }
243
244    /**
245     * @param Tree   $tree
246     * @param string $xref
247     * @param string $note
248     */
249    private function addRecordFavorite(Tree $tree, string $xref, string $note)
250    {
251        $favorite = Database::prepare(
252            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND xref = :xref"
253        )->execute([
254            'gedcom_id' => $tree->getTreeId(),
255            'xref'      => $xref,
256        ])->fetchOneRow();
257
258        if ($favorite === null) {
259            Database::prepare(
260                "INSERT INTO `##favorite` (gedcom_id, xref, note) VALUES (:gedcom_id, :xref, :note)"
261            )->execute([
262                'gedcom_id' => $tree->getTreeId(),
263                'xref'      => $xref,
264                'note'      => $note,
265            ]);
266        } else {
267            Database::prepare(
268                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
269            )->execute([
270                'note'        => $note,
271                'favorite_id' => $favorite->favorite_id,
272            ]);
273        }
274    }
275}
276