Passed
Push — master ( 8fa102...0e5196 )
by Greg
06:22
created

InteractiveTreeModule::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
24
use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Individual;
27
use Fisharebest\Webtrees\Menu;
28
use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
29
use Fisharebest\Webtrees\Tree;
30
use InvalidArgumentException;
31
use Psr\Http\Message\ResponseInterface;
32
use Psr\Http\Message\ServerRequestInterface;
33
34
use function assert;
35
36
/**
37
 * Class InteractiveTreeModule
38
 * Tip : you could change the number of generations loaded before ajax calls both in individual page and in treeview page to optimize speed and server load
39
 */
40
class InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
41
{
42
    use ModuleChartTrait;
43
    use ModuleTabTrait;
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 */
53
        return I18N::translate('Interactive tree');
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 the “Interactive tree” module */
64
        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
65
    }
66
67
    /**
68
     * The default position for this tab.  It can be changed in the control panel.
69
     *
70
     * @return int
71
     */
72
    public function defaultTabOrder(): int
73
    {
74
        return 7;
75
    }
76
77
    /**
78
     * Generate the HTML content of this tab.
79
     *
80
     * @param Individual $individual
81
     *
82
     * @return string
83
     */
84
    public function getTabContent(Individual $individual): string
85
    {
86
        $treeview = new TreeView('tvTab');
87
88
        [$html, $js] = $treeview->drawViewport($individual, 3);
89
90
        return view('modules/interactive-tree/tab', [
91
            'html' => $html,
92
            'js'   => $js,
93
        ]);
94
    }
95
96
    /**
97
     * Is this tab empty? If so, we don't always need to display it.
98
     *
99
     * @param Individual $individual
100
     *
101
     * @return bool
102
     */
103
    public function hasTabContent(Individual $individual): bool
104
    {
105
        return true;
106
    }
107
108
    /**
109
     * A greyed out tab has no actual content, but may perhaps have
110
     * options to create content.
111
     *
112
     * @param Individual $individual
113
     *
114
     * @return bool
115
     */
116
    public function isGrayedOut(Individual $individual): bool
117
    {
118
        return false;
119
    }
120
121
    /**
122
     * Can this tab load asynchronously?
123
     *
124
     * @return bool
125
     */
126
    public function canLoadAjax(): bool
127
    {
128
        return true;
129
    }
130
131
    /**
132
     * CSS class for the URL.
133
     *
134
     * @return string
135
     */
136
    public function chartMenuClass(): string
137
    {
138
        return 'menu-chart-tree';
139
    }
140
141
    /**
142
     * Return a menu item for this chart - for use in individual boxes.
143
     *
144
     * @param Individual $individual
145
     *
146
     * @return Menu|null
147
     */
148
    public function chartBoxMenu(Individual $individual): ?Menu
149
    {
150
        return $this->chartMenu($individual);
151
    }
152
153
    /**
154
     * The title for a specific instance of this chart.
155
     *
156
     * @param Individual $individual
157
     *
158
     * @return string
159
     */
160
    public function chartTitle(Individual $individual): string
161
    {
162
        /* I18N: %s is an individual’s name */
163
        return I18N::translate('Interactive tree of %s', $individual->fullName());
164
    }
165
166
    /**
167
     * The URL for this chart.
168
     *
169
     * @param Individual $individual
170
     * @param mixed[]    $parameters
171
     *
172
     * @return string
173
     */
174
    public function chartUrl(Individual $individual, array $parameters = []): string
175
    {
176
        return route('module', [
177
                'module' => $this->name(),
178
                'action' => 'Chart',
179
                'xref'   => $individual->xref(),
180
                'tree'    => $individual->tree()->name(),
181
            ] + $parameters);
182
    }
183
184
    /**
185
     * @param ServerRequestInterface $request
186
     *
187
     * @return ResponseInterface
188
     */
189
    public function getChartAction(ServerRequestInterface $request): ResponseInterface
190
    {
191
        $tree = $request->getAttribute('tree');
192
        assert($tree instanceof Tree, new InvalidArgumentException());
193
194
        $user = $request->getAttribute('user');
195
        $xref = $request->getQueryParams()['xref'];
196
197
        $individual = Individual::getInstance($xref, $tree);
198
199
        Auth::checkIndividualAccess($individual);
200
        Auth::checkComponentAccess($this, 'chart', $tree, $user);
201
202
        $tv = new TreeView('tv');
203
204
        [$html, $js] = $tv->drawViewport($individual, 4);
205
206
        return $this->viewResponse('modules/interactive-tree/page', [
207
            'html'       => $html,
208
            'individual' => $individual,
209
            'js'         => $js,
210
            'module'     => $this->name(),
211
            'title'      => $this->chartTitle($individual),
212
            'tree'       => $tree,
213
        ]);
214
    }
215
216
217
    /**
218
     * @param ServerRequestInterface $request
219
     *
220
     * @return ResponseInterface
221
     */
222
    public function postChartAction(ServerRequestInterface $request): ResponseInterface
223
    {
224
        $tree = $request->getAttribute('tree');
225
        assert($tree instanceof Tree, new InvalidArgumentException());
226
227
        return redirect(route('module', [
228
            'module' => $this->name(),
229
            'action' => 'Chart',
230
            'tree'   => $tree->name(),
231
            'xref'   => $request->getParsedBody()['xref'] ?? '',
232
        ]));
233
    }
234
235
    /**
236
     * @param ServerRequestInterface $request
237
     *
238
     * @return ResponseInterface
239
     */
240
    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
241
    {
242
        $tree       = $request->getAttribute('tree');
243
        $pid        = $request->getQueryParams()['pid'];
244
        $individual = Individual::getInstance($pid, $tree);
245
246
        if ($individual === null) {
247
            throw new IndividualNotFoundException();
248
        }
249
250
        if (!$individual->canShow()) {
251
            throw new IndividualAccessDeniedException();
252
        }
253
254
        $instance = $request->getQueryParams()['instance'];
255
        $treeview = new TreeView($instance);
256
257
        return response($treeview->getDetails($individual));
258
    }
259
260
    /**
261
     * @param ServerRequestInterface $request
262
     *
263
     * @return ResponseInterface
264
     */
265
    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
266
    {
267
        $tree     = $request->getAttribute('tree');
268
        $q        = $request->getQueryParams()['q'];
269
        $instance = $request->getQueryParams()['instance'];
270
        $treeview = new TreeView($instance);
271
272
        return response($treeview->getIndividuals($tree, $q));
273
    }
274
}
275