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