xref: /webtrees/app/Module/ResearchTaskModule.php (revision ddeb33548102a68bedfc3a35cbda68303ed365ca)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
218c2e8227SGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
248e74ca62SGreg Roachuse Fisharebest\Webtrees\Family;
2564b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
278e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual;
28e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
298e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
308e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause;
318e74ca62SGreg Roachuse Illuminate\Support\Collection;
321e7a7a28SGreg Roachuse Illuminate\Support\Str;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
348c2e8227SGreg Roach
358c2e8227SGreg Roach/**
368c2e8227SGreg Roach * Class ResearchTaskModule
378c2e8227SGreg Roach */
3837eb8894SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
39c1010edaSGreg Roach{
4049a243cbSGreg Roach    use ModuleBlockTrait;
4149a243cbSGreg Roach
4216d6367aSGreg Roach    private const DEFAULT_SHOW_OTHER      = '1';
4316d6367aSGreg Roach    private const DEFAULT_SHOW_UNASSIGNED = '1';
4416d6367aSGreg Roach    private const DEFAULT_SHOW_FUTURE     = '1';
4564b9f5b2SGreg Roach
46961ec755SGreg Roach    /**
470cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
48961ec755SGreg Roach     *
49961ec755SGreg Roach     * @return string
50961ec755SGreg Roach     */
5149a243cbSGreg Roach    public function title(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Name of a module. Tasks that need further research. */
54bbb76c12SGreg Roach        return I18N::translate('Research tasks');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
57961ec755SGreg Roach    /**
58961ec755SGreg Roach     * A sentence describing what this module does.
59961ec755SGreg Roach     *
60961ec755SGreg Roach     * @return string
61961ec755SGreg Roach     */
6249a243cbSGreg Roach    public function description(): string
63c1010edaSGreg Roach    {
64bbb76c12SGreg Roach        /* I18N: Description of “Research tasks” module */
65bbb76c12SGreg Roach        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
668c2e8227SGreg Roach    }
678c2e8227SGreg Roach
6876692c8bSGreg Roach    /**
6976692c8bSGreg Roach     * Generate the HTML content of this block.
7076692c8bSGreg Roach     *
71e490cd80SGreg Roach     * @param Tree     $tree
7276692c8bSGreg Roach     * @param int      $block_id
733caaa4d2SGreg Roach     * @param string   $context
743caaa4d2SGreg Roach     * @param string[] $config
7576692c8bSGreg Roach     *
7676692c8bSGreg Roach     * @return string
7776692c8bSGreg Roach     */
783caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
79c1010edaSGreg Roach    {
8064b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
8164b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
8264b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
838c2e8227SGreg Roach
843caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
858c2e8227SGreg Roach
864459dc9aSGreg Roach        $end_jd      = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay();
878e74ca62SGreg Roach        $individuals = $this->individualsWithTasks($tree, $end_jd);
888e74ca62SGreg Roach        $families    = $this->familiesWithTasks($tree, $end_jd);
8964b9f5b2SGreg Roach
908e74ca62SGreg Roach        /** @var GedcomRecord[] $records */
918e74ca62SGreg Roach        $records = $individuals->merge($families);
92f7f4b984SGreg Roach
93f7f4b984SGreg Roach        $tasks = [];
94f7f4b984SGreg Roach
95f7f4b984SGreg Roach        foreach ($records as $record) {
968d0ebef0SGreg Roach            foreach ($record->facts(['_TODO']) as $task) {
974852d81fSGreg Roach                $user_name = $task->attribute('_WT_USER');
98f7f4b984SGreg Roach
99*ddeb3354SGreg Roach                if ($user_name === Auth::user()->userName()) {
100*ddeb3354SGreg Roach                    // Tasks belonging to us.
101*ddeb3354SGreg Roach                    $tasks[] = $task;
102*ddeb3354SGreg Roach                } elseif ($user_name === '' && $show_unassigned) {
103*ddeb3354SGreg Roach                    // Tasks belonging to nobody.
104*ddeb3354SGreg Roach                    $tasks[] = $task;
105*ddeb3354SGreg Roach                } elseif ($user_name !== '' && $show_other) {
106*ddeb3354SGreg Roach                    // Tasks belonging to others.
107f7f4b984SGreg Roach                    $tasks[] = $task;
108f7f4b984SGreg Roach                }
109f7f4b984SGreg Roach            }
110f7f4b984SGreg Roach        }
111f7f4b984SGreg Roach
112f7f4b984SGreg Roach        if (empty($records)) {
113a078385aSGreg Roach            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
114a078385aSGreg Roach        } else {
115147e99aaSGreg Roach            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
1168c2e8227SGreg Roach        }
1178c2e8227SGreg Roach
1183caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
119147e99aaSGreg Roach            return view('modules/block-template', [
1201e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1219c6524dcSGreg Roach                'id'         => $block_id,
1223caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
12349a243cbSGreg Roach                'title'      => $this->title(),
1249c6524dcSGreg Roach                'content'    => $content,
1259c6524dcSGreg Roach            ]);
1268c2e8227SGreg Roach        }
127b2ce94c6SRico Sonntag
128b2ce94c6SRico Sonntag        return $content;
1298c2e8227SGreg Roach    }
1308c2e8227SGreg Roach
1313caaa4d2SGreg Roach    /**
1323caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1333caaa4d2SGreg Roach     *
1343caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1353caaa4d2SGreg Roach     *
1363caaa4d2SGreg Roach     * @return bool
1373caaa4d2SGreg Roach     */
138c1010edaSGreg Roach    public function loadAjax(): bool
139c1010edaSGreg Roach    {
1408c2e8227SGreg Roach        return false;
1418c2e8227SGreg Roach    }
1428c2e8227SGreg Roach
1433caaa4d2SGreg Roach    /**
1443caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1453caaa4d2SGreg Roach     *
1463caaa4d2SGreg Roach     * @return bool
1473caaa4d2SGreg Roach     */
148c1010edaSGreg Roach    public function isUserBlock(): bool
149c1010edaSGreg Roach    {
1508c2e8227SGreg Roach        return true;
1518c2e8227SGreg Roach    }
1528c2e8227SGreg Roach
1533caaa4d2SGreg Roach    /**
1543caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1553caaa4d2SGreg Roach     *
1563caaa4d2SGreg Roach     * @return bool
1573caaa4d2SGreg Roach     */
15863276d8fSGreg Roach    public function isTreeBlock(): bool
159c1010edaSGreg Roach    {
1608c2e8227SGreg Roach        return true;
1618c2e8227SGreg Roach    }
1628c2e8227SGreg Roach
16376692c8bSGreg Roach    /**
164a45f9889SGreg Roach     * Update the configuration for a block.
165a45f9889SGreg Roach     *
1666ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
167a45f9889SGreg Roach     * @param int     $block_id
168a45f9889SGreg Roach     *
169a45f9889SGreg Roach     * @return void
170a45f9889SGreg Roach     */
1716ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
172a45f9889SGreg Roach    {
173b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
174b6b9dcc9SGreg Roach
175b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_other', $params['show_other']);
176b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']);
177b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_future', $params['show_future']);
178a45f9889SGreg Roach    }
179a45f9889SGreg Roach
180a45f9889SGreg Roach    /**
18176692c8bSGreg Roach     * An HTML form to edit block settings
18276692c8bSGreg Roach     *
183e490cd80SGreg Roach     * @param Tree $tree
18476692c8bSGreg Roach     * @param int  $block_id
185a9430be8SGreg Roach     *
1863caaa4d2SGreg Roach     * @return string
18776692c8bSGreg Roach     */
1883caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
189c1010edaSGreg Roach    {
19064b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
19164b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
19264b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1938c2e8227SGreg Roach
1943caaa4d2SGreg Roach        return view('modules/todo/config', [
195c385536dSGreg Roach            'show_future'     => $show_future,
196c385536dSGreg Roach            'show_other'      => $show_other,
197c385536dSGreg Roach            'show_unassigned' => $show_unassigned,
198c385536dSGreg Roach        ]);
1998c2e8227SGreg Roach    }
2008e74ca62SGreg Roach
2018e74ca62SGreg Roach    /**
2028e74ca62SGreg Roach     * @param Tree $tree
2038e74ca62SGreg Roach     * @param int  $max_julian_day
2048e74ca62SGreg Roach     *
2058e74ca62SGreg Roach     * @return Collection
2068e74ca62SGreg Roach     */
2078e74ca62SGreg Roach    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
2088e74ca62SGreg Roach    {
2098e74ca62SGreg Roach        return DB::table('families')
2100b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
2118e74ca62SGreg Roach                $join
2128e74ca62SGreg Roach                    ->on('f_file', '=', 'd_file')
2138e74ca62SGreg Roach                    ->on('f_id', '=', 'd_gid');
2148e74ca62SGreg Roach            })
2158e74ca62SGreg Roach            ->where('f_file', '=', $tree->id())
2168e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
2178e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
218c0804649SGreg Roach            ->select(['families.*'])
219c0804649SGreg Roach            ->distinct()
2208e74ca62SGreg Roach            ->get()
2214146fabcSGreg Roach            ->map(Family::rowMapper())
2224146fabcSGreg Roach            ->filter(GedcomRecord::accessFilter());
2238e74ca62SGreg Roach    }
2248e74ca62SGreg Roach
2258e74ca62SGreg Roach    /**
2268e74ca62SGreg Roach     * @param Tree $tree
2278e74ca62SGreg Roach     * @param int  $max_julian_day
2288e74ca62SGreg Roach     *
2298e74ca62SGreg Roach     * @return Collection
2308e74ca62SGreg Roach     */
2318e74ca62SGreg Roach    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
2328e74ca62SGreg Roach    {
2338e74ca62SGreg Roach        return DB::table('individuals')
2340b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
2358e74ca62SGreg Roach                $join
2368e74ca62SGreg Roach                    ->on('i_file', '=', 'd_file')
2378e74ca62SGreg Roach                    ->on('i_id', '=', 'd_gid');
2388e74ca62SGreg Roach            })
2398e74ca62SGreg Roach            ->where('i_file', '=', $tree->id())
2408e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
2418e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
242c0804649SGreg Roach            ->select(['individuals.*'])
243c0804649SGreg Roach            ->distinct()
2448e74ca62SGreg Roach            ->get()
2454146fabcSGreg Roach            ->map(Individual::rowMapper())
2464146fabcSGreg Roach            ->filter(GedcomRecord::accessFilter());
2478e74ca62SGreg Roach    }
2488c2e8227SGreg Roach}
249