xref: /webtrees/app/Module/ResearchTaskModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
198c2e8227SGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
2164b9f5b2SGreg Roachuse Fisharebest\Webtrees\Database;
2264b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class ResearchTaskModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3264b9f5b2SGreg Roach    const DEFAULT_SHOW_OTHER      = '1';
3364b9f5b2SGreg Roach    const DEFAULT_SHOW_UNASSIGNED = '1';
3464b9f5b2SGreg Roach    const DEFAULT_SHOW_FUTURE     = '1';
3564b9f5b2SGreg Roach    const DEFAULT_BLOCK           = '1';
3664b9f5b2SGreg Roach
378c2e8227SGreg Roach    /** {@inheritdoc} */
388f53f488SRico Sonntag    public function getTitle(): string
39c1010edaSGreg Roach    {
40bbb76c12SGreg Roach        /* I18N: Name of a module. Tasks that need further research. */
41bbb76c12SGreg Roach        return I18N::translate('Research tasks');
428c2e8227SGreg Roach    }
438c2e8227SGreg Roach
448c2e8227SGreg Roach    /** {@inheritdoc} */
458f53f488SRico Sonntag    public function getDescription(): string
46c1010edaSGreg Roach    {
47bbb76c12SGreg Roach        /* I18N: Description of “Research tasks” module */
48bbb76c12SGreg Roach        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
498c2e8227SGreg Roach    }
508c2e8227SGreg Roach
5176692c8bSGreg Roach    /**
5276692c8bSGreg Roach     * Generate the HTML content of this block.
5376692c8bSGreg Roach     *
54e490cd80SGreg Roach     * @param Tree     $tree
5576692c8bSGreg Roach     * @param int      $block_id
565f2ae573SGreg Roach     * @param string   $ctype
57727f238cSGreg Roach     * @param string[] $cfg
5876692c8bSGreg Roach     *
5976692c8bSGreg Roach     * @return string
6076692c8bSGreg Roach     */
615f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
62c1010edaSGreg 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([
7572cf66d4SGreg Roach            'tree_id' => $tree->id(),
7664b9f5b2SGreg Roach            'jd'      => $end_jd,
7713abd6f3SGreg Roach        ])->fetchOneColumn();
7864b9f5b2SGreg Roach
7918d7a90dSGreg Roach        $records = array_map(function ($xref) use ($tree): GedcomRecord {
80e490cd80SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
81f7f4b984SGreg Roach        }, $xrefs);
82f7f4b984SGreg Roach
83f7f4b984SGreg Roach        $tasks = [];
84f7f4b984SGreg Roach
85f7f4b984SGreg Roach        foreach ($records as $record) {
868d0ebef0SGreg Roach            foreach ($record->facts(['_TODO']) as $task) {
874852d81fSGreg Roach                $user_name = $task->attribute('_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
1016a8879feSGreg Roach        if ($ctype !== '') {
102e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
103c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
104c1010edaSGreg Roach                    'block_id' => $block_id,
105aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
106c1010edaSGreg Roach                ]);
107397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
108c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
109c1010edaSGreg Roach                    'block_id' => $block_id,
110aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
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        }
124b2ce94c6SRico Sonntag
125b2ce94c6SRico Sonntag        return $content;
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