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