xref: /webtrees/app/Module/ResearchTaskModule.php (revision b5c8fd7e66957665381ee23f19cf39bda22bc768)
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
46961ec755SGreg Roach    /**
470cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
48961ec755SGreg Roach     *
49961ec755SGreg Roach     * @return string
50961ec755SGreg Roach     */
5149a243cbSGreg Roach    public function title(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Name of a module. Tasks that need further research. */
54bbb76c12SGreg Roach        return I18N::translate('Research tasks');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
57961ec755SGreg Roach    /**
58961ec755SGreg Roach     * A sentence describing what this module does.
59961ec755SGreg Roach     *
60961ec755SGreg Roach     * @return string
61961ec755SGreg Roach     */
6249a243cbSGreg Roach    public function description(): string
63c1010edaSGreg Roach    {
64bbb76c12SGreg Roach        /* I18N: Description of “Research tasks” module */
65bbb76c12SGreg Roach        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
668c2e8227SGreg Roach    }
678c2e8227SGreg Roach
6876692c8bSGreg Roach    /**
6976692c8bSGreg Roach     * Generate the HTML content of this block.
7076692c8bSGreg Roach     *
71e490cd80SGreg Roach     * @param Tree     $tree
7276692c8bSGreg Roach     * @param int      $block_id
733caaa4d2SGreg Roach     * @param string   $context
743caaa4d2SGreg Roach     * @param string[] $config
7576692c8bSGreg Roach     *
7676692c8bSGreg Roach     * @return string
7776692c8bSGreg Roach     */
783caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
79c1010edaSGreg Roach    {
8064b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
8164b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
8264b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
838c2e8227SGreg Roach
843caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
858c2e8227SGreg Roach
864459dc9aSGreg Roach        $end_jd      = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay();
878e74ca62SGreg Roach        $individuals = $this->individualsWithTasks($tree, $end_jd);
888e74ca62SGreg Roach        $families    = $this->familiesWithTasks($tree, $end_jd);
8964b9f5b2SGreg Roach
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
98ddeb3354SGreg Roach                if ($user_name === Auth::user()->userName()) {
99ddeb3354SGreg Roach                    // Tasks belonging to us.
100ddeb3354SGreg Roach                    $tasks[] = $task;
101ddeb3354SGreg Roach                } elseif ($user_name === '' && $show_unassigned) {
102ddeb3354SGreg Roach                    // Tasks belonging to nobody.
103ddeb3354SGreg Roach                    $tasks[] = $task;
104ddeb3354SGreg Roach                } elseif ($user_name !== '' && $show_other) {
105ddeb3354SGreg Roach                    // Tasks belonging to others.
106f7f4b984SGreg Roach                    $tasks[] = $task;
107f7f4b984SGreg Roach                }
108f7f4b984SGreg Roach            }
109f7f4b984SGreg Roach        }
110f7f4b984SGreg Roach
111075d1a05SGreg Roach        if ($records->isEmpty()) {
112a078385aSGreg Roach            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
113a078385aSGreg Roach        } else {
114147e99aaSGreg Roach            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
1158c2e8227SGreg Roach        }
1168c2e8227SGreg Roach
1173caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
118147e99aaSGreg Roach            return view('modules/block-template', [
1191e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1209c6524dcSGreg Roach                'id'         => $block_id,
1213caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
12249a243cbSGreg Roach                'title'      => $this->title(),
1239c6524dcSGreg Roach                'content'    => $content,
1249c6524dcSGreg Roach            ]);
1258c2e8227SGreg Roach        }
126b2ce94c6SRico Sonntag
127b2ce94c6SRico Sonntag        return $content;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
1303caaa4d2SGreg Roach    /**
1313caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1323caaa4d2SGreg Roach     *
1333caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1343caaa4d2SGreg Roach     *
1353caaa4d2SGreg Roach     * @return bool
1363caaa4d2SGreg Roach     */
137c1010edaSGreg Roach    public function loadAjax(): bool
138c1010edaSGreg Roach    {
1398c2e8227SGreg Roach        return false;
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
1423caaa4d2SGreg Roach    /**
1433caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1443caaa4d2SGreg Roach     *
1453caaa4d2SGreg Roach     * @return bool
1463caaa4d2SGreg Roach     */
147c1010edaSGreg Roach    public function isUserBlock(): bool
148c1010edaSGreg Roach    {
1498c2e8227SGreg Roach        return true;
1508c2e8227SGreg Roach    }
1518c2e8227SGreg Roach
1523caaa4d2SGreg Roach    /**
1533caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1543caaa4d2SGreg Roach     *
1553caaa4d2SGreg Roach     * @return bool
1563caaa4d2SGreg Roach     */
15763276d8fSGreg Roach    public function isTreeBlock(): bool
158c1010edaSGreg Roach    {
1598c2e8227SGreg Roach        return true;
1608c2e8227SGreg Roach    }
1618c2e8227SGreg Roach
16276692c8bSGreg Roach    /**
163a45f9889SGreg Roach     * Update the configuration for a block.
164a45f9889SGreg Roach     *
1656ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
166a45f9889SGreg Roach     * @param int     $block_id
167a45f9889SGreg Roach     *
168a45f9889SGreg Roach     * @return void
169a45f9889SGreg Roach     */
1706ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
171a45f9889SGreg Roach    {
172b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
173b6b9dcc9SGreg Roach
174b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_other', $params['show_other']);
175b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']);
176b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'show_future', $params['show_future']);
177a45f9889SGreg Roach    }
178a45f9889SGreg Roach
179a45f9889SGreg Roach    /**
18076692c8bSGreg Roach     * An HTML form to edit block settings
18176692c8bSGreg Roach     *
182e490cd80SGreg Roach     * @param Tree $tree
18376692c8bSGreg Roach     * @param int  $block_id
184a9430be8SGreg Roach     *
1853caaa4d2SGreg Roach     * @return string
18676692c8bSGreg Roach     */
1873caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
188c1010edaSGreg Roach    {
18964b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
19064b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
19164b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1928c2e8227SGreg Roach
1933caaa4d2SGreg Roach        return view('modules/todo/config', [
194c385536dSGreg Roach            'show_future'     => $show_future,
195c385536dSGreg Roach            'show_other'      => $show_other,
196c385536dSGreg Roach            'show_unassigned' => $show_unassigned,
197c385536dSGreg Roach        ]);
1988c2e8227SGreg Roach    }
1998e74ca62SGreg Roach
2008e74ca62SGreg Roach    /**
2018e74ca62SGreg Roach     * @param Tree $tree
2028e74ca62SGreg Roach     * @param int  $max_julian_day
2038e74ca62SGreg Roach     *
204*b5c8fd7eSGreg Roach     * @return Collection<Family>
2058e74ca62SGreg Roach     */
2068e74ca62SGreg Roach    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
2078e74ca62SGreg Roach    {
2088e74ca62SGreg Roach        return DB::table('families')
2090b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
2108e74ca62SGreg Roach                $join
2118e74ca62SGreg Roach                    ->on('f_file', '=', 'd_file')
2128e74ca62SGreg Roach                    ->on('f_id', '=', 'd_gid');
2138e74ca62SGreg Roach            })
2148e74ca62SGreg Roach            ->where('f_file', '=', $tree->id())
2158e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
2168e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
217c0804649SGreg Roach            ->select(['families.*'])
218c0804649SGreg Roach            ->distinct()
2198e74ca62SGreg Roach            ->get()
220d5ad3db0SGreg Roach            ->map(Family::rowMapper($tree))
2214146fabcSGreg Roach            ->filter(GedcomRecord::accessFilter());
2228e74ca62SGreg Roach    }
2238e74ca62SGreg Roach
2248e74ca62SGreg Roach    /**
2258e74ca62SGreg Roach     * @param Tree $tree
2268e74ca62SGreg Roach     * @param int  $max_julian_day
2278e74ca62SGreg Roach     *
228*b5c8fd7eSGreg Roach     * @return Collection<Individual>
2298e74ca62SGreg Roach     */
2308e74ca62SGreg Roach    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
2318e74ca62SGreg Roach    {
2328e74ca62SGreg Roach        return DB::table('individuals')
2330b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
2348e74ca62SGreg Roach                $join
2358e74ca62SGreg Roach                    ->on('i_file', '=', 'd_file')
2368e74ca62SGreg Roach                    ->on('i_id', '=', 'd_gid');
2378e74ca62SGreg Roach            })
2388e74ca62SGreg Roach            ->where('i_file', '=', $tree->id())
2398e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
2408e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
241c0804649SGreg Roach            ->select(['individuals.*'])
242c0804649SGreg Roach            ->distinct()
2438e74ca62SGreg Roach            ->get()
244d5ad3db0SGreg Roach            ->map(Individual::rowMapper($tree))
2454146fabcSGreg Roach            ->filter(GedcomRecord::accessFilter());
2468e74ca62SGreg Roach    }
2478c2e8227SGreg Roach}
248