xref: /webtrees/app/Module/ResearchTaskModule.php (revision b6b9dcc9af05c3f136144fb4f6f9b69a8abd1995)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg 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;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
228e74ca62SGreg Roachuse Fisharebest\Webtrees\Family;
2364b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
258e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual;
26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
278e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
288e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause;
298e74ca62SGreg Roachuse Illuminate\Support\Collection;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
328c2e8227SGreg Roach
338c2e8227SGreg Roach/**
348c2e8227SGreg Roach * Class ResearchTaskModule
358c2e8227SGreg Roach */
3637eb8894SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleBlockTrait;
3949a243cbSGreg Roach
4016d6367aSGreg Roach    private const DEFAULT_SHOW_OTHER      = '1';
4116d6367aSGreg Roach    private const DEFAULT_SHOW_UNASSIGNED = '1';
4216d6367aSGreg Roach    private const DEFAULT_SHOW_FUTURE     = '1';
4364b9f5b2SGreg Roach
44961ec755SGreg Roach    /**
450cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
46961ec755SGreg Roach     *
47961ec755SGreg Roach     * @return string
48961ec755SGreg Roach     */
4949a243cbSGreg Roach    public function title(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Name of a module. Tasks that need further research. */
52bbb76c12SGreg Roach        return I18N::translate('Research tasks');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
55961ec755SGreg Roach    /**
56961ec755SGreg Roach     * A sentence describing what this module does.
57961ec755SGreg Roach     *
58961ec755SGreg Roach     * @return string
59961ec755SGreg Roach     */
6049a243cbSGreg Roach    public function description(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of “Research tasks” module */
63bbb76c12SGreg Roach        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6676692c8bSGreg Roach    /**
6776692c8bSGreg Roach     * Generate the HTML content of this block.
6876692c8bSGreg Roach     *
69e490cd80SGreg Roach     * @param Tree     $tree
7076692c8bSGreg Roach     * @param int      $block_id
715f2ae573SGreg Roach     * @param string   $ctype
72727f238cSGreg Roach     * @param string[] $cfg
7376692c8bSGreg Roach     *
7476692c8bSGreg Roach     * @return string
7576692c8bSGreg Roach     */
765f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
77c1010edaSGreg Roach    {
7864b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
7964b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
8064b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
818c2e8227SGreg Roach
82c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
838c2e8227SGreg Roach
844459dc9aSGreg Roach        $end_jd      = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay();
858e74ca62SGreg Roach        $individuals = $this->individualsWithTasks($tree, $end_jd);
868e74ca62SGreg Roach        $families    = $this->familiesWithTasks($tree, $end_jd);
8764b9f5b2SGreg Roach
888e74ca62SGreg Roach        /** @var GedcomRecord[] $records */
898e74ca62SGreg Roach        $records = $individuals->merge($families);
90f7f4b984SGreg Roach
91f7f4b984SGreg Roach        $tasks = [];
92f7f4b984SGreg Roach
93f7f4b984SGreg Roach        foreach ($records as $record) {
948d0ebef0SGreg Roach            foreach ($record->facts(['_TODO']) as $task) {
954852d81fSGreg Roach                $user_name = $task->attribute('_WT_USER');
96f7f4b984SGreg Roach
97e5a6b4d4SGreg Roach                if ($user_name === Auth::user()->userName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
98f7f4b984SGreg Roach                    $tasks[] = $task;
99f7f4b984SGreg Roach                }
100f7f4b984SGreg Roach            }
101f7f4b984SGreg Roach        }
102f7f4b984SGreg Roach
103f7f4b984SGreg Roach        if (empty($records)) {
104a078385aSGreg Roach            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
105a078385aSGreg Roach        } else {
106147e99aaSGreg Roach            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
1078c2e8227SGreg Roach        }
1088c2e8227SGreg Roach
1096a8879feSGreg Roach        if ($ctype !== '') {
110e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
111c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
112c1010edaSGreg Roach                    'block_id' => $block_id,
113aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
114c1010edaSGreg Roach                ]);
115397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
116c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
117c1010edaSGreg Roach                    'block_id' => $block_id,
118aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
119c1010edaSGreg Roach                ]);
1208cbbfdceSGreg Roach            } else {
1218cbbfdceSGreg Roach                $config_url = '';
1228cbbfdceSGreg Roach            }
1238cbbfdceSGreg Roach
124147e99aaSGreg Roach            return view('modules/block-template', [
1251e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1269c6524dcSGreg Roach                'id'         => $block_id,
1278cbbfdceSGreg Roach                'config_url' => $config_url,
12849a243cbSGreg Roach                'title'      => $this->title(),
1299c6524dcSGreg Roach                'content'    => $content,
1309c6524dcSGreg Roach            ]);
1318c2e8227SGreg Roach        }
132b2ce94c6SRico Sonntag
133b2ce94c6SRico Sonntag        return $content;
1348c2e8227SGreg Roach    }
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach    /** {@inheritdoc} */
137c1010edaSGreg Roach    public function loadAjax(): bool
138c1010edaSGreg Roach    {
1398c2e8227SGreg Roach        return false;
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach    /** {@inheritdoc} */
143c1010edaSGreg Roach    public function isUserBlock(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return true;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach    /** {@inheritdoc} */
14963276d8fSGreg Roach    public function isTreeBlock(): bool
150c1010edaSGreg Roach    {
1518c2e8227SGreg Roach        return true;
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
15476692c8bSGreg Roach    /**
155a45f9889SGreg Roach     * Update the configuration for a block.
156a45f9889SGreg Roach     *
1576ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
158a45f9889SGreg Roach     * @param int     $block_id
159a45f9889SGreg Roach     *
160a45f9889SGreg Roach     * @return void
161a45f9889SGreg Roach     */
1626ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
163a45f9889SGreg Roach    {
164*b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
165*b6b9dcc9SGreg Roach
166*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_other', $params['show_other']);
167*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']);
168*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_future', $params['show_future']);
169a45f9889SGreg Roach    }
170a45f9889SGreg Roach
171a45f9889SGreg Roach    /**
17276692c8bSGreg Roach     * An HTML form to edit block settings
17376692c8bSGreg Roach     *
174e490cd80SGreg Roach     * @param Tree $tree
17576692c8bSGreg Roach     * @param int  $block_id
176a9430be8SGreg Roach     *
177a9430be8SGreg Roach     * @return void
17876692c8bSGreg Roach     */
179e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
180c1010edaSGreg Roach    {
18164b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
18264b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
18364b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1848c2e8227SGreg Roach
185147e99aaSGreg Roach        echo view('modules/todo/config', [
186c385536dSGreg Roach            'show_future'     => $show_future,
187c385536dSGreg Roach            'show_other'      => $show_other,
188c385536dSGreg Roach            'show_unassigned' => $show_unassigned,
189c385536dSGreg Roach        ]);
1908c2e8227SGreg Roach    }
1918e74ca62SGreg Roach
1928e74ca62SGreg Roach    /**
1938e74ca62SGreg Roach     * @param Tree $tree
1948e74ca62SGreg Roach     * @param int  $max_julian_day
1958e74ca62SGreg Roach     *
1968e74ca62SGreg Roach     * @return Collection
1978e74ca62SGreg Roach     */
1988e74ca62SGreg Roach    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
1998e74ca62SGreg Roach    {
2008e74ca62SGreg Roach        return DB::table('families')
2010b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
2028e74ca62SGreg Roach                $join
2038e74ca62SGreg Roach                    ->on('f_file', '=', 'd_file')
2048e74ca62SGreg Roach                    ->on('f_id', '=', 'd_gid');
2058e74ca62SGreg Roach            })
2068e74ca62SGreg Roach            ->where('f_file', '=', $tree->id())
2078e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
2088e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
209c0804649SGreg Roach            ->select(['families.*'])
210c0804649SGreg Roach            ->distinct()
2118e74ca62SGreg Roach            ->get()
2124146fabcSGreg Roach            ->map(Family::rowMapper())
2134146fabcSGreg Roach            ->filter(GedcomRecord::accessFilter());
2148e74ca62SGreg Roach    }
2158e74ca62SGreg Roach
2168e74ca62SGreg Roach    /**
2178e74ca62SGreg Roach     * @param Tree $tree
2188e74ca62SGreg Roach     * @param int  $max_julian_day
2198e74ca62SGreg Roach     *
2208e74ca62SGreg Roach     * @return Collection
2218e74ca62SGreg Roach     */
2228e74ca62SGreg Roach    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
2238e74ca62SGreg Roach    {
2248e74ca62SGreg Roach        return DB::table('individuals')
2250b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
2268e74ca62SGreg Roach                $join
2278e74ca62SGreg Roach                    ->on('i_file', '=', 'd_file')
2288e74ca62SGreg Roach                    ->on('i_id', '=', 'd_gid');
2298e74ca62SGreg Roach            })
2308e74ca62SGreg Roach            ->where('i_file', '=', $tree->id())
2318e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
2328e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
233c0804649SGreg Roach            ->select(['individuals.*'])
234c0804649SGreg Roach            ->distinct()
2358e74ca62SGreg Roach            ->get()
2364146fabcSGreg Roach            ->map(Individual::rowMapper())
2374146fabcSGreg Roach            ->filter(GedcomRecord::accessFilter());
2388e74ca62SGreg Roach    }
2398c2e8227SGreg Roach}
240