xref: /webtrees/app/Module/ResearchTaskModule.php (revision e24053e5c68a36a62ca2714caf7fca093e7dc791)
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        $records = $individuals->merge($families);
91
92        $tasks = new Collection();
93
94        foreach ($records as $record) {
95            foreach ($record->facts(['_TODO']) as $task) {
96                $user_name = $task->attribute('_WT_USER');
97
98                if ($user_name === Auth::user()->userName()) {
99                    // Tasks belonging to us.
100                    $tasks->add($task);
101                } elseif ($user_name === '' && $show_unassigned) {
102                    // Tasks belonging to nobody.
103                    $tasks->add($task);
104                } elseif ($user_name !== '' && $show_other) {
105                    // Tasks belonging to others.
106                    $tasks->add($task);
107                }
108            }
109        }
110
111        if ($records->isEmpty()) {
112            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
113        } else {
114            $content = view('modules/todo/research-tasks', [
115                'limit' => 10,
116                'tasks' => $tasks,
117            ]);
118        }
119
120        if ($context !== self::CONTEXT_EMBED) {
121            return view('modules/block-template', [
122                'block'      => Str::kebab($this->name()),
123                'id'         => $block_id,
124                'config_url' => $this->configUrl($tree, $context, $block_id),
125                'title'      => $this->title(),
126                'content'    => $content,
127            ]);
128        }
129
130        return $content;
131    }
132
133    /**
134     * Should this block load asynchronously using AJAX?
135     *
136     * Simple blocks are faster in-line, more complex ones can be loaded later.
137     *
138     * @return bool
139     */
140    public function loadAjax(): bool
141    {
142        return false;
143    }
144
145    /**
146     * Can this block be shown on the user’s home page?
147     *
148     * @return bool
149     */
150    public function isUserBlock(): bool
151    {
152        return true;
153    }
154
155    /**
156     * Can this block be shown on the tree’s home page?
157     *
158     * @return bool
159     */
160    public function isTreeBlock(): bool
161    {
162        return true;
163    }
164
165    /**
166     * Update the configuration for a block.
167     *
168     * @param ServerRequestInterface $request
169     * @param int     $block_id
170     *
171     * @return void
172     */
173    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
174    {
175        $params = (array) $request->getParsedBody();
176
177        $this->setBlockSetting($block_id, 'show_other', $params['show_other']);
178        $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']);
179        $this->setBlockSetting($block_id, 'show_future', $params['show_future']);
180    }
181
182    /**
183     * An HTML form to edit block settings
184     *
185     * @param Tree $tree
186     * @param int  $block_id
187     *
188     * @return string
189     */
190    public function editBlockConfiguration(Tree $tree, int $block_id): string
191    {
192        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
193        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
194        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
195
196        return view('modules/todo/config', [
197            'show_future'     => $show_future,
198            'show_other'      => $show_other,
199            'show_unassigned' => $show_unassigned,
200        ]);
201    }
202
203    /**
204     * @param Tree $tree
205     * @param int  $max_julian_day
206     *
207     * @return Collection<Family>
208     */
209    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
210    {
211        return DB::table('families')
212            ->join('dates', static function (JoinClause $join): void {
213                $join
214                    ->on('f_file', '=', 'd_file')
215                    ->on('f_id', '=', 'd_gid');
216            })
217            ->where('f_file', '=', $tree->id())
218            ->where('d_fact', '=', '_TODO')
219            ->where('d_julianday1', '<', $max_julian_day)
220            ->select(['families.*'])
221            ->distinct()
222            ->get()
223            ->map(Family::rowMapper($tree))
224            ->filter(GedcomRecord::accessFilter());
225    }
226
227    /**
228     * @param Tree $tree
229     * @param int  $max_julian_day
230     *
231     * @return Collection<Individual>
232     */
233    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
234    {
235        return DB::table('individuals')
236            ->join('dates', static function (JoinClause $join): void {
237                $join
238                    ->on('i_file', '=', 'd_file')
239                    ->on('i_id', '=', 'd_gid');
240            })
241            ->where('i_file', '=', $tree->id())
242            ->where('d_fact', '=', '_TODO')
243            ->where('d_julianday1', '<', $max_julian_day)
244            ->select(['individuals.*'])
245            ->distinct()
246            ->get()
247            ->map(Individual::rowMapper($tree))
248            ->filter(GedcomRecord::accessFilter());
249    }
250}
251