xref: /webtrees/app/Module/StoriesModule.php (revision 7ff867dcdca30dc068e32b6a2ce421b753e8df89)
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        ]);
116    }
117
118    /**
119     * @param Individual $individual
120     *
121     * @return stdClass[]
122     */
123    private function getStoriesForIndividual(Individual $individual): array
124    {
125        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
126        assert($locale instanceof LocaleInterface);
127
128        $block_ids = DB::table('block')
129            ->where('module_name', '=', $this->name())
130            ->where('xref', '=', $individual->xref())
131            ->where('gedcom_id', '=', $individual->tree()->id())
132            ->pluck('block_id');
133
134        $stories = [];
135        foreach ($block_ids as $block_id) {
136            $block_id = (int) $block_id;
137
138            // Only show this block for certain languages
139            $languages = $this->getBlockSetting($block_id, 'languages');
140            if ($languages === '' || in_array($locale->languageTag(), explode(',', $languages), true)) {
141                $stories[] = (object) [
142                    'block_id'   => $block_id,
143                    'title'      => $this->getBlockSetting($block_id, 'title'),
144                    'story_body' => $this->getBlockSetting($block_id, 'story_body'),
145                ];
146            }
147        }
148
149        return $stories;
150    }
151
152    /**
153     * Is this tab empty? If so, we don't always need to display it.
154     *
155     * @param Individual $individual
156     *
157     * @return bool
158     */
159    public function hasTabContent(Individual $individual): bool
160    {
161        return Auth::isManager($individual->tree()) || $this->getStoriesForIndividual($individual) !== [];
162    }
163
164    /**
165     * A greyed out tab has no actual content, but may perhaps have
166     * options to create content.
167     *
168     * @param Individual $individual
169     *
170     * @return bool
171     */
172    public function isGrayedOut(Individual $individual): bool
173    {
174        return $this->getStoriesForIndividual($individual) !== [];
175    }
176
177    /**
178     * Can this tab load asynchronously?
179     *
180     * @return bool
181     */
182    public function canLoadAjax(): bool
183    {
184        return false;
185    }
186
187    /**
188     * A menu, to be added to the main application menu.
189     *
190     * @param Tree $tree
191     *
192     * @return Menu|null
193     */
194    public function getMenu(Tree $tree): ?Menu
195    {
196        $menu = new Menu($this->title(), route('module', [
197            'module' => $this->name(),
198            'action' => 'ShowList',
199            'tree'    => $tree->name(),
200        ]), 'menu-story');
201
202        return $menu;
203    }
204
205    /**
206     * How should this module be identified in the control panel, etc.?
207     *
208     * @return string
209     */
210    public function title(): string
211    {
212        /* I18N: Name of a module */
213        return I18N::translate('Stories');
214    }
215
216    /**
217     * @param ServerRequestInterface $request
218     *
219     * @return ResponseInterface
220     */
221    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
222    {
223        $this->layout = 'layouts/administration';
224
225        // This module can't run without a tree
226        $tree = $request->getAttribute('tree');
227
228        if (!$tree instanceof Tree) {
229            $tree = $this->tree_service->all()->first();
230            if ($tree instanceof Tree) {
231                return redirect(route('module', ['module' => $this->name(), 'action' => 'Admin', 'tree' => $tree->name()]));
232            }
233
234            return redirect(route(ControlPanel::class));
235        }
236
237        $stories = DB::table('block')
238            ->where('module_name', '=', $this->name())
239            ->where('gedcom_id', '=', $tree->id())
240            ->orderBy('xref')
241            ->get();
242
243        foreach ($stories as $story) {
244            $block_id = (int) $story->block_id;
245
246            $story->individual = Individual::getInstance($story->xref, $tree);
247            $story->title      = $this->getBlockSetting($block_id, 'title');
248            $story->languages  = $this->getBlockSetting($block_id, 'languages');
249        }
250
251        $tree_names = $this->tree_service->all()->map(static function (Tree $tree): string {
252            return $tree->title();
253        });
254
255        return $this->viewResponse('modules/stories/config', [
256            'module'     => $this->name(),
257            'stories'    => $stories,
258            'title'      => $this->title() . ' — ' . $tree->title(),
259            'tree'       => $tree,
260            'tree_names' => $tree_names,
261        ]);
262    }
263
264    /**
265     * @param ServerRequestInterface $request
266     *
267     * @return ResponseInterface
268     */
269    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
270    {
271        return redirect(route('module', [
272            'module' => $this->name(),
273            'action' => 'Admin',
274            'tree'   => $request->getParsedBody()['tree'] ?? '',
275        ]));
276    }
277
278    /**
279     * @param ServerRequestInterface $request
280     *
281     * @return ResponseInterface
282     */
283    public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface
284    {
285        $this->layout = 'layouts/administration';
286
287        $tree     = $request->getAttribute('tree');
288        $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0);
289
290        if ($block_id === 0) {
291            // Creating a new story
292            $individual  = null;
293            $story_title = '';
294            $story_body  = '';
295            $languages   = [];
296
297            $title = I18N::translate('Add a story') . ' — ' . e($tree->title());
298        } else {
299            // Editing an existing story
300            $xref = (string) DB::table('block')
301                ->where('block_id', '=', $block_id)
302                ->value('xref');
303
304            $individual  = Individual::getInstance($xref, $tree);
305            $story_title = $this->getBlockSetting($block_id, 'title');
306            $story_body  = $this->getBlockSetting($block_id, 'story_body');
307            $languages   = explode(',', $this->getBlockSetting($block_id, 'languages'));
308
309            $title = I18N::translate('Edit the story') . ' — ' . e($tree->title());
310        }
311
312        return $this->viewResponse('modules/stories/edit', [
313            'block_id'    => $block_id,
314            'languages'   => $languages,
315            'story_body'  => $story_body,
316            'story_title' => $story_title,
317            'title'       => $title,
318            'tree'        => $tree,
319            'individual'  => $individual,
320        ]);
321    }
322
323    /**
324     * @param ServerRequestInterface $request
325     *
326     * @return ResponseInterface
327     */
328    public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface
329    {
330        $tree     = $request->getAttribute('tree');
331        $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0);
332
333        $params = $request->getParsedBody();
334
335        $xref        = $params['xref'];
336        $story_body  = $params['story_body'];
337        $story_title = $params['story_title'];
338        $languages   = $params['languages'] ?? [];
339
340        $story_body  = $this->html_service->sanitize($story_body);
341        $story_title = $this->html_service->sanitize($story_title);
342
343        if ($block_id !== 0) {
344            DB::table('block')
345                ->where('block_id', '=', $block_id)
346                ->update([
347                    'gedcom_id' => $tree->id(),
348                    'xref'      => $xref,
349                ]);
350        } else {
351            DB::table('block')->insert([
352                'gedcom_id'   => $tree->id(),
353                'xref'        => $xref,
354                'module_name' => $this->name(),
355                'block_order' => 0,
356            ]);
357
358            $block_id = (int) DB::connection()->getPdo()->lastInsertId();
359        }
360
361        $this->setBlockSetting($block_id, 'story_body', $story_body);
362        $this->setBlockSetting($block_id, 'title', $story_title);
363        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
364
365        $url = route('module', [
366            'module' => $this->name(),
367            'action' => 'Admin',
368            'tree'    => $tree->name(),
369        ]);
370
371        return redirect($url);
372    }
373
374    /**
375     * @param ServerRequestInterface $request
376     *
377     * @return ResponseInterface
378     */
379    public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface
380    {
381        $tree     = $request->getAttribute('tree');
382        $block_id = $request->getQueryParams()['block_id'];
383
384        DB::table('block_setting')
385            ->where('block_id', '=', $block_id)
386            ->delete();
387
388        DB::table('block')
389            ->where('block_id', '=', $block_id)
390            ->delete();
391
392        $url = route('module', [
393            'module' => $this->name(),
394            'action' => 'Admin',
395            'tree'    => $tree->name(),
396        ]);
397
398        return redirect($url);
399    }
400
401    /**
402     * @param ServerRequestInterface $request
403     *
404     * @return ResponseInterface
405     */
406    public function getShowListAction(ServerRequestInterface $request): ResponseInterface
407    {
408        $tree = $request->getAttribute('tree');
409        assert($tree instanceof Tree);
410
411        $locale = $request->getAttribute('locale');
412        assert($locale instanceof LocaleInterface);
413
414        $stories = DB::table('block')
415            ->where('module_name', '=', $this->name())
416            ->where('gedcom_id', '=', $tree->id())
417            ->get()
418            ->map(function (stdClass $story) use ($tree): stdClass {
419                $block_id = (int) $story->block_id;
420
421                $story->individual = Individual::getInstance($story->xref, $tree);
422                $story->title      = $this->getBlockSetting($block_id, 'title');
423                $story->languages  = $this->getBlockSetting($block_id, 'languages');
424
425                return $story;
426            })->filter(static function (stdClass $story): bool {
427                // Filter non-existant and private individuals.
428                return $story->individual instanceof Individual && $story->individual->canShow();
429            })->filter(static function (stdClass $story) use ($locale): bool {
430                // Filter foreign languages.
431                return $story->languages === '' || in_array($locale->languageTag(), explode(',', $story->languages), true);
432            });
433
434        return $this->viewResponse('modules/stories/list', [
435            'stories' => $stories,
436            'title'   => $this->title(),
437        ]);
438    }
439}
440