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