xref: /webtrees/app/Module/StoriesModule.php (revision b63ebfff10c4f748c10374a14a84550d3b653912)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Menu;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\HtmlService;
29use Fisharebest\Webtrees\Services\TreeService;
30use Fisharebest\Webtrees\Tree;
31use Fisharebest\Webtrees\Validator;
32use Illuminate\Database\Capsule\Manager as DB;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35
36use function redirect;
37use function route;
38
39/**
40 * Class StoriesModule
41 */
42class StoriesModule extends AbstractModule implements ModuleConfigInterface, ModuleMenuInterface, ModuleTabInterface
43{
44    use ModuleTabTrait;
45    use ModuleConfigTrait;
46    use ModuleMenuTrait;
47
48    private HtmlService $html_service;
49
50    private TreeService $tree_service;
51
52    /**
53     * StoriesModule constructor.
54     *
55     * @param HtmlService $html_service
56     * @param TreeService $tree_service
57     */
58    public function __construct(HtmlService $html_service, TreeService $tree_service)
59    {
60        $this->html_service = $html_service;
61        $this->tree_service = $tree_service;
62    }
63
64    /** @var int The default access level for this module.  It can be changed in the control panel. */
65    protected int $access_level = Auth::PRIV_HIDE;
66
67    /**
68     * A sentence describing what this module does.
69     *
70     * @return string
71     */
72    public function description(): string
73    {
74        /* I18N: Description of the “Stories” module */
75        return I18N::translate('Add narrative stories to individuals in the family tree.');
76    }
77
78    /**
79     * The default position for this menu.  It can be changed in the control panel.
80     *
81     * @return int
82     */
83    public function defaultMenuOrder(): int
84    {
85        return 7;
86    }
87
88    /**
89     * The default position for this tab.  It can be changed in the control panel.
90     *
91     * @return int
92     */
93    public function defaultTabOrder(): int
94    {
95        return 9;
96    }
97
98    /**
99     * Generate the HTML content of this tab.
100     *
101     * @param Individual $individual
102     *
103     * @return string
104     */
105    public function getTabContent(Individual $individual): string
106    {
107        return view('modules/stories/tab', [
108            'is_admin'   => Auth::isAdmin(),
109            'individual' => $individual,
110            'stories'    => $this->getStoriesForIndividual($individual),
111            'tree'       => $individual->tree(),
112        ]);
113    }
114
115    /**
116     * @param Individual $individual
117     *
118     * @return array<object>
119     */
120    private function getStoriesForIndividual(Individual $individual): array
121    {
122        $block_ids = DB::table('block')
123            ->where('module_name', '=', $this->name())
124            ->where('xref', '=', $individual->xref())
125            ->where('gedcom_id', '=', $individual->tree()->id())
126            ->pluck('block_id');
127
128        $stories = [];
129        foreach ($block_ids as $block_id) {
130            $block_id = (int) $block_id;
131
132            // Only show this block for certain languages
133            $languages = $this->getBlockSetting($block_id, 'languages');
134            if ($languages === '' || in_array(I18N::languageTag(), explode(',', $languages), true)) {
135                $stories[] = (object) [
136                    'block_id'   => $block_id,
137                    'title'      => $this->getBlockSetting($block_id, 'title'),
138                    'story_body' => $this->getBlockSetting($block_id, 'story_body'),
139                ];
140            }
141        }
142
143        return $stories;
144    }
145
146    /**
147     * Is this tab empty? If so, we don't always need to display it.
148     *
149     * @param Individual $individual
150     *
151     * @return bool
152     */
153    public function hasTabContent(Individual $individual): bool
154    {
155        return Auth::isManager($individual->tree()) || $this->getStoriesForIndividual($individual) !== [];
156    }
157
158    /**
159     * A greyed out tab has no actual content, but may perhaps have
160     * options to create content.
161     *
162     * @param Individual $individual
163     *
164     * @return bool
165     */
166    public function isGrayedOut(Individual $individual): bool
167    {
168        return $this->getStoriesForIndividual($individual) === [];
169    }
170
171    /**
172     * Can this tab load asynchronously?
173     *
174     * @return bool
175     */
176    public function canLoadAjax(): bool
177    {
178        return false;
179    }
180
181    /**
182     * A menu, to be added to the main application menu.
183     *
184     * @param Tree $tree
185     *
186     * @return Menu|null
187     */
188    public function getMenu(Tree $tree): ?Menu
189    {
190        return new Menu($this->title(), route('module', [
191            'module' => $this->name(),
192            'action' => 'ShowList',
193            'tree'    => $tree->name(),
194        ]), 'menu-story');
195    }
196
197    /**
198     * How should this module be identified in the control panel, etc.?
199     *
200     * @return string
201     */
202    public function title(): string
203    {
204        /* I18N: Name of a module */
205        return I18N::translate('Stories');
206    }
207
208    /**
209     * @param ServerRequestInterface $request
210     *
211     * @return ResponseInterface
212     */
213    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
214    {
215        $this->layout = 'layouts/administration';
216
217        // This module can't run without a tree
218        $tree = Validator::attributes($request)->treeOptional();
219
220        if (!$tree instanceof Tree) {
221            $tree = $this->tree_service->all()->first();
222            if ($tree instanceof Tree) {
223                return redirect(route('module', ['module' => $this->name(), 'action' => 'Admin', 'tree' => $tree->name()]));
224            }
225
226            return redirect(route(ControlPanel::class));
227        }
228
229        $stories = DB::table('block')
230            ->where('module_name', '=', $this->name())
231            ->where('gedcom_id', '=', $tree->id())
232            ->orderBy('xref')
233            ->get();
234
235        foreach ($stories as $story) {
236            $block_id = (int) $story->block_id;
237            $xref     = (string) $story->xref;
238
239            $story->individual = Registry::individualFactory()->make($xref, $tree);
240            $story->title      = $this->getBlockSetting($block_id, 'title');
241            $story->languages  = $this->getBlockSetting($block_id, 'languages');
242        }
243
244        $tree_names = $this->tree_service->all()->map(static function (Tree $tree): string {
245            return $tree->title();
246        });
247
248        return $this->viewResponse('modules/stories/config', [
249            'module'     => $this->name(),
250            'stories'    => $stories,
251            'title'      => $this->title() . ' — ' . $tree->title(),
252            'tree'       => $tree,
253            'tree_names' => $tree_names,
254        ]);
255    }
256
257    /**
258     * @param ServerRequestInterface $request
259     *
260     * @return ResponseInterface
261     */
262    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
263    {
264        $params = (array) $request->getParsedBody();
265
266        return redirect(route('module', [
267            'module' => $this->name(),
268            'action' => 'Admin',
269            'tree'   => $params['tree'] ?? '',
270        ]));
271    }
272
273    /**
274     * @param ServerRequestInterface $request
275     *
276     * @return ResponseInterface
277     */
278    public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface
279    {
280        $this->layout = 'layouts/administration';
281
282        $tree     = Validator::attributes($request)->tree();
283        $block_id = Validator::queryParams($request)->integer('block_id', 0);
284        $url      = Validator::queryParams($request)->string('url', '');
285
286        if ($block_id === 0) {
287            // Creating a new story
288            $story_title = '';
289            $story_body  = '';
290            $languages   = [];
291            $xref        = $request->getQueryParams()['xref'] ?? '';
292            $title       = I18N::translate('Add a story') . ' — ' . e($tree->title());
293        } else {
294            // Editing an existing story
295            $xref = (string) DB::table('block')
296                ->where('block_id', '=', $block_id)
297                ->value('xref');
298
299            $story_title = $this->getBlockSetting($block_id, 'title');
300            $story_body  = $this->getBlockSetting($block_id, 'story_body');
301            $languages   = explode(',', $this->getBlockSetting($block_id, 'languages'));
302            $title       = I18N::translate('Edit the story') . ' — ' . e($tree->title());
303        }
304
305        $individual = Registry::individualFactory()->make($xref, $tree);
306
307        return $this->viewResponse('modules/stories/edit', [
308            'block_id'    => $block_id,
309            'languages'   => $languages,
310            'story_body'  => $story_body,
311            'story_title' => $story_title,
312            'title'       => $title,
313            'tree'        => $tree,
314            'url'         => $url,
315            'individual'  => $individual,
316        ]);
317    }
318
319    /**
320     * @param ServerRequestInterface $request
321     *
322     * @return ResponseInterface
323     */
324    public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface
325    {
326        $tree        = Validator::attributes($request)->tree();
327        $block_id    = Validator::queryParams($request)->integer('block_id', 0);
328        $xref        = Validator::parsedBody($request)->string('xref');
329        $story_body  = Validator::parsedBody($request)->string('story_body');
330        $story_title = Validator::parsedBody($request)->string('story_title');
331        $languages   = Validator::parsedBody($request)->array('languages');
332        $default_url = route('module', ['module' => $this->name(), 'action' => 'Admin', 'tree' => $tree->name()]);
333        $url         = Validator::parsedBody($request)->isLocalUrl()->string('url', $default_url);
334        $story_body  = $this->html_service->sanitize($story_body);
335
336        if ($block_id !== 0) {
337            DB::table('block')
338                ->where('block_id', '=', $block_id)
339                ->update([
340                    'gedcom_id' => $tree->id(),
341                    'xref'      => $xref,
342                ]);
343        } else {
344            DB::table('block')->insert([
345                'gedcom_id'   => $tree->id(),
346                'xref'        => $xref,
347                'module_name' => $this->name(),
348                'block_order' => 0,
349            ]);
350
351            $block_id = (int) DB::connection()->getPdo()->lastInsertId();
352        }
353
354        $this->setBlockSetting($block_id, 'story_body', $story_body);
355        $this->setBlockSetting($block_id, 'title', $story_title);
356        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
357
358        return redirect($url);
359    }
360
361    /**
362     * @param ServerRequestInterface $request
363     *
364     * @return ResponseInterface
365     */
366    public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface
367    {
368        $tree     = Validator::attributes($request)->tree();
369        $block_id = $request->getQueryParams()['block_id'];
370
371        DB::table('block_setting')
372            ->where('block_id', '=', $block_id)
373            ->delete();
374
375        DB::table('block')
376            ->where('block_id', '=', $block_id)
377            ->delete();
378
379        $url = route('module', [
380            'module' => $this->name(),
381            'action' => 'Admin',
382            'tree'    => $tree->name(),
383        ]);
384
385        return redirect($url);
386    }
387
388    /**
389     * @param ServerRequestInterface $request
390     *
391     * @return ResponseInterface
392     */
393    public function getShowListAction(ServerRequestInterface $request): ResponseInterface
394    {
395        $tree = Validator::attributes($request)->tree();
396
397        $stories = DB::table('block')
398            ->where('module_name', '=', $this->name())
399            ->where('gedcom_id', '=', $tree->id())
400            ->get()
401            ->map(function (object $story) use ($tree): object {
402                $block_id = (int) $story->block_id;
403                $xref     = (string) $story->xref;
404
405                $story->individual = Registry::individualFactory()->make($xref, $tree);
406                $story->title      = $this->getBlockSetting($block_id, 'title');
407                $story->languages  = $this->getBlockSetting($block_id, 'languages');
408
409                return $story;
410            })->filter(static function (object $story): bool {
411                // Filter non-existent and private individuals.
412                return $story->individual instanceof Individual && $story->individual->canShow();
413            })->filter(static function (object $story): bool {
414                // Filter foreign languages.
415                return $story->languages === '' || in_array(I18N::languageTag(), explode(',', $story->languages), true);
416            });
417
418        return $this->viewResponse('modules/stories/list', [
419            'stories' => $stories,
420            'title'   => $this->title(),
421            'tree'    => $tree,
422        ]);
423    }
424}
425