xref: /webtrees/app/Module/ResearchTaskModule.php (revision ddf438a50ef4ca889f688fe1c566ddf89d256ed1)
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\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Tree;
25use Symfony\Component\HttpFoundation\Request;
26
27/**
28 * Class ResearchTaskModule
29 */
30class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
31{
32    const DEFAULT_SHOW_OTHER      = '1';
33    const DEFAULT_SHOW_UNASSIGNED = '1';
34    const DEFAULT_SHOW_FUTURE     = '1';
35    const DEFAULT_BLOCK           = '1';
36
37    /** {@inheritdoc} */
38    public function getTitle(): string
39    {
40        /* I18N: Name of a module. Tasks that need further research. */
41        return I18N::translate('Research tasks');
42    }
43
44    /** {@inheritdoc} */
45    public function getDescription(): string
46    {
47        /* I18N: Description of “Research tasks” module */
48        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
49    }
50
51    /**
52     * Generate the HTML content of this block.
53     *
54     * @param Tree     $tree
55     * @param int      $block_id
56     * @param bool     $template
57     * @param string[] $cfg
58     *
59     * @return string
60     */
61    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
62    {
63        global $ctype;
64
65        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
66        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
67        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
68
69        extract($cfg, EXTR_OVERWRITE);
70
71        $end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
72
73        $xrefs = Database::prepare(
74            "SELECT DISTINCT d_gid FROM `##dates`" .
75            " WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
76        )->execute([
77            'tree_id' => $tree->id(),
78            'jd'      => $end_jd,
79        ])->fetchOneColumn();
80
81        $records = array_map(function ($xref) use ($tree): GedcomRecord {
82            return GedcomRecord::getInstance($xref, $tree);
83        }, $xrefs);
84
85        $tasks = [];
86
87        foreach ($records as $record) {
88            foreach ($record->facts(['_TODO']) as $task) {
89                $user_name = $task->attribute('_WT_USER');
90
91                if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
92                    $tasks[] = $task;
93                }
94            }
95        }
96
97        if (empty($records)) {
98            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
99        } else {
100            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
101        }
102
103        if ($template) {
104            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
105                $config_url = route('tree-page-block-edit', [
106                    'block_id' => $block_id,
107                    'ged'      => $tree->name(),
108                ]);
109            } elseif ($ctype === 'user' && Auth::check()) {
110                $config_url = route('user-page-block-edit', [
111                    'block_id' => $block_id,
112                    'ged'      => $tree->name(),
113                ]);
114            } else {
115                $config_url = '';
116            }
117
118            return view('modules/block-template', [
119                'block'      => str_replace('_', '-', $this->getName()),
120                'id'         => $block_id,
121                'config_url' => $config_url,
122                'title'      => $this->getTitle(),
123                'content'    => $content,
124            ]);
125        }
126
127        return $content;
128    }
129
130    /** {@inheritdoc} */
131    public function loadAjax(): bool
132    {
133        return false;
134    }
135
136    /** {@inheritdoc} */
137    public function isUserBlock(): bool
138    {
139        return true;
140    }
141
142    /** {@inheritdoc} */
143    public function isGedcomBlock(): bool
144    {
145        return true;
146    }
147
148    /**
149     * Update the configuration for a block.
150     *
151     * @param Request $request
152     * @param int     $block_id
153     *
154     * @return void
155     */
156    public function saveBlockConfiguration(Request $request, int $block_id)
157    {
158        $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', ''));
159        $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', ''));
160        $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', ''));
161    }
162
163    /**
164     * An HTML form to edit block settings
165     *
166     * @param Tree $tree
167     * @param int  $block_id
168     *
169     * @return void
170     */
171    public function editBlockConfiguration(Tree $tree, int $block_id)
172    {
173        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
174        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
175        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
176
177        echo view('modules/todo/config', [
178            'show_future'     => $show_future,
179            'show_other'      => $show_other,
180            'show_unassigned' => $show_unassigned,
181        ]);
182    }
183}
184