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