. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Carbon; use Fisharebest\Webtrees\Elements\ResearchTask; use Fisharebest\Webtrees\Elements\TransmissionDate; use Fisharebest\Webtrees\Elements\WebtreesUser; use Fisharebest\Webtrees\Family; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Registry; 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'; // Pagination private const LIMIT_LOW = 10; private const LIMIT_HIGH = 20; public function boot(): void { Registry::elementFactory()->register([ 'FAM:_TODO' => new ResearchTask(I18N::translate('Research task')), 'FAM:_TODO:DATE' => new TransmissionDate(I18N::translate('Date')), 'FAM:_TODO:_WT_USER' => new WebtreesUser(I18N::translate('User')), 'INDI:_TODO' => new ResearchTask(I18N::translate('Research task')), 'INDI:_TODO:DATE' => new TransmissionDate(I18N::translate('Date')), 'INDI:_TODO:_WT_USER' => new WebtreesUser(I18N::translate('User')), ]); } /** * 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); $records = $individuals->merge($families); $tasks = new Collection(); 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->add($task); } elseif ($user_name === '' && $show_unassigned) { // Tasks belonging to nobody. $tasks->add($task); } elseif ($user_name !== '' && $show_other) { // Tasks belonging to others. $tasks->add($task); } } } if ($records->isEmpty()) { $content = '
' . I18N::translate('There are no research tasks in this family tree.') . '
'; } else { $content = view('modules/todo/research-tasks', [ 'limit_low' => self::LIMIT_LOW, 'limit_high' => self::LIMIT_HIGH, '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; } /** * @param Tree $tree * @param int $max_julian_day * * @return Collection