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 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Report; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Carbon; 24use Fisharebest\Webtrees\I18N; 25 26use function preg_match; 27use function strtoupper; 28 29/** 30 * Class ReportParserSetup - parse a report.xml file and extract the setup options. 31 */ 32class ReportParserSetup extends ReportParserBase 33{ 34 /** @var array An array of report options/parameters */ 35 private $data = []; 36 37 /** @var string[] An array of input attributes */ 38 private $input; 39 40 /** 41 * Return the parsed data. 42 * 43 * @return array 44 */ 45 public function reportProperties(): array 46 { 47 return $this->data; 48 } 49 50 /** 51 * Handle <report> 52 * 53 * @param string[] $attrs 54 * 55 * @return void 56 */ 57 protected function reportStartHandler(array $attrs): void 58 { 59 $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE; 60 61 $this->data['icon'] = $attrs['icon'] ?? ''; 62 } 63 64 /** 65 * Handle <var var="" /> 66 * 67 * @param string[] $attrs 68 * 69 * @return void 70 */ 71 protected function varStartHandler(array $attrs): void 72 { 73 if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) { 74 $this->text .= I18N::number((int) $match[1]); 75 } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) { 76 $this->text .= I18N::translate($match[1]); 77 } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) { 78 $this->text .= I18N::translateContext($match[1], $match[2]); 79 } else { 80 $this->text .= $attrs['var']; 81 } 82 } 83 84 /** 85 * Handle <title> 86 * 87 * @return void 88 */ 89 protected function titleStartHandler(): void 90 { 91 $this->text = ''; 92 } 93 94 /** 95 * Handle </title> 96 * 97 * @return void 98 */ 99 protected function titleEndHandler(): void 100 { 101 $this->data['title'] = $this->text; 102 103 $this->text = ''; 104 } 105 106 /** 107 * Handle </description> 108 * 109 * @return void 110 */ 111 protected function descriptionEndHandler(): void 112 { 113 $this->data['description'] = $this->text; 114 115 $this->text = ''; 116 } 117 118 /** 119 * Handle <input> 120 * 121 * @param string[] $attrs 122 * 123 * @return void 124 */ 125 protected function inputStartHandler(array $attrs): void 126 { 127 $this->text = ''; 128 $this->input = [ 129 'name' => $attrs['name'] ?? '', 130 'type' => $attrs['type'] ?? '', 131 'lookup' => $attrs['lookup'] ?? '', 132 'options' => $attrs['options'] ?? '', 133 'default' => '', 134 'value' => '', 135 ]; 136 137 if (isset($attrs['default'])) { 138 if ($attrs['default'] === 'NOW') { 139 $date = Carbon::now(); 140 $this->input['default'] = strtoupper($date->format('d M Y')); 141 } else { 142 $match = []; 143 if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) { 144 $date = Carbon::now()->addDays((int) $match[1]); 145 $this->input['default'] = strtoupper($date->format('d M Y')); 146 } else { 147 $this->input['default'] = $attrs['default']; 148 } 149 } 150 } elseif ($attrs['name'] === 'pageSize') { 151 $this->input['default'] = I18N::locale()->territory()->paperSize(); 152 } 153 } 154 155 /** 156 * Handle </input> 157 * 158 * @return void 159 */ 160 protected function inputEndHandler(): void 161 { 162 $this->input['value'] = $this->text; 163 if (!isset($this->data['inputs'])) { 164 $this->data['inputs'] = []; 165 } 166 $this->data['inputs'][] = $this->input; 167 $this->text = ''; 168 } 169} 170