xref: /webtrees/app/Module/StoriesModule.php (revision ad7270802ba57a28d195fc58fcc55f26458a779a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Menu;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
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 ModuleConfigInterface, ModuleMenuInterface, ModuleTabInterface
35{
36    use ModuleTabTrait;
37    use ModuleConfigTrait;
38    use ModuleMenuTrait;
39
40    /** @var int The default access level for this module.  It can be changed in the control panel. */
41    protected $access_level = Auth::PRIV_HIDE;
42
43    /**
44     * How should this module be labelled on tabs, menus, etc.?
45     *
46     * @return string
47     */
48    public function title(): string
49    {
50        /* I18N: Name of a module */
51        return I18N::translate('Stories');
52    }
53
54    /**
55     * A sentence describing what this module does.
56     *
57     * @return string
58     */
59    public function description(): string
60    {
61        /* I18N: Description of the “Stories” module */
62        return I18N::translate('Add narrative stories to individuals in the family tree.');
63    }
64
65    /**
66     * The default position for this menu.  It can be changed in the control panel.
67     *
68     * @return int
69     */
70    public function defaultMenuOrder(): int
71    {
72        return 7;
73    }
74
75    /**
76     * The default position for this tab.  It can be changed in the control panel.
77     *
78     * @return int
79     */
80    public function defaultTabOrder(): int
81    {
82        return 7;
83    }
84
85    /** {@inheritdoc} */
86    public function getTabContent(Individual $individual): string
87    {
88        return view('modules/stories/tab', [
89            'is_admin'   => Auth::isAdmin(),
90            'individual' => $individual,
91            'stories'    => $this->getStoriesForIndividual($individual),
92        ]);
93    }
94
95    /** {@inheritdoc} */
96    public function hasTabContent(Individual $individual): bool
97    {
98        return Auth::isManager($individual->tree()) || !empty($this->getStoriesForIndividual($individual));
99    }
100
101    /** {@inheritdoc} */
102    public function isGrayedOut(Individual $individual): bool
103    {
104        return !empty($this->getStoriesForIndividual($individual));
105    }
106
107    /** {@inheritdoc} */
108    public function canLoadAjax(): bool
109    {
110        return false;
111    }
112
113    /**
114     * @param Individual $individual
115     *
116     * @return stdClass[]
117     */
118    private function getStoriesForIndividual(Individual $individual): array
119    {
120        $block_ids = DB::table('block')
121            ->where('module_name', '=', $this->name())
122            ->where('xref', '=', $individual->xref())
123            ->where('gedcom_id', '=', $individual->tree()->id())
124            ->pluck('block_id');
125
126        $stories = [];
127        foreach ($block_ids as $block_id) {
128            $block_id = (int) $block_id;
129
130            // Only show this block for certain languages
131            $languages = $this->getBlockSetting($block_id, 'languages', '');
132            if ($languages === '' || in_array(WT_LOCALE, explode(',', $languages))) {
133                $stories[] = (object) [
134                    'block_id'   => $block_id,
135                    'title'      => $this->getBlockSetting($block_id, 'title'),
136                    'story_body' => $this->getBlockSetting($block_id, 'story_body'),
137                ];
138            }
139        }
140
141        return $stories;
142    }
143
144    /**
145     * A menu, to be added to the main application menu.
146     *
147     * @param Tree $tree
148     *
149     * @return Menu|null
150     */
151    public function getMenu(Tree $tree): ?Menu
152    {
153        $menu = new Menu($this->title(), route('module', [
154            'module' => $this->name(),
155            'action' => 'ShowList',
156            'ged'    => $tree->name(),
157        ]), 'menu-story');
158
159        return $menu;
160    }
161
162    /**
163     * @param Tree $tree
164     *
165     * @return Response
166     */
167    public function getAdminAction(Tree $tree): Response
168    {
169        $this->layout = 'layouts/administration';
170
171        $stories = DB::table('block')
172            ->where('module_name', '=', $this->name())
173            ->where('gedcom_id', '=', $tree->id())
174            ->orderBy('xref')
175            ->get();
176
177        foreach ($stories as $story) {
178            $block_id = (int) $story->block_id;
179
180            $story->individual = Individual::getInstance($story->xref, $tree);
181            $story->title      = $this->getBlockSetting($block_id, 'title');
182            $story->languages  = $this->getBlockSetting($block_id, 'languages');
183        }
184
185        return $this->viewResponse('modules/stories/config', [
186            'stories'    => $stories,
187            'title'      => $this->title() . ' — ' . $tree->title(),
188            'tree'       => $tree,
189            'tree_names' => Tree::getNameList(),
190        ]);
191    }
192
193    /**
194     * @param Request $request
195     * @param Tree    $tree
196     *
197     * @return Response
198     */
199    public function getAdminEditAction(Request $request, Tree $tree): Response
200    {
201        $this->layout = 'layouts/administration';
202
203        $block_id = (int) $request->get('block_id');
204
205        if ($block_id === 0) {
206            // Creating a new story
207            $individual  = Individual::getInstance($request->get('xref', ''), $tree);
208            $story_title = '';
209            $story_body  = '';
210            $languages   = [];
211
212            $title = I18N::translate('Add a story') . ' — ' . e($tree->title());
213        } else {
214            // Editing an existing story
215            $xref = (string) DB::table('block')
216                ->where('block_id', '=', $block_id)
217                ->value('xref');
218
219            $individual  = Individual::getInstance($xref, $tree);
220            $story_title = $this->getBlockSetting($block_id, 'title', '');
221            $story_body  = $this->getBlockSetting($block_id, 'story_body', '');
222            $languages   = explode(',', $this->getBlockSetting($block_id, 'languages'));
223
224            $title = I18N::translate('Edit the story') . ' — ' . e($tree->title());
225        }
226
227        return $this->viewResponse('modules/stories/edit', [
228            'block_id'    => $block_id,
229            'languages'   => $languages,
230            'story_body'  => $story_body,
231            'story_title' => $story_title,
232            'title'       => $title,
233            'tree'        => $tree,
234            'individual'  => $individual,
235        ]);
236    }
237
238    /**
239     * @param Request $request
240     * @param Tree    $tree
241     *
242     * @return RedirectResponse
243     */
244    public function postAdminEditAction(Request $request, Tree $tree): RedirectResponse
245    {
246        $block_id    = (int) $request->get('block_id');
247        $xref        = $request->get('xref', '');
248        $story_body  = $request->get('story_body', '');
249        $story_title = $request->get('story_title', '');
250        $languages   = $request->get('languages', []);
251
252        if ($block_id !== 0) {
253            DB::table('block')
254                ->where('block_id', '=', $block_id)
255                ->update([
256                    'gedcom_id' => $tree->id(),
257                    'xref'      => $xref,
258                ]);
259        } else {
260            DB::table('block')->insert([
261                'gedcom_id'   => $tree->id(),
262                'xref'        => $xref,
263                'module_name' => $this->name(),
264                'block_order' => 0,
265            ]);
266
267            $block_id = (int) DB::connection()->getPdo()->lastInsertId();
268        }
269
270        $this->setBlockSetting($block_id, 'story_body', $story_body);
271        $this->setBlockSetting($block_id, 'title', $story_title);
272        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
273
274        $url = route('module', [
275            'module' => $this->name(),
276            'action' => 'Admin',
277            'ged'    => $tree->name(),
278        ]);
279
280        return new RedirectResponse($url);
281    }
282
283    /**
284     * @param Request $request
285     * @param Tree    $tree
286     *
287     * @return Response
288     */
289    public function postAdminDeleteAction(Request $request, Tree $tree): Response
290    {
291        $block_id = (int) $request->get('block_id');
292
293        DB::table('block_setting')
294            ->where('block_id', '=', $block_id)
295            ->delete();
296
297        DB::table('block')
298            ->where('block_id', '=', $block_id)
299            ->delete();
300
301        $url = route('module', [
302            'module' => $this->name(),
303            'action' => 'Admin',
304            'ged'    => $tree->name(),
305        ]);
306
307        return new RedirectResponse($url);
308    }
309
310    /**
311     * @param Tree $tree
312     *
313     * @return Response
314     */
315    public function getShowListAction(Tree $tree): Response
316    {
317        $stories = DB::table('block')
318            ->where('module_name', '=', $this->name())
319            ->where('gedcom_id', '=', $tree->id())
320            ->get()
321            ->map(function (stdClass $story) use ($tree): stdClass {
322                $block_id = (int) $story->block_id;
323
324                $story->individual = Individual::getInstance($story->xref, $tree);
325                $story->title      = $this->getBlockSetting($block_id, 'title');
326                $story->languages  = $this->getBlockSetting($block_id, 'languages');
327
328                return $story;
329            })->filter(function (stdClass $story): bool {
330                // Filter non-existant and private individuals.
331                return $story->individual instanceof Individual && $story->individual->canShow();
332            })->filter(function (stdClass $story): bool {
333                // Filter foreign languages.
334                return $story->languages === '' || in_array(WT_LOCALE, explode(',', $story->languages));
335            });
336
337        return $this->viewResponse('modules/stories/list', [
338            'stories' => $stories,
339            'title'   => $this->title(),
340        ]);
341    }
342}
343