xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision 25cd7ed957527b02a19f452fffaf9bf46c879409)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
198c2e8227SGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
249807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
25a6afca4cSGreg Roachuse Fisharebest\Webtrees\User;
269807cf72SGreg Roachuse stdClass;
27a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
28a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\Request;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class FamilyTreeFavoritesModule
328c2e8227SGreg Roach */
33c1010edaSGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3576692c8bSGreg Roach    /**
3676692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3776692c8bSGreg Roach     *
3876692c8bSGreg Roach     * @return string
3976692c8bSGreg Roach     */
408f53f488SRico Sonntag    public function getTitle(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module */
43bbb76c12SGreg Roach        return I18N::translate('Favorites');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
4776692c8bSGreg Roach     * A sentence describing what this module does.
4876692c8bSGreg Roach     *
4976692c8bSGreg Roach     * @return string
5076692c8bSGreg Roach     */
518f53f488SRico Sonntag    public function getDescription(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
54bbb76c12SGreg Roach        return I18N::translate('Display and manage a family tree’s favorite pages.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * Generate the HTML content of this block.
5976692c8bSGreg Roach     *
60e490cd80SGreg Roach     * @param Tree     $tree
6176692c8bSGreg Roach     * @param int      $block_id
6276692c8bSGreg Roach     * @param bool     $template
63727f238cSGreg Roach     * @param string[] $cfg
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return string
6676692c8bSGreg Roach     */
67c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
68c1010edaSGreg Roach    {
69a4439c01SGreg Roach        $content = view('modules/gedcom_favorites/favorites', [
70a4439c01SGreg Roach            'block_id'   => $block_id,
71a4439c01SGreg Roach            'favorites'  => $this->getFavorites($tree),
72e490cd80SGreg Roach            'is_manager' => Auth::isManager($tree),
73e490cd80SGreg Roach            'tree'       => $tree,
749807cf72SGreg Roach        ]);
759807cf72SGreg Roach
768c2e8227SGreg Roach        if ($template) {
77147e99aaSGreg Roach            return view('modules/block-template', [
789c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
799c6524dcSGreg Roach                'id'         => $block_id,
809c6524dcSGreg Roach                'config_url' => '',
819c6524dcSGreg Roach                'title'      => $this->getTitle(),
829c6524dcSGreg Roach                'content'    => $content,
839c6524dcSGreg Roach            ]);
848c2e8227SGreg Roach        }
85b2ce94c6SRico Sonntag
86b2ce94c6SRico Sonntag        return $content;
878c2e8227SGreg Roach    }
888c2e8227SGreg Roach
8976692c8bSGreg Roach    /**
9076692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
9176692c8bSGreg Roach     *
9276692c8bSGreg Roach     * Simple blocks are faster in-line, more comples ones
9376692c8bSGreg Roach     * can be loaded later.
9476692c8bSGreg Roach     *
9576692c8bSGreg Roach     * @return bool
9676692c8bSGreg Roach     */
97c1010edaSGreg Roach    public function loadAjax(): bool
98c1010edaSGreg Roach    {
998c2e8227SGreg Roach        return false;
1008c2e8227SGreg Roach    }
1018c2e8227SGreg Roach
10276692c8bSGreg Roach    /**
10376692c8bSGreg Roach     * Can this block be shown on the user’s home page?
10476692c8bSGreg Roach     *
10576692c8bSGreg Roach     * @return bool
10676692c8bSGreg Roach     */
107c1010edaSGreg Roach    public function isUserBlock(): bool
108c1010edaSGreg Roach    {
1098c2e8227SGreg Roach        return false;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
11276692c8bSGreg Roach    /**
11376692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
11476692c8bSGreg Roach     *
11576692c8bSGreg Roach     * @return bool
11676692c8bSGreg Roach     */
117c1010edaSGreg Roach    public function isGedcomBlock(): bool
118c1010edaSGreg Roach    {
1198c2e8227SGreg Roach        return true;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
12276692c8bSGreg Roach    /**
123a45f9889SGreg Roach     * Update the configuration for a block.
124a45f9889SGreg Roach     *
125a45f9889SGreg Roach     * @param Request $request
126a45f9889SGreg Roach     * @param int     $block_id
127a45f9889SGreg Roach     *
128a45f9889SGreg Roach     * @return void
129a45f9889SGreg Roach     */
130a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
131a45f9889SGreg Roach    {
132a45f9889SGreg Roach    }
133a45f9889SGreg Roach
134a45f9889SGreg Roach    /**
13576692c8bSGreg Roach     * An HTML form to edit block settings
13676692c8bSGreg Roach     *
137e490cd80SGreg Roach     * @param Tree $tree
13876692c8bSGreg Roach     * @param int  $block_id
139a9430be8SGreg Roach     *
140a9430be8SGreg Roach     * @return void
14176692c8bSGreg Roach     */
142a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
143c1010edaSGreg Roach    {
1448d68cabeSGreg Roach    }
1458c2e8227SGreg Roach
1468c2e8227SGreg Roach    /**
147a4439c01SGreg Roach     * Get favorites for a family tree
1488c2e8227SGreg Roach     *
1499807cf72SGreg Roach     * @param Tree $tree
1508c2e8227SGreg Roach     *
1519807cf72SGreg Roach     * @return stdClass[]
1528c2e8227SGreg Roach     */
1538f53f488SRico Sonntag    public function getFavorites(Tree $tree): array
154c1010edaSGreg Roach    {
155bdb3725aSGreg Roach        $favorites = Database::prepare(
156e5588fb0SGreg Roach            "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
157bdb3725aSGreg Roach            " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id IS NULL"
158bdb3725aSGreg Roach        )->execute([
15972cf66d4SGreg Roach            'tree_id' => $tree->id(),
160bdb3725aSGreg Roach        ])->fetchAll();
1619807cf72SGreg Roach
1629807cf72SGreg Roach        foreach ($favorites as $favorite) {
163*25cd7ed9SGreg Roach            if ($favorite->xref !== null) {
1649807cf72SGreg Roach                $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
165*25cd7ed9SGreg Roach            } else {
166*25cd7ed9SGreg Roach                $favorite->record = null;
167*25cd7ed9SGreg Roach            }
1689807cf72SGreg Roach        }
1699807cf72SGreg Roach
1709807cf72SGreg Roach        return $favorites;
1718c2e8227SGreg Roach    }
172a4439c01SGreg Roach
173a4439c01SGreg Roach    /**
174a4439c01SGreg Roach     * @param Request $request
175b6db7c1fSGreg Roach     * @param Tree    $tree
176b6db7c1fSGreg Roach     * @param User    $user
177a4439c01SGreg Roach     *
178a4439c01SGreg Roach     * @return RedirectResponse
179a4439c01SGreg Roach     */
180b6db7c1fSGreg Roach    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
181c1010edaSGreg Roach    {
182a4439c01SGreg Roach        $note  = $request->get('note', '');
183a4439c01SGreg Roach        $title = $request->get('title', '');
184a4439c01SGreg Roach        $url   = $request->get('url', '');
185a4439c01SGreg Roach        $xref  = $request->get('xref', '');
186a4439c01SGreg Roach
187*25cd7ed9SGreg Roach        $record = GedcomRecord::getInstance($xref, $tree);
188*25cd7ed9SGreg Roach
189a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
190a4439c01SGreg Roach            if ($url !== '') {
191a4439c01SGreg Roach                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
192a4439c01SGreg Roach            } else {
193*25cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $record, $note);
194a4439c01SGreg Roach            }
195a4439c01SGreg Roach        }
196a4439c01SGreg Roach
197aa6f03bbSGreg Roach        $url = route('tree-page', ['ged' => $tree->name()]);
198a4439c01SGreg Roach
199a4439c01SGreg Roach        return new RedirectResponse($url);
200a4439c01SGreg Roach    }
201a4439c01SGreg Roach
202a4439c01SGreg Roach    /**
203a4439c01SGreg Roach     * @param Request $request
204b6db7c1fSGreg Roach     * @param Tree    $tree
205b6db7c1fSGreg Roach     * @param User    $user
206a4439c01SGreg Roach     *
207a4439c01SGreg Roach     * @return RedirectResponse
208a4439c01SGreg Roach     */
209b6db7c1fSGreg Roach    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
210c1010edaSGreg Roach    {
211a4439c01SGreg Roach        $favorite_id = (int) $request->get('favorite_id');
212a4439c01SGreg Roach
213a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
214a4439c01SGreg Roach            Database::prepare(
215a4439c01SGreg Roach                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND gedcom_id = :tree_id"
216a4439c01SGreg Roach            )->execute([
217a4439c01SGreg Roach                'favorite_id' => $favorite_id,
21872cf66d4SGreg Roach                'tree_id'     => $tree->id(),
219a4439c01SGreg Roach            ]);
220a4439c01SGreg Roach        }
221a4439c01SGreg Roach
222aa6f03bbSGreg Roach        $url = route('tree-page', ['ged' => $tree->name()]);
223a4439c01SGreg Roach
224a4439c01SGreg Roach        return new RedirectResponse($url);
225a4439c01SGreg Roach    }
226a4439c01SGreg Roach
227a4439c01SGreg Roach    /**
228a4439c01SGreg Roach     * @param Tree   $tree
229a4439c01SGreg Roach     * @param string $url
230a4439c01SGreg Roach     * @param string $title
231a4439c01SGreg Roach     * @param string $note
23218d7a90dSGreg Roach     *
23318d7a90dSGreg Roach     * @return void
234a4439c01SGreg Roach     */
235c1010edaSGreg Roach    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note)
236c1010edaSGreg Roach    {
237a4439c01SGreg Roach        $favorite = Database::prepare(
238a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND url = :url"
239a4439c01SGreg Roach        )->execute([
24072cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
241a4439c01SGreg Roach            'url'       => $url,
242a4439c01SGreg Roach        ])->fetchOneRow();
243a4439c01SGreg Roach
244a4439c01SGreg Roach        if ($favorite === null) {
245a4439c01SGreg Roach            Database::prepare(
246*25cd7ed9SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, favorite_type, url, note, title) VALUES (:gedcom_id, 'URL', :url, :note, :title)"
247a4439c01SGreg Roach            )->execute([
24872cf66d4SGreg Roach                'gedcom_id' => $tree->id(),
249a4439c01SGreg Roach                'url'       => $url,
250a4439c01SGreg Roach                'note'      => $note,
251a4439c01SGreg Roach                'title'     => $title,
252a4439c01SGreg Roach            ]);
253a4439c01SGreg Roach        } else {
254a4439c01SGreg Roach            Database::prepare(
255a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
256a4439c01SGreg Roach            )->execute([
257a4439c01SGreg Roach                'note'        => $note,
258a4439c01SGreg Roach                'title'       => $title,
259a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
260a4439c01SGreg Roach            ]);
261a4439c01SGreg Roach        }
262a4439c01SGreg Roach    }
263a4439c01SGreg Roach
264a4439c01SGreg Roach    /**
265a4439c01SGreg Roach     * @param Tree         $tree
266*25cd7ed9SGreg Roach     * @param GedcomRecord $record
267a4439c01SGreg Roach     * @param string       $note
26818d7a90dSGreg Roach     *
26918d7a90dSGreg Roach     * @return void
270a4439c01SGreg Roach     */
271*25cd7ed9SGreg Roach    private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note)
272c1010edaSGreg Roach    {
273a4439c01SGreg Roach        $favorite = Database::prepare(
274a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND xref = :xref"
275a4439c01SGreg Roach        )->execute([
27672cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
277*25cd7ed9SGreg Roach            'xref'      => $record->xref(),
278a4439c01SGreg Roach        ])->fetchOneRow();
279a4439c01SGreg Roach
280a4439c01SGreg Roach        if ($favorite === null) {
281a4439c01SGreg Roach            Database::prepare(
282*25cd7ed9SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, favorite_type, xref, note) VALUES (:gedcom_id, :favorite_type, :xref, :note)"
283a4439c01SGreg Roach            )->execute([
28472cf66d4SGreg Roach                'gedcom_id'     => $tree->id(),
285*25cd7ed9SGreg Roach                'favorite_type' => $record::RECORD_TYPE,
286*25cd7ed9SGreg Roach                'xref'          => $record->xref(),
287a4439c01SGreg Roach                'note'          => $note,
288a4439c01SGreg Roach            ]);
289a4439c01SGreg Roach        } else {
290a4439c01SGreg Roach            Database::prepare(
291a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
292a4439c01SGreg Roach            )->execute([
293a4439c01SGreg Roach                'note'        => $note,
294a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
295a4439c01SGreg Roach            ]);
296a4439c01SGreg Roach        }
297a4439c01SGreg Roach    }
2988c2e8227SGreg Roach}
299