xref: /webtrees/app/Module/ChartsBlockModule.php (revision 24ec66ce7e77188cd2495b0f8d4dd0ae6e8c9c52)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
188c2e8227SGreg Roach
198c2e8227SGreg Roach/**
208c2e8227SGreg Roach * Class ChartsBlockModule
218c2e8227SGreg Roach */
228c2e8227SGreg Roachclass ChartsBlockModule extends Module implements ModuleBlockInterface {
238c2e8227SGreg Roach	/** {@inheritdoc} */
248c2e8227SGreg Roach	public function getTitle() {
258c2e8227SGreg Roach		return /* I18N: Name of a module/block */ I18N::translate('Charts');
268c2e8227SGreg Roach	}
278c2e8227SGreg Roach
288c2e8227SGreg Roach	/** {@inheritdoc} */
298c2e8227SGreg Roach	public function getDescription() {
308c2e8227SGreg Roach		return /* I18N: Description of the “Charts” module */ I18N::translate('An alternative way to display charts.');
318c2e8227SGreg Roach	}
328c2e8227SGreg Roach
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getBlock($block_id, $template = true, $cfg = null) {
358c2e8227SGreg Roach		global $WT_TREE, $ctype, $controller;
368c2e8227SGreg Roach
378c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
384b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
398c2e8227SGreg Roach
408c2e8227SGreg Roach		$details = get_block_setting($block_id, 'details', '0');
418c2e8227SGreg Roach		$type    = get_block_setting($block_id, 'type', 'pedigree');
424b9ff166SGreg Roach		$pid     = get_block_setting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
438c2e8227SGreg Roach
448c2e8227SGreg Roach		if ($cfg) {
458c2e8227SGreg Roach			foreach (array('details', 'type', 'pid', 'block') as $name) {
468c2e8227SGreg Roach				if (array_key_exists($name, $cfg)) {
478c2e8227SGreg Roach					$$name = $cfg[$name];
488c2e8227SGreg Roach				}
498c2e8227SGreg Roach			}
508c2e8227SGreg Roach		}
518c2e8227SGreg Roach
52*24ec66ceSGreg Roach		$person = Individual::getInstance($pid, $WT_TREE);
538c2e8227SGreg Roach		if (!$person) {
548c2e8227SGreg Roach			$pid = $PEDIGREE_ROOT_ID;
558c2e8227SGreg Roach			set_block_setting($block_id, 'pid', $pid);
56*24ec66ceSGreg Roach			$person = Individual::getInstance($pid, $WT_TREE);
578c2e8227SGreg Roach		}
588c2e8227SGreg Roach
598c2e8227SGreg Roach		$id = $this->getName() . $block_id;
608c2e8227SGreg Roach		$class = $this->getName() . '_block';
614b9ff166SGreg Roach		if ($ctype == 'gedcom' && Auth::isManager($WT_TREE) || $ctype == 'user' && Auth::check()) {
628c2e8227SGreg Roach			$title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>';
638c2e8227SGreg Roach		} else {
648c2e8227SGreg Roach			$title = '';
658c2e8227SGreg Roach		}
668c2e8227SGreg Roach
678c2e8227SGreg Roach		if ($person) {
688c2e8227SGreg Roach			$content = '<table cellspacing="0" cellpadding="0" border="0"><tr>';
698c2e8227SGreg Roach			switch ($type) {
708c2e8227SGreg Roach			case 'pedigree':
718c2e8227SGreg Roach				$title .= I18N::translate('Pedigree of %s', $person->getFullName());
728c2e8227SGreg Roach				$chartController = new HourglassController($person->getXref(), $details, false);
738c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
748c2e8227SGreg Roach				$content .= '<td valign="middle">';
758c2e8227SGreg Roach				ob_start();
768c2e8227SGreg Roach				print_pedigree_person($person, $details);
778c2e8227SGreg Roach				$content .= ob_get_clean();
788c2e8227SGreg Roach				$content .= '</td>';
798c2e8227SGreg Roach				$content .= '<td valign="middle">';
808c2e8227SGreg Roach				ob_start();
818c2e8227SGreg Roach				$chartController->printPersonPedigree($person, 1);
828c2e8227SGreg Roach				$content .= ob_get_clean();
838c2e8227SGreg Roach				$content .= '</td>';
848c2e8227SGreg Roach				break;
858c2e8227SGreg Roach			case 'descendants':
868c2e8227SGreg Roach				$title .= I18N::translate('Descendants of %s', $person->getFullName());
878c2e8227SGreg Roach				$chartController = new HourglassController($person->getXref(), $details, false);
888c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
898c2e8227SGreg Roach				$content .= '<td valign="middle">';
908c2e8227SGreg Roach				ob_start();
918c2e8227SGreg Roach				$chartController->printDescendency($person, 1, false);
928c2e8227SGreg Roach				$content .= ob_get_clean();
938c2e8227SGreg Roach				$content .= '</td>';
948c2e8227SGreg Roach				break;
958c2e8227SGreg Roach			case 'hourglass':
968c2e8227SGreg Roach				$title .= I18N::translate('Hourglass chart of %s', $person->getFullName());
978c2e8227SGreg Roach				$chartController = new HourglassController($person->getXref(), $details, false);
988c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
998c2e8227SGreg Roach				$content .= '<td valign="middle">';
1008c2e8227SGreg Roach				ob_start();
1018c2e8227SGreg Roach				$chartController->printDescendency($person, 1, false);
1028c2e8227SGreg Roach				$content .= ob_get_clean();
1038c2e8227SGreg Roach				$content .= '</td>';
1048c2e8227SGreg Roach				$content .= '<td valign="middle">';
1058c2e8227SGreg Roach				ob_start();
1068c2e8227SGreg Roach				$chartController->printPersonPedigree($person, 1);
1078c2e8227SGreg Roach				$content .= ob_get_clean();
1088c2e8227SGreg Roach				$content .= '</td>';
1098c2e8227SGreg Roach				break;
1108c2e8227SGreg Roach			case 'treenav':
1118c2e8227SGreg Roach				$title .= I18N::translate('Interactive tree of %s', $person->getFullName());
1128c2e8227SGreg Roach				$mod = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
1138c2e8227SGreg Roach				$tv = new TreeView;
1148c2e8227SGreg Roach				$content .= '<td>';
1158c2e8227SGreg Roach				$content .= '<script>jQuery("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
1168c2e8227SGreg Roach				$content .= '<script src="' . $mod->js() . '"></script>';
1178c2e8227SGreg Roach				list($html, $js) = $tv->drawViewport($person, 2);
1188c2e8227SGreg Roach				$content .= $html . '<script>' . $js . '</script>';
1198c2e8227SGreg Roach				$content .= '</td>';
1208c2e8227SGreg Roach				break;
1218c2e8227SGreg Roach			}
1228c2e8227SGreg Roach			$content .= '</tr></table>';
1238c2e8227SGreg Roach		} else {
1248c2e8227SGreg Roach			$content = I18N::translate('You must select an individual and chart type in the block configuration settings.');
1258c2e8227SGreg Roach		}
1268c2e8227SGreg Roach
1278c2e8227SGreg Roach		if ($template) {
1288c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1298c2e8227SGreg Roach		} else {
1308c2e8227SGreg Roach			return $content;
1318c2e8227SGreg Roach		}
1328c2e8227SGreg Roach	}
1338c2e8227SGreg Roach
1348c2e8227SGreg Roach	/** {@inheritdoc} */
1358c2e8227SGreg Roach	public function loadAjax() {
1368c2e8227SGreg Roach		return true;
1378c2e8227SGreg Roach	}
1388c2e8227SGreg Roach
1398c2e8227SGreg Roach	/** {@inheritdoc} */
1408c2e8227SGreg Roach	public function isUserBlock() {
1418c2e8227SGreg Roach		return true;
1428c2e8227SGreg Roach	}
1438c2e8227SGreg Roach
1448c2e8227SGreg Roach	/** {@inheritdoc} */
1458c2e8227SGreg Roach	public function isGedcomBlock() {
1468c2e8227SGreg Roach		return true;
1478c2e8227SGreg Roach	}
1488c2e8227SGreg Roach
1498c2e8227SGreg Roach	/** {@inheritdoc} */
1508c2e8227SGreg Roach	public function configureBlock($block_id) {
1518c2e8227SGreg Roach		global $WT_TREE, $controller;
1528c2e8227SGreg Roach
1538c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
1544b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
1558c2e8227SGreg Roach
1568c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
1578c2e8227SGreg Roach			set_block_setting($block_id, 'details', Filter::postBool('details'));
1588c2e8227SGreg Roach			set_block_setting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
1598c2e8227SGreg Roach			set_block_setting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
1608c2e8227SGreg Roach		}
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach		$details = get_block_setting($block_id, 'details', '0');
1638c2e8227SGreg Roach		$type    = get_block_setting($block_id, 'type', 'pedigree');
1644b9ff166SGreg Roach		$pid     = get_block_setting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach		$controller
1678c2e8227SGreg Roach			->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
1688c2e8227SGreg Roach			->addInlineJavascript('autocomplete();');
1698c2e8227SGreg Roach	?>
1708c2e8227SGreg Roach		<tr>
1718c2e8227SGreg Roach			<td colspan="2">
1728c2e8227SGreg Roach				<?php echo I18N::translate('This block allows a pedigree, descendancy, or hourglass chart to appear on your “My page” or the “Home page”.  Because of space limitations, the charts should be placed only on the left side of the page.<br><br>When this block appears on the “Home page”, the root individual and the type of chart to be displayed are determined by the administrator.  When this block appears on the user’s “My page”, these options are determined by the user.<br><br>The behavior of these charts is identical to their behavior when they are called up from the menus.  Click on the box of an individual to see more details about them.'); ?>
1738c2e8227SGreg Roach			</td>
1748c2e8227SGreg Roach		</tr>
1758c2e8227SGreg Roach		<tr>
1768c2e8227SGreg Roach			<td class="descriptionbox wrap width33"><?php echo I18N::translate('Chart type'); ?></td>
1778c2e8227SGreg Roach			<td class="optionbox">
1788c2e8227SGreg Roach				<?php echo select_edit_control('type',
1798c2e8227SGreg Roach				array(
1808c2e8227SGreg Roach					'pedigree'    => I18N::translate('Pedigree'),
1818c2e8227SGreg Roach					'descendants' => I18N::translate('Descendants'),
1828c2e8227SGreg Roach					'hourglass'   => I18N::translate('Hourglass chart'),
1838c2e8227SGreg Roach					'treenav'     => I18N::translate('Interactive tree')),
1848c2e8227SGreg Roach				null, $type); ?>
1858c2e8227SGreg Roach			</td>
1868c2e8227SGreg Roach		</tr>
1878c2e8227SGreg Roach		<tr>
1888c2e8227SGreg Roach			<td class="descriptionbox wrap width33"><?php echo I18N::translate('Show details'); ?></td>
1898c2e8227SGreg Roach		<td class="optionbox">
1908c2e8227SGreg Roach			<?php echo edit_field_yes_no('details', $details); ?>
1918c2e8227SGreg Roach			</td>
1928c2e8227SGreg Roach		</tr>
1938c2e8227SGreg Roach		<tr>
1948c2e8227SGreg Roach			<td class="descriptionbox wrap width33"><?php echo I18N::translate('Individual'); ?></td>
1958c2e8227SGreg Roach			<td class="optionbox">
1968c2e8227SGreg Roach				<input data-autocomplete-type="INDI" type="text" name="pid" id="pid" value="<?php echo $pid; ?>" size="5">
1978c2e8227SGreg Roach				<?php
1988c2e8227SGreg Roach				echo print_findindi_link('pid');
199*24ec66ceSGreg Roach				$root = Individual::getInstance($pid, $WT_TREE);
2008c2e8227SGreg Roach				if ($root) {
2018c2e8227SGreg Roach					echo ' <span class="list_item">', $root->getFullName(), $root->format_first_major_fact(WT_EVENTS_BIRT, 1), '</span>';
2028c2e8227SGreg Roach				}
2038c2e8227SGreg Roach				?>
2048c2e8227SGreg Roach			</td>
2058c2e8227SGreg Roach		</tr>
2068c2e8227SGreg Roach		<?php
2078c2e8227SGreg Roach	}
2088c2e8227SGreg Roach}
209