xref: /webtrees/app/Module/ResearchTaskModule.php (revision a45f98897789fc9ff88705eb09ae5f037bf49c10)
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\GedcomRecord;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Tree;
23use Symfony\Component\HttpFoundation\Request;
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     * Update the configuration for a block.
148     *
149     * @param Request $request
150     * @param int     $block_id
151     *
152     * @return void
153     */
154    public function saveBlockConfiguration(Request $request, int $block_id)
155    {
156        $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', ''));
157        $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', ''));
158        $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', ''));
159    }
160
161    /**
162     * An HTML form to edit block settings
163     *
164     * @param Tree $tree
165     * @param int  $block_id
166     *
167     * @return void
168     */
169    public function editBlockConfiguration(Tree $tree, int $block_id)
170    {
171        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
172        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
173        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
174
175        echo view('modules/todo/config', [
176            'show_future'     => $show_future,
177            'show_other'      => $show_other,
178            'show_unassigned' => $show_unassigned,
179        ]);
180    }
181}
182