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