xref: /webtrees/app/Module/StoriesModule.php (revision e24444eeefe1caed93aa11866313e0407b4ce028)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Database;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Menu;
25use Fisharebest\Webtrees\Tree;
26use stdClass;
27use Symfony\Component\HttpFoundation\RedirectResponse;
28use Symfony\Component\HttpFoundation\Request;
29use Symfony\Component\HttpFoundation\Response;
30
31/**
32 * Class StoriesModule
33 */
34class StoriesModule extends AbstractModule implements ModuleTabInterface, ModuleConfigInterface, ModuleMenuInterface
35{
36    /** {@inheritdoc} */
37    public function getTitle(): string
38    {
39        /* I18N: Name of a module */
40        return I18N::translate('Stories');
41    }
42
43    /** {@inheritdoc} */
44    public function getDescription(): string
45    {
46        /* I18N: Description of the “Stories” module */
47        return I18N::translate('Add narrative stories to individuals in the family tree.');
48    }
49
50    /**
51     * The URL to a page where the user can modify the configuration of this module.
52     *
53     * @return string
54     */
55    public function getConfigLink(): string
56    {
57        return route('module', [
58            'module' => $this->getName(),
59            'action' => 'Admin',
60        ]);
61    }
62
63    /** {@inheritdoc} */
64    public function defaultTabOrder(): int
65    {
66        return 55;
67    }
68
69    /** {@inheritdoc} */
70    public function getTabContent(Individual $individual): string
71    {
72        return view('modules/stories/tab', [
73            'is_admin'   => Auth::isAdmin(),
74            'individual' => $individual,
75            'stories'    => $this->getStoriesForIndividual($individual),
76        ]);
77    }
78
79    /** {@inheritdoc} */
80    public function hasTabContent(Individual $individual): bool
81    {
82        return Auth::isManager($individual->getTree()) || !empty($this->getStoriesForIndividual($individual));
83    }
84
85    /** {@inheritdoc} */
86    public function isGrayedOut(Individual $individual): bool
87    {
88        return !empty($this->getStoriesForIndividual($individual));
89    }
90
91    /** {@inheritdoc} */
92    public function canLoadAjax(): bool
93    {
94        return false;
95    }
96
97    /**
98     * @param Individual $individual
99     *
100     * @return stdClass[]
101     */
102    private function getStoriesForIndividual(Individual $individual): array
103    {
104        $block_ids =
105            Database::prepare(
106                "SELECT block_id" .
107                " FROM `##block`" .
108                " WHERE module_name = :module_name" .
109                " AND xref          = :xref" .
110                " AND gedcom_id     = :tree_id"
111            )->execute([
112                'module_name' => $this->getName(),
113                'xref'        => $individual->getXref(),
114                'tree_id'     => $individual->getTree()->getTreeId(),
115            ])->fetchOneColumn();
116
117        $stories = [];
118        foreach ($block_ids as $block_id) {
119            $block_id = (int) $block_id;
120
121            // Only show this block for certain languages
122            $languages = $this->getBlockSetting($block_id, 'languages', '');
123            if ($languages === '' || in_array(WT_LOCALE, explode(',', $languages))) {
124                $stories[] = (object) [
125                    'block_id'   => $block_id,
126                    'title'      => $this->getBlockSetting($block_id, 'title'),
127                    'story_body' => $this->getBlockSetting($block_id, 'story_body'),
128                ];
129            }
130        }
131
132        return $stories;
133    }
134
135    /**
136     * The user can re-order menus. Until they do, they are shown in this order.
137     *
138     * @return int
139     */
140    public function defaultMenuOrder(): int
141    {
142        return 30;
143    }
144
145    /**
146     * What is the default access level for this module?
147     *
148     * Some modules are aimed at admins or managers, and are not generally shown to users.
149     *
150     * @return int
151     */
152    public function defaultAccessLevel(): int
153    {
154        return Auth::PRIV_HIDE;
155    }
156
157    /**
158     * A menu, to be added to the main application menu.
159     *
160     * @param Tree $tree
161     *
162     * @return Menu|null
163     */
164    public function getMenu(Tree $tree)
165    {
166        $menu = new Menu($this->getTitle(), route('module', [
167            'module' => $this->getName(),
168            'action' => 'ShowList',
169            'ged'    => $tree->getName(),
170        ]), 'menu-story');
171
172        return $menu;
173    }
174
175    /**
176     * @param Tree $tree
177     *
178     * @return Response
179     */
180    public function getAdminAction(Tree $tree): Response
181    {
182        $this->layout = 'layouts/administration';
183
184        $stories = Database::prepare(
185            "SELECT block_id, xref, gedcom_id" .
186            " FROM `##block` b" .
187            " WHERE module_name = :module_name" .
188            " AND gedcom_id = :tree_id" .
189            " ORDER BY gedcom_id, xref"
190        )->execute([
191            'tree_id'     => $tree->getTreeId(),
192            'module_name' => $this->getName(),
193        ])->fetchAll();
194
195        foreach ($stories as $story) {
196            $story->individual = Individual::getInstance($story->xref, $tree);
197            $story->title      = $this->getBlockSetting($story->block_id, 'title');
198            $story->languages  = $this->getBlockSetting($story->block_id, 'languages');
199        }
200
201        return $this->viewResponse('modules/stories/config', [
202            'stories'    => $stories,
203            'title'      => $this->getTitle() . ' — ' . $tree->getTitle(),
204            'tree'       => $tree,
205            'tree_names' => Tree::getNameList(),
206        ]);
207    }
208
209    /**
210     * @param Request $request
211     * @param Tree    $tree
212     *
213     * @return Response
214     */
215    public function getAdminEditAction(Request $request, Tree $tree): Response
216    {
217        $this->layout = 'layouts/administration';
218
219        $block_id = (int) $request->get('block_id');
220
221        if ($block_id === 0) {
222            // Creating a new story
223            $individual  = Individual::getInstance($request->get('xref', ''), $tree);
224            $story_title = '';
225            $story_body  = '';
226            $languages   = [];
227
228            $title = I18N::translate('Add a story') . ' — ' . e($tree->getTitle());
229        } else {
230            // Editing an existing story
231            $xref = Database::prepare(
232                "SELECT xref FROM `##block` WHERE block_id = :block_id"
233            )->execute([
234                'block_id' => $block_id,
235            ])->fetchOne();
236
237            $individual  = Individual::getInstance($xref, $tree);
238            $story_title = $this->getBlockSetting($block_id, 'title', '');
239            $story_body  = $this->getBlockSetting($block_id, 'story_body', '');
240            $languages   = explode(',', $this->getBlockSetting($block_id, 'languages'));
241
242            $title = I18N::translate('Edit the story') . ' — ' . e($tree->getTitle());
243        }
244
245        return $this->viewResponse('modules/stories/edit', [
246            'block_id'    => $block_id,
247            'languages'   => $languages,
248            'story_body'  => $story_body,
249            'story_title' => $story_title,
250            'title'       => $title,
251            'tree'        => $tree,
252            'individual'  => $individual,
253        ]);
254    }
255
256    /**
257     * @param Request $request
258     * @param Tree    $tree
259     *
260     * @return RedirectResponse
261     */
262    public function postAdminEditAction(Request $request, Tree $tree): RedirectResponse
263    {
264        $block_id    = (int) $request->get('block_id');
265        $xref        = $request->get('xref', '');
266        $story_body  = $request->get('story_body', '');
267        $story_title = $request->get('story_title', '');
268        $languages   = $request->get('languages', []);
269
270        if ($block_id !== 0) {
271            Database::prepare(
272                "UPDATE `##block` SET gedcom_id = :tree_id, xref = :xref WHERE block_id = :block_id"
273            )->execute([
274                'tree_id'  => $tree->getTreeId(),
275                'xref'     => $xref,
276                'block_id' => $block_id,
277            ]);
278        } else {
279            Database::prepare(
280                "INSERT INTO `##block` (gedcom_id, xref, module_name, block_order) VALUES (:tree_id, :xref, 'stories', 0)"
281            )->execute([
282                'tree_id' => $tree->getTreeId(),
283                'xref'    => $xref,
284            ]);
285
286            $block_id = Database::lastInsertId();
287        }
288
289        $this->setBlockSetting($block_id, 'story_body', $story_body);
290        $this->setBlockSetting($block_id, 'title', $story_title);
291        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
292
293        $url = route('module', [
294            'module' => 'stories',
295            'action' => 'Admin',
296            'ged'    => $tree->getName(),
297        ]);
298
299        return new RedirectResponse($url);
300    }
301
302    /**
303     * @param Request $request
304     * @param Tree    $tree
305     *
306     * @return Response
307     */
308    public function postAdminDeleteAction(Request $request, Tree $tree): Response
309    {
310        $block_id = (int) $request->get('block_id');
311
312        Database::prepare(
313            "DELETE FROM `##block_setting` WHERE block_id = :block_id"
314        )->execute([
315            'block_id' => $block_id,
316        ]);
317
318        Database::prepare(
319            "DELETE FROM `##block` WHERE block_id = :block_id"
320        )->execute([
321            'block_id' => $block_id,
322        ]);
323
324        $url = route('module', [
325            'module' => 'stories',
326            'action' => 'Admin',
327            'ged'    => $tree->getName(),
328        ]);
329
330        return new RedirectResponse($url);
331    }
332
333    /**
334     * @param Tree $tree
335     *
336     * @return Response
337     */
338    public function getShowListAction(Tree $tree): Response
339    {
340        $stories = Database::prepare(
341            "SELECT block_id, xref" .
342            " FROM `##block` b" .
343            " WHERE module_name = :module_name" .
344            " AND gedcom_id = :tree_id" .
345            " ORDER BY xref"
346        )->execute([
347            'module_name' => $this->getName(),
348            'tree_id'     => $tree->getTreeId(),
349        ])->fetchAll();
350
351        foreach ($stories as $story) {
352            $story->individual = Individual::getInstance($story->xref, $tree);
353            $story->title      = $this->getBlockSetting($story->block_id, 'title');
354            $story->languages  = $this->getBlockSetting($story->block_id, 'languages');
355        }
356
357        // Filter non-existant and private individuals.
358        $stories = array_filter($stories, function (stdClass $story): bool {
359            return $story->individual !== null && $story->individual->canShow();
360        });
361
362        // Filter foreign languages.
363        $stories = array_filter($stories, function (stdClass $story): bool {
364            return $story->languages === '' || in_array(WT_LOCALE, explode(',', $story->languages));
365        });
366
367        return $this->viewResponse('modules/stories/list', [
368            'stories' => $stories,
369            'title'   => $this->getTitle(),
370        ]);
371    }
372}
373