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