Issues (2559)

app/Module/UserFavoritesModule.php (7 issues)

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Auth;
0 ignored issues
show
The type Fisharebest\Webtrees\Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Contracts\UserInterface;
0 ignored issues
show
The type Fisharebest\Webtrees\Contracts\UserInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Fisharebest\Webtrees\DB;
0 ignored issues
show
The type Fisharebest\Webtrees\DB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Fisharebest\Webtrees\GedcomRecord;
0 ignored issues
show
The type Fisharebest\Webtrees\GedcomRecord was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
27
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Fisharebest\Webtrees\Registry;
29
use Fisharebest\Webtrees\Tree;
30
use Fisharebest\Webtrees\Validator;
31
use Illuminate\Support\Str;
32
use Psr\Http\Message\ResponseInterface;
33
use Psr\Http\Message\ServerRequestInterface;
34
35
class UserFavoritesModule extends AbstractModule implements ModuleBlockInterface
0 ignored issues
show
The type Fisharebest\Webtrees\Module\ModuleBlockInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
{
37
    use ModuleBlockTrait;
38
39
    public function title(): string
40
    {
41
        /* I18N: Name of a module */
42
        return I18N::translate('Favorites');
43
    }
44
45
    public function description(): string
46
    {
47
        /* I18N: Description of the “Favorites” module */
48
        return I18N::translate('Display and manage a user’s favorite pages.');
49
    }
50
51
    /**
52
     * Generate the HTML content of this block.
53
     *
54
     * @param Tree                 $tree
55
     * @param int                  $block_id
56
     * @param string               $context
57
     * @param array<string,string> $config
58
     *
59
     * @return string
60
     */
61
    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
62
    {
63
        $content = view('modules/favorites/favorites', [
64
            'block_id'    => $block_id,
65
            'can_edit'    => true,
66
            'favorites'   => $this->getFavorites($tree, Auth::user()),
67
            'module_name' => $this->name(),
68
            'tree'        => $tree,
69
        ]);
70
71
        if ($context !== self::CONTEXT_EMBED) {
72
            return view('modules/block-template', [
73
                'block'      => Str::kebab($this->name()),
74
                'id'         => $block_id,
75
                'config_url' => '',
76
                'title'      => $this->title(),
77
                'content'    => $content,
78
            ]);
79
        }
80
81
        return $content;
82
    }
83
84
    /**
85
     * Should this block load asynchronously using AJAX?
86
     * Simple blocks are faster in-line, more complex ones can be loaded later.
87
     *
88
     * @return bool
89
     */
90
    public function loadAjax(): bool
91
    {
92
        return false;
93
    }
94
95
    /**
96
     * Can this block be shown on the user’s home page?
97
     *
98
     * @return bool
99
     */
100
    public function isUserBlock(): bool
101
    {
102
        return true;
103
    }
104
105
    /**
106
     * Can this block be shown on the tree’s home page?
107
     *
108
     * @return bool
109
     */
110
    public function isTreeBlock(): bool
111
    {
112
        return false;
113
    }
114
115
    /**
116
     * Get the favorites for a user
117
     *
118
     * @param Tree          $tree
119
     * @param UserInterface $user
120
     *
121
     * @return array<int,object{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,object{ at position 4 could not be parsed: Expected '>' at position 4, but found 'object'.
Loading history...
122
     *      favorite_id:string,
123
     *      favorite_type:string,
124
     *      url:string|null,
125
     *      note:string|null,
126
     *      title:string|null,
127
     *      record:GedcomRecord|null
128
     *  }>
129
     */
130
    public function getFavorites(Tree $tree, UserInterface $user): array
131
    {
132
        return DB::table('favorite')
133
            ->where('gedcom_id', '=', $tree->id())
134
            ->where('user_id', '=', $user->id())
135
            ->select(['favorite_id', 'xref', 'favorite_type', 'url', 'title', 'note'])
136
            ->get()
137
            ->map(static function (object $row) use ($tree): object {
138
                if ($row->xref !== null) {
139
                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
140
141
                    if ($row->record instanceof GedcomRecord && !$row->record->canShowName()) {
142
                        $row->record = null;
143
                        $row->note   = null;
144
                    }
145
                } else {
146
                    $row->record = null;
147
                }
148
149
                return $row;
150
            })
151
            ->all();
152
    }
153
154
    /**
155
     * @param ServerRequestInterface $request
156
     *
157
     * @return ResponseInterface
158
     */
159
    public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface
160
    {
161
        $tree   = Validator::attributes($request)->tree();
162
        $user   = Validator::attributes($request)->user();
163
        $note   = Validator::parsedBody($request)->string('note');
164
        $title  = Validator::parsedBody($request)->string('title');
165
        $url    = Validator::parsedBody($request)->string('url');
166
        $type   = Validator::parsedBody($request)->string('type');
167
        $xref   = Validator::parsedBody($request)->string($type . '-xref', '');
168
        $record = $this->getRecordForType($type, $xref, $tree);
169
170
        if (Auth::check()) {
171
            if ($type === 'url' && $url !== '') {
172
                $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note);
173
            }
174
175
            if ($record instanceof GedcomRecord && $record->canShow()) {
176
                $this->addRecordFavorite($tree, $user, $record, $note);
177
            }
178
        }
179
180
        $url = route(UserPage::class, ['tree' => $tree->name()]);
181
182
        return redirect($url);
183
    }
184
185
    /**
186
     * @param ServerRequestInterface $request
187
     *
188
     * @return ResponseInterface
189
     */
190
    public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface
191
    {
192
        $tree        = Validator::attributes($request)->tree();
193
        $user        = Validator::attributes($request)->user();
194
        $favorite_id = Validator::queryParams($request)->integer('favorite_id');
195
196
        if (Auth::check()) {
197
            DB::table('favorite')
198
                ->where('favorite_id', '=', $favorite_id)
199
                ->where('user_id', '=', $user->id())
200
                ->delete();
201
        }
202
203
        $url = route(UserPage::class, ['tree' => $tree->name()]);
204
205
        return redirect($url);
206
    }
207
208
    /**
209
     * @param Tree          $tree
210
     * @param UserInterface $user
211
     * @param string        $url
212
     * @param string        $title
213
     * @param string        $note
214
     *
215
     * @return void
216
     */
217
    private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void
218
    {
219
        DB::table('favorite')->updateOrInsert([
220
            'gedcom_id' => $tree->id(),
221
            'user_id'   => $user->id(),
222
            'url'       => $url,
223
        ], [
224
            'favorite_type' => 'URL',
225
            'note'          => $note,
226
            'title'         => $title,
227
        ]);
228
    }
229
230
    /**
231
     * @param Tree          $tree
232
     * @param UserInterface $user
233
     * @param GedcomRecord  $record
234
     * @param string        $note
235
     *
236
     * @return void
237
     */
238
    private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void
239
    {
240
        DB::table('favorite')->updateOrInsert([
241
            'gedcom_id' => $tree->id(),
242
            'user_id'   => $user->id(),
243
            'xref'      => $record->xref(),
244
        ], [
245
            'favorite_type' => $record->tag(),
246
            'note'          => $note,
247
        ]);
248
    }
249
250
    private function getRecordForType(string $type, string $xref, Tree $tree): GedcomRecord|null
251
    {
252
        switch ($type) {
253
            case 'indi':
254
                return Registry::individualFactory()->make($xref, $tree);
255
256
            case 'fam':
257
                return Registry::familyFactory()->make($xref, $tree);
258
259
            case 'sour':
260
                return Registry::sourceFactory()->make($xref, $tree);
261
262
            case 'repo':
263
                return Registry::repositoryFactory()->make($xref, $tree);
264
265
            case 'obje':
266
                return Registry::mediaFactory()->make($xref, $tree);
267
268
            default:
269
                return null;
270
        }
271
    }
272
}
273