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