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