Passed
Push — master ( 453a3d...5bbfbb )
by Greg
05:14
created

AdminController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Http\Controllers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Family;
24
use Fisharebest\Webtrees\FlashMessages;
25
use Fisharebest\Webtrees\GedcomRecord;
26
use Fisharebest\Webtrees\GedcomTag;
27
use Fisharebest\Webtrees\I18N;
28
use Fisharebest\Webtrees\Individual;
29
use Fisharebest\Webtrees\Media;
30
use Fisharebest\Webtrees\Note;
31
use Fisharebest\Webtrees\Repository;
32
use Fisharebest\Webtrees\Services\TreeService;
33
use Fisharebest\Webtrees\Source;
34
use Fisharebest\Webtrees\Tree;
35
use Illuminate\Database\Capsule\Manager as DB;
36
use Illuminate\Database\Query\Expression;
37
use Psr\Http\Message\ResponseInterface;
38
use Psr\Http\Message\ServerRequestInterface;
39
use stdClass;
40
41
use function assert;
42
43
/**
44
 * Controller for the administration pages
45
 */
46
class AdminController extends AbstractBaseController
47
{
48
    /** @var string */
49
    protected $layout = 'layouts/administration';
50
51
    /** @var TreeService */
52
    private $tree_service;
53
54
    /**
55
     * TreesMenuModule constructor.
56
     *
57
     * @param TreeService $tree_service
58
     */
59
    public function __construct(TreeService $tree_service)
60
    {
61
        $this->tree_service = $tree_service;
62
    }
63
64
    /**
65
     * @param ServerRequestInterface $request
66
     *
67
     * @return ResponseInterface
68
     */
69
    public function treePrivacyEdit(ServerRequestInterface $request): ResponseInterface
70
    {
71
        $tree = $request->getAttribute('tree');
72
        assert($tree instanceof Tree);
73
74
        $title                = e($tree->name()) . ' — ' . I18N::translate('Privacy');
75
        $all_tags             = $this->tagsForPrivacy($tree);
76
        $privacy_constants    = $this->privacyConstants();
77
        $privacy_restrictions = $this->privacyRestrictions($tree);
78
79
        return $this->viewResponse('admin/trees-privacy', [
80
            'all_tags'             => $all_tags,
81
            'count_trees'          => $this->tree_service->all()->count(),
82
            'privacy_constants'    => $privacy_constants,
83
            'privacy_restrictions' => $privacy_restrictions,
84
            'title'                => $title,
85
            'tree'                 => $tree,
86
        ]);
87
    }
88
89
    /**
90
     * @param ServerRequestInterface $request
91
     *
92
     * @return ResponseInterface
93
     */
94
    public function treePrivacyUpdate(ServerRequestInterface $request): ResponseInterface
95
    {
96
        $tree = $request->getAttribute('tree');
97
        assert($tree instanceof Tree);
98
99
        $params = $request->getParsedBody();
100
101
        $delete_default_resn_id = $params['delete'] ?? [];
102
103
        DB::table('default_resn')
104
            ->whereIn('default_resn_id', $delete_default_resn_id)
105
            ->delete();
106
107
        $xrefs     = $params['xref'] ?? [];
108
        $tag_types = $params['tag_type'] ?? [];
109
        $resns     = $params['resn'] ?? [];
110
111
        foreach ($xrefs as $n => $xref) {
112
            $tag_type = $tag_types[$n];
113
            $resn     = $resns[$n];
114
115
            // Delete any existing data
116
            if ($tag_type !== '' && $xref !== '') {
117
                DB::table('default_resn')
118
                    ->where('gedcom_id', '=', $tree->id())
119
                    ->where('tag_type', '=', $tag_type)
120
                    ->where('xref', '=', $xref)
121
                    ->delete();
122
            }
123
124
            if ($tag_type !== '' && $xref === '') {
125
                DB::table('default_resn')
126
                    ->where('gedcom_id', '=', $tree->id())
127
                    ->where('tag_type', '=', $tag_type)
128
                    ->whereNull('xref')
129
                    ->delete();
130
            }
131
132
            if ($tag_type === '' && $xref !== '') {
133
                DB::table('default_resn')
134
                    ->where('gedcom_id', '=', $tree->id())
135
                    ->whereNull('tag_type')
136
                    ->where('xref', '=', $xref)
137
                    ->delete();
138
            }
139
140
            // Add (or update) the new data
141
            if ($tag_type !== '' || $xref !== '') {
142
                DB::table('default_resn')->insert([
143
                    'gedcom_id' => $tree->id(),
144
                    'xref'      => $xref === '' ? null : $xref,
145
                    'tag_type'  => $tag_type === '' ? null : $tag_type,
146
                    'resn'      => $resn,
147
                ]);
148
            }
149
        }
150
151
        $tree->setPreference('HIDE_LIVE_PEOPLE', $params['HIDE_LIVE_PEOPLE']);
152
        $tree->setPreference('KEEP_ALIVE_YEARS_BIRTH', $params['KEEP_ALIVE_YEARS_BIRTH']);
153
        $tree->setPreference('KEEP_ALIVE_YEARS_DEATH', $params['KEEP_ALIVE_YEARS_DEATH']);
154
        $tree->setPreference('MAX_ALIVE_AGE', $params['MAX_ALIVE_AGE']);
155
        $tree->setPreference('REQUIRE_AUTHENTICATION', $params['REQUIRE_AUTHENTICATION']);
156
        $tree->setPreference('SHOW_DEAD_PEOPLE', $params['SHOW_DEAD_PEOPLE']);
157
        $tree->setPreference('SHOW_LIVING_NAMES', $params['SHOW_LIVING_NAMES']);
158
        $tree->setPreference('SHOW_PRIVATE_RELATIONSHIPS', $params['SHOW_PRIVATE_RELATIONSHIPS']);
159
160
        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title()), 'success'));
161
162
        // Coming soon...
163
        $all_trees = $request->getParsedBody()['all_trees'] ?? '';
164
        $new_trees = $request->getParsedBody()['new_trees'] ?? '';
165
166
        if ($all_trees === 'on') {
167
            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.', e($tree->title())), 'success');
168
        }
169
        if ($new_trees === 'on') {
170
            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.', e($tree->title())), 'success');
171
        }
172
173
        return redirect(route('manage-trees', ['tree' => $tree->name()]));
174
    }
175
176
    /**
177
     * Names of our privacy levels
178
     *
179
     * @return array
180
     */
181
    private function privacyConstants(): array
182
    {
183
        return [
184
            'none'         => I18N::translate('Show to visitors'),
185
            'privacy'      => I18N::translate('Show to members'),
186
            'confidential' => I18N::translate('Show to managers'),
187
            'hidden'       => I18N::translate('Hide from everyone'),
188
        ];
189
    }
190
191
    /**
192
     * The current privacy restrictions for a tree.
193
     *
194
     * @param Tree $tree
195
     *
196
     * @return array
197
     */
198
    private function privacyRestrictions(Tree $tree): array
199
    {
200
        return DB::table('default_resn')
201
            ->where('gedcom_id', '=', $tree->id())
202
            ->get()
203
            ->map(static function (stdClass $row) use ($tree): stdClass {
204
                $row->record = null;
205
                $row->label  = '';
206
207
                if ($row->xref !== null) {
208
                    $row->record = GedcomRecord::getInstance($row->xref, $tree);
209
                }
210
211
                if ($row->tag_type) {
212
                    $row->tag_label = GedcomTag::getLabel($row->tag_type);
213
                } else {
214
                    $row->tag_label = '';
215
                }
216
217
                return $row;
218
            })
219
            ->sort(static function (stdClass $x, stdClass $y): int {
220
                return I18N::strcasecmp($x->tag_label, $y->tag_label);
221
            })
222
            ->all();
223
    }
224
225
    /**
226
     * Generate a list of potential problems with the server.
227
     *
228
     * @param Tree $tree
229
     *
230
     * @return string[]
231
     */
232
    private function tagsForPrivacy(Tree $tree): array
233
    {
234
        $tags = array_unique(array_merge(
235
            explode(',', $tree->getPreference('INDI_FACTS_ADD')),
236
            explode(',', $tree->getPreference('INDI_FACTS_UNIQUE')),
237
            explode(',', $tree->getPreference('FAM_FACTS_ADD')),
238
            explode(',', $tree->getPreference('FAM_FACTS_UNIQUE')),
239
            explode(',', $tree->getPreference('NOTE_FACTS_ADD')),
240
            explode(',', $tree->getPreference('NOTE_FACTS_UNIQUE')),
241
            explode(',', $tree->getPreference('SOUR_FACTS_ADD')),
242
            explode(',', $tree->getPreference('SOUR_FACTS_UNIQUE')),
243
            explode(',', $tree->getPreference('REPO_FACTS_ADD')),
244
            explode(',', $tree->getPreference('REPO_FACTS_UNIQUE')),
245
            [
246
                'SOUR',
247
                'REPO',
248
                'OBJE',
249
                '_PRIM',
250
                'NOTE',
251
                'SUBM',
252
                'SUBN',
253
                '_UID',
254
                'CHAN',
255
            ]
256
        ));
257
258
        $all_tags = [];
259
260
        foreach ($tags as $tag) {
261
            if ($tag) {
262
                $all_tags[$tag] = GedcomTag::getLabel($tag);
263
            }
264
        }
265
266
        uasort($all_tags, '\Fisharebest\Webtrees\I18N::strcasecmp');
267
268
        return array_merge(
269
            ['' => I18N::translate('All facts and events')],
270
            $all_tags
271
        );
272
    }
273
}
274