xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision eb235819d2dcd26d677ffd48caefaff86620ce99)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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;
21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
228ec20abdSGreg Roachuse Fisharebest\Webtrees\Family;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
258ec20abdSGreg Roachuse Fisharebest\Webtrees\Individual;
268ec20abdSGreg Roachuse Fisharebest\Webtrees\Media;
278ec20abdSGreg Roachuse Fisharebest\Webtrees\Repository;
288ec20abdSGreg Roachuse Fisharebest\Webtrees\Source;
299807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
300587a5b3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
311e7a7a28SGreg Roachuse Illuminate\Support\Str;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
349807cf72SGreg Roachuse stdClass;
358c2e8227SGreg Roach
368c2e8227SGreg Roach/**
378c2e8227SGreg Roach * Class FamilyTreeFavoritesModule
388c2e8227SGreg Roach */
3937eb8894SGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface
40c1010edaSGreg Roach{
4149a243cbSGreg Roach    use ModuleBlockTrait;
4249a243cbSGreg Roach
4376692c8bSGreg Roach    /**
440cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4576692c8bSGreg Roach     *
4676692c8bSGreg Roach     * @return string
4776692c8bSGreg Roach     */
4849a243cbSGreg Roach    public function title(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Name of a module */
51bbb76c12SGreg Roach        return I18N::translate('Favorites');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
5476692c8bSGreg Roach    /**
5576692c8bSGreg Roach     * A sentence describing what this module does.
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @return string
5876692c8bSGreg Roach     */
5949a243cbSGreg Roach    public function description(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Description of the “Favorites” module */
62bbb76c12SGreg Roach        return I18N::translate('Display and manage a family tree’s favorite pages.');
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
6576692c8bSGreg Roach    /**
6676692c8bSGreg Roach     * Generate the HTML content of this block.
6776692c8bSGreg Roach     *
68e490cd80SGreg Roach     * @param Tree     $tree
6976692c8bSGreg Roach     * @param int      $block_id
705f2ae573SGreg Roach     * @param string   $ctype
71727f238cSGreg Roach     * @param string[] $cfg
7276692c8bSGreg Roach     *
7376692c8bSGreg Roach     * @return string
7476692c8bSGreg Roach     */
755f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
76c1010edaSGreg Roach    {
778ec20abdSGreg Roach        $content = view('modules/favorites/favorites', [
78a4439c01SGreg Roach            'block_id'    => $block_id,
798ec20abdSGreg Roach            'can_edit'    => Auth::isManager($tree),
80a4439c01SGreg Roach            'favorites'   => $this->getFavorites($tree),
818ec20abdSGreg Roach            'module_name' => $this->name(),
82e490cd80SGreg Roach            'tree'        => $tree,
839807cf72SGreg Roach        ]);
849807cf72SGreg Roach
856a8879feSGreg Roach        if ($ctype !== '') {
86147e99aaSGreg Roach            return view('modules/block-template', [
871e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
889c6524dcSGreg Roach                'id'         => $block_id,
899c6524dcSGreg Roach                'config_url' => '',
9049a243cbSGreg Roach                'title'      => $this->title(),
919c6524dcSGreg Roach                'content'    => $content,
929c6524dcSGreg Roach            ]);
938c2e8227SGreg Roach        }
94b2ce94c6SRico Sonntag
95b2ce94c6SRico Sonntag        return $content;
968c2e8227SGreg Roach    }
978c2e8227SGreg Roach
9876692c8bSGreg Roach    /**
9976692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
10076692c8bSGreg Roach     * Simple blocks are faster in-line, more comples ones
10176692c8bSGreg Roach     * can be loaded later.
10276692c8bSGreg Roach     *
10376692c8bSGreg Roach     * @return bool
10476692c8bSGreg Roach     */
105c1010edaSGreg Roach    public function loadAjax(): bool
106c1010edaSGreg Roach    {
1078c2e8227SGreg Roach        return false;
1088c2e8227SGreg Roach    }
1098c2e8227SGreg Roach
11076692c8bSGreg Roach    /**
11176692c8bSGreg Roach     * Can this block be shown on the user’s home page?
11276692c8bSGreg Roach     *
11376692c8bSGreg Roach     * @return bool
11476692c8bSGreg Roach     */
115c1010edaSGreg Roach    public function isUserBlock(): bool
116c1010edaSGreg Roach    {
1178c2e8227SGreg Roach        return false;
1188c2e8227SGreg Roach    }
1198c2e8227SGreg Roach
12076692c8bSGreg Roach    /**
12176692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
12276692c8bSGreg Roach     *
12376692c8bSGreg Roach     * @return bool
12476692c8bSGreg Roach     */
12563276d8fSGreg Roach    public function isTreeBlock(): bool
126c1010edaSGreg Roach    {
1278c2e8227SGreg Roach        return true;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
13076692c8bSGreg Roach    /**
131a45f9889SGreg Roach     * Update the configuration for a block.
132a45f9889SGreg Roach     *
1336ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
134a45f9889SGreg Roach     * @param int     $block_id
135a45f9889SGreg Roach     *
136a45f9889SGreg Roach     * @return void
137a45f9889SGreg Roach     */
1386ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
139a45f9889SGreg Roach    {
140a45f9889SGreg Roach    }
141a45f9889SGreg Roach
142a45f9889SGreg Roach    /**
14376692c8bSGreg Roach     * An HTML form to edit block settings
14476692c8bSGreg Roach     *
145e490cd80SGreg Roach     * @param Tree $tree
14676692c8bSGreg Roach     * @param int  $block_id
147a9430be8SGreg Roach     *
148a9430be8SGreg Roach     * @return void
14976692c8bSGreg Roach     */
150e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
151c1010edaSGreg Roach    {
1528d68cabeSGreg Roach    }
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach    /**
1558ec20abdSGreg Roach     * Get the favorites for a family tree
1568c2e8227SGreg Roach     *
1579807cf72SGreg Roach     * @param Tree $tree
1588c2e8227SGreg Roach     *
1599807cf72SGreg Roach     * @return stdClass[]
1608c2e8227SGreg Roach     */
1618f53f488SRico Sonntag    public function getFavorites(Tree $tree): array
162c1010edaSGreg Roach    {
1630587a5b3SGreg Roach        return DB::table('favorite')
1640587a5b3SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1650587a5b3SGreg Roach            ->whereNull('user_id')
1660587a5b3SGreg Roach            ->get()
1670b5fd0a6SGreg Roach            ->map(static function (stdClass $row) use ($tree): stdClass {
1680587a5b3SGreg Roach                if ($row->xref !== null) {
1690587a5b3SGreg Roach                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
17025cd7ed9SGreg Roach                } else {
1710587a5b3SGreg Roach                    $row->record = null;
1729807cf72SGreg Roach                }
1739807cf72SGreg Roach
1740587a5b3SGreg Roach                return $row;
1750587a5b3SGreg Roach            })
1760587a5b3SGreg Roach            ->all();
1778c2e8227SGreg Roach    }
178a4439c01SGreg Roach
179a4439c01SGreg Roach    /**
1806ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
181b6db7c1fSGreg Roach     * @param Tree                   $tree
182e5a6b4d4SGreg Roach     * @param UserInterface          $user
183a4439c01SGreg Roach     *
1846ccdf4f0SGreg Roach     * @return ResponseInterface
185a4439c01SGreg Roach     */
1866ccdf4f0SGreg Roach    public function postAddFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
187c1010edaSGreg Roach    {
188*eb235819SGreg Roach        $params = $request->getParsedBody();
189*eb235819SGreg Roach
190*eb235819SGreg Roach        $note  = $params['note'];
191*eb235819SGreg Roach        $title = $params['title'];
192*eb235819SGreg Roach        $url   = $params['url'];
193*eb235819SGreg Roach        $type  = $params['type'];
194*eb235819SGreg Roach        $xref  = $params[$type . '-xref'] ?? '';
195a4439c01SGreg Roach
1968ec20abdSGreg Roach        $record = $this->getRecordForType($type, $xref, $tree);
19725cd7ed9SGreg Roach
198a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
1998ec20abdSGreg Roach            if ($type === 'url' && $url !== '') {
200a4439c01SGreg Roach                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
2015c007fdfSGreg Roach            }
2025c007fdfSGreg Roach
2038ec20abdSGreg Roach            if ($record instanceof GedcomRecord && $record->canShow()) {
20425cd7ed9SGreg Roach                $this->addRecordFavorite($tree, $record, $note);
205a4439c01SGreg Roach            }
206a4439c01SGreg Roach        }
207a4439c01SGreg Roach
208aa6f03bbSGreg Roach        $url = route('tree-page', ['ged' => $tree->name()]);
209a4439c01SGreg Roach
2106ccdf4f0SGreg Roach        return redirect($url);
211a4439c01SGreg Roach    }
212a4439c01SGreg Roach
213a4439c01SGreg Roach    /**
2146ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
215b6db7c1fSGreg Roach     * @param Tree                   $tree
216e5a6b4d4SGreg Roach     * @param UserInterface          $user
217a4439c01SGreg Roach     *
2186ccdf4f0SGreg Roach     * @return ResponseInterface
219a4439c01SGreg Roach     */
2206ccdf4f0SGreg Roach    public function postDeleteFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
221c1010edaSGreg Roach    {
222*eb235819SGreg Roach        $favorite_id = $request->getQueryParams()['favorite_id'];
223a4439c01SGreg Roach
224a4439c01SGreg Roach        if (Auth::isManager($tree, $user)) {
2250587a5b3SGreg Roach            DB::table('favorite')
2260587a5b3SGreg Roach                ->where('favorite_id', '=', $favorite_id)
2270587a5b3SGreg Roach                ->whereNull('user_id')
2280587a5b3SGreg Roach                ->delete();
229a4439c01SGreg Roach        }
230a4439c01SGreg Roach
231aa6f03bbSGreg Roach        $url = route('tree-page', ['ged' => $tree->name()]);
232a4439c01SGreg Roach
2336ccdf4f0SGreg Roach        return redirect($url);
234a4439c01SGreg Roach    }
235a4439c01SGreg Roach
236a4439c01SGreg Roach    /**
237a4439c01SGreg Roach     * @param Tree   $tree
238a4439c01SGreg Roach     * @param string $url
239a4439c01SGreg Roach     * @param string $title
240a4439c01SGreg Roach     * @param string $note
24118d7a90dSGreg Roach     *
24218d7a90dSGreg Roach     * @return void
243a4439c01SGreg Roach     */
244e364afe4SGreg Roach    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note): void
245c1010edaSGreg Roach    {
2460587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
24772cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2480587a5b3SGreg Roach            'user_id'   => null,
249a4439c01SGreg Roach            'url'       => $url,
2500587a5b3SGreg Roach        ], [
2510587a5b3SGreg Roach            'favorite_type' => 'URL',
252a4439c01SGreg Roach            'note'          => $note,
253a4439c01SGreg Roach            'title'         => $title,
254a4439c01SGreg Roach        ]);
255a4439c01SGreg Roach    }
256a4439c01SGreg Roach
257a4439c01SGreg Roach    /**
258a4439c01SGreg Roach     * @param Tree         $tree
25925cd7ed9SGreg Roach     * @param GedcomRecord $record
260a4439c01SGreg Roach     * @param string       $note
26118d7a90dSGreg Roach     *
26218d7a90dSGreg Roach     * @return void
263a4439c01SGreg Roach     */
264e364afe4SGreg Roach    private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note): void
265c1010edaSGreg Roach    {
2660587a5b3SGreg Roach        DB::table('favorite')->updateOrInsert([
26772cf66d4SGreg Roach            'gedcom_id' => $tree->id(),
2680587a5b3SGreg Roach            'user_id'   => null,
26925cd7ed9SGreg Roach            'xref'      => $record->xref(),
2700587a5b3SGreg Roach        ], [
27125cd7ed9SGreg Roach            'favorite_type' => $record::RECORD_TYPE,
272a4439c01SGreg Roach            'note'          => $note,
273a4439c01SGreg Roach        ]);
274a4439c01SGreg Roach    }
2758ec20abdSGreg Roach
2768ec20abdSGreg Roach    /**
2778ec20abdSGreg Roach     * @param string $type
2788ec20abdSGreg Roach     * @param string $xref
2798ec20abdSGreg Roach     * @param Tree   $tree
2808ec20abdSGreg Roach     *
2818ec20abdSGreg Roach     * @return GedcomRecord|null
2828ec20abdSGreg Roach     */
2838ec20abdSGreg Roach    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
2848ec20abdSGreg Roach    {
2858ec20abdSGreg Roach        switch ($type) {
2868ec20abdSGreg Roach            case 'indi':
2878ec20abdSGreg Roach                return Individual::getInstance($xref, $tree);
2888ec20abdSGreg Roach
2898ec20abdSGreg Roach            case 'fam':
2908ec20abdSGreg Roach                return Family::getInstance($xref, $tree);
2918ec20abdSGreg Roach
2928ec20abdSGreg Roach            case 'sour':
2938ec20abdSGreg Roach                return Source::getInstance($xref, $tree);
2948ec20abdSGreg Roach
2958ec20abdSGreg Roach            case 'repo':
2968ec20abdSGreg Roach                return Repository::getInstance($xref, $tree);
2978ec20abdSGreg Roach
2988ec20abdSGreg Roach            case 'obje':
2998ec20abdSGreg Roach                return Media::getInstance($xref, $tree);
3008ec20abdSGreg Roach
3018ec20abdSGreg Roach            default:
3028ec20abdSGreg Roach                return null;
3038ec20abdSGreg Roach        }
3048ec20abdSGreg Roach    }
3058c2e8227SGreg Roach}
306