xref: /webtrees/app/Module/ResearchTaskModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
1964b9f5b2SGreg Roachuse Fisharebest\Webtrees\Database;
2064b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
22e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
23a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class ResearchTaskModule
278c2e8227SGreg Roach */
28c1010edaSGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
29c1010edaSGreg Roach{
3064b9f5b2SGreg Roach    const DEFAULT_SHOW_OTHER      = '1';
3164b9f5b2SGreg Roach    const DEFAULT_SHOW_UNASSIGNED = '1';
3264b9f5b2SGreg Roach    const DEFAULT_SHOW_FUTURE     = '1';
3364b9f5b2SGreg Roach    const DEFAULT_BLOCK           = '1';
3464b9f5b2SGreg Roach
358c2e8227SGreg Roach    /** {@inheritdoc} */
36*8f53f488SRico Sonntag    public function getTitle(): string
37c1010edaSGreg Roach    {
38bbb76c12SGreg Roach        /* I18N: Name of a module. Tasks that need further research. */
39bbb76c12SGreg Roach        return I18N::translate('Research tasks');
408c2e8227SGreg Roach    }
418c2e8227SGreg Roach
428c2e8227SGreg Roach    /** {@inheritdoc} */
43*8f53f488SRico Sonntag    public function getDescription(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Description of “Research tasks” module */
46bbb76c12SGreg Roach        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
4976692c8bSGreg Roach    /**
5076692c8bSGreg Roach     * Generate the HTML content of this block.
5176692c8bSGreg Roach     *
52e490cd80SGreg Roach     * @param Tree     $tree
5376692c8bSGreg Roach     * @param int      $block_id
5476692c8bSGreg Roach     * @param bool     $template
55727f238cSGreg Roach     * @param string[] $cfg
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @return string
5876692c8bSGreg Roach     */
59c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
60c1010edaSGreg Roach    {
61e490cd80SGreg Roach        global $ctype;
628c2e8227SGreg Roach
6364b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
6464b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
6564b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
668c2e8227SGreg Roach
67c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
688c2e8227SGreg Roach
698c2e8227SGreg Roach        $end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
7064b9f5b2SGreg Roach
7164b9f5b2SGreg Roach        $xrefs = Database::prepare(
72c19940adSGreg Roach            "SELECT DISTINCT d_gid FROM `##dates`" .
73c19940adSGreg Roach            " WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
7413abd6f3SGreg Roach        )->execute([
75e490cd80SGreg Roach            'tree_id' => $tree->getTreeId(),
7664b9f5b2SGreg Roach            'jd'      => $end_jd,
7713abd6f3SGreg Roach        ])->fetchOneColumn();
7864b9f5b2SGreg Roach
79e490cd80SGreg Roach        $records = array_map(function ($xref) use ($tree) {
80e490cd80SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
81f7f4b984SGreg Roach        }, $xrefs);
82f7f4b984SGreg Roach
83f7f4b984SGreg Roach        $tasks = [];
84f7f4b984SGreg Roach
85f7f4b984SGreg Roach        foreach ($records as $record) {
86f7f4b984SGreg Roach            foreach ($record->getFacts('_TODO') as $task) {
87f7f4b984SGreg Roach                $user_name = $task->getAttribute('_WT_USER');
88f7f4b984SGreg Roach
89f7f4b984SGreg Roach                if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
90f7f4b984SGreg Roach                    $tasks[] = $task;
91f7f4b984SGreg Roach                }
92f7f4b984SGreg Roach            }
93f7f4b984SGreg Roach        }
94f7f4b984SGreg Roach
95f7f4b984SGreg Roach        if (empty($records)) {
96a078385aSGreg Roach            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
97a078385aSGreg Roach        } else {
98147e99aaSGreg Roach            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
998c2e8227SGreg Roach        }
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach        if ($template) {
102e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
103c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
104c1010edaSGreg Roach                    'block_id' => $block_id,
105c1010edaSGreg Roach                    'ged'      => $tree->getName(),
106c1010edaSGreg Roach                ]);
107397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
108c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
109c1010edaSGreg Roach                    'block_id' => $block_id,
110c1010edaSGreg Roach                    'ged'      => $tree->getName(),
111c1010edaSGreg Roach                ]);
1128cbbfdceSGreg Roach            } else {
1138cbbfdceSGreg Roach                $config_url = '';
1148cbbfdceSGreg Roach            }
1158cbbfdceSGreg Roach
116147e99aaSGreg Roach            return view('modules/block-template', [
1179c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1189c6524dcSGreg Roach                'id'         => $block_id,
1198cbbfdceSGreg Roach                'config_url' => $config_url,
1209c6524dcSGreg Roach                'title'      => $this->getTitle(),
1219c6524dcSGreg Roach                'content'    => $content,
1229c6524dcSGreg Roach            ]);
1238c2e8227SGreg Roach        } else {
1248c2e8227SGreg Roach            return $content;
1258c2e8227SGreg Roach        }
1268c2e8227SGreg Roach    }
1278c2e8227SGreg Roach
1288c2e8227SGreg Roach    /** {@inheritdoc} */
129c1010edaSGreg Roach    public function loadAjax(): bool
130c1010edaSGreg Roach    {
1318c2e8227SGreg Roach        return false;
1328c2e8227SGreg Roach    }
1338c2e8227SGreg Roach
1348c2e8227SGreg Roach    /** {@inheritdoc} */
135c1010edaSGreg Roach    public function isUserBlock(): bool
136c1010edaSGreg Roach    {
1378c2e8227SGreg Roach        return true;
1388c2e8227SGreg Roach    }
1398c2e8227SGreg Roach
1408c2e8227SGreg Roach    /** {@inheritdoc} */
141c1010edaSGreg Roach    public function isGedcomBlock(): bool
142c1010edaSGreg Roach    {
1438c2e8227SGreg Roach        return true;
1448c2e8227SGreg Roach    }
1458c2e8227SGreg Roach
14676692c8bSGreg Roach    /**
147a45f9889SGreg Roach     * Update the configuration for a block.
148a45f9889SGreg Roach     *
149a45f9889SGreg Roach     * @param Request $request
150a45f9889SGreg Roach     * @param int     $block_id
151a45f9889SGreg Roach     *
152a45f9889SGreg Roach     * @return void
153a45f9889SGreg Roach     */
154a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
155a45f9889SGreg Roach    {
156a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', ''));
157a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', ''));
158a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', ''));
159a45f9889SGreg Roach    }
160a45f9889SGreg Roach
161a45f9889SGreg Roach    /**
16276692c8bSGreg Roach     * An HTML form to edit block settings
16376692c8bSGreg Roach     *
164e490cd80SGreg Roach     * @param Tree $tree
16576692c8bSGreg Roach     * @param int  $block_id
166a9430be8SGreg Roach     *
167a9430be8SGreg Roach     * @return void
16876692c8bSGreg Roach     */
169a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
170c1010edaSGreg Roach    {
17164b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
17264b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
17364b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1748c2e8227SGreg Roach
175147e99aaSGreg Roach        echo view('modules/todo/config', [
176c385536dSGreg Roach            'show_future'     => $show_future,
177c385536dSGreg Roach            'show_other'      => $show_other,
178c385536dSGreg Roach            'show_unassigned' => $show_unassigned,
179c385536dSGreg Roach        ]);
1808c2e8227SGreg Roach    }
1818c2e8227SGreg Roach}
182