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