xref: /webtrees/app/Module/ResearchTaskModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
24099c152eSGreg Roachuse Fisharebest\Webtrees\Elements\DateValueToday;
25ad5dbf37SGreg Roachuse Fisharebest\Webtrees\Elements\NoteStructure;
263d2c98d1SGreg Roachuse Fisharebest\Webtrees\Elements\ResearchTask;
273d2c98d1SGreg Roachuse Fisharebest\Webtrees\Elements\WebtreesUser;
288e74ca62SGreg Roachuse Fisharebest\Webtrees\Family;
2964b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
318e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual;
323d2c98d1SGreg Roachuse Fisharebest\Webtrees\Registry;
33e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
34748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
358e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause;
368e74ca62SGreg Roachuse Illuminate\Support\Collection;
371e7a7a28SGreg Roachuse Illuminate\Support\Str;
386ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
398c2e8227SGreg Roach
408c2e8227SGreg Roach/**
418c2e8227SGreg Roach * Class ResearchTaskModule
428c2e8227SGreg Roach */
4337eb8894SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
44c1010edaSGreg Roach{
4549a243cbSGreg Roach    use ModuleBlockTrait;
4649a243cbSGreg Roach
4716d6367aSGreg Roach    private const DEFAULT_SHOW_OTHER      = '1';
4816d6367aSGreg Roach    private const DEFAULT_SHOW_UNASSIGNED = '1';
4916d6367aSGreg Roach    private const DEFAULT_SHOW_FUTURE     = '1';
5064b9f5b2SGreg Roach
514118eb07SGreg Roach    // 31 DEC 9999
524118eb07SGreg Roach    private const MAXIMUM_JULIAN_DAY = 5373484;
534118eb07SGreg Roach
5401221f27SGreg Roach    // Pagination
5501221f27SGreg Roach    private const LIMIT_LOW  = 10;
5601221f27SGreg Roach    private const LIMIT_HIGH = 20;
5701221f27SGreg Roach
5892a78a2fSGreg Roach    /**
5992a78a2fSGreg Roach     * Early initialisation.  Called before most of the middleware.
6092a78a2fSGreg Roach     */
613d2c98d1SGreg Roach    public function boot(): void
62c1010edaSGreg Roach    {
6300c92694SGreg Roach        Registry::elementFactory()->registerTags([
643d2c98d1SGreg Roach            'FAM:_TODO'           => new ResearchTask(I18N::translate('Research task')),
65099c152eSGreg Roach            'FAM:_TODO:DATE'      => new DateValueToday(I18N::translate('Date')),
66ad5dbf37SGreg Roach            'FAM:_TODO:NOTE'      => new NoteStructure(I18N::translate('Note')),
673d2c98d1SGreg Roach            'FAM:_TODO:_WT_USER'  => new WebtreesUser(I18N::translate('User')),
683d2c98d1SGreg Roach            'INDI:_TODO'          => new ResearchTask(I18N::translate('Research task')),
69099c152eSGreg Roach            'INDI:_TODO:DATE'     => new DateValueToday(I18N::translate('Date')),
70ad5dbf37SGreg Roach            'INDI:_TODO:NOTE'     => new NoteStructure(I18N::translate('Note')),
713d2c98d1SGreg Roach            'INDI:_TODO:_WT_USER' => new WebtreesUser(I18N::translate('User')),
723d2c98d1SGreg Roach        ]);
73dbfc3863SGreg Roach
74dbfc3863SGreg Roach        Registry::elementFactory()->make('FAM')->subtag('_TODO', '0:M');
75dbfc3863SGreg Roach        Registry::elementFactory()->make('INDI')->subtag('_TODO', '0:M');
768c2e8227SGreg Roach    }
778c2e8227SGreg Roach
7849a243cbSGreg Roach    public function description(): string
79c1010edaSGreg Roach    {
80bbb76c12SGreg Roach        /* I18N: Description of “Research tasks” module */
81bbb76c12SGreg Roach        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
828c2e8227SGreg Roach    }
838c2e8227SGreg Roach
8476692c8bSGreg Roach    /**
8576692c8bSGreg Roach     * Generate the HTML content of this block.
8676692c8bSGreg Roach     *
87e490cd80SGreg Roach     * @param Tree                 $tree
8876692c8bSGreg Roach     * @param int                  $block_id
893caaa4d2SGreg Roach     * @param string               $context
9076d39c55SGreg Roach     * @param array<string,string> $config
9176692c8bSGreg Roach     *
9276692c8bSGreg Roach     * @return string
9376692c8bSGreg Roach     */
943caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
95c1010edaSGreg Roach    {
9664b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
9764b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
9864b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
998c2e8227SGreg Roach
1003caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1018c2e8227SGreg Roach
1024118eb07SGreg Roach        $end_jd      = $show_future ? self::MAXIMUM_JULIAN_DAY : Registry::timestampFactory()->now()->julianDay();
1038e74ca62SGreg Roach        $individuals = $this->individualsWithTasks($tree, $end_jd);
1048e74ca62SGreg Roach        $families    = $this->familiesWithTasks($tree, $end_jd);
10564b9f5b2SGreg Roach
1068e74ca62SGreg Roach        $records = $individuals->merge($families);
107f7f4b984SGreg Roach
108e24053e5SGreg Roach        $tasks = new Collection();
109f7f4b984SGreg Roach
110f7f4b984SGreg Roach        foreach ($records as $record) {
111ad5dbf37SGreg Roach            foreach ($record->facts(['_TODO'], false, null, true) as $task) {
1124852d81fSGreg Roach                $user_name = $task->attribute('_WT_USER');
113f7f4b984SGreg Roach
114ddeb3354SGreg Roach                if ($user_name === Auth::user()->userName()) {
115ddeb3354SGreg Roach                    // Tasks belonging to us.
116e24053e5SGreg Roach                    $tasks->add($task);
117ddeb3354SGreg Roach                } elseif ($user_name === '' && $show_unassigned) {
118ddeb3354SGreg Roach                    // Tasks belonging to nobody.
119e24053e5SGreg Roach                    $tasks->add($task);
120ddeb3354SGreg Roach                } elseif ($user_name !== '' && $show_other) {
121ddeb3354SGreg Roach                    // Tasks belonging to others.
122e24053e5SGreg Roach                    $tasks->add($task);
123f7f4b984SGreg Roach                }
124f7f4b984SGreg Roach            }
125f7f4b984SGreg Roach        }
126f7f4b984SGreg Roach
127075d1a05SGreg Roach        if ($records->isEmpty()) {
128a078385aSGreg Roach            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
129a078385aSGreg Roach        } else {
130e24053e5SGreg Roach            $content = view('modules/todo/research-tasks', [
13101221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
13201221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
133e24053e5SGreg Roach                'tasks'      => $tasks,
134e24053e5SGreg Roach            ]);
1358c2e8227SGreg Roach        }
1368c2e8227SGreg Roach
1373caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
138147e99aaSGreg Roach            return view('modules/block-template', [
1391e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1409c6524dcSGreg Roach                'id'         => $block_id,
1413caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
14249a243cbSGreg Roach                'title'      => $this->title(),
1439c6524dcSGreg Roach                'content'    => $content,
1449c6524dcSGreg Roach            ]);
1458c2e8227SGreg Roach        }
146b2ce94c6SRico Sonntag
147b2ce94c6SRico Sonntag        return $content;
1488c2e8227SGreg Roach    }
1498c2e8227SGreg Roach
1503caaa4d2SGreg Roach    /**
1513d2c98d1SGreg Roach     * @param Tree $tree
1523d2c98d1SGreg Roach     * @param int  $max_julian_day
1533d2c98d1SGreg Roach     *
15436779af1SGreg Roach     * @return Collection<int,Individual>
1553d2c98d1SGreg Roach     */
1563d2c98d1SGreg Roach    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
1573d2c98d1SGreg Roach    {
1583d2c98d1SGreg Roach        return DB::table('individuals')
1593d2c98d1SGreg Roach            ->join('dates', static function (JoinClause $join): void {
1603d2c98d1SGreg Roach                $join
1613d2c98d1SGreg Roach                    ->on('i_file', '=', 'd_file')
1623d2c98d1SGreg Roach                    ->on('i_id', '=', 'd_gid');
1633d2c98d1SGreg Roach            })
1643d2c98d1SGreg Roach            ->where('i_file', '=', $tree->id())
1653d2c98d1SGreg Roach            ->where('d_fact', '=', '_TODO')
1663d2c98d1SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
1673d2c98d1SGreg Roach            ->distinct()
1680aed7de4SGreg Roach            ->select(['individuals.*'])
1693d2c98d1SGreg Roach            ->get()
1703d2c98d1SGreg Roach            ->map(Registry::individualFactory()->mapper($tree))
1713d2c98d1SGreg Roach            ->filter(GedcomRecord::accessFilter());
1723d2c98d1SGreg Roach    }
1733d2c98d1SGreg Roach
1743d2c98d1SGreg Roach    /**
1753d2c98d1SGreg Roach     * @param Tree $tree
1763d2c98d1SGreg Roach     * @param int  $max_julian_day
1773d2c98d1SGreg Roach     *
17836779af1SGreg Roach     * @return Collection<int,Family>
1793d2c98d1SGreg Roach     */
1803d2c98d1SGreg Roach    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
1813d2c98d1SGreg Roach    {
1823d2c98d1SGreg Roach        return DB::table('families')
1833d2c98d1SGreg Roach            ->join('dates', static function (JoinClause $join): void {
1843d2c98d1SGreg Roach                $join
1853d2c98d1SGreg Roach                    ->on('f_file', '=', 'd_file')
1863d2c98d1SGreg Roach                    ->on('f_id', '=', 'd_gid');
1873d2c98d1SGreg Roach            })
1883d2c98d1SGreg Roach            ->where('f_file', '=', $tree->id())
1893d2c98d1SGreg Roach            ->where('d_fact', '=', '_TODO')
1903d2c98d1SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
1913d2c98d1SGreg Roach            ->distinct()
1920aed7de4SGreg Roach            ->select(['families.*'])
1933d2c98d1SGreg Roach            ->get()
1943d2c98d1SGreg Roach            ->map(Registry::familyFactory()->mapper($tree))
1953d2c98d1SGreg Roach            ->filter(GedcomRecord::accessFilter());
1963d2c98d1SGreg Roach    }
1973d2c98d1SGreg Roach
1983d2c98d1SGreg Roach    /**
1993d2c98d1SGreg Roach     * How should this module be identified in the control panel, etc.?
2003d2c98d1SGreg Roach     *
2013d2c98d1SGreg Roach     * @return string
2023d2c98d1SGreg Roach     */
2033d2c98d1SGreg Roach    public function title(): string
2043d2c98d1SGreg Roach    {
2053d2c98d1SGreg Roach        /* I18N: Name of a module. Tasks that need further research. */
2063d2c98d1SGreg Roach        return I18N::translate('Research tasks');
2073d2c98d1SGreg Roach    }
2083d2c98d1SGreg Roach
2093d2c98d1SGreg Roach    /**
2103caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2113caaa4d2SGreg Roach     *
2123caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2133caaa4d2SGreg Roach     *
2143caaa4d2SGreg Roach     * @return bool
2153caaa4d2SGreg Roach     */
216c1010edaSGreg Roach    public function loadAjax(): bool
217c1010edaSGreg Roach    {
2188c2e8227SGreg Roach        return false;
2198c2e8227SGreg Roach    }
2208c2e8227SGreg Roach
2213caaa4d2SGreg Roach    /**
2223caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2233caaa4d2SGreg Roach     *
2243caaa4d2SGreg Roach     * @return bool
2253caaa4d2SGreg Roach     */
226c1010edaSGreg Roach    public function isUserBlock(): bool
227c1010edaSGreg Roach    {
2288c2e8227SGreg Roach        return true;
2298c2e8227SGreg Roach    }
2308c2e8227SGreg Roach
2313caaa4d2SGreg Roach    /**
2323caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2333caaa4d2SGreg Roach     *
2343caaa4d2SGreg Roach     * @return bool
2353caaa4d2SGreg Roach     */
23663276d8fSGreg Roach    public function isTreeBlock(): bool
237c1010edaSGreg Roach    {
2388c2e8227SGreg Roach        return true;
2398c2e8227SGreg Roach    }
2408c2e8227SGreg Roach
24176692c8bSGreg Roach    /**
242a45f9889SGreg Roach     * Update the configuration for a block.
243a45f9889SGreg Roach     *
2446ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
245a45f9889SGreg Roach     * @param int                    $block_id
246a45f9889SGreg Roach     *
247a45f9889SGreg Roach     * @return void
248a45f9889SGreg Roach     */
2496ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
250a45f9889SGreg Roach    {
251748dbe15SGreg Roach        $show_other      = Validator::parsedBody($request)->boolean('show_other', false);
252748dbe15SGreg Roach        $show_unassigned = Validator::parsedBody($request)->boolean('show_unassigned', false);
253748dbe15SGreg Roach        $show_future     = Validator::parsedBody($request)->boolean('show_future', false);
254b6b9dcc9SGreg Roach
255748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'show_other', (string) $show_other);
256748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'show_unassigned', (string) $show_unassigned);
257748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'show_future', (string) $show_future);
258a45f9889SGreg Roach    }
259a45f9889SGreg Roach
260a45f9889SGreg Roach    /**
26176692c8bSGreg Roach     * An HTML form to edit block settings
26276692c8bSGreg Roach     *
263e490cd80SGreg Roach     * @param Tree $tree
26476692c8bSGreg Roach     * @param int  $block_id
265a9430be8SGreg Roach     *
2663caaa4d2SGreg Roach     * @return string
26776692c8bSGreg Roach     */
2683caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
269c1010edaSGreg Roach    {
27064b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
27164b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
27264b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
2738c2e8227SGreg Roach
2743caaa4d2SGreg Roach        return view('modules/todo/config', [
275c385536dSGreg Roach            'show_future'     => $show_future,
276c385536dSGreg Roach            'show_other'      => $show_other,
277c385536dSGreg Roach            'show_unassigned' => $show_unassigned,
278c385536dSGreg Roach        ]);
2798c2e8227SGreg Roach    }
2808c2e8227SGreg Roach}
281