xref: /webtrees/app/Module/UserFavoritesModule.php (revision 8121b9bec19818120092699199161a1357bb8f3f)
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\GedcomRecord;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Tree;
24use Fisharebest\Webtrees\User;
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 labelled on tabs, menus, 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     *
93     * Simple blocks are faster in-line, more comples ones
94     * can be loaded later.
95     *
96     * @return bool
97     */
98    public function loadAjax(): bool
99    {
100        return false;
101    }
102
103    /**
104     * Can this block be shown on the user’s home page?
105     *
106     * @return bool
107     */
108    public function isUserBlock(): bool
109    {
110        return true;
111    }
112
113    /**
114     * Can this block be shown on the tree’s home page?
115     *
116     * @return bool
117     */
118    public function isTreeBlock(): bool
119    {
120        return false;
121    }
122
123    /**
124     * Update the configuration for a block.
125     *
126     * @param Request $request
127     * @param int     $block_id
128     *
129     * @return void
130     */
131    public function saveBlockConfiguration(Request $request, int $block_id)
132    {
133    }
134
135    /**
136     * An HTML form to edit block settings
137     *
138     * @param Tree $tree
139     * @param int  $block_id
140     *
141     * @return void
142     */
143    public function editBlockConfiguration(Tree $tree, int $block_id)
144    {
145    }
146
147    /**
148     * Get the favorites for a user
149     *
150     * @param Tree $tree
151     * @param User $user
152     *
153     * @return stdClass[]
154     */
155    public function getFavorites(Tree $tree, User $user): array
156    {
157        return DB::table('favorite')
158            ->where('gedcom_id', '=', $tree->id())
159            ->where('user_id', '=', $user->id())
160            ->get()
161            ->map(function (stdClass $row) use ($tree): stdClass {
162                if ($row->xref !== null) {
163                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
164                } else {
165                    $row->record = null;
166                }
167
168                return $row;
169            })
170            ->all();
171    }
172
173    /**
174     * @param Request $request
175     * @param Tree    $tree
176     * @param User    $user
177     *
178     * @return RedirectResponse
179     */
180    public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
181    {
182        $note         = $request->get('note', '');
183        $title        = $request->get('title', '');
184        $url          = $request->get('url', '');
185        $xref         = $request->get('xref', '');
186        $fav_category = $request->get('fav_category', '');
187
188        $record = GedcomRecord::getInstance($xref, $tree);
189
190        if (Auth::check()) {
191            if ($fav_category === 'url' && $url !== '') {
192                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
193            }
194
195            if ($fav_category === 'record' && $record instanceof GedcomRecord && $record->canShow()) {
196                $this->addRecordFavorite($tree, $user, $record, $note);
197            }
198        }
199
200        $url = route('user-page', ['ged' => $tree->name()]);
201
202        return new RedirectResponse($url);
203    }
204
205    /**
206     * @param Request $request
207     * @param Tree    $tree
208     * @param User    $user
209     *
210     * @return RedirectResponse
211     */
212    public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse
213    {
214        $favorite_id = (int) $request->get('favorite_id');
215
216        DB::table('favorite')
217            ->where('favorite_id', '=', $favorite_id)
218            ->where('user_id', '=', $user->id())
219            ->delete();
220
221        $url = route('user-page', ['ged' => $tree->name()]);
222
223        return new RedirectResponse($url);
224    }
225
226    /**
227     * @param Tree   $tree
228     * @param User   $user
229     * @param string $url
230     * @param string $title
231     * @param string $note
232     *
233     * @return void
234     */
235    private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note)
236    {
237        DB::table('favorite')->updateOrInsert([
238            'gedcom_id' => $tree->id(),
239            'user_id'   => $user->id(),
240            'url'       => $url,
241        ], [
242            'favorite_type' => 'URL',
243            'note'          => $note,
244            'title'         => $title,
245        ]);
246    }
247
248    /**
249     * @param Tree         $tree
250     * @param User         $user
251     * @param GedcomRecord $record
252     * @param string       $note
253     *
254     * @return void
255     */
256    private function addRecordFavorite(Tree $tree, User $user, GedcomRecord $record, string $note)
257    {
258        DB::table('favorite')->updateOrInsert([
259            'gedcom_id' => $tree->id(),
260            'user_id'   => $user->id(),
261            'xref'      => $record->xref(),
262        ], [
263            'favorite_type' => $record::RECORD_TYPE,
264            'note'          => $note,
265        ]);
266    }
267}
268