Issues (2559)

app/Module/UserJournalModule.php (5 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 DateTimeImmutable;
23
use DateTimeZone;
24
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...
25
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...
26
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...
27
use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
28
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
29
use Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
30
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...
31
use Fisharebest\Webtrees\Registry;
32
use Fisharebest\Webtrees\Services\HtmlService;
33
use Fisharebest\Webtrees\Tree;
34
use Fisharebest\Webtrees\Validator;
35
use Illuminate\Support\Str;
36
use Psr\Http\Message\ResponseInterface;
37
use Psr\Http\Message\ServerRequestInterface;
38
39
use function redirect;
40
41
class UserJournalModule 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...
42
{
43
    use ModuleBlockTrait;
44
45
    private HtmlService $html_service;
46
47
    public function __construct(HtmlService $html_service)
48
    {
49
        $this->html_service = $html_service;
50
    }
51
52
    public function description(): string
53
    {
54
        /* I18N: Description of the “Journal” module */
55
        return I18N::translate('A private area to record notes or keep a journal.');
56
    }
57
58
    /**
59
     * Generate the HTML content of this block.
60
     *
61
     * @param array<string,string> $config
62
     */
63
    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
64
    {
65
        $articles = DB::table('news')
66
            ->where('user_id', '=', Auth::id())
67
            ->orderByDesc('updated')
68
            ->get()
69
            ->map(static function (object $row): object {
70
                $row->updated = Registry::timestampFactory()->fromString($row->updated);
71
72
                return $row;
73
            });
74
75
        $content = view('modules/user_blog/list', [
76
            'articles' => $articles,
77
            'block_id' => $block_id,
78
            'limit'    => 5,
79
            'tree'     => $tree,
80
        ]);
81
82
        if ($context !== self::CONTEXT_EMBED) {
83
            return view('modules/block-template', [
84
                'block'      => Str::kebab($this->name()),
85
                'id'         => $block_id,
86
                'config_url' => '',
87
                'title'      => $this->title(),
88
                'content'    => $content,
89
            ]);
90
        }
91
92
        return $content;
93
    }
94
95
    public function title(): string
96
    {
97
        /* I18N: Name of a module */
98
        return I18N::translate('Journal');
99
    }
100
101
    public function loadAjax(): bool
102
    {
103
        return false;
104
    }
105
106
    public function isUserBlock(): bool
107
    {
108
        return true;
109
    }
110
111
    public function isTreeBlock(): bool
112
    {
113
        return false;
114
    }
115
116
    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
117
    {
118
        $tree = Validator::attributes($request)->tree();
119
120
        if (!Auth::check()) {
121
            throw new HttpAccessDeniedException();
122
        }
123
124
        $news_id = Validator::queryParams($request)->integer('news_id', 0);
125
126
        $timezone = new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC'));
127
        $utc      = new DateTimeZone('UTC');
128
129
        if ($news_id !== 0) {
130
            $row = DB::table('news')
131
                ->where('news_id', '=', $news_id)
132
                ->where('user_id', '=', Auth::id())
133
                ->first();
134
135
            // Record was deleted before we could read it?
136
            if ($row === null) {
137
                throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'news_id:' . $news_id));
138
            }
139
140
            $body    = $row->body;
141
            $subject = $row->subject;
142
            $updated = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $row->updated, $utc)
143
                ->setTimezone($timezone);
144
        } else {
145
            $body    = '';
146
            $subject = '';
147
            $updated = Registry::timestampFactory()->now(Auth::user());
148
        }
149
150
        return $this->viewResponse('modules/user_blog/edit', [
151
            'body'    => $body,
152
            'news_id' => $news_id,
153
            'subject' => $subject,
154
            'title'   => $this->title(),
155
            'tree'    => $tree,
156
            'updated' => $updated->format('Y-m-d H:i:s'),
157
        ]);
158
    }
159
160
    public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface
161
    {
162
        $tree = Validator::attributes($request)->tree();
163
164
        if (!Auth::check()) {
165
            throw new HttpAccessDeniedException();
166
        }
167
168
        $news_id = Validator::queryParams($request)->integer('news_id', 0);
169
        $subject = Validator::parsedBody($request)->string('subject');
170
        $body    = Validator::parsedBody($request)->string('body');
171
172
        $subject = $this->html_service->sanitize($subject);
173
        $body    = $this->html_service->sanitize($body);
174
175
        $use_current_timestamp = Validator::parsedBody($request)->boolean('use-current-timestamp', false);
176
177
        if ($use_current_timestamp) {
178
            $updated = Registry::timestampFactory()->now();
179
        } else {
180
            $timestamp = Validator::parsedBody($request)->string('timestamp');
181
            $timezone  = new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC'));
182
            $utc       = new DateTimeZone('UTC');
183
            $updated   = DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s', $timestamp, $timezone)
184
                ->setTimezone($utc);
185
        }
186
187
        if ($news_id !== 0) {
188
            DB::table('news')
189
                ->where('news_id', '=', $news_id)
190
                ->where('user_id', '=', Auth::id()) // Check this is our own page - validates news_id
191
                ->update([
192
                    'body'    => $body,
193
                    'subject' => $subject,
194
                    'updated' => $updated->format('Y-m-d H:i:s'),
195
                ]);
196
        } else {
197
            DB::table('news')->insert([
198
                'body'    => $body,
199
                'subject' => $subject,
200
                'user_id' => Auth::id(),
201
                'updated' => $updated->format('Y-m-d H:i:s'),
202
            ]);
203
        }
204
205
        $url = route(UserPage::class, ['tree' => $tree->name()]);
206
207
        return redirect($url);
208
    }
209
210
    public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface
211
    {
212
        $tree    = Validator::attributes($request)->tree();
213
        $news_id = Validator::queryParams($request)->integer('news_id');
214
215
        DB::table('news')
216
            ->where('news_id', '=', $news_id)
217
            ->where('user_id', '=', Auth::id())
218
            ->delete();
219
220
        $url = route(UserPage::class, ['tree' => $tree->name()]);
221
222
        return redirect($url);
223
    }
224
}
225