Issues (2502)

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