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