xref: /webtrees/app/Module/ResearchTaskModule.php (revision c0804649bf60e006885fb046f075cf3008bf7cb9)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
198c2e8227SGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
218e74ca62SGreg Roachuse Fisharebest\Webtrees\Family;
2264b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
248e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
268e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
278e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause;
288e74ca62SGreg Roachuse Illuminate\Support\Collection;
29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class ResearchTaskModule
338c2e8227SGreg Roach */
34c1010edaSGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
3616d6367aSGreg Roach    private const DEFAULT_SHOW_OTHER      = '1';
3716d6367aSGreg Roach    private const DEFAULT_SHOW_UNASSIGNED = '1';
3816d6367aSGreg Roach    private const DEFAULT_SHOW_FUTURE     = '1';
3964b9f5b2SGreg Roach
408c2e8227SGreg Roach    /** {@inheritdoc} */
418f53f488SRico Sonntag    public function getTitle(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Name of a module. Tasks that need further research. */
44bbb76c12SGreg Roach        return I18N::translate('Research tasks');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
478c2e8227SGreg Roach    /** {@inheritdoc} */
488f53f488SRico Sonntag    public function getDescription(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Description of “Research tasks” module */
51bbb76c12SGreg Roach        return I18N::translate('A list of tasks and activities that are linked to the family tree.');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
5476692c8bSGreg Roach    /**
5576692c8bSGreg Roach     * Generate the HTML content of this block.
5676692c8bSGreg Roach     *
57e490cd80SGreg Roach     * @param Tree     $tree
5876692c8bSGreg Roach     * @param int      $block_id
595f2ae573SGreg Roach     * @param string   $ctype
60727f238cSGreg Roach     * @param string[] $cfg
6176692c8bSGreg Roach     *
6276692c8bSGreg Roach     * @return string
6376692c8bSGreg Roach     */
645f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
65c1010edaSGreg Roach    {
6664b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
6764b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
6864b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
698c2e8227SGreg Roach
70c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
718c2e8227SGreg Roach
728c2e8227SGreg Roach        $end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
7364b9f5b2SGreg Roach
748e74ca62SGreg Roach        $individuals = $this->individualsWithTasks($tree, $end_jd);
758e74ca62SGreg Roach        $families    = $this->familiesWithTasks($tree, $end_jd);
7664b9f5b2SGreg Roach
778e74ca62SGreg Roach        /** @var GedcomRecord[] $records */
788e74ca62SGreg Roach        $records = $individuals->merge($families);
79f7f4b984SGreg Roach
80f7f4b984SGreg Roach        $tasks = [];
81f7f4b984SGreg Roach
82f7f4b984SGreg Roach        foreach ($records as $record) {
838d0ebef0SGreg Roach            foreach ($record->facts(['_TODO']) as $task) {
844852d81fSGreg Roach                $user_name = $task->attribute('_WT_USER');
85f7f4b984SGreg Roach
86f7f4b984SGreg Roach                if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
87f7f4b984SGreg Roach                    $tasks[] = $task;
88f7f4b984SGreg Roach                }
89f7f4b984SGreg Roach            }
90f7f4b984SGreg Roach        }
91f7f4b984SGreg Roach
92f7f4b984SGreg Roach        if (empty($records)) {
93a078385aSGreg Roach            $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
94a078385aSGreg Roach        } else {
95147e99aaSGreg Roach            $content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
968c2e8227SGreg Roach        }
978c2e8227SGreg Roach
986a8879feSGreg Roach        if ($ctype !== '') {
99e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
100c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
101c1010edaSGreg Roach                    'block_id' => $block_id,
102aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
103c1010edaSGreg Roach                ]);
104397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
105c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
106c1010edaSGreg Roach                    'block_id' => $block_id,
107aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
108c1010edaSGreg Roach                ]);
1098cbbfdceSGreg Roach            } else {
1108cbbfdceSGreg Roach                $config_url = '';
1118cbbfdceSGreg Roach            }
1128cbbfdceSGreg Roach
113147e99aaSGreg Roach            return view('modules/block-template', [
1149c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1159c6524dcSGreg Roach                'id'         => $block_id,
1168cbbfdceSGreg Roach                'config_url' => $config_url,
1179c6524dcSGreg Roach                'title'      => $this->getTitle(),
1189c6524dcSGreg Roach                'content'    => $content,
1199c6524dcSGreg Roach            ]);
1208c2e8227SGreg Roach        }
121b2ce94c6SRico Sonntag
122b2ce94c6SRico Sonntag        return $content;
1238c2e8227SGreg Roach    }
1248c2e8227SGreg Roach
1258c2e8227SGreg Roach    /** {@inheritdoc} */
126c1010edaSGreg Roach    public function loadAjax(): bool
127c1010edaSGreg Roach    {
1288c2e8227SGreg Roach        return false;
1298c2e8227SGreg Roach    }
1308c2e8227SGreg Roach
1318c2e8227SGreg Roach    /** {@inheritdoc} */
132c1010edaSGreg Roach    public function isUserBlock(): bool
133c1010edaSGreg Roach    {
1348c2e8227SGreg Roach        return true;
1358c2e8227SGreg Roach    }
1368c2e8227SGreg Roach
1378c2e8227SGreg Roach    /** {@inheritdoc} */
138c1010edaSGreg Roach    public function isGedcomBlock(): bool
139c1010edaSGreg Roach    {
1408c2e8227SGreg Roach        return true;
1418c2e8227SGreg Roach    }
1428c2e8227SGreg Roach
14376692c8bSGreg Roach    /**
144a45f9889SGreg Roach     * Update the configuration for a block.
145a45f9889SGreg Roach     *
146a45f9889SGreg Roach     * @param Request $request
147a45f9889SGreg Roach     * @param int     $block_id
148a45f9889SGreg Roach     *
149a45f9889SGreg Roach     * @return void
150a45f9889SGreg Roach     */
151a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
152a45f9889SGreg Roach    {
153a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', ''));
154a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', ''));
155a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', ''));
156a45f9889SGreg Roach    }
157a45f9889SGreg Roach
158a45f9889SGreg Roach    /**
15976692c8bSGreg Roach     * An HTML form to edit block settings
16076692c8bSGreg Roach     *
161e490cd80SGreg Roach     * @param Tree $tree
16276692c8bSGreg Roach     * @param int  $block_id
163a9430be8SGreg Roach     *
164a9430be8SGreg Roach     * @return void
16576692c8bSGreg Roach     */
166a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
167c1010edaSGreg Roach    {
16864b9f5b2SGreg Roach        $show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
16964b9f5b2SGreg Roach        $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
17064b9f5b2SGreg Roach        $show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1718c2e8227SGreg Roach
172147e99aaSGreg Roach        echo view('modules/todo/config', [
173c385536dSGreg Roach            'show_future'     => $show_future,
174c385536dSGreg Roach            'show_other'      => $show_other,
175c385536dSGreg Roach            'show_unassigned' => $show_unassigned,
176c385536dSGreg Roach        ]);
1778c2e8227SGreg Roach    }
1788e74ca62SGreg Roach
1798e74ca62SGreg Roach    /**
1808e74ca62SGreg Roach     * @param Tree $tree
1818e74ca62SGreg Roach     * @param int  $max_julian_day
1828e74ca62SGreg Roach     *
1838e74ca62SGreg Roach     * @return Collection
1848e74ca62SGreg Roach     */
1858e74ca62SGreg Roach    private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection
1868e74ca62SGreg Roach    {
1878e74ca62SGreg Roach        return DB::table('families')
1888e74ca62SGreg Roach            ->join('dates', function (JoinClause $join): void {
1898e74ca62SGreg Roach                $join
1908e74ca62SGreg Roach                    ->on('f_file', '=', 'd_file')
1918e74ca62SGreg Roach                    ->on('f_id', '=', 'd_gid');
1928e74ca62SGreg Roach            })
1938e74ca62SGreg Roach            ->where('f_file', '=', $tree->id())
1948e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
1958e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
196*c0804649SGreg Roach            ->select(['families.*'])
197*c0804649SGreg Roach            ->distinct()
1988e74ca62SGreg Roach            ->get()
199*c0804649SGreg Roach            ->map(Family::rowMapper());
2008e74ca62SGreg Roach    }
2018e74ca62SGreg Roach
2028e74ca62SGreg Roach    /**
2038e74ca62SGreg Roach     * @param Tree $tree
2048e74ca62SGreg Roach     * @param int  $max_julian_day
2058e74ca62SGreg Roach     *
2068e74ca62SGreg Roach     * @return Collection
2078e74ca62SGreg Roach     */
2088e74ca62SGreg Roach    private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection
2098e74ca62SGreg Roach    {
2108e74ca62SGreg Roach        return DB::table('individuals')
2118e74ca62SGreg Roach            ->join('dates', function (JoinClause $join): void {
2128e74ca62SGreg Roach                $join
2138e74ca62SGreg Roach                    ->on('i_file', '=', 'd_file')
2148e74ca62SGreg Roach                    ->on('i_id', '=', 'd_gid');
2158e74ca62SGreg Roach            })
2168e74ca62SGreg Roach            ->where('i_file', '=', $tree->id())
2178e74ca62SGreg Roach            ->where('d_fact', '=', '_TODO')
2188e74ca62SGreg Roach            ->where('d_julianday1', '<', $max_julian_day)
219*c0804649SGreg Roach            ->select(['individuals.*'])
220*c0804649SGreg Roach            ->distinct()
2218e74ca62SGreg Roach            ->get()
2228e74ca62SGreg Roach            ->map(Individual::rowMapper($tree));
2238e74ca62SGreg Roach    }
2248c2e8227SGreg Roach}
225