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