xref: /webtrees/app/Module/ResearchTaskModule.php (revision cf7c85896a54223772c76826381a9151d4a21e10)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Carbon;
24use Fisharebest\Webtrees\Factory;
25use Fisharebest\Webtrees\Family;
26use Fisharebest\Webtrees\GedcomRecord;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Individual;
29use Fisharebest\Webtrees\Tree;
30use Illuminate\Database\Capsule\Manager as DB;
31use Illuminate\Database\Query\JoinClause;
32use Illuminate\Support\Collection;
33use Illuminate\Support\Str;
34use Psr\Http\Message\ServerRequestInterface;
35
36/**
37 * Class ResearchTaskModule
38 */
39class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
40{
41    use ModuleBlockTrait;
42
43    private const DEFAULT_SHOW_OTHER      = '1';
44    private const DEFAULT_SHOW_UNASSIGNED = '1';
45    private const DEFAULT_SHOW_FUTURE     = '1';
46
47    // Pagination
48    private const LIMIT_LOW  = 10;
49    private const LIMIT_HIGH = 20;
50
51    /**
52     * How should this module be identified in the control panel, etc.?
53     *
54     * @return string
55     */
56    public function title(): string
57    {
58        /* I18N: Name of a module. Tasks that need further research. */
59        return I18N::translate('Research tasks');
60    }
61
62    /**
63     * A sentence describing what this module does.
64     *
65     * @return string
66     */
67    public function description(): string
68    {
69        /* I18N: Description of “Research tasks” module */
70        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
71    }
72
73    /**
74     * Generate the HTML content of this block.
75     *
76     * @param Tree     $tree
77     * @param int      $block_id
78     * @param string   $context
79     * @param string[] $config
80     *
81     * @return string
82     */
83    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
84    {
85        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
86        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
87        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
88
89        extract($config, EXTR_OVERWRITE);
90
91        $end_jd      = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay();
92        $individuals = $this->individualsWithTasks($tree, $end_jd);
93        $families    = $this->familiesWithTasks($tree, $end_jd);
94
95        $records = $individuals->merge($families);
96
97        $tasks = new Collection();
98
99        foreach ($records as $record) {
100            foreach ($record->facts(['_TODO']) as $task) {
101                $user_name = $task->attribute('_WT_USER');
102
103                if ($user_name === Auth::user()->userName()) {
104                    // Tasks belonging to us.
105                    $tasks->add($task);
106                } elseif ($user_name === '' && $show_unassigned) {
107                    // Tasks belonging to nobody.
108                    $tasks->add($task);
109                } elseif ($user_name !== '' && $show_other) {
110                    // Tasks belonging to others.
111                    $tasks->add($task);
112                }
113            }
114        }
115
116        if ($records->isEmpty()) {
117            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
118        } else {
119            $content = view('modules/todo/research-tasks', [
120                'limit_low'  => self::LIMIT_LOW,
121                'limit_high' => self::LIMIT_HIGH,
122                'tasks'      => $tasks,
123            ]);
124        }
125
126        if ($context !== self::CONTEXT_EMBED) {
127            return view('modules/block-template', [
128                'block'      => Str::kebab($this->name()),
129                'id'         => $block_id,
130                'config_url' => $this->configUrl($tree, $context, $block_id),
131                'title'      => $this->title(),
132                'content'    => $content,
133            ]);
134        }
135
136        return $content;
137    }
138
139    /**
140     * Should this block load asynchronously using AJAX?
141     *
142     * Simple blocks are faster in-line, more complex ones can be loaded later.
143     *
144     * @return bool
145     */
146    public function loadAjax(): bool
147    {
148        return false;
149    }
150
151    /**
152     * Can this block be shown on the user’s home page?
153     *
154     * @return bool
155     */
156    public function isUserBlock(): bool
157    {
158        return true;
159    }
160
161    /**
162     * Can this block be shown on the tree’s home page?
163     *
164     * @return bool
165     */
166    public function isTreeBlock(): bool
167    {
168        return true;
169    }
170
171    /**
172     * Update the configuration for a block.
173     *
174     * @param ServerRequestInterface $request
175     * @param int     $block_id
176     *
177     * @return void
178     */
179    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
180    {
181        $params = (array) $request->getParsedBody();
182
183        $this->setBlockSetting($block_id, 'show_other', $params['show_other']);
184        $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']);
185        $this->setBlockSetting($block_id, 'show_future', $params['show_future']);
186    }
187
188    /**
189     * An HTML form to edit block settings
190     *
191     * @param Tree $tree
192     * @param int  $block_id
193     *
194     * @return string
195     */
196    public function editBlockConfiguration(Tree $tree, int $block_id): string
197    {
198        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
199        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
200        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
201
202        return view('modules/todo/config', [
203            'show_future'     => $show_future,
204            'show_other'      => $show_other,
205            'show_unassigned' => $show_unassigned,
206        ]);
207    }
208
209    /**
210     * @param Tree $tree
211     * @param int  $max_julian_day
212     *
213     * @return Collection<Family>
214     */
215    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
216    {
217        return DB::table('families')
218            ->join('dates', static function (JoinClause $join): void {
219                $join
220                    ->on('f_file', '=', 'd_file')
221                    ->on('f_id', '=', 'd_gid');
222            })
223            ->where('f_file', '=', $tree->id())
224            ->where('d_fact', '=', '_TODO')
225            ->where('d_julianday1', '<', $max_julian_day)
226            ->select(['families.*'])
227            ->distinct()
228            ->get()
229            ->map(Factory::family()->mapper($tree))
230            ->filter(GedcomRecord::accessFilter());
231    }
232
233    /**
234     * @param Tree $tree
235     * @param int  $max_julian_day
236     *
237     * @return Collection<Individual>
238     */
239    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
240    {
241        return DB::table('individuals')
242            ->join('dates', static function (JoinClause $join): void {
243                $join
244                    ->on('i_file', '=', 'd_file')
245                    ->on('i_id', '=', 'd_gid');
246            })
247            ->where('i_file', '=', $tree->id())
248            ->where('d_fact', '=', '_TODO')
249            ->where('d_julianday1', '<', $max_julian_day)
250            ->select(['individuals.*'])
251            ->distinct()
252            ->get()
253            ->map(Factory::individual()->mapper($tree))
254            ->filter(GedcomRecord::accessFilter());
255    }
256}
257