xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision b6db7c1f82484c632c75d5225ce767d398ddc27f)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
229807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
23a6afca4cSGreg Roachuse Fisharebest\Webtrees\User;
249807cf72SGreg Roachuse stdClass;
25a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
26a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class FamilyTreeFavoritesModule
308c2e8227SGreg Roach */
31c1010edaSGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3376692c8bSGreg Roach    /**
3476692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3576692c8bSGreg Roach     *
3676692c8bSGreg Roach     * @return string
3776692c8bSGreg Roach     */
38c1010edaSGreg Roach    public function getTitle()
39c1010edaSGreg Roach    {
40c1010edaSGreg Roach        return /* I18N: Name of a module */
41c1010edaSGreg Roach            I18N::translate('Favorites');
428c2e8227SGreg Roach    }
438c2e8227SGreg Roach
4476692c8bSGreg Roach    /**
4576692c8bSGreg Roach     * A sentence describing what this module does.
4676692c8bSGreg Roach     *
4776692c8bSGreg Roach     * @return string
4876692c8bSGreg Roach     */
49c1010edaSGreg Roach    public function getDescription()
50c1010edaSGreg Roach    {
51c1010edaSGreg Roach        return /* I18N: Description of the “Favorites” module */
52c1010edaSGreg Roach            I18N::translate('Display and manage a family tree’s favorite pages.');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
5576692c8bSGreg Roach    /**
5676692c8bSGreg Roach     * Generate the HTML content of this block.
5776692c8bSGreg Roach     *
58e490cd80SGreg Roach     * @param Tree     $tree
5976692c8bSGreg Roach     * @param int      $block_id
6076692c8bSGreg Roach     * @param bool     $template
61727f238cSGreg Roach     * @param string[] $cfg
6276692c8bSGreg Roach     *
6376692c8bSGreg Roach     * @return string
6476692c8bSGreg Roach     */
65c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
66c1010edaSGreg Roach    {
67a4439c01SGreg Roach        $content = view('modules/gedcom_favorites/favorites', [
68a4439c01SGreg Roach            'block_id'   => $block_id,
69a4439c01SGreg Roach            'favorites'  => $this->getFavorites($tree),
70e490cd80SGreg Roach            'is_manager' => Auth::isManager($tree),
71e490cd80SGreg Roach            'tree'       => $tree,
729807cf72SGreg Roach        ]);
739807cf72SGreg Roach
748c2e8227SGreg Roach        if ($template) {
75147e99aaSGreg Roach            return view('modules/block-template', [
769c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
779c6524dcSGreg Roach                'id'         => $block_id,
789c6524dcSGreg Roach                'config_url' => '',
799c6524dcSGreg Roach                'title'      => $this->getTitle(),
809c6524dcSGreg Roach                'content'    => $content,
819c6524dcSGreg Roach            ]);
828c2e8227SGreg Roach        } else {
838c2e8227SGreg Roach            return $content;
848c2e8227SGreg Roach        }
858c2e8227SGreg Roach    }
868c2e8227SGreg Roach
8776692c8bSGreg Roach    /**
8876692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
8976692c8bSGreg Roach     *
9076692c8bSGreg Roach     * Simple blocks are faster in-line, more comples ones
9176692c8bSGreg Roach     * can be loaded later.
9276692c8bSGreg Roach     *
9376692c8bSGreg Roach     * @return bool
9476692c8bSGreg Roach     */
95c1010edaSGreg Roach    public function loadAjax(): bool
96c1010edaSGreg Roach    {
978c2e8227SGreg Roach        return false;
988c2e8227SGreg Roach    }
998c2e8227SGreg Roach
10076692c8bSGreg Roach    /**
10176692c8bSGreg Roach     * Can this block be shown on the user’s home page?
10276692c8bSGreg Roach     *
10376692c8bSGreg Roach     * @return bool
10476692c8bSGreg Roach     */
105c1010edaSGreg Roach    public function isUserBlock(): bool
106c1010edaSGreg Roach    {
1078c2e8227SGreg Roach        return false;
1088c2e8227SGreg Roach    }
1098c2e8227SGreg Roach
11076692c8bSGreg Roach    /**
11176692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
11276692c8bSGreg Roach     *
11376692c8bSGreg Roach     * @return bool
11476692c8bSGreg Roach     */
115c1010edaSGreg Roach    public function isGedcomBlock(): bool
116c1010edaSGreg Roach    {
1178c2e8227SGreg Roach        return true;
1188c2e8227SGreg Roach    }
1198c2e8227SGreg Roach
12076692c8bSGreg Roach    /**
12176692c8bSGreg Roach     * An HTML form to edit block settings
12276692c8bSGreg Roach     *
123e490cd80SGreg Roach     * @param Tree $tree
12476692c8bSGreg Roach     * @param int  $block_id
125a9430be8SGreg Roach     *
126a9430be8SGreg Roach     * @return void
12776692c8bSGreg Roach     */
128c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
129c1010edaSGreg Roach    {
1308d68cabeSGreg Roach    }
1318c2e8227SGreg Roach
1328c2e8227SGreg Roach    /**
133a4439c01SGreg Roach     * Get favorites for a family tree
1348c2e8227SGreg Roach     *
1359807cf72SGreg Roach     * @param Tree $tree
1368c2e8227SGreg Roach     *
1379807cf72SGreg Roach     * @return stdClass[]
1388c2e8227SGreg Roach     */
139c1010edaSGreg Roach    public function getFavorites(Tree $tree)
140c1010edaSGreg Roach    {
1419807cf72SGreg Roach        $favorites =
1428c2e8227SGreg Roach            Database::prepare(
143e5588fb0SGreg Roach                "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
1449807cf72SGreg Roach                " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id IS NULL")
1459807cf72SGreg Roach                ->execute([
1469807cf72SGreg Roach                    'tree_id' => $tree->getTreeId(),
1479807cf72SGreg Roach                ])
1489807cf72SGreg Roach                ->fetchAll();
1499807cf72SGreg Roach
1509807cf72SGreg Roach        foreach ($favorites as $favorite) {
1519807cf72SGreg Roach            $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
1529807cf72SGreg Roach        }
1539807cf72SGreg Roach
1549807cf72SGreg Roach        return $favorites;
1558c2e8227SGreg Roach    }
156a4439c01SGreg Roach
157a4439c01SGreg Roach    /**
158a4439c01SGreg Roach     * @param Request $request
159*b6db7c1fSGreg Roach     * @param Tree    $tree
160*b6db7c1fSGreg Roach     * @param User    $user
161a4439c01SGreg Roach     *
162a4439c01SGreg Roach     * @return RedirectResponse
163a4439c01SGreg Roach     */
164*b6db7c1fSGreg Roach    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
165c1010edaSGreg Roach    {
166a4439c01SGreg Roach        $note  = $request->get('note', '');
167a4439c01SGreg Roach        $title = $request->get('title', '');
168a4439c01SGreg Roach        $url   = $request->get('url', '');
169a4439c01SGreg Roach        $xref  = $request->get('xref', '');
170a4439c01SGreg Roach
171a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
172a4439c01SGreg Roach            if ($url !== '') {
173a4439c01SGreg Roach                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
174a4439c01SGreg Roach            } else {
175a4439c01SGreg Roach                $this->addRecordFavorite($tree, $xref, $note);
176a4439c01SGreg Roach            }
177a4439c01SGreg Roach        }
178a4439c01SGreg Roach
179a4439c01SGreg Roach        $url = route('tree-page', ['ged' => $tree->getName()]);
180a4439c01SGreg Roach
181a4439c01SGreg Roach        return new RedirectResponse($url);
182a4439c01SGreg Roach    }
183a4439c01SGreg Roach
184a4439c01SGreg Roach    /**
185a4439c01SGreg Roach     * @param Request $request
186*b6db7c1fSGreg Roach     * @param Tree    $tree
187*b6db7c1fSGreg Roach     * @param User    $user
188a4439c01SGreg Roach     *
189a4439c01SGreg Roach     * @return RedirectResponse
190a4439c01SGreg Roach     */
191*b6db7c1fSGreg Roach    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
192c1010edaSGreg Roach    {
193a4439c01SGreg Roach        $favorite_id = (int)$request->get('favorite_id');
194a4439c01SGreg Roach
195a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
196a4439c01SGreg Roach            Database::prepare(
197a4439c01SGreg Roach                "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND gedcom_id = :tree_id"
198a4439c01SGreg Roach            )->execute([
199a4439c01SGreg Roach                'favorite_id' => $favorite_id,
200a4439c01SGreg Roach                'tree_id'     => $tree->getTreeId(),
201a4439c01SGreg Roach            ]);
202a4439c01SGreg Roach        }
203a4439c01SGreg Roach
204a4439c01SGreg Roach        $url = route('tree-page', ['ged' => $tree->getName()]);
205a4439c01SGreg Roach
206a4439c01SGreg Roach        return new RedirectResponse($url);
207a4439c01SGreg Roach    }
208a4439c01SGreg Roach
209a4439c01SGreg Roach    /**
210a4439c01SGreg Roach     * @param Tree   $tree
211a4439c01SGreg Roach     * @param string $url
212a4439c01SGreg Roach     * @param string $title
213a4439c01SGreg Roach     * @param string $note
214a4439c01SGreg Roach     */
215c1010edaSGreg Roach    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note)
216c1010edaSGreg Roach    {
217a4439c01SGreg Roach        $favorite = Database::prepare(
218a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND url = :url"
219a4439c01SGreg Roach        )->execute([
220a4439c01SGreg Roach            'gedcom_id' => $tree->getTreeId(),
221a4439c01SGreg Roach            'url'       => $url,
222a4439c01SGreg Roach        ])->fetchOneRow();
223a4439c01SGreg Roach
224a4439c01SGreg Roach        if ($favorite === null) {
225a4439c01SGreg Roach            Database::prepare(
226a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)"
227a4439c01SGreg Roach            )->execute([
228a4439c01SGreg Roach                'gedcom_id' => $tree->getTreeId(),
229a4439c01SGreg Roach                'url'       => $url,
230a4439c01SGreg Roach                'note'      => $note,
231a4439c01SGreg Roach                'title'     => $title,
232a4439c01SGreg Roach            ]);
233a4439c01SGreg Roach        } else {
234a4439c01SGreg Roach            Database::prepare(
235a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id"
236a4439c01SGreg Roach            )->execute([
237a4439c01SGreg Roach                'note'        => $note,
238a4439c01SGreg Roach                'title'       => $title,
239a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
240a4439c01SGreg Roach            ]);
241a4439c01SGreg Roach        }
242a4439c01SGreg Roach    }
243a4439c01SGreg Roach
244a4439c01SGreg Roach    /**
245a4439c01SGreg Roach     * @param Tree   $tree
246a4439c01SGreg Roach     * @param string $xref
247a4439c01SGreg Roach     * @param string $note
248a4439c01SGreg Roach     */
249c1010edaSGreg Roach    private function addRecordFavorite(Tree $tree, string $xref, string $note)
250c1010edaSGreg Roach    {
251a4439c01SGreg Roach        $favorite = Database::prepare(
252a4439c01SGreg Roach            "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND xref = :xref"
253a4439c01SGreg Roach        )->execute([
254a4439c01SGreg Roach            'gedcom_id' => $tree->getTreeId(),
255a4439c01SGreg Roach            'xref'      => $xref,
256a4439c01SGreg Roach        ])->fetchOneRow();
257a4439c01SGreg Roach
258a4439c01SGreg Roach        if ($favorite === null) {
259a4439c01SGreg Roach            Database::prepare(
260a4439c01SGreg Roach                "INSERT INTO `##favorite` (gedcom_id, xref, note) VALUES (:gedcom_id, :xref, :note)"
261a4439c01SGreg Roach            )->execute([
262a4439c01SGreg Roach                'gedcom_id' => $tree->getTreeId(),
263a4439c01SGreg Roach                'xref'      => $xref,
264a4439c01SGreg Roach                'note'      => $note,
265a4439c01SGreg Roach            ]);
266a4439c01SGreg Roach        } else {
267a4439c01SGreg Roach            Database::prepare(
268a4439c01SGreg Roach                "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id"
269a4439c01SGreg Roach            )->execute([
270a4439c01SGreg Roach                'note'        => $note,
271a4439c01SGreg Roach                'favorite_id' => $favorite->favorite_id,
272a4439c01SGreg Roach            ]);
273a4439c01SGreg Roach        }
274a4439c01SGreg Roach    }
2758c2e8227SGreg Roach}
276