xref: /webtrees/app/Module/UserFavoritesModule.php (revision 9a3accd78795046143d5b995599032fcff82a467)
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\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26use stdClass;
27use Symfony\Component\HttpFoundation\RedirectResponse;
28use Symfony\Component\HttpFoundation\Request;
29
30/**
31 * Class UserFavoritesModule
32 */
33class UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
34{
35    use ModuleBlockTrait;
36
37    /**
38     * How should this module be identified in the control panel, etc.?
39     *
40     * @return string
41     */
42    public function title(): string
43    {
44        /* I18N: Name of a module */
45        return I18N::translate('Favorites');
46    }
47
48    /**
49     * A sentence describing what this module does.
50     *
51     * @return string
52     */
53    public function description(): string
54    {
55        /* I18N: Description of the “Favorites” module */
56        return I18N::translate('Display and manage a user’s favorite pages.');
57    }
58
59    /**
60     * Generate the HTML content of this block.
61     *
62     * @param Tree     $tree
63     * @param int      $block_id
64     * @param string   $ctype
65     * @param string[] $cfg
66     *
67     * @return string
68     */
69    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
70    {
71        $content = view('modules/user_favorites/favorites', [
72            'block_id'  => $block_id,
73            'favorites' => $this->getFavorites($tree, Auth::user()),
74            'tree'      => $tree,
75        ]);
76
77        if ($ctype !== '') {
78            return view('modules/block-template', [
79                'block'      => str_replace('_', '-', $this->name()),
80                'id'         => $block_id,
81                'config_url' => '',
82                'title'      => $this->title(),
83                'content'    => $content,
84            ]);
85        }
86
87        return $content;
88    }
89
90    /**
91     * Should this block load asynchronously using AJAX?
92     * Simple blocks are faster in-line, more comples ones
93     * can be loaded later.
94     *
95     * @return bool
96     */
97    public function loadAjax(): bool
98    {
99        return false;
100    }
101
102    /**
103     * Can this block be shown on the user’s home page?
104     *
105     * @return bool
106     */
107    public function isUserBlock(): bool
108    {
109        return true;
110    }
111
112    /**
113     * Can this block be shown on the tree’s home page?
114     *
115     * @return bool
116     */
117    public function isTreeBlock(): bool
118    {
119        return false;
120    }
121
122    /**
123     * Update the configuration for a block.
124     *
125     * @param Request $request
126     * @param int     $block_id
127     *
128     * @return void
129     */
130    public function saveBlockConfiguration(Request $request, int $block_id): void
131    {
132    }
133
134    /**
135     * An HTML form to edit block settings
136     *
137     * @param Tree $tree
138     * @param int  $block_id
139     *
140     * @return void
141     */
142    public function editBlockConfiguration(Tree $tree, int $block_id): void
143    {
144    }
145
146    /**
147     * Get the favorites for a user
148     *
149     * @param Tree          $tree
150     * @param UserInterface $user
151     *
152     * @return stdClass[]
153     */
154    public function getFavorites(Tree $tree, UserInterface $user): array
155    {
156        return DB::table('favorite')
157            ->where('gedcom_id', '=', $tree->id())
158            ->where('user_id', '=', $user->id())
159            ->get()
160            ->map(function (stdClass $row) use ($tree): stdClass {
161                if ($row->xref !== null) {
162                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
163                } else {
164                    $row->record = null;
165                }
166
167                return $row;
168            })
169            ->all();
170    }
171
172    /**
173     * @param Request       $request
174     * @param Tree          $tree
175     * @param UserInterface $user
176     *
177     * @return RedirectResponse
178     */
179    public function postAddFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse
180    {
181        $note         = $request->get('note', '');
182        $title        = $request->get('title', '');
183        $url          = $request->get('url', '');
184        $xref         = $request->get('xref', '');
185        $fav_category = $request->get('fav_category', '');
186
187        $record = GedcomRecord::getInstance($xref, $tree);
188
189        if (Auth::check()) {
190            if ($fav_category === 'url' && $url !== '') {
191                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
192            }
193
194            if ($fav_category === 'record' && $record instanceof GedcomRecord && $record->canShow()) {
195                $this->addRecordFavorite($tree, $user, $record, $note);
196            }
197        }
198
199        $url = route('user-page', ['ged' => $tree->name()]);
200
201        return new RedirectResponse($url);
202    }
203
204    /**
205     * @param Request       $request
206     * @param Tree          $tree
207     * @param UserInterface $user
208     *
209     * @return RedirectResponse
210     */
211    public function postDeleteFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse
212    {
213        $favorite_id = (int) $request->get('favorite_id');
214
215        DB::table('favorite')
216            ->where('favorite_id', '=', $favorite_id)
217            ->where('user_id', '=', $user->id())
218            ->delete();
219
220        $url = route('user-page', ['ged' => $tree->name()]);
221
222        return new RedirectResponse($url);
223    }
224
225    /**
226     * @param Tree          $tree
227     * @param UserInterface $user
228     * @param string        $url
229     * @param string        $title
230     * @param string        $note
231     *
232     * @return void
233     */
234    private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void
235    {
236        DB::table('favorite')->updateOrInsert([
237            'gedcom_id' => $tree->id(),
238            'user_id'   => $user->id(),
239            'url'       => $url,
240        ], [
241            'favorite_type' => 'URL',
242            'note'          => $note,
243            'title'         => $title,
244        ]);
245    }
246
247    /**
248     * @param Tree          $tree
249     * @param UserInterface $user
250     * @param GedcomRecord  $record
251     * @param string        $note
252     *
253     * @return void
254     */
255    private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void
256    {
257        DB::table('favorite')->updateOrInsert([
258            'gedcom_id' => $tree->id(),
259            'user_id'   => $user->id(),
260            'xref'      => $record->xref(),
261        ], [
262            'favorite_type' => $record::RECORD_TYPE,
263            'note'          => $note,
264        ]);
265    }
266}
267