xref: /webtrees/app/Module/ResearchTaskModule.php (revision aa6f03bb51be5a55a78fe8aed18753116689a6d3)
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 */
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
5676692c8bSGreg Roach     * @param bool     $template
57727f238cSGreg Roach     * @param string[] $cfg
5876692c8bSGreg Roach     *
5976692c8bSGreg Roach     * @return string
6076692c8bSGreg Roach     */
61c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
62c1010edaSGreg Roach    {
63e490cd80SGreg Roach        global $ctype;
648c2e8227SGreg Roach
6564b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
6664b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
6764b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
688c2e8227SGreg Roach
69c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
708c2e8227SGreg Roach
718c2e8227SGreg Roach        $end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
7264b9f5b2SGreg Roach
7364b9f5b2SGreg Roach        $xrefs = Database::prepare(
74c19940adSGreg Roach            "SELECT DISTINCT d_gid FROM `##dates`" .
75c19940adSGreg Roach            " WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
7613abd6f3SGreg Roach        )->execute([
7772cf66d4SGreg Roach            'tree_id' => $tree->id(),
7864b9f5b2SGreg Roach            'jd'      => $end_jd,
7913abd6f3SGreg Roach        ])->fetchOneColumn();
8064b9f5b2SGreg Roach
8118d7a90dSGreg Roach        $records = array_map(function ($xref) use ($tree): GedcomRecord {
82e490cd80SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
83f7f4b984SGreg Roach        }, $xrefs);
84f7f4b984SGreg Roach
85f7f4b984SGreg Roach        $tasks = [];
86f7f4b984SGreg Roach
87f7f4b984SGreg Roach        foreach ($records as $record) {
88f7f4b984SGreg Roach            foreach ($record->getFacts('_TODO') as $task) {
89f7f4b984SGreg Roach                $user_name = $task->getAttribute('_WT_USER');
90f7f4b984SGreg Roach
91f7f4b984SGreg Roach                if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
92f7f4b984SGreg Roach                    $tasks[] = $task;
93f7f4b984SGreg Roach                }
94f7f4b984SGreg Roach            }
95f7f4b984SGreg Roach        }
96f7f4b984SGreg Roach
97f7f4b984SGreg Roach        if (empty($records)) {
98a078385aSGreg Roach            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
99a078385aSGreg Roach        } else {
100147e99aaSGreg Roach            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
1018c2e8227SGreg Roach        }
1028c2e8227SGreg Roach
1038c2e8227SGreg Roach        if ($template) {
104e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
105c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
106c1010edaSGreg Roach                    'block_id' => $block_id,
107*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
108c1010edaSGreg Roach                ]);
109397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
110c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
111c1010edaSGreg Roach                    'block_id' => $block_id,
112*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
113c1010edaSGreg Roach                ]);
1148cbbfdceSGreg Roach            } else {
1158cbbfdceSGreg Roach                $config_url = '';
1168cbbfdceSGreg Roach            }
1178cbbfdceSGreg Roach
118147e99aaSGreg Roach            return view('modules/block-template', [
1199c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1209c6524dcSGreg Roach                'id'         => $block_id,
1218cbbfdceSGreg Roach                'config_url' => $config_url,
1229c6524dcSGreg Roach                'title'      => $this->getTitle(),
1239c6524dcSGreg Roach                'content'    => $content,
1249c6524dcSGreg Roach            ]);
1258c2e8227SGreg Roach        }
126b2ce94c6SRico Sonntag
127b2ce94c6SRico Sonntag        return $content;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
1308c2e8227SGreg Roach    /** {@inheritdoc} */
131c1010edaSGreg Roach    public function loadAjax(): bool
132c1010edaSGreg Roach    {
1338c2e8227SGreg Roach        return false;
1348c2e8227SGreg Roach    }
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach    /** {@inheritdoc} */
137c1010edaSGreg Roach    public function isUserBlock(): bool
138c1010edaSGreg Roach    {
1398c2e8227SGreg Roach        return true;
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach    /** {@inheritdoc} */
143c1010edaSGreg Roach    public function isGedcomBlock(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return true;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
14876692c8bSGreg Roach    /**
149a45f9889SGreg Roach     * Update the configuration for a block.
150a45f9889SGreg Roach     *
151a45f9889SGreg Roach     * @param Request $request
152a45f9889SGreg Roach     * @param int     $block_id
153a45f9889SGreg Roach     *
154a45f9889SGreg Roach     * @return void
155a45f9889SGreg Roach     */
156a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
157a45f9889SGreg Roach    {
158a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', ''));
159a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', ''));
160a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', ''));
161a45f9889SGreg Roach    }
162a45f9889SGreg Roach
163a45f9889SGreg Roach    /**
16476692c8bSGreg Roach     * An HTML form to edit block settings
16576692c8bSGreg Roach     *
166e490cd80SGreg Roach     * @param Tree $tree
16776692c8bSGreg Roach     * @param int  $block_id
168a9430be8SGreg Roach     *
169a9430be8SGreg Roach     * @return void
17076692c8bSGreg Roach     */
171a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
172c1010edaSGreg Roach    {
17364b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
17464b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
17564b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1768c2e8227SGreg Roach
177147e99aaSGreg Roach        echo view('modules/todo/config', [
178c385536dSGreg Roach            'show_future'     => $show_future,
179c385536dSGreg Roach            'show_other'      => $show_other,
180c385536dSGreg Roach            'show_unassigned' => $show_unassigned,
181c385536dSGreg Roach        ]);
1828c2e8227SGreg Roach    }
1838c2e8227SGreg Roach}
184