18c2e8227SGreg Roach<?php 2*3976b470SGreg 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 */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 208c2e8227SGreg Roach 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 238e74ca62SGreg Roachuse Fisharebest\Webtrees\Family; 2464b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 268e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual; 27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 288e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 298e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause; 308e74ca62SGreg Roachuse Illuminate\Support\Collection; 311e7a7a28SGreg Roachuse Illuminate\Support\Str; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 338c2e8227SGreg Roach 348c2e8227SGreg Roach/** 358c2e8227SGreg Roach * Class ResearchTaskModule 368c2e8227SGreg Roach */ 3737eb8894SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface 38c1010edaSGreg Roach{ 3949a243cbSGreg Roach use ModuleBlockTrait; 4049a243cbSGreg Roach 4116d6367aSGreg Roach private const DEFAULT_SHOW_OTHER = '1'; 4216d6367aSGreg Roach private const DEFAULT_SHOW_UNASSIGNED = '1'; 4316d6367aSGreg Roach private const DEFAULT_SHOW_FUTURE = '1'; 4464b9f5b2SGreg Roach 45961ec755SGreg Roach /** 460cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 47961ec755SGreg Roach * 48961ec755SGreg Roach * @return string 49961ec755SGreg Roach */ 5049a243cbSGreg Roach public function title(): string 51c1010edaSGreg Roach { 52bbb76c12SGreg Roach /* I18N: Name of a module. Tasks that need further research. */ 53bbb76c12SGreg Roach return I18N::translate('Research tasks'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 56961ec755SGreg Roach /** 57961ec755SGreg Roach * A sentence describing what this module does. 58961ec755SGreg Roach * 59961ec755SGreg Roach * @return string 60961ec755SGreg Roach */ 6149a243cbSGreg Roach public function description(): string 62c1010edaSGreg Roach { 63bbb76c12SGreg Roach /* I18N: Description of “Research tasks” module */ 64bbb76c12SGreg Roach return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 658c2e8227SGreg Roach } 668c2e8227SGreg Roach 6776692c8bSGreg Roach /** 6876692c8bSGreg Roach * Generate the HTML content of this block. 6976692c8bSGreg Roach * 70e490cd80SGreg Roach * @param Tree $tree 7176692c8bSGreg Roach * @param int $block_id 723caaa4d2SGreg Roach * @param string $context 733caaa4d2SGreg Roach * @param string[] $config 7476692c8bSGreg Roach * 7576692c8bSGreg Roach * @return string 7676692c8bSGreg Roach */ 773caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 78c1010edaSGreg Roach { 7964b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 8064b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 8164b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 828c2e8227SGreg Roach 833caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 848c2e8227SGreg Roach 854459dc9aSGreg Roach $end_jd = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay(); 868e74ca62SGreg Roach $individuals = $this->individualsWithTasks($tree, $end_jd); 878e74ca62SGreg Roach $families = $this->familiesWithTasks($tree, $end_jd); 8864b9f5b2SGreg Roach 898e74ca62SGreg Roach /** @var GedcomRecord[] $records */ 908e74ca62SGreg Roach $records = $individuals->merge($families); 91f7f4b984SGreg Roach 92f7f4b984SGreg Roach $tasks = []; 93f7f4b984SGreg Roach 94f7f4b984SGreg Roach foreach ($records as $record) { 958d0ebef0SGreg Roach foreach ($record->facts(['_TODO']) as $task) { 964852d81fSGreg Roach $user_name = $task->attribute('_WT_USER'); 97f7f4b984SGreg Roach 98e5a6b4d4SGreg Roach if ($user_name === Auth::user()->userName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) { 99f7f4b984SGreg Roach $tasks[] = $task; 100f7f4b984SGreg Roach } 101f7f4b984SGreg Roach } 102f7f4b984SGreg Roach } 103f7f4b984SGreg Roach 104f7f4b984SGreg Roach if (empty($records)) { 105a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 106a078385aSGreg Roach } else { 107147e99aaSGreg Roach $content = view('modules/todo/research-tasks', ['tasks' => $tasks]); 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 1103caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 111147e99aaSGreg Roach return view('modules/block-template', [ 1121e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1139c6524dcSGreg Roach 'id' => $block_id, 1143caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 11549a243cbSGreg Roach 'title' => $this->title(), 1169c6524dcSGreg Roach 'content' => $content, 1179c6524dcSGreg Roach ]); 1188c2e8227SGreg Roach } 119b2ce94c6SRico Sonntag 120b2ce94c6SRico Sonntag return $content; 1218c2e8227SGreg Roach } 1228c2e8227SGreg Roach 1233caaa4d2SGreg Roach /** 1243caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1253caaa4d2SGreg Roach * 1263caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1273caaa4d2SGreg Roach * 1283caaa4d2SGreg Roach * @return bool 1293caaa4d2SGreg Roach */ 130c1010edaSGreg Roach public function loadAjax(): bool 131c1010edaSGreg Roach { 1328c2e8227SGreg Roach return false; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 1353caaa4d2SGreg Roach /** 1363caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1373caaa4d2SGreg Roach * 1383caaa4d2SGreg Roach * @return bool 1393caaa4d2SGreg Roach */ 140c1010edaSGreg Roach public function isUserBlock(): bool 141c1010edaSGreg Roach { 1428c2e8227SGreg Roach return true; 1438c2e8227SGreg Roach } 1448c2e8227SGreg Roach 1453caaa4d2SGreg Roach /** 1463caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 1473caaa4d2SGreg Roach * 1483caaa4d2SGreg Roach * @return bool 1493caaa4d2SGreg Roach */ 15063276d8fSGreg Roach public function isTreeBlock(): bool 151c1010edaSGreg Roach { 1528c2e8227SGreg Roach return true; 1538c2e8227SGreg Roach } 1548c2e8227SGreg Roach 15576692c8bSGreg Roach /** 156a45f9889SGreg Roach * Update the configuration for a block. 157a45f9889SGreg Roach * 1586ccdf4f0SGreg Roach * @param ServerRequestInterface $request 159a45f9889SGreg Roach * @param int $block_id 160a45f9889SGreg Roach * 161a45f9889SGreg Roach * @return void 162a45f9889SGreg Roach */ 1636ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 164a45f9889SGreg Roach { 165b6b9dcc9SGreg Roach $params = $request->getParsedBody(); 166b6b9dcc9SGreg Roach 167b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_other', $params['show_other']); 168b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']); 169b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_future', $params['show_future']); 170a45f9889SGreg Roach } 171a45f9889SGreg Roach 172a45f9889SGreg Roach /** 17376692c8bSGreg Roach * An HTML form to edit block settings 17476692c8bSGreg Roach * 175e490cd80SGreg Roach * @param Tree $tree 17676692c8bSGreg Roach * @param int $block_id 177a9430be8SGreg Roach * 1783caaa4d2SGreg Roach * @return string 17976692c8bSGreg Roach */ 1803caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 181c1010edaSGreg Roach { 18264b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 18364b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 18464b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 1858c2e8227SGreg Roach 1863caaa4d2SGreg Roach return view('modules/todo/config', [ 187c385536dSGreg Roach 'show_future' => $show_future, 188c385536dSGreg Roach 'show_other' => $show_other, 189c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 190c385536dSGreg Roach ]); 1918c2e8227SGreg Roach } 1928e74ca62SGreg Roach 1938e74ca62SGreg Roach /** 1948e74ca62SGreg Roach * @param Tree $tree 1958e74ca62SGreg Roach * @param int $max_julian_day 1968e74ca62SGreg Roach * 1978e74ca62SGreg Roach * @return Collection 1988e74ca62SGreg Roach */ 1998e74ca62SGreg Roach private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 2008e74ca62SGreg Roach { 2018e74ca62SGreg Roach return DB::table('families') 2020b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 2038e74ca62SGreg Roach $join 2048e74ca62SGreg Roach ->on('f_file', '=', 'd_file') 2058e74ca62SGreg Roach ->on('f_id', '=', 'd_gid'); 2068e74ca62SGreg Roach }) 2078e74ca62SGreg Roach ->where('f_file', '=', $tree->id()) 2088e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2098e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 210c0804649SGreg Roach ->select(['families.*']) 211c0804649SGreg Roach ->distinct() 2128e74ca62SGreg Roach ->get() 2134146fabcSGreg Roach ->map(Family::rowMapper()) 2144146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2158e74ca62SGreg Roach } 2168e74ca62SGreg Roach 2178e74ca62SGreg Roach /** 2188e74ca62SGreg Roach * @param Tree $tree 2198e74ca62SGreg Roach * @param int $max_julian_day 2208e74ca62SGreg Roach * 2218e74ca62SGreg Roach * @return Collection 2228e74ca62SGreg Roach */ 2238e74ca62SGreg Roach private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 2248e74ca62SGreg Roach { 2258e74ca62SGreg Roach return DB::table('individuals') 2260b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 2278e74ca62SGreg Roach $join 2288e74ca62SGreg Roach ->on('i_file', '=', 'd_file') 2298e74ca62SGreg Roach ->on('i_id', '=', 'd_gid'); 2308e74ca62SGreg Roach }) 2318e74ca62SGreg Roach ->where('i_file', '=', $tree->id()) 2328e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2338e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 234c0804649SGreg Roach ->select(['individuals.*']) 235c0804649SGreg Roach ->distinct() 2368e74ca62SGreg Roach ->get() 2374146fabcSGreg Roach ->map(Individual::rowMapper()) 2384146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2398e74ca62SGreg Roach } 2408c2e8227SGreg Roach} 241