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