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