xref: /webtrees/app/Module/ResearchTaskModule.php (revision 9483aecd71c61904a8b3a2a683a942c6571afde6)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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\Family;
25use Fisharebest\Webtrees\GedcomRecord;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Tree;
29use Illuminate\Database\Capsule\Manager as DB;
30use Illuminate\Database\Query\JoinClause;
31use Illuminate\Support\Collection;
32use Illuminate\Support\Str;
33use Psr\Http\Message\ServerRequestInterface;
34
35/**
36 * Class ResearchTaskModule
37 */
38class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
39{
40    use ModuleBlockTrait;
41
42    private const DEFAULT_SHOW_OTHER      = '1';
43    private const DEFAULT_SHOW_UNASSIGNED = '1';
44    private const DEFAULT_SHOW_FUTURE     = '1';
45
46    /**
47     * How should this module be identified in the control panel, etc.?
48     *
49     * @return string
50     */
51    public function title(): string
52    {
53        /* I18N: Name of a module. Tasks that need further research. */
54        return I18N::translate('Research tasks');
55    }
56
57    /**
58     * A sentence describing what this module does.
59     *
60     * @return string
61     */
62    public function description(): string
63    {
64        /* I18N: Description of “Research tasks” module */
65        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
66    }
67
68    /**
69     * Generate the HTML content of this block.
70     *
71     * @param Tree     $tree
72     * @param int      $block_id
73     * @param string   $context
74     * @param string[] $config
75     *
76     * @return string
77     */
78    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
79    {
80        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
81        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
82        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
83
84        extract($config, EXTR_OVERWRITE);
85
86        $end_jd      = $show_future ? Carbon::maxValue()->julianDay() : Carbon::now()->julianDay();
87        $individuals = $this->individualsWithTasks($tree, $end_jd);
88        $families    = $this->familiesWithTasks($tree, $end_jd);
89
90        /** @var GedcomRecord[] $records */
91        $records = $individuals->merge($families);
92
93        $tasks = [];
94
95        foreach ($records as $record) {
96            foreach ($record->facts(['_TODO']) as $task) {
97                $user_name = $task->attribute('_WT_USER');
98
99                if ($user_name === Auth::user()->userName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
100                    $tasks[] = $task;
101                }
102            }
103        }
104
105        if (empty($records)) {
106            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
107        } else {
108            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
109        }
110
111        if ($context !== self::CONTEXT_EMBED) {
112            return view('modules/block-template', [
113                'block'      => Str::kebab($this->name()),
114                'id'         => $block_id,
115                'config_url' => $this->configUrl($tree, $context, $block_id),
116                'title'      => $this->title(),
117                'content'    => $content,
118            ]);
119        }
120
121        return $content;
122    }
123
124    /**
125     * Should this block load asynchronously using AJAX?
126     *
127     * Simple blocks are faster in-line, more complex ones can be loaded later.
128     *
129     * @return bool
130     */
131    public function loadAjax(): bool
132    {
133        return false;
134    }
135
136    /**
137     * Can this block be shown on the user’s home page?
138     *
139     * @return bool
140     */
141    public function isUserBlock(): bool
142    {
143        return true;
144    }
145
146    /**
147     * Can this block be shown on the tree’s home page?
148     *
149     * @return bool
150     */
151    public function isTreeBlock(): bool
152    {
153        return true;
154    }
155
156    /**
157     * Update the configuration for a block.
158     *
159     * @param ServerRequestInterface $request
160     * @param int     $block_id
161     *
162     * @return void
163     */
164    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
165    {
166        $params = $request->getParsedBody();
167
168        $this->setBlockSetting($block_id, 'show_other', $params['show_other']);
169        $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']);
170        $this->setBlockSetting($block_id, 'show_future', $params['show_future']);
171    }
172
173    /**
174     * An HTML form to edit block settings
175     *
176     * @param Tree $tree
177     * @param int  $block_id
178     *
179     * @return string
180     */
181    public function editBlockConfiguration(Tree $tree, int $block_id): string
182    {
183        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
184        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
185        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
186
187        return view('modules/todo/config', [
188            'show_future'     => $show_future,
189            'show_other'      => $show_other,
190            'show_unassigned' => $show_unassigned,
191        ]);
192    }
193
194    /**
195     * @param Tree $tree
196     * @param int  $max_julian_day
197     *
198     * @return Collection
199     */
200    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
201    {
202        return DB::table('families')
203            ->join('dates', static function (JoinClause $join): void {
204                $join
205                    ->on('f_file', '=', 'd_file')
206                    ->on('f_id', '=', 'd_gid');
207            })
208            ->where('f_file', '=', $tree->id())
209            ->where('d_fact', '=', '_TODO')
210            ->where('d_julianday1', '<', $max_julian_day)
211            ->select(['families.*'])
212            ->distinct()
213            ->get()
214            ->map(Family::rowMapper())
215            ->filter(GedcomRecord::accessFilter());
216    }
217
218    /**
219     * @param Tree $tree
220     * @param int  $max_julian_day
221     *
222     * @return Collection
223     */
224    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
225    {
226        return DB::table('individuals')
227            ->join('dates', static function (JoinClause $join): void {
228                $join
229                    ->on('i_file', '=', 'd_file')
230                    ->on('i_id', '=', 'd_gid');
231            })
232            ->where('i_file', '=', $tree->id())
233            ->where('d_fact', '=', '_TODO')
234            ->where('d_julianday1', '<', $max_julian_day)
235            ->select(['individuals.*'])
236            ->distinct()
237            ->get()
238            ->map(Individual::rowMapper())
239            ->filter(GedcomRecord::accessFilter());
240    }
241}
242