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