xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision 2ef4478d349014c4aa9a01f8d0d3d0fd198f39eb)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Contracts\UserInterface;
22use Fisharebest\Webtrees\Family;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Media;
27use Fisharebest\Webtrees\Repository;
28use Fisharebest\Webtrees\Source;
29use Fisharebest\Webtrees\Tree;
30use Illuminate\Database\Capsule\Manager as DB;
31use Illuminate\Support\Str;
32use stdClass;
33use Symfony\Component\HttpFoundation\RedirectResponse;
34use Symfony\Component\HttpFoundation\Request;
35
36/**
37 * Class FamilyTreeFavoritesModule
38 */
39class FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface
40{
41    use ModuleBlockTrait;
42
43    /**
44     * How should this module be identified in the control panel, etc.?
45     *
46     * @return string
47     */
48    public function title(): string
49    {
50        /* I18N: Name of a module */
51        return I18N::translate('Favorites');
52    }
53
54    /**
55     * A sentence describing what this module does.
56     *
57     * @return string
58     */
59    public function description(): string
60    {
61        /* I18N: Description of the “Favorites” module */
62        return I18N::translate('Display and manage a family tree’s favorite pages.');
63    }
64
65    /**
66     * Generate the HTML content of this block.
67     *
68     * @param Tree     $tree
69     * @param int      $block_id
70     * @param string   $ctype
71     * @param string[] $cfg
72     *
73     * @return string
74     */
75    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
76    {
77        $content = view('modules/favorites/favorites', [
78            'block_id'    => $block_id,
79            'can_edit'    => Auth::isManager($tree),
80            'favorites'   => $this->getFavorites($tree),
81            'module_name' => $this->name(),
82            'tree'        => $tree,
83        ]);
84
85        if ($ctype !== '') {
86            return view('modules/block-template', [
87                'block'      => Str::kebab($this->name()),
88                'id'         => $block_id,
89                'config_url' => '',
90                'title'      => $this->title(),
91                'content'    => $content,
92            ]);
93        }
94
95        return $content;
96    }
97
98    /**
99     * Should this block load asynchronously using AJAX?
100     * Simple blocks are faster in-line, more comples ones
101     * can be loaded later.
102     *
103     * @return bool
104     */
105    public function loadAjax(): bool
106    {
107        return false;
108    }
109
110    /**
111     * Can this block be shown on the user’s home page?
112     *
113     * @return bool
114     */
115    public function isUserBlock(): bool
116    {
117        return false;
118    }
119
120    /**
121     * Can this block be shown on the tree’s home page?
122     *
123     * @return bool
124     */
125    public function isTreeBlock(): bool
126    {
127        return true;
128    }
129
130    /**
131     * Update the configuration for a block.
132     *
133     * @param Request $request
134     * @param int     $block_id
135     *
136     * @return void
137     */
138    public function saveBlockConfiguration(Request $request, int $block_id): void
139    {
140    }
141
142    /**
143     * An HTML form to edit block settings
144     *
145     * @param Tree $tree
146     * @param int  $block_id
147     *
148     * @return void
149     */
150    public function editBlockConfiguration(Tree $tree, int $block_id): void
151    {
152    }
153
154    /**
155     * Get the favorites for a family tree
156     *
157     * @param Tree $tree
158     *
159     * @return stdClass[]
160     */
161    public function getFavorites(Tree $tree): array
162    {
163        return DB::table('favorite')
164            ->where('gedcom_id', '=', $tree->id())
165            ->whereNull('user_id')
166            ->get()
167            ->map(function (stdClass $row) use ($tree): stdClass {
168                if ($row->xref !== null) {
169                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
170                } else {
171                    $row->record = null;
172                }
173
174                return $row;
175            })
176            ->all();
177    }
178
179    /**
180     * @param Request       $request
181     * @param Tree          $tree
182     * @param UserInterface $user
183     *
184     * @return RedirectResponse
185     */
186    public function postAddFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse
187    {
188        $note  = $request->get('note', '');
189        $title = $request->get('title', '');
190        $url   = $request->get('url', '');
191        $type  = $request->get('type', '');
192        $xref  = $request->get($type . '-xref', '');
193
194        $record = $this->getRecordForType($type, $xref, $tree);
195
196        if (Auth::isManager($tree, $user)) {
197            if ($type === 'url' && $url !== '') {
198                $this->addUrlFavorite($tree, $url, $title ?: $url, $note);
199            }
200
201            if ($record instanceof GedcomRecord && $record->canShow()) {
202                $this->addRecordFavorite($tree, $record, $note);
203            }
204        }
205
206        $url = route('tree-page', ['ged' => $tree->name()]);
207
208        return new RedirectResponse($url);
209    }
210
211    /**
212     * @param Request       $request
213     * @param Tree          $tree
214     * @param UserInterface $user
215     *
216     * @return RedirectResponse
217     */
218    public function postDeleteFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse
219    {
220        $favorite_id = (int) $request->get('favorite_id');
221
222        if (Auth::isManager($tree, $user)) {
223            DB::table('favorite')
224                ->where('favorite_id', '=', $favorite_id)
225                ->whereNull('user_id')
226                ->delete();
227        }
228
229        $url = route('tree-page', ['ged' => $tree->name()]);
230
231        return new RedirectResponse($url);
232    }
233
234    /**
235     * @param Tree   $tree
236     * @param string $url
237     * @param string $title
238     * @param string $note
239     *
240     * @return void
241     */
242    private function addUrlFavorite(Tree $tree, string $url, string $title, string $note): void
243    {
244        DB::table('favorite')->updateOrInsert([
245            'gedcom_id' => $tree->id(),
246            'user_id'   => null,
247            'url'       => $url,
248        ], [
249            'favorite_type' => 'URL',
250            'note'          => $note,
251            'title'         => $title,
252        ]);
253    }
254
255    /**
256     * @param Tree         $tree
257     * @param GedcomRecord $record
258     * @param string       $note
259     *
260     * @return void
261     */
262    private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note): void
263    {
264        DB::table('favorite')->updateOrInsert([
265            'gedcom_id' => $tree->id(),
266            'user_id'   => null,
267            'xref'      => $record->xref(),
268        ], [
269            'favorite_type' => $record::RECORD_TYPE,
270            'note'          => $note,
271        ]);
272    }
273
274    /**
275     * @param string $type
276     * @param string $xref
277     * @param Tree   $tree
278     *
279     * @return GedcomRecord|null
280     */
281    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
282    {
283        switch ($type) {
284            case 'indi':
285                return Individual::getInstance($xref, $tree);
286
287            case 'fam':
288                return Family::getInstance($xref, $tree);
289
290            case 'sour':
291                return Source::getInstance($xref, $tree);
292
293            case 'repo':
294                return Repository::getInstance($xref, $tree);
295
296            case 'obje':
297                return Media::getInstance($xref, $tree);
298
299            default:
300                return null;
301        }
302    }
303}
304