xref: /webtrees/app/Module/ChartsBlockModule.php (revision f679d3ac2458fd5b8e8ff473f62ff4a6a98924d6)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4369c0ce6SGreg Roach * Copyright (C) 2016 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Controller\HourglassController;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class ChartsBlockModule
308c2e8227SGreg Roach */
31e2a378d3SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface {
328c2e8227SGreg Roach	/** {@inheritdoc} */
338c2e8227SGreg Roach	public function getTitle() {
348c2e8227SGreg Roach		return /* I18N: Name of a module/block */ I18N::translate('Charts');
358c2e8227SGreg Roach	}
368c2e8227SGreg Roach
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function getDescription() {
398c2e8227SGreg Roach		return /* I18N: Description of the “Charts” module */ I18N::translate('An alternative way to display charts.');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
4276692c8bSGreg Roach	/**
4376692c8bSGreg Roach	 * Generate the HTML content of this block.
4476692c8bSGreg Roach	 *
4576692c8bSGreg Roach	 * @param int      $block_id
4676692c8bSGreg Roach	 * @param bool     $template
47727f238cSGreg Roach	 * @param string[] $cfg
4876692c8bSGreg Roach	 *
4976692c8bSGreg Roach	 * @return string
5076692c8bSGreg Roach	 */
5176692c8bSGreg Roach	public function getBlock($block_id, $template = true, $cfg = array()) {
528c2e8227SGreg Roach		global $WT_TREE, $ctype, $controller;
538c2e8227SGreg Roach
548c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
554b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
568c2e8227SGreg Roach
57e2a378d3SGreg Roach		$details = $this->getBlockSetting($block_id, 'details', '0');
58e2a378d3SGreg Roach		$type    = $this->getBlockSetting($block_id, 'type', 'pedigree');
59e2a378d3SGreg Roach		$pid     = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
608c2e8227SGreg Roach
618c2e8227SGreg Roach		foreach (array('details', 'type', 'pid', 'block') as $name) {
628c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
638c2e8227SGreg Roach				$$name = $cfg[$name];
648c2e8227SGreg Roach			}
658c2e8227SGreg Roach		}
668c2e8227SGreg Roach
6724ec66ceSGreg Roach		$person = Individual::getInstance($pid, $WT_TREE);
688c2e8227SGreg Roach		if (!$person) {
698c2e8227SGreg Roach			$pid = $PEDIGREE_ROOT_ID;
70e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'pid', $pid);
7124ec66ceSGreg Roach			$person = Individual::getInstance($pid, $WT_TREE);
728c2e8227SGreg Roach		}
738c2e8227SGreg Roach
748c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
758c2e8227SGreg Roach		$class = $this->getName() . '_block';
764b9ff166SGreg Roach		if ($ctype == 'gedcom' && Auth::isManager($WT_TREE) || $ctype == 'user' && Auth::check()) {
776d1c8039SGreg Roach			$title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
788c2e8227SGreg Roach		} else {
798c2e8227SGreg Roach			$title = '';
808c2e8227SGreg Roach		}
818c2e8227SGreg Roach
828c2e8227SGreg Roach		if ($person) {
83*f679d3acSGreg Roach			$content = '';
848c2e8227SGreg Roach			switch ($type) {
858c2e8227SGreg Roach			case 'pedigree':
868c2e8227SGreg Roach				$title .= I18N::translate('Pedigree of %s', $person->getFullName());
878c2e8227SGreg Roach				$chartController = new HourglassController($person->getXref(), $details, false);
888c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
89*f679d3acSGreg Roach				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
90a86dd8b1SGreg Roach				$content .= '<td>';
918c2e8227SGreg Roach				ob_start();
923d7a8a4cSGreg Roach				FunctionsPrint::printPedigreePerson($person, $details);
938c2e8227SGreg Roach				$content .= ob_get_clean();
948c2e8227SGreg Roach				$content .= '</td>';
95a86dd8b1SGreg Roach				$content .= '<td>';
968c2e8227SGreg Roach				ob_start();
978c2e8227SGreg Roach				$chartController->printPersonPedigree($person, 1);
988c2e8227SGreg Roach				$content .= ob_get_clean();
998c2e8227SGreg Roach				$content .= '</td>';
100*f679d3acSGreg Roach				$content .= '</tr></table>';
1018c2e8227SGreg Roach				break;
1028c2e8227SGreg Roach			case 'descendants':
1038c2e8227SGreg Roach				$title .= I18N::translate('Descendants of %s', $person->getFullName());
1048c2e8227SGreg Roach				$chartController = new HourglassController($person->getXref(), $details, false);
1058c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
1068c2e8227SGreg Roach				ob_start();
1078c2e8227SGreg Roach				$chartController->printDescendency($person, 1, false);
1088c2e8227SGreg Roach				$content .= ob_get_clean();
1098c2e8227SGreg Roach				break;
1108c2e8227SGreg Roach			case 'hourglass':
1118c2e8227SGreg Roach				$title .= I18N::translate('Hourglass chart of %s', $person->getFullName());
1128c2e8227SGreg Roach				$chartController = new HourglassController($person->getXref(), $details, false);
1138c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
114*f679d3acSGreg Roach				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
115a86dd8b1SGreg Roach				$content .= '<td>';
1168c2e8227SGreg Roach				ob_start();
1178c2e8227SGreg Roach				$chartController->printDescendency($person, 1, false);
1188c2e8227SGreg Roach				$content .= ob_get_clean();
1198c2e8227SGreg Roach				$content .= '</td>';
120a86dd8b1SGreg Roach				$content .= '<td>';
1218c2e8227SGreg Roach				ob_start();
1228c2e8227SGreg Roach				$chartController->printPersonPedigree($person, 1);
1238c2e8227SGreg Roach				$content .= ob_get_clean();
1248c2e8227SGreg Roach				$content .= '</td>';
125*f679d3acSGreg Roach				$content .= '</tr></table>';
1268c2e8227SGreg Roach				break;
1278c2e8227SGreg Roach			case 'treenav':
1288c2e8227SGreg Roach				$title .= I18N::translate('Interactive tree of %s', $person->getFullName());
1298c2e8227SGreg Roach				$mod = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
1308c2e8227SGreg Roach				$tv  = new TreeView;
1318c2e8227SGreg Roach				$content .= '<script>jQuery("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
1328c2e8227SGreg Roach				$content .= '<script src="' . $mod->js() . '"></script>';
1338c2e8227SGreg Roach				list($html, $js) = $tv->drawViewport($person, 2);
1348c2e8227SGreg Roach				$content .= $html . '<script>' . $js . '</script>';
1358c2e8227SGreg Roach				break;
1368c2e8227SGreg Roach			}
1378c2e8227SGreg Roach		} else {
1386e7bf391SGreg Roach			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
1398c2e8227SGreg Roach		}
1408c2e8227SGreg Roach
1418c2e8227SGreg Roach		if ($template) {
1428c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1438c2e8227SGreg Roach		} else {
1448c2e8227SGreg Roach			return $content;
1458c2e8227SGreg Roach		}
1468c2e8227SGreg Roach	}
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach	/** {@inheritdoc} */
1498c2e8227SGreg Roach	public function loadAjax() {
1508c2e8227SGreg Roach		return true;
1518c2e8227SGreg Roach	}
1528c2e8227SGreg Roach
1538c2e8227SGreg Roach	/** {@inheritdoc} */
1548c2e8227SGreg Roach	public function isUserBlock() {
1558c2e8227SGreg Roach		return true;
1568c2e8227SGreg Roach	}
1578c2e8227SGreg Roach
1588c2e8227SGreg Roach	/** {@inheritdoc} */
1598c2e8227SGreg Roach	public function isGedcomBlock() {
1608c2e8227SGreg Roach		return true;
1618c2e8227SGreg Roach	}
1628c2e8227SGreg Roach
16376692c8bSGreg Roach	/**
16476692c8bSGreg Roach	 * An HTML form to edit block settings
16576692c8bSGreg Roach	 *
16676692c8bSGreg Roach	 * @param int $block_id
16776692c8bSGreg Roach	 */
1688c2e8227SGreg Roach	public function configureBlock($block_id) {
1698c2e8227SGreg Roach		global $WT_TREE, $controller;
1708c2e8227SGreg Roach
1718c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
1724b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
1738c2e8227SGreg Roach
1748c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
175e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'details', Filter::postBool('details'));
176e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
1788c2e8227SGreg Roach		}
1798c2e8227SGreg Roach
180e2a378d3SGreg Roach		$details = $this->getBlockSetting($block_id, 'details', '0');
181e2a378d3SGreg Roach		$type    = $this->getBlockSetting($block_id, 'type', 'pedigree');
182e2a378d3SGreg Roach		$pid     = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
1838c2e8227SGreg Roach
184fddfbb14SGreg Roach		$charts = array(
185fddfbb14SGreg Roach			'pedigree'    => I18N::translate('Pedigree'),
186fddfbb14SGreg Roach			'descendants' => I18N::translate('Descendants'),
187fddfbb14SGreg Roach			'hourglass'   => I18N::translate('Hourglass chart'),
188fddfbb14SGreg Roach			'treenav'     => I18N::translate('Interactive tree'),
189fddfbb14SGreg Roach		);
190fddfbb14SGreg Roach		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
191fddfbb14SGreg Roach
1928c2e8227SGreg Roach		$controller
1938c2e8227SGreg Roach			->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
1948c2e8227SGreg Roach			->addInlineJavascript('autocomplete();');
1958c2e8227SGreg Roach	?>
1968c2e8227SGreg Roach		<tr>
197054bfc98SGreg Roach			<td class="descriptionbox wrap width33">
198054bfc98SGreg Roach				<?php echo I18N::translate('Chart type'); ?>
199054bfc98SGreg Roach			</td>
2008c2e8227SGreg Roach			<td class="optionbox">
201fddfbb14SGreg Roach				<?php echo FunctionsEdit::selectEditControl('type', $charts, null, $type); ?>
2028c2e8227SGreg Roach			</td>
2038c2e8227SGreg Roach		</tr>
2048c2e8227SGreg Roach		<tr>
205054bfc98SGreg Roach			<td class="descriptionbox wrap width33">
206054bfc98SGreg Roach				<?php echo I18N::translate('Show details'); ?>
207054bfc98SGreg Roach			</td>
2088c2e8227SGreg Roach		<td class="optionbox">
2093d7a8a4cSGreg Roach			<?php echo FunctionsEdit::editFieldYesNo('details', $details); ?>
2108c2e8227SGreg Roach			</td>
2118c2e8227SGreg Roach		</tr>
2128c2e8227SGreg Roach		<tr>
213054bfc98SGreg Roach			<td class="descriptionbox wrap width33">
214054bfc98SGreg Roach				<label for="pid">
215054bfc98SGreg Roach					<?php echo I18N::translate('Individual'); ?>
216054bfc98SGreg Roach				</label>
217054bfc98SGreg Roach			</td>
2188c2e8227SGreg Roach			<td class="optionbox">
2198c2e8227SGreg Roach				<input data-autocomplete-type="INDI" type="text" name="pid" id="pid" value="<?php echo $pid; ?>" size="5">
2208c2e8227SGreg Roach				<?php
2213d7a8a4cSGreg Roach				echo FunctionsPrint::printFindIndividualLink('pid');
22224ec66ceSGreg Roach				$root = Individual::getInstance($pid, $WT_TREE);
2238c2e8227SGreg Roach				if ($root) {
22418f3ef81SGreg Roach					echo ' <span class="list_item">', $root->getFullName(), $root->formatFirstMajorFact(WT_EVENTS_BIRT, 1), '</span>';
2258c2e8227SGreg Roach				}
2268c2e8227SGreg Roach				?>
2278c2e8227SGreg Roach			</td>
2288c2e8227SGreg Roach		</tr>
2298c2e8227SGreg Roach		<?php
2308c2e8227SGreg Roach	}
2318c2e8227SGreg Roach}
232