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 46*01221f27SGreg Roach // Pagination 47*01221f27SGreg Roach private const LIMIT_LOW = 10; 48*01221f27SGreg Roach private const LIMIT_HIGH = 20; 49*01221f27SGreg Roach 50961ec755SGreg Roach /** 510cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 52961ec755SGreg Roach * 53961ec755SGreg Roach * @return string 54961ec755SGreg Roach */ 5549a243cbSGreg Roach public function title(): string 56c1010edaSGreg Roach { 57bbb76c12SGreg Roach /* I18N: Name of a module. Tasks that need further research. */ 58bbb76c12SGreg Roach return I18N::translate('Research tasks'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 61961ec755SGreg Roach /** 62961ec755SGreg Roach * A sentence describing what this module does. 63961ec755SGreg Roach * 64961ec755SGreg Roach * @return string 65961ec755SGreg Roach */ 6649a243cbSGreg Roach public function description(): string 67c1010edaSGreg Roach { 68bbb76c12SGreg Roach /* I18N: Description of “Research tasks” module */ 69bbb76c12SGreg Roach return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 708c2e8227SGreg Roach } 718c2e8227SGreg Roach 7276692c8bSGreg Roach /** 7376692c8bSGreg Roach * Generate the HTML content of this block. 7476692c8bSGreg Roach * 75e490cd80SGreg Roach * @param Tree $tree 7676692c8bSGreg Roach * @param int $block_id 773caaa4d2SGreg Roach * @param string $context 783caaa4d2SGreg Roach * @param string[] $config 7976692c8bSGreg Roach * 8076692c8bSGreg Roach * @return string 8176692c8bSGreg Roach */ 823caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 83c1010edaSGreg Roach { 8464b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 8564b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 8664b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 878c2e8227SGreg Roach 883caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 898c2e8227SGreg Roach 904459dc9aSGreg Roach $end_jd = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay(); 918e74ca62SGreg Roach $individuals = $this->individualsWithTasks($tree, $end_jd); 928e74ca62SGreg Roach $families = $this->familiesWithTasks($tree, $end_jd); 9364b9f5b2SGreg Roach 948e74ca62SGreg Roach $records = $individuals->merge($families); 95f7f4b984SGreg Roach 96e24053e5SGreg Roach $tasks = new Collection(); 97f7f4b984SGreg Roach 98f7f4b984SGreg Roach foreach ($records as $record) { 998d0ebef0SGreg Roach foreach ($record->facts(['_TODO']) as $task) { 1004852d81fSGreg Roach $user_name = $task->attribute('_WT_USER'); 101f7f4b984SGreg Roach 102ddeb3354SGreg Roach if ($user_name === Auth::user()->userName()) { 103ddeb3354SGreg Roach // Tasks belonging to us. 104e24053e5SGreg Roach $tasks->add($task); 105ddeb3354SGreg Roach } elseif ($user_name === '' && $show_unassigned) { 106ddeb3354SGreg Roach // Tasks belonging to nobody. 107e24053e5SGreg Roach $tasks->add($task); 108ddeb3354SGreg Roach } elseif ($user_name !== '' && $show_other) { 109ddeb3354SGreg Roach // Tasks belonging to others. 110e24053e5SGreg Roach $tasks->add($task); 111f7f4b984SGreg Roach } 112f7f4b984SGreg Roach } 113f7f4b984SGreg Roach } 114f7f4b984SGreg Roach 115075d1a05SGreg Roach if ($records->isEmpty()) { 116a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 117a078385aSGreg Roach } else { 118e24053e5SGreg Roach $content = view('modules/todo/research-tasks', [ 119*01221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 120*01221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 121e24053e5SGreg Roach 'tasks' => $tasks, 122e24053e5SGreg Roach ]); 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 1253caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 126147e99aaSGreg Roach return view('modules/block-template', [ 1271e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1289c6524dcSGreg Roach 'id' => $block_id, 1293caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 13049a243cbSGreg Roach 'title' => $this->title(), 1319c6524dcSGreg Roach 'content' => $content, 1329c6524dcSGreg Roach ]); 1338c2e8227SGreg Roach } 134b2ce94c6SRico Sonntag 135b2ce94c6SRico Sonntag return $content; 1368c2e8227SGreg Roach } 1378c2e8227SGreg Roach 1383caaa4d2SGreg Roach /** 1393caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1403caaa4d2SGreg Roach * 1413caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1423caaa4d2SGreg Roach * 1433caaa4d2SGreg Roach * @return bool 1443caaa4d2SGreg Roach */ 145c1010edaSGreg Roach public function loadAjax(): bool 146c1010edaSGreg Roach { 1478c2e8227SGreg Roach return false; 1488c2e8227SGreg Roach } 1498c2e8227SGreg Roach 1503caaa4d2SGreg Roach /** 1513caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1523caaa4d2SGreg Roach * 1533caaa4d2SGreg Roach * @return bool 1543caaa4d2SGreg Roach */ 155c1010edaSGreg Roach public function isUserBlock(): bool 156c1010edaSGreg Roach { 1578c2e8227SGreg Roach return true; 1588c2e8227SGreg Roach } 1598c2e8227SGreg Roach 1603caaa4d2SGreg Roach /** 1613caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 1623caaa4d2SGreg Roach * 1633caaa4d2SGreg Roach * @return bool 1643caaa4d2SGreg Roach */ 16563276d8fSGreg Roach public function isTreeBlock(): bool 166c1010edaSGreg Roach { 1678c2e8227SGreg Roach return true; 1688c2e8227SGreg Roach } 1698c2e8227SGreg Roach 17076692c8bSGreg Roach /** 171a45f9889SGreg Roach * Update the configuration for a block. 172a45f9889SGreg Roach * 1736ccdf4f0SGreg Roach * @param ServerRequestInterface $request 174a45f9889SGreg Roach * @param int $block_id 175a45f9889SGreg Roach * 176a45f9889SGreg Roach * @return void 177a45f9889SGreg Roach */ 1786ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 179a45f9889SGreg Roach { 180b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 181b6b9dcc9SGreg Roach 182b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_other', $params['show_other']); 183b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']); 184b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'show_future', $params['show_future']); 185a45f9889SGreg Roach } 186a45f9889SGreg Roach 187a45f9889SGreg Roach /** 18876692c8bSGreg Roach * An HTML form to edit block settings 18976692c8bSGreg Roach * 190e490cd80SGreg Roach * @param Tree $tree 19176692c8bSGreg Roach * @param int $block_id 192a9430be8SGreg Roach * 1933caaa4d2SGreg Roach * @return string 19476692c8bSGreg Roach */ 1953caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 196c1010edaSGreg Roach { 19764b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 19864b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 19964b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 2008c2e8227SGreg Roach 2013caaa4d2SGreg Roach return view('modules/todo/config', [ 202c385536dSGreg Roach 'show_future' => $show_future, 203c385536dSGreg Roach 'show_other' => $show_other, 204c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 205c385536dSGreg Roach ]); 2068c2e8227SGreg Roach } 2078e74ca62SGreg Roach 2088e74ca62SGreg Roach /** 2098e74ca62SGreg Roach * @param Tree $tree 2108e74ca62SGreg Roach * @param int $max_julian_day 2118e74ca62SGreg Roach * 212b5c8fd7eSGreg Roach * @return Collection<Family> 2138e74ca62SGreg Roach */ 2148e74ca62SGreg Roach private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 2158e74ca62SGreg Roach { 2168e74ca62SGreg Roach return DB::table('families') 2170b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 2188e74ca62SGreg Roach $join 2198e74ca62SGreg Roach ->on('f_file', '=', 'd_file') 2208e74ca62SGreg Roach ->on('f_id', '=', 'd_gid'); 2218e74ca62SGreg Roach }) 2228e74ca62SGreg Roach ->where('f_file', '=', $tree->id()) 2238e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2248e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 225c0804649SGreg Roach ->select(['families.*']) 226c0804649SGreg Roach ->distinct() 2278e74ca62SGreg Roach ->get() 228d5ad3db0SGreg Roach ->map(Family::rowMapper($tree)) 2294146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2308e74ca62SGreg Roach } 2318e74ca62SGreg Roach 2328e74ca62SGreg Roach /** 2338e74ca62SGreg Roach * @param Tree $tree 2348e74ca62SGreg Roach * @param int $max_julian_day 2358e74ca62SGreg Roach * 236b5c8fd7eSGreg Roach * @return Collection<Individual> 2378e74ca62SGreg Roach */ 2388e74ca62SGreg Roach private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 2398e74ca62SGreg Roach { 2408e74ca62SGreg Roach return DB::table('individuals') 2410b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 2428e74ca62SGreg Roach $join 2438e74ca62SGreg Roach ->on('i_file', '=', 'd_file') 2448e74ca62SGreg Roach ->on('i_id', '=', 'd_gid'); 2458e74ca62SGreg Roach }) 2468e74ca62SGreg Roach ->where('i_file', '=', $tree->id()) 2478e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2488e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 249c0804649SGreg Roach ->select(['individuals.*']) 250c0804649SGreg Roach ->distinct() 2518e74ca62SGreg Roach ->get() 252d5ad3db0SGreg Roach ->map(Individual::rowMapper($tree)) 2534146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2548e74ca62SGreg Roach } 2558c2e8227SGreg Roach} 256