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