. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Carbon; use Fisharebest\Webtrees\Family; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Tree; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Psr\Http\Message\ServerRequestInterface; /** * Class ResearchTaskModule */ class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface { use ModuleBlockTrait; private const DEFAULT_SHOW_OTHER = '1'; private const DEFAULT_SHOW_UNASSIGNED = '1'; private const DEFAULT_SHOW_FUTURE = '1'; /** * How should this module be identified in the control panel, etc.? * * @return string */ public function title(): string { /* I18N: Name of a module. Tasks that need further research. */ return I18N::translate('Research tasks'); } /** * A sentence describing what this module does. * * @return string */ public function description(): string { /* I18N: Description of “Research tasks” module */ return I18N::translate('A list of tasks and activities that are linked to the family tree.'); } /** * Generate the HTML content of this block. * * @param Tree $tree * @param int $block_id * @param string $context * @param string[] $config * * @return string */ public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string { $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); extract($config, EXTR_OVERWRITE); $end_jd = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay(); $individuals = $this->individualsWithTasks($tree, $end_jd); $families = $this->familiesWithTasks($tree, $end_jd); /** @var GedcomRecord[] $records */ $records = $individuals->merge($families); $tasks = []; foreach ($records as $record) { foreach ($record->facts(['_TODO']) as $task) { $user_name = $task->attribute('_WT_USER'); if ($user_name === Auth::user()->userName()) { // Tasks belonging to us. $tasks[] = $task; } elseif ($user_name === '' && $show_unassigned) { // Tasks belonging to nobody. $tasks[] = $task; } elseif ($user_name !== '' && $show_other) { // Tasks belonging to others. $tasks[] = $task; } } } if (empty($records)) { $content = '
' . I18N::translate('There are no research tasks in this family tree.') . '
'; } else { $content = view('modules/todo/research-tasks', ['tasks' => $tasks]); } if ($context !== self::CONTEXT_EMBED) { return view('modules/block-template', [ 'block' => Str::kebab($this->name()), 'id' => $block_id, 'config_url' => $this->configUrl($tree, $context, $block_id), 'title' => $this->title(), 'content' => $content, ]); } return $content; } /** * Should this block load asynchronously using AJAX? * * Simple blocks are faster in-line, more complex ones can be loaded later. * * @return bool */ public function loadAjax(): bool { return false; } /** * Can this block be shown on the user’s home page? * * @return bool */ public function isUserBlock(): bool { return true; } /** * Can this block be shown on the tree’s home page? * * @return bool */ public function isTreeBlock(): bool { return true; } /** * Update the configuration for a block. * * @param ServerRequestInterface $request * @param int $block_id * * @return void */ public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void { $params = $request->getParsedBody(); $this->setBlockSetting($block_id, 'show_other', $params['show_other']); $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']); $this->setBlockSetting($block_id, 'show_future', $params['show_future']); } /** * An HTML form to edit block settings * * @param Tree $tree * @param int $block_id * * @return string */ public function editBlockConfiguration(Tree $tree, int $block_id): string { $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); return view('modules/todo/config', [ 'show_future' => $show_future, 'show_other' => $show_other, 'show_unassigned' => $show_unassigned, ]); } /** * @param Tree $tree * @param int $max_julian_day * * @return Collection */ private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection { return DB::table('families') ->join('dates', static function (JoinClause $join): void { $join ->on('f_file', '=', 'd_file') ->on('f_id', '=', 'd_gid'); }) ->where('f_file', '=', $tree->id()) ->where('d_fact', '=', '_TODO') ->where('d_julianday1', '<', $max_julian_day) ->select(['families.*']) ->distinct() ->get() ->map(Family::rowMapper($tree)) ->filter(GedcomRecord::accessFilter()); } /** * @param Tree $tree * @param int $max_julian_day * * @return Collection */ private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection { return DB::table('individuals') ->join('dates', static function (JoinClause $join): void { $join ->on('i_file', '=', 'd_file') ->on('i_id', '=', 'd_gid'); }) ->where('i_file', '=', $tree->id()) ->where('d_fact', '=', '_TODO') ->where('d_julianday1', '<', $max_julian_day) ->select(['individuals.*']) ->distinct() ->get() ->map(Individual::rowMapper($tree)) ->filter(GedcomRecord::accessFilter()); } }