xref: /webtrees/app/Module/UserFavoritesModule.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
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 Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use stdClass;
35
36/**
37 * Class UserFavoritesModule
38 */
39class UserFavoritesModule 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 user’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'    => true,
80            'favorites'   => $this->getFavorites($tree, Auth::user()),
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 true;
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 false;
128    }
129
130    /**
131     * Update the configuration for a block.
132     *
133     * @param ServerRequestInterface $request
134     * @param int     $block_id
135     *
136     * @return void
137     */
138    public function saveBlockConfiguration(ServerRequestInterface $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 user
156     *
157     * @param Tree          $tree
158     * @param UserInterface $user
159     *
160     * @return stdClass[]
161     */
162    public function getFavorites(Tree $tree, UserInterface $user): array
163    {
164        return DB::table('favorite')
165            ->where('gedcom_id', '=', $tree->id())
166            ->where('user_id', '=', $user->id())
167            ->get()
168            ->map(static function (stdClass $row) use ($tree): stdClass {
169                if ($row->xref !== null) {
170                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
171                } else {
172                    $row->record = null;
173                }
174
175                return $row;
176            })
177            ->all();
178    }
179
180    /**
181     * @param ServerRequestInterface $request
182     * @param Tree                   $tree
183     * @param UserInterface          $user
184     *
185     * @return ResponseInterface
186     */
187    public function postAddFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
188    {
189        $note  = $request->get('note', '');
190        $title = $request->get('title', '');
191        $url   = $request->get('url', '');
192        $type  = $request->get('type', '');
193        $xref  = $request->get($type . '-xref', '');
194
195        $record = $this->getRecordForType($type, $xref, $tree);
196
197        if (Auth::check()) {
198            if ($type === 'url' && $url !== '') {
199                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
200            }
201
202            if ($record instanceof GedcomRecord && $record->canShow()) {
203                $this->addRecordFavorite($tree, $user, $record, $note);
204            }
205        }
206
207        $url = route('user-page', ['ged' => $tree->name()]);
208
209        return redirect($url);
210    }
211
212    /**
213     * @param ServerRequestInterface $request
214     * @param Tree                   $tree
215     * @param UserInterface          $user
216     *
217     * @return ResponseInterface
218     */
219    public function postDeleteFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
220    {
221        $favorite_id = (int) $request->get('favorite_id');
222
223        if (Auth::check()) {
224            DB::table('favorite')
225                ->where('favorite_id', '=', $favorite_id)
226                ->where('user_id', '=', $user->id())
227                ->delete();
228        }
229
230        $url = route('user-page', ['ged' => $tree->name()]);
231
232        return redirect($url);
233    }
234
235    /**
236     * @param Tree          $tree
237     * @param UserInterface $user
238     * @param string        $url
239     * @param string        $title
240     * @param string        $note
241     *
242     * @return void
243     */
244    private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void
245    {
246        DB::table('favorite')->updateOrInsert([
247            'gedcom_id' => $tree->id(),
248            'user_id'   => $user->id(),
249            'url'       => $url,
250        ], [
251            'favorite_type' => 'URL',
252            'note'          => $note,
253            'title'         => $title,
254        ]);
255    }
256
257    /**
258     * @param Tree          $tree
259     * @param UserInterface $user
260     * @param GedcomRecord  $record
261     * @param string        $note
262     *
263     * @return void
264     */
265    private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void
266    {
267        DB::table('favorite')->updateOrInsert([
268            'gedcom_id' => $tree->id(),
269            'user_id'   => $user->id(),
270            'xref'      => $record->xref(),
271        ], [
272            'favorite_type' => $record::RECORD_TYPE,
273            'note'          => $note,
274        ]);
275    }
276
277    /**
278     * @param string $type
279     * @param string $xref
280     * @param Tree   $tree
281     *
282     * @return GedcomRecord|null
283     */
284    private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord
285    {
286        switch ($type) {
287            case 'indi':
288                return Individual::getInstance($xref, $tree);
289
290            case 'fam':
291                return Family::getInstance($xref, $tree);
292
293            case 'sour':
294                return Source::getInstance($xref, $tree);
295
296            case 'repo':
297                return Repository::getInstance($xref, $tree);
298
299            case 'obje':
300                return Media::getInstance($xref, $tree);
301
302            default:
303                return null;
304        }
305    }
306}
307