xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision aa6f03bb51be5a55a78fe8aed18753116689a6d3)
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) {
1639807cf72SGreg Roach            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
1649807cf72SGreg Roach        }
1659807cf72SGreg Roach
1669807cf72SGreg Roach        return $favorites;
1678c2e8227SGreg Roach    }
168a4439c01SGreg Roach
169a4439c01SGreg Roach    /**
170a4439c01SGreg Roach     * @param Request $request
171b6db7c1fSGreg Roach     * @param Tree    $tree
172b6db7c1fSGreg Roach     * @param User    $user
173a4439c01SGreg Roach     *
174a4439c01SGreg Roach     * @return RedirectResponse
175a4439c01SGreg Roach     */
176b6db7c1fSGreg Roach    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
177c1010edaSGreg Roach    {
178a4439c01SGreg Roach        $note  = $request->get('note', '');
179a4439c01SGreg Roach        $title = $request->get('title', '');
180a4439c01SGreg Roach        $url   = $request->get('url', '');
181a4439c01SGreg Roach        $xref  = $request->get('xref', '');
182a4439c01SGreg Roach
183a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
184a4439c01SGreg Roach            if ($url !== '') {
185a4439c01SGreg Roach                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
186a4439c01SGreg Roach            } else {
187a4439c01SGreg Roach                $this->addRecordFavorite($tree, $xref, $note);
188a4439c01SGreg Roach            }
189a4439c01SGreg Roach        }
190a4439c01SGreg Roach
191*aa6f03bbSGreg Roach        $url = route('tree-page', ['ged' => $tree->name()]);
192a4439c01SGreg Roach
193a4439c01SGreg Roach        return new RedirectResponse($url);
194a4439c01SGreg Roach    }
195a4439c01SGreg Roach
196a4439c01SGreg Roach    /**
197a4439c01SGreg Roach     * @param Request $request
198b6db7c1fSGreg Roach     * @param Tree    $tree
199b6db7c1fSGreg Roach     * @param User    $user
200a4439c01SGreg Roach     *
201a4439c01SGreg Roach     * @return RedirectResponse
202a4439c01SGreg Roach     */
203b6db7c1fSGreg Roach    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
204c1010edaSGreg Roach    {
205a4439c01SGreg Roach        $favorite_id = (int) $request->get('favorite_id');
206a4439c01SGreg Roach
207a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
208a4439c01SGreg Roach            Database::prepare(
209a4439c01SGreg Roach                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND gedcom_id = :tree_id"
210a4439c01SGreg Roach            )->execute([
211a4439c01SGreg Roach                'favorite_id' => $favorite_id,
21272cf66d4SGreg Roach                'tree_id'     => $tree->id(),
213a4439c01SGreg Roach            ]);
214a4439c01SGreg Roach        }
215a4439c01SGreg Roach
216*aa6f03bbSGreg Roach        $url = route('tree-page', ['ged' => $tree->name()]);
217a4439c01SGreg Roach
218a4439c01SGreg Roach        return new RedirectResponse($url);
219a4439c01SGreg Roach    }
220a4439c01SGreg Roach
221a4439c01SGreg Roach    /**
222a4439c01SGreg Roach     * @param Tree   $tree
223a4439c01SGreg Roach     * @param string $url
224a4439c01SGreg Roach     * @param string $title
225a4439c01SGreg Roach     * @param string $note
22618d7a90dSGreg Roach     *
22718d7a90dSGreg Roach     * @return void
228a4439c01SGreg Roach     */
229c1010edaSGreg Roach    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note)
230c1010edaSGreg Roach    {
231a4439c01SGreg Roach        $favorite = Database::prepare(
232a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND url = :url"
233a4439c01SGreg Roach        )->execute([
23472cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
235a4439c01SGreg Roach            'url'       => $url,
236a4439c01SGreg Roach        ])->fetchOneRow();
237a4439c01SGreg Roach
238a4439c01SGreg Roach        if ($favorite === null) {
239a4439c01SGreg Roach            Database::prepare(
240a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
241a4439c01SGreg Roach            )->execute([
24272cf66d4SGreg Roach                'gedcom_id' => $tree->id(),
243a4439c01SGreg Roach                'url'       => $url,
244a4439c01SGreg Roach                'note'      => $note,
245a4439c01SGreg Roach                'title'     => $title,
246a4439c01SGreg Roach            ]);
247a4439c01SGreg Roach        } else {
248a4439c01SGreg Roach            Database::prepare(
249a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
250a4439c01SGreg Roach            )->execute([
251a4439c01SGreg Roach                'note'        => $note,
252a4439c01SGreg Roach                'title'       => $title,
253a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
254a4439c01SGreg Roach            ]);
255a4439c01SGreg Roach        }
256a4439c01SGreg Roach    }
257a4439c01SGreg Roach
258a4439c01SGreg Roach    /**
259a4439c01SGreg Roach     * @param Tree   $tree
260a4439c01SGreg Roach     * @param string $xref
261a4439c01SGreg Roach     * @param string $note
26218d7a90dSGreg Roach     *
26318d7a90dSGreg Roach     * @return void
264a4439c01SGreg Roach     */
265c1010edaSGreg Roach    private function addRecordFavorite(Tree $tree, string $xref, string $note)
266c1010edaSGreg Roach    {
267a4439c01SGreg Roach        $favorite = Database::prepare(
268a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND xref = :xref"
269a4439c01SGreg Roach        )->execute([
27072cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
271a4439c01SGreg Roach            'xref'      => $xref,
272a4439c01SGreg Roach        ])->fetchOneRow();
273a4439c01SGreg Roach
274a4439c01SGreg Roach        if ($favorite === null) {
275a4439c01SGreg Roach            Database::prepare(
276a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, xref, note) VALUES (:gedcom_id, :xref, :note)"
277a4439c01SGreg Roach            )->execute([
27872cf66d4SGreg Roach                'gedcom_id' => $tree->id(),
279a4439c01SGreg Roach                'xref'      => $xref,
280a4439c01SGreg Roach                'note'      => $note,
281a4439c01SGreg Roach            ]);
282a4439c01SGreg Roach        } else {
283a4439c01SGreg Roach            Database::prepare(
284a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
285a4439c01SGreg Roach            )->execute([
286a4439c01SGreg Roach                'note'        => $note,
287a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
288a4439c01SGreg Roach            ]);
289a4439c01SGreg Roach        }
290a4439c01SGreg Roach    }
2918c2e8227SGreg Roach}
292