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