18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 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; 2339b152e6SGreg Roachuse Fisharebest\Webtrees\Elements\DateValue; 243d2c98d1SGreg Roachuse Fisharebest\Webtrees\Elements\ResearchTask; 253d2c98d1SGreg Roachuse Fisharebest\Webtrees\Elements\WebtreesUser; 268e74ca62SGreg Roachuse Fisharebest\Webtrees\Family; 2764b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 298e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual; 303d2c98d1SGreg Roachuse Fisharebest\Webtrees\Registry; 31e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 328e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 338e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause; 348e74ca62SGreg Roachuse Illuminate\Support\Collection; 351e7a7a28SGreg Roachuse Illuminate\Support\Str; 366ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 378c2e8227SGreg Roach 38d97083feSGreg Roachuse const PHP_INT_MAX; 39d97083feSGreg 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 51*4118eb07SGreg Roach // 31 DEC 9999 52*4118eb07SGreg Roach private const MAXIMUM_JULIAN_DAY = 5373484; 53*4118eb07SGreg 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')), 6539b152e6SGreg Roach 'FAM:_TODO:DATE' => new DateValue(I18N::translate('Date')), 663d2c98d1SGreg Roach 'FAM:_TODO:_WT_USER' => new WebtreesUser(I18N::translate('User')), 673d2c98d1SGreg Roach 'INDI:_TODO' => new ResearchTask(I18N::translate('Research task')), 6839b152e6SGreg Roach 'INDI:_TODO:DATE' => new DateValue(I18N::translate('Date')), 693d2c98d1SGreg Roach 'INDI:_TODO:_WT_USER' => new WebtreesUser(I18N::translate('User')), 703d2c98d1SGreg Roach ]); 71dbfc3863SGreg Roach 72dbfc3863SGreg Roach Registry::elementFactory()->make('FAM')->subtag('_TODO', '0:M'); 73dbfc3863SGreg Roach Registry::elementFactory()->make('INDI')->subtag('_TODO', '0:M'); 748c2e8227SGreg Roach } 758c2e8227SGreg Roach 76961ec755SGreg Roach /** 77961ec755SGreg Roach * A sentence describing what this module does. 78961ec755SGreg Roach * 79961ec755SGreg Roach * @return string 80961ec755SGreg Roach */ 8149a243cbSGreg Roach public function description(): string 82c1010edaSGreg Roach { 83bbb76c12SGreg Roach /* I18N: Description of “Research tasks” module */ 84bbb76c12SGreg Roach return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 858c2e8227SGreg Roach } 868c2e8227SGreg Roach 8776692c8bSGreg Roach /** 8876692c8bSGreg Roach * Generate the HTML content of this block. 8976692c8bSGreg Roach * 90e490cd80SGreg Roach * @param Tree $tree 9176692c8bSGreg Roach * @param int $block_id 923caaa4d2SGreg Roach * @param string $context 9376d39c55SGreg Roach * @param array<string,string> $config 9476692c8bSGreg Roach * 9576692c8bSGreg Roach * @return string 9676692c8bSGreg Roach */ 973caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 98c1010edaSGreg Roach { 9964b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 10064b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 10164b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 1028c2e8227SGreg Roach 1033caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1048c2e8227SGreg Roach 105*4118eb07SGreg Roach $end_jd = $show_future ? self::MAXIMUM_JULIAN_DAY : Registry::timestampFactory()->now()->julianDay(); 1068e74ca62SGreg Roach $individuals = $this->individualsWithTasks($tree, $end_jd); 1078e74ca62SGreg Roach $families = $this->familiesWithTasks($tree, $end_jd); 10864b9f5b2SGreg Roach 1098e74ca62SGreg Roach $records = $individuals->merge($families); 110f7f4b984SGreg Roach 111e24053e5SGreg Roach $tasks = new Collection(); 112f7f4b984SGreg Roach 113f7f4b984SGreg Roach foreach ($records as $record) { 1148d0ebef0SGreg Roach foreach ($record->facts(['_TODO']) as $task) { 1154852d81fSGreg Roach $user_name = $task->attribute('_WT_USER'); 116f7f4b984SGreg Roach 117ddeb3354SGreg Roach if ($user_name === Auth::user()->userName()) { 118ddeb3354SGreg Roach // Tasks belonging to us. 119e24053e5SGreg Roach $tasks->add($task); 120ddeb3354SGreg Roach } elseif ($user_name === '' && $show_unassigned) { 121ddeb3354SGreg Roach // Tasks belonging to nobody. 122e24053e5SGreg Roach $tasks->add($task); 123ddeb3354SGreg Roach } elseif ($user_name !== '' && $show_other) { 124ddeb3354SGreg Roach // Tasks belonging to others. 125e24053e5SGreg Roach $tasks->add($task); 126f7f4b984SGreg Roach } 127f7f4b984SGreg Roach } 128f7f4b984SGreg Roach } 129f7f4b984SGreg Roach 130075d1a05SGreg Roach if ($records->isEmpty()) { 131a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 132a078385aSGreg Roach } else { 133e24053e5SGreg Roach $content = view('modules/todo/research-tasks', [ 13401221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 13501221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 136e24053e5SGreg Roach 'tasks' => $tasks, 137e24053e5SGreg Roach ]); 1388c2e8227SGreg Roach } 1398c2e8227SGreg Roach 1403caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 141147e99aaSGreg Roach return view('modules/block-template', [ 1421e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1439c6524dcSGreg Roach 'id' => $block_id, 1443caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 14549a243cbSGreg Roach 'title' => $this->title(), 1469c6524dcSGreg Roach 'content' => $content, 1479c6524dcSGreg Roach ]); 1488c2e8227SGreg Roach } 149b2ce94c6SRico Sonntag 150b2ce94c6SRico Sonntag return $content; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 1533caaa4d2SGreg Roach /** 1543d2c98d1SGreg Roach * @param Tree $tree 1553d2c98d1SGreg Roach * @param int $max_julian_day 1563d2c98d1SGreg Roach * 15736779af1SGreg Roach * @return Collection<int,Individual> 1583d2c98d1SGreg Roach */ 1593d2c98d1SGreg Roach private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 1603d2c98d1SGreg Roach { 1613d2c98d1SGreg Roach return DB::table('individuals') 1623d2c98d1SGreg Roach ->join('dates', static function (JoinClause $join): void { 1633d2c98d1SGreg Roach $join 1643d2c98d1SGreg Roach ->on('i_file', '=', 'd_file') 1653d2c98d1SGreg Roach ->on('i_id', '=', 'd_gid'); 1663d2c98d1SGreg Roach }) 1673d2c98d1SGreg Roach ->where('i_file', '=', $tree->id()) 1683d2c98d1SGreg Roach ->where('d_fact', '=', '_TODO') 1693d2c98d1SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 1703d2c98d1SGreg Roach ->select(['individuals.*']) 1713d2c98d1SGreg Roach ->distinct() 1723d2c98d1SGreg Roach ->get() 1733d2c98d1SGreg Roach ->map(Registry::individualFactory()->mapper($tree)) 1743d2c98d1SGreg Roach ->filter(GedcomRecord::accessFilter()); 1753d2c98d1SGreg Roach } 1763d2c98d1SGreg Roach 1773d2c98d1SGreg Roach /** 1783d2c98d1SGreg Roach * @param Tree $tree 1793d2c98d1SGreg Roach * @param int $max_julian_day 1803d2c98d1SGreg Roach * 18136779af1SGreg Roach * @return Collection<int,Family> 1823d2c98d1SGreg Roach */ 1833d2c98d1SGreg Roach private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 1843d2c98d1SGreg Roach { 1853d2c98d1SGreg Roach return DB::table('families') 1863d2c98d1SGreg Roach ->join('dates', static function (JoinClause $join): void { 1873d2c98d1SGreg Roach $join 1883d2c98d1SGreg Roach ->on('f_file', '=', 'd_file') 1893d2c98d1SGreg Roach ->on('f_id', '=', 'd_gid'); 1903d2c98d1SGreg Roach }) 1913d2c98d1SGreg Roach ->where('f_file', '=', $tree->id()) 1923d2c98d1SGreg Roach ->where('d_fact', '=', '_TODO') 1933d2c98d1SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 1943d2c98d1SGreg Roach ->select(['families.*']) 1953d2c98d1SGreg Roach ->distinct() 1963d2c98d1SGreg Roach ->get() 1973d2c98d1SGreg Roach ->map(Registry::familyFactory()->mapper($tree)) 1983d2c98d1SGreg Roach ->filter(GedcomRecord::accessFilter()); 1993d2c98d1SGreg Roach } 2003d2c98d1SGreg Roach 2013d2c98d1SGreg Roach /** 2023d2c98d1SGreg Roach * How should this module be identified in the control panel, etc.? 2033d2c98d1SGreg Roach * 2043d2c98d1SGreg Roach * @return string 2053d2c98d1SGreg Roach */ 2063d2c98d1SGreg Roach public function title(): string 2073d2c98d1SGreg Roach { 2083d2c98d1SGreg Roach /* I18N: Name of a module. Tasks that need further research. */ 2093d2c98d1SGreg Roach return I18N::translate('Research tasks'); 2103d2c98d1SGreg Roach } 2113d2c98d1SGreg Roach 2123d2c98d1SGreg Roach /** 2133caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 2143caaa4d2SGreg Roach * 2153caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 2163caaa4d2SGreg Roach * 2173caaa4d2SGreg Roach * @return bool 2183caaa4d2SGreg Roach */ 219c1010edaSGreg Roach public function loadAjax(): bool 220c1010edaSGreg Roach { 2218c2e8227SGreg Roach return false; 2228c2e8227SGreg Roach } 2238c2e8227SGreg Roach 2243caaa4d2SGreg Roach /** 2253caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2263caaa4d2SGreg Roach * 2273caaa4d2SGreg Roach * @return bool 2283caaa4d2SGreg Roach */ 229c1010edaSGreg Roach public function isUserBlock(): bool 230c1010edaSGreg Roach { 2318c2e8227SGreg Roach return true; 2328c2e8227SGreg Roach } 2338c2e8227SGreg Roach 2343caaa4d2SGreg Roach /** 2353caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2363caaa4d2SGreg Roach * 2373caaa4d2SGreg Roach * @return bool 2383caaa4d2SGreg Roach */ 23963276d8fSGreg Roach public function isTreeBlock(): bool 240c1010edaSGreg Roach { 2418c2e8227SGreg Roach return true; 2428c2e8227SGreg Roach } 2438c2e8227SGreg Roach 24476692c8bSGreg Roach /** 245a45f9889SGreg Roach * Update the configuration for a block. 246a45f9889SGreg Roach * 2476ccdf4f0SGreg Roach * @param ServerRequestInterface $request 248a45f9889SGreg Roach * @param int $block_id 249a45f9889SGreg Roach * 250a45f9889SGreg Roach * @return void 251a45f9889SGreg Roach */ 2526ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 253a45f9889SGreg Roach { 254b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 255b6b9dcc9SGreg Roach 256b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_other', $params['show_other']); 257b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']); 258b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_future', $params['show_future']); 259a45f9889SGreg Roach } 260a45f9889SGreg Roach 261a45f9889SGreg Roach /** 26276692c8bSGreg Roach * An HTML form to edit block settings 26376692c8bSGreg Roach * 264e490cd80SGreg Roach * @param Tree $tree 26576692c8bSGreg Roach * @param int $block_id 266a9430be8SGreg Roach * 2673caaa4d2SGreg Roach * @return string 26876692c8bSGreg Roach */ 2693caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 270c1010edaSGreg Roach { 27164b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 27264b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 27364b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 2748c2e8227SGreg Roach 2753caaa4d2SGreg Roach return view('modules/todo/config', [ 276c385536dSGreg Roach 'show_future' => $show_future, 277c385536dSGreg Roach 'show_other' => $show_other, 278c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 279c385536dSGreg Roach ]); 2808c2e8227SGreg Roach } 2818c2e8227SGreg Roach} 282