18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 198c2e8227SGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 228e74ca62SGreg Roachuse Fisharebest\Webtrees\Family; 2364b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 258e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual; 26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 278e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 288e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause; 298e74ca62SGreg Roachuse Illuminate\Support\Collection; 30a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 318c2e8227SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class ResearchTaskModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 3916d6367aSGreg Roach private const DEFAULT_SHOW_OTHER = '1'; 4016d6367aSGreg Roach private const DEFAULT_SHOW_UNASSIGNED = '1'; 4116d6367aSGreg Roach private const DEFAULT_SHOW_FUTURE = '1'; 4264b9f5b2SGreg Roach 43961ec755SGreg Roach /** 44*0cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 45961ec755SGreg Roach * 46961ec755SGreg Roach * @return string 47961ec755SGreg Roach */ 4849a243cbSGreg Roach public function title(): string 49c1010edaSGreg Roach { 50bbb76c12SGreg Roach /* I18N: Name of a module. Tasks that need further research. */ 51bbb76c12SGreg Roach return I18N::translate('Research tasks'); 528c2e8227SGreg Roach } 538c2e8227SGreg Roach 54961ec755SGreg Roach /** 55961ec755SGreg Roach * A sentence describing what this module does. 56961ec755SGreg Roach * 57961ec755SGreg Roach * @return string 58961ec755SGreg Roach */ 5949a243cbSGreg Roach public function description(): string 60c1010edaSGreg Roach { 61bbb76c12SGreg Roach /* I18N: Description of “Research tasks” module */ 62bbb76c12SGreg Roach return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 6576692c8bSGreg Roach /** 6676692c8bSGreg Roach * Generate the HTML content of this block. 6776692c8bSGreg Roach * 68e490cd80SGreg Roach * @param Tree $tree 6976692c8bSGreg Roach * @param int $block_id 705f2ae573SGreg Roach * @param string $ctype 71727f238cSGreg Roach * @param string[] $cfg 7276692c8bSGreg Roach * 7376692c8bSGreg Roach * @return string 7476692c8bSGreg Roach */ 755f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 76c1010edaSGreg Roach { 7764b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 7864b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 7964b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 808c2e8227SGreg Roach 81c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 828c2e8227SGreg Roach 834459dc9aSGreg Roach $end_jd = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay(); 848e74ca62SGreg Roach $individuals = $this->individualsWithTasks($tree, $end_jd); 858e74ca62SGreg Roach $families = $this->familiesWithTasks($tree, $end_jd); 8664b9f5b2SGreg Roach 878e74ca62SGreg Roach /** @var GedcomRecord[] $records */ 888e74ca62SGreg Roach $records = $individuals->merge($families); 89f7f4b984SGreg Roach 90f7f4b984SGreg Roach $tasks = []; 91f7f4b984SGreg Roach 92f7f4b984SGreg Roach foreach ($records as $record) { 938d0ebef0SGreg Roach foreach ($record->facts(['_TODO']) as $task) { 944852d81fSGreg Roach $user_name = $task->attribute('_WT_USER'); 95f7f4b984SGreg Roach 96e5a6b4d4SGreg Roach if ($user_name === Auth::user()->userName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) { 97f7f4b984SGreg Roach $tasks[] = $task; 98f7f4b984SGreg Roach } 99f7f4b984SGreg Roach } 100f7f4b984SGreg Roach } 101f7f4b984SGreg Roach 102f7f4b984SGreg Roach if (empty($records)) { 103a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 104a078385aSGreg Roach } else { 105147e99aaSGreg Roach $content = view('modules/todo/research-tasks', ['tasks' => $tasks]); 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach 1086a8879feSGreg Roach if ($ctype !== '') { 109e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 110c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 111c1010edaSGreg Roach 'block_id' => $block_id, 112aa6f03bbSGreg Roach 'ged' => $tree->name(), 113c1010edaSGreg Roach ]); 114397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 115c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 116c1010edaSGreg Roach 'block_id' => $block_id, 117aa6f03bbSGreg Roach 'ged' => $tree->name(), 118c1010edaSGreg Roach ]); 1198cbbfdceSGreg Roach } else { 1208cbbfdceSGreg Roach $config_url = ''; 1218cbbfdceSGreg Roach } 1228cbbfdceSGreg Roach 123147e99aaSGreg Roach return view('modules/block-template', [ 12426684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1259c6524dcSGreg Roach 'id' => $block_id, 1268cbbfdceSGreg Roach 'config_url' => $config_url, 12749a243cbSGreg Roach 'title' => $this->title(), 1289c6524dcSGreg Roach 'content' => $content, 1299c6524dcSGreg Roach ]); 1308c2e8227SGreg Roach } 131b2ce94c6SRico Sonntag 132b2ce94c6SRico Sonntag return $content; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 1358c2e8227SGreg Roach /** {@inheritdoc} */ 136c1010edaSGreg Roach public function loadAjax(): bool 137c1010edaSGreg Roach { 1388c2e8227SGreg Roach return false; 1398c2e8227SGreg Roach } 1408c2e8227SGreg Roach 1418c2e8227SGreg Roach /** {@inheritdoc} */ 142c1010edaSGreg Roach public function isUserBlock(): bool 143c1010edaSGreg Roach { 1448c2e8227SGreg Roach return true; 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 1478c2e8227SGreg Roach /** {@inheritdoc} */ 14863276d8fSGreg Roach public function isTreeBlock(): bool 149c1010edaSGreg Roach { 1508c2e8227SGreg Roach return true; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 15376692c8bSGreg Roach /** 154a45f9889SGreg Roach * Update the configuration for a block. 155a45f9889SGreg Roach * 156a45f9889SGreg Roach * @param Request $request 157a45f9889SGreg Roach * @param int $block_id 158a45f9889SGreg Roach * 159a45f9889SGreg Roach * @return void 160a45f9889SGreg Roach */ 161a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 162a45f9889SGreg Roach { 163a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', '')); 164a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', '')); 165a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', '')); 166a45f9889SGreg Roach } 167a45f9889SGreg Roach 168a45f9889SGreg Roach /** 16976692c8bSGreg Roach * An HTML form to edit block settings 17076692c8bSGreg Roach * 171e490cd80SGreg Roach * @param Tree $tree 17276692c8bSGreg Roach * @param int $block_id 173a9430be8SGreg Roach * 174a9430be8SGreg Roach * @return void 17576692c8bSGreg Roach */ 176a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 177c1010edaSGreg Roach { 17864b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 17964b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 18064b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 1818c2e8227SGreg Roach 182147e99aaSGreg Roach echo view('modules/todo/config', [ 183c385536dSGreg Roach 'show_future' => $show_future, 184c385536dSGreg Roach 'show_other' => $show_other, 185c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 186c385536dSGreg Roach ]); 1878c2e8227SGreg Roach } 1888e74ca62SGreg Roach 1898e74ca62SGreg Roach /** 1908e74ca62SGreg Roach * @param Tree $tree 1918e74ca62SGreg Roach * @param int $max_julian_day 1928e74ca62SGreg Roach * 1938e74ca62SGreg Roach * @return Collection 1948e74ca62SGreg Roach */ 1958e74ca62SGreg Roach private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 1968e74ca62SGreg Roach { 1978e74ca62SGreg Roach return DB::table('families') 1988e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 1998e74ca62SGreg Roach $join 2008e74ca62SGreg Roach ->on('f_file', '=', 'd_file') 2018e74ca62SGreg Roach ->on('f_id', '=', 'd_gid'); 2028e74ca62SGreg Roach }) 2038e74ca62SGreg Roach ->where('f_file', '=', $tree->id()) 2048e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2058e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 206c0804649SGreg Roach ->select(['families.*']) 207c0804649SGreg Roach ->distinct() 2088e74ca62SGreg Roach ->get() 2094146fabcSGreg Roach ->map(Family::rowMapper()) 2104146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2118e74ca62SGreg Roach } 2128e74ca62SGreg Roach 2138e74ca62SGreg Roach /** 2148e74ca62SGreg Roach * @param Tree $tree 2158e74ca62SGreg Roach * @param int $max_julian_day 2168e74ca62SGreg Roach * 2178e74ca62SGreg Roach * @return Collection 2188e74ca62SGreg Roach */ 2198e74ca62SGreg Roach private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 2208e74ca62SGreg Roach { 2218e74ca62SGreg Roach return DB::table('individuals') 2228e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 2238e74ca62SGreg Roach $join 2248e74ca62SGreg Roach ->on('i_file', '=', 'd_file') 2258e74ca62SGreg Roach ->on('i_id', '=', 'd_gid'); 2268e74ca62SGreg Roach }) 2278e74ca62SGreg Roach ->where('i_file', '=', $tree->id()) 2288e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2298e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 230c0804649SGreg Roach ->select(['individuals.*']) 231c0804649SGreg Roach ->distinct() 2328e74ca62SGreg Roach ->get() 2334146fabcSGreg Roach ->map(Individual::rowMapper()) 2344146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2358e74ca62SGreg Roach } 2368c2e8227SGreg Roach} 237