xref: /webtrees/app/Module/ChartsBlockModule.php (revision 35e2e946f342051d77c0da0b3dc48a896b1ed34a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Bootstrap4;
20use Fisharebest\Webtrees\Controller\HourglassController;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\Functions\FunctionsPrint;
24use Fisharebest\Webtrees\Html;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
28use Fisharebest\Webtrees\View;
29
30/**
31 * Class ChartsBlockModule
32 */
33class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface {
34	/** {@inheritdoc} */
35	public function getTitle() {
36		return /* I18N: Name of a module/block */ I18N::translate('Charts');
37	}
38
39	/** {@inheritdoc} */
40	public function getDescription() {
41		return /* I18N: Description of the “Charts” module */ I18N::translate('An alternative way to display charts.');
42	}
43
44	/**
45	 * Generate the HTML content of this block.
46	 *
47	 * @param int      $block_id
48	 * @param bool     $template
49	 * @param string[] $cfg
50	 *
51	 * @return string
52	 */
53	public function getBlock($block_id, $template = true, $cfg = []) {
54		global $WT_TREE, $ctype, $controller;
55
56		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
57		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
58
59		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
60		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
61
62		foreach (['type', 'pid'] as $name) {
63			if (array_key_exists($name, $cfg)) {
64				$$name = $cfg[$name];
65			}
66		}
67
68		$person = Individual::getInstance($pid, $WT_TREE);
69		if (!$person) {
70			$pid = $PEDIGREE_ROOT_ID;
71			$this->setBlockSetting($block_id, 'pid', $pid);
72			$person = Individual::getInstance($pid, $WT_TREE);
73		}
74
75		$title = $this->getTitle();
76
77		if ($person) {
78			$content = '';
79			switch ($type) {
80			case 'pedigree':
81				$title           = I18N::translate('Pedigree of %s', $person->getFullName());
82				$chartController = new HourglassController($person->getXref());
83				$controller->addInlineJavascript($chartController->setupJavascript());
84				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
85				$content .= '<td class="myCharts">';
86				ob_start();
87				FunctionsPrint::printPedigreePerson($person);
88				$content .= ob_get_clean();
89				$content .= '</td>';
90				$content .= '<td>';
91				ob_start();
92				$chartController->printPersonPedigree($person, 1);
93				$content .= ob_get_clean();
94				$content .= '</td>';
95				$content .= '</tr></table>';
96				break;
97			case 'descendants':
98				$title           = I18N::translate('Descendants of %s', $person->getFullName());
99				$chartController = new HourglassController($person->getXref());
100				$controller->addInlineJavascript($chartController->setupJavascript());
101				ob_start();
102				$chartController->printDescendency($person, 1, false);
103				$content .= ob_get_clean();
104				break;
105			case 'hourglass':
106				$title           = I18N::translate('Hourglass chart of %s', $person->getFullName());
107				$chartController = new HourglassController($person->getXref());
108				$controller->addInlineJavascript($chartController->setupJavascript());
109				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
110				$content .= '<td>';
111				ob_start();
112				$chartController->printDescendency($person, 1, false);
113				$content .= ob_get_clean();
114				$content .= '</td>';
115				$content .= '<td>';
116				ob_start();
117				$chartController->printPersonPedigree($person, 1);
118				$content .= ob_get_clean();
119				$content .= '</td>';
120				$content .= '</tr></table>';
121				break;
122			case 'treenav':
123				$title = I18N::translate('Interactive tree of %s', $person->getFullName());
124				$mod   = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
125				$tv    = new TreeView;
126				$content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
127				$content .= '<script src="' . $mod->js() . '"></script>';
128				list($html, $js) = $tv->drawViewport($person, 2);
129				$content .= $html . '<script>' . $js . '</script>';
130				break;
131			}
132		} else {
133			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
134		}
135
136		if ($template) {
137			if ($ctype == 'gedcom' && Auth::isManager($WT_TREE) || $ctype == 'user' && Auth::check()) {
138				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
139			} else {
140				$config_url = '';
141			}
142
143			return View::make('blocks/template', [
144				'block'      => str_replace('_', '-', $this->getName()),
145				'id'         => $block_id,
146				'config_url' => $config_url,
147				'title'      => strip_tags($title),
148				'content'    => $content,
149			]);
150		} else {
151			return $content;
152		}
153	}
154
155	/** {@inheritdoc} */
156	public function loadAjax() {
157		return true;
158	}
159
160	/** {@inheritdoc} */
161	public function isUserBlock() {
162		return true;
163	}
164
165	/** {@inheritdoc} */
166	public function isGedcomBlock() {
167		return true;
168	}
169
170	/**
171	 * An HTML form to edit block settings
172	 *
173	 * @param int $block_id
174	 */
175	public function configureBlock($block_id) {
176		global $WT_TREE, $controller;
177
178		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
179		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
180
181		if (Filter::postBool('save') && Filter::checkCsrf()) {
182			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
183			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
184		}
185
186		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
187		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
188
189		$charts = [
190			'pedigree'    => I18N::translate('Pedigree'),
191			'descendants' => I18N::translate('Descendants'),
192			'hourglass'   => I18N::translate('Hourglass chart'),
193			'treenav'     => I18N::translate('Interactive tree'),
194		];
195		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
196
197		?>
198		<div class="form-group row">
199			<label class="col-sm-3 col-form-label" for="type">
200				<?= I18N::translate('Chart type') ?>
201			</label>
202			<div class="col-sm-9">
203				<?= Bootstrap4::select($charts, $type, ['id' => 'type', 'name' => 'type']) ?>
204			</div>
205		</div>
206		<div class="form-group row">
207			<label class="col-sm-3 col-form-label" for="pid">
208				<label for="pid">
209					<?= I18N::translate('Individual') ?>
210				</label>
211			</label>
212			<div class="col-sm-9">
213				<?= FunctionsEdit::formControlIndividual(Individual::getInstance($pid, $WT_TREE), ['id' => 'pid', 'name' => 'pid']) ?>
214			</div>
215		</div>
216		<?php
217	}
218}
219