1*852ede8cSGreg Roach<?php 2*852ede8cSGreg Roach 3*852ede8cSGreg Roach/** 4*852ede8cSGreg Roach * webtrees: online genealogy 5*852ede8cSGreg Roach * Copyright (C) 2019 webtrees development team 6*852ede8cSGreg Roach * This program is free software: you can redistribute it and/or modify 7*852ede8cSGreg Roach * it under the terms of the GNU General Public License as published by 8*852ede8cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*852ede8cSGreg Roach * (at your option) any later version. 10*852ede8cSGreg Roach * This program is distributed in the hope that it will be useful, 11*852ede8cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*852ede8cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*852ede8cSGreg Roach * GNU General Public License for more details. 14*852ede8cSGreg Roach * You should have received a copy of the GNU General Public License 15*852ede8cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*852ede8cSGreg Roach */ 17*852ede8cSGreg Roach 18*852ede8cSGreg Roachdeclare(strict_types=1); 19*852ede8cSGreg Roach 20*852ede8cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21*852ede8cSGreg Roach 22*852ede8cSGreg Roachuse Fisharebest\Webtrees\I18N; 23*852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree; 24*852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface; 25*852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 26*852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 27*852ede8cSGreg Roach 28*852ede8cSGreg Roachuse function assert; 29*852ede8cSGreg Roach 30*852ede8cSGreg Roach/** 31*852ede8cSGreg Roach * Process a form to create a new source. 32*852ede8cSGreg Roach */ 33*852ede8cSGreg Roachclass CreateSourceModal implements RequestHandlerInterface 34*852ede8cSGreg Roach{ 35*852ede8cSGreg Roach /** 36*852ede8cSGreg Roach * @param ServerRequestInterface $request 37*852ede8cSGreg Roach * 38*852ede8cSGreg Roach * @return ResponseInterface 39*852ede8cSGreg Roach */ 40*852ede8cSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 41*852ede8cSGreg Roach { 42*852ede8cSGreg Roach $tree = $request->getAttribute('tree'); 43*852ede8cSGreg Roach assert($tree instanceof Tree); 44*852ede8cSGreg Roach 45*852ede8cSGreg Roach return response(view('modals/create-source', [ 46*852ede8cSGreg Roach 'tree' => $tree, 47*852ede8cSGreg Roach ])); 48*852ede8cSGreg Roach } 49*852ede8cSGreg Roach 50*852ede8cSGreg Roach /** 51*852ede8cSGreg Roach * @param ServerRequestInterface $request 52*852ede8cSGreg Roach * 53*852ede8cSGreg Roach * @return ResponseInterface 54*852ede8cSGreg Roach */ 55*852ede8cSGreg Roach public function createSourceAction(ServerRequestInterface $request): ResponseInterface 56*852ede8cSGreg Roach { 57*852ede8cSGreg Roach $tree = $request->getAttribute('tree'); 58*852ede8cSGreg Roach assert($tree instanceof Tree); 59*852ede8cSGreg Roach 60*852ede8cSGreg Roach $params = $request->getParsedBody(); 61*852ede8cSGreg Roach $title = $params['source-title']; 62*852ede8cSGreg Roach $abbreviation = $params['source-abbreviation']; 63*852ede8cSGreg Roach $author = $params['source-author']; 64*852ede8cSGreg Roach $publication = $params['source-publication']; 65*852ede8cSGreg Roach $repository = $params['source-repository']; 66*852ede8cSGreg Roach $call_number = $params['source-call-number']; 67*852ede8cSGreg Roach $text = $params['source-text']; 68*852ede8cSGreg Roach $privacy_restriction = $params['privacy-restriction']; 69*852ede8cSGreg Roach $edit_restriction = $params['edit-restriction']; 70*852ede8cSGreg Roach 71*852ede8cSGreg Roach // Fix whitespace 72*852ede8cSGreg Roach $title = trim(preg_replace('/\s+/', ' ', $title)); 73*852ede8cSGreg Roach $abbreviation = trim(preg_replace('/\s+/', ' ', $abbreviation)); 74*852ede8cSGreg Roach $author = trim(preg_replace('/\s+/', ' ', $author)); 75*852ede8cSGreg Roach $publication = trim(preg_replace('/\s+/', ' ', $publication)); 76*852ede8cSGreg Roach $repository = trim(preg_replace('/\s+/', ' ', $repository)); 77*852ede8cSGreg Roach $call_number = trim(preg_replace('/\s+/', ' ', $call_number)); 78*852ede8cSGreg Roach 79*852ede8cSGreg Roach // Convert line endings to GEDDCOM continuations 80*852ede8cSGreg Roach $text = str_replace([ 81*852ede8cSGreg Roach "\r\n", 82*852ede8cSGreg Roach "\r", 83*852ede8cSGreg Roach "\n", 84*852ede8cSGreg Roach ], "\n1 CONT ", $text); 85*852ede8cSGreg Roach 86*852ede8cSGreg Roach $gedcom = "0 @@ SOUR\n\n1 TITL " . $title; 87*852ede8cSGreg Roach 88*852ede8cSGreg Roach if ($abbreviation !== '') { 89*852ede8cSGreg Roach $gedcom .= "\n1 ABBR " . $abbreviation; 90*852ede8cSGreg Roach } 91*852ede8cSGreg Roach 92*852ede8cSGreg Roach if ($author !== '') { 93*852ede8cSGreg Roach $gedcom .= "\n1 AUTH " . $author; 94*852ede8cSGreg Roach } 95*852ede8cSGreg Roach 96*852ede8cSGreg Roach if ($publication !== '') { 97*852ede8cSGreg Roach $gedcom .= "\n1 PUBL " . $publication; 98*852ede8cSGreg Roach } 99*852ede8cSGreg Roach 100*852ede8cSGreg Roach if ($text !== '') { 101*852ede8cSGreg Roach $gedcom .= "\n1 TEXT " . $text; 102*852ede8cSGreg Roach } 103*852ede8cSGreg Roach 104*852ede8cSGreg Roach if ($repository !== '') { 105*852ede8cSGreg Roach $gedcom .= "\n1 REPO @" . $repository . '@'; 106*852ede8cSGreg Roach 107*852ede8cSGreg Roach if ($call_number !== '') { 108*852ede8cSGreg Roach $gedcom .= "\n2 CALN " . $call_number; 109*852ede8cSGreg Roach } 110*852ede8cSGreg Roach } 111*852ede8cSGreg Roach 112*852ede8cSGreg Roach if (in_array($privacy_restriction, ['none', 'privacy', 'confidential'], true)) { 113*852ede8cSGreg Roach $gedcom .= "\n1 RESN " . $privacy_restriction; 114*852ede8cSGreg Roach } 115*852ede8cSGreg Roach 116*852ede8cSGreg Roach if ($edit_restriction === 'locked') { 117*852ede8cSGreg Roach $gedcom .= "\n1 RESN " . $edit_restriction; 118*852ede8cSGreg Roach } 119*852ede8cSGreg Roach 120*852ede8cSGreg Roach $record = $tree->createRecord($gedcom); 121*852ede8cSGreg Roach 122*852ede8cSGreg Roach // id and text are for select2 / autocomplete 123*852ede8cSGreg Roach // html is for interactive modals 124*852ede8cSGreg Roach return response([ 125*852ede8cSGreg Roach 'id' => $record->xref(), 126*852ede8cSGreg Roach 'text' => view('selects/source', [ 127*852ede8cSGreg Roach 'source' => $record, 128*852ede8cSGreg Roach ]), 129*852ede8cSGreg Roach 'html' => view('modals/record-created', [ 130*852ede8cSGreg Roach 'title' => I18N::translate('The source has been created'), 131*852ede8cSGreg Roach 'name' => $record->fullName(), 132*852ede8cSGreg Roach 'url' => $record->url(), 133*852ede8cSGreg Roach ]), 134*852ede8cSGreg Roach ]); 135*852ede8cSGreg Roach } 136*852ede8cSGreg Roach} 137