xref: /webtrees/app/Http/RequestHandlers/CreateTreeAction.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
15afbc57aSGreg Roach<?php
25afbc57aSGreg Roach
35afbc57aSGreg Roach/**
45afbc57aSGreg Roach * webtrees: online genealogy
55afbc57aSGreg Roach * Copyright (C) 2019 webtrees development team
65afbc57aSGreg Roach * This program is free software: you can redistribute it and/or modify
75afbc57aSGreg Roach * it under the terms of the GNU General Public License as published by
85afbc57aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
95afbc57aSGreg Roach * (at your option) any later version.
105afbc57aSGreg Roach * This program is distributed in the hope that it will be useful,
115afbc57aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
125afbc57aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135afbc57aSGreg Roach * GNU General Public License for more details.
145afbc57aSGreg Roach * You should have received a copy of the GNU General Public License
155afbc57aSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
165afbc57aSGreg Roach */
17fcfa147eSGreg Roach
185afbc57aSGreg Roachdeclare(strict_types=1);
195afbc57aSGreg Roach
205afbc57aSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
215afbc57aSGreg Roach
225afbc57aSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
235afbc57aSGreg Roachuse Fisharebest\Webtrees\Http\Controllers\AbstractBaseController;
245afbc57aSGreg Roachuse Fisharebest\Webtrees\I18N;
255afbc57aSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
265afbc57aSGreg Roachuse Fisharebest\Webtrees\Tree;
275afbc57aSGreg Roachuse Psr\Http\Message\ResponseInterface;
285afbc57aSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
295afbc57aSGreg Roach
305afbc57aSGreg Roachuse function e;
315afbc57aSGreg Roachuse function redirect;
325afbc57aSGreg Roachuse function route;
335afbc57aSGreg Roach
345afbc57aSGreg Roach/**
355afbc57aSGreg Roach * Create a new tree.
365afbc57aSGreg Roach */
375afbc57aSGreg Roachclass CreateTreeAction extends AbstractBaseController
385afbc57aSGreg Roach{
395afbc57aSGreg Roach    /** @var TreeService */
405afbc57aSGreg Roach    private $tree_service;
415afbc57aSGreg Roach
425afbc57aSGreg Roach    /**
435afbc57aSGreg Roach     * CreateTreePage constructor.
445afbc57aSGreg Roach     *
455afbc57aSGreg Roach     * @param TreeService $tree_service
465afbc57aSGreg Roach     */
475afbc57aSGreg Roach    public function __construct(TreeService $tree_service)
485afbc57aSGreg Roach    {
495afbc57aSGreg Roach        $this->tree_service = $tree_service;
505afbc57aSGreg Roach    }
515afbc57aSGreg Roach
525afbc57aSGreg Roach    /**
535afbc57aSGreg Roach     * @param ServerRequestInterface $request
545afbc57aSGreg Roach     *
555afbc57aSGreg Roach     * @return ResponseInterface
565afbc57aSGreg Roach     */
575afbc57aSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
585afbc57aSGreg Roach    {
59*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
60*b46c87bdSGreg Roach        $name   = $params['name'];
61*b46c87bdSGreg Roach        $title  = $params['title'];
625afbc57aSGreg Roach
635afbc57aSGreg Roach        if ($this->tree_service->all()->get($name) instanceof Tree) {
645afbc57aSGreg Roach            FlashMessages::addMessage(I18N::translate('The family tree “%s” already exists.', e($name)), 'danger');
655afbc57aSGreg Roach
665afbc57aSGreg Roach            return redirect(route(CreateTreePage::class, ['title' => $title]));
675afbc57aSGreg Roach        }
685afbc57aSGreg Roach
695afbc57aSGreg Roach        $tree = $this->tree_service->create($name, $title);
705afbc57aSGreg Roach
715afbc57aSGreg Roach        FlashMessages::addMessage(I18N::translate('The family tree “%s” has been created.', e($name)), 'success');
725afbc57aSGreg Roach
730c0910bfSGreg Roach        return redirect(route('manage-trees', ['tree' => $tree->name()]));
745afbc57aSGreg Roach    }
755afbc57aSGreg Roach}
76