xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 24ec66ce7e77188cd2495b0f8d4dd0ae6e8c9c52)
1<?php
2namespace Fisharebest\Webtrees;
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 */
18
19use Zend_Session;
20
21/**
22 * Class InteractiveTreeModule
23 * Tip : you could change the number of generations loaded before ajax calls both in individual page and in treeview page to optimize speed and server load
24 */
25class InteractiveTreeModule extends Module implements ModuleTabInterface {
26	var $headers; // CSS and script to include in the top of <head> section, before theme’s CSS
27	var $js; // the TreeViewHandler javascript
28
29	/** {@inheritdoc} */
30	public function getTitle() {
31		return /* I18N: Name of a module */ I18N::translate('Interactive tree');
32	}
33
34	/** {@inheritdoc} */
35	public function getDescription() {
36		return /* I18N: Description of the “Interactive tree” module */ I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
37	}
38
39	/** {@inheritdoc} */
40	public function defaultTabOrder() {
41		return 68;
42	}
43
44	/** {@inheritdoc} */
45	public function getTabContent() {
46		global $controller;
47
48		$tv = new TreeView('tvTab');
49		list($html, $js) = $tv->drawViewport($controller->record, 3);
50		return
51			'<script src="' . $this->js() . '"></script>' .
52			'<script src="' . WT_JQUERYUI_TOUCH_PUNCH_URL . '"></script>' .
53			'<script>' . $js . '</script>' .
54			$html;
55	}
56
57	/** {@inheritdoc} */
58	public function hasTabContent() {
59		return !Auth::isSearchEngine();
60	}
61
62	/** {@inheritdoc} */
63	public function isGrayedOut() {
64		return false;
65	}
66
67	/** {@inheritdoc} */
68	public function canLoadAjax() {
69		return true;
70	}
71
72	/** {@inheritdoc} */
73	public function getPreLoadContent() {
74		// We cannot use jQuery("head").append(<link rel="stylesheet" ...as jQuery is not loaded at this time
75		return
76			'<script>
77			if (document.createStyleSheet) {
78				document.createStyleSheet("' . $this->css() . '"); // For Internet Explorer
79			} else {
80				var newSheet=document.createElement("link");
81    		newSheet.setAttribute("rel","stylesheet");
82    		newSheet.setAttribute("type","text/css");
83   			newSheet.setAttribute("href","' . $this->css() . '");
84		    document.getElementsByTagName("head")[0].appendChild(newSheet);
85			}
86			</script>';
87	}
88
89	/** {@inheritdoc} */
90	public function modAction($mod_action) {
91		global $controller, $WT_TREE;
92
93		switch ($mod_action) {
94		case 'treeview':
95			$controller = new ChartController;
96			$tv = new TreeView('tv');
97			ob_start();
98
99			$person = $controller->getSignificantIndividual();
100
101			list($html, $js) = $tv->drawViewport($person, 4);
102
103			$controller
104				->setPageTitle(I18N::translate('Interactive tree of %s', $person->getFullName()))
105				->pageHeader()
106				->addExternalJavascript($this->js())
107				->addExternalJavascript(WT_JQUERYUI_TOUCH_PUNCH_URL)
108				->addInlineJavascript($js)
109				->addInlineJavascript('
110					if (document.createStyleSheet) {
111						document.createStyleSheet("' . $this->css() . '"); // For Internet Explorer
112					} else {
113						jQuery("head").append(\'<link rel="stylesheet" type="text/css" href="' . $this->css() . '">\');
114					}
115				');
116			echo $html;
117			break;
118
119		case 'getDetails':
120			Zend_Session::writeClose();
121			header('Content-Type: text/html; charset=UTF-8');
122			$pid = Filter::get('pid', WT_REGEX_XREF);
123			$i = Filter::get('instance');
124			$tv = new TreeView($i);
125			$individual = Individual::getInstance($pid, $WT_TREE);
126			if ($individual) {
127				echo $tv->getDetails($individual);
128			}
129			break;
130
131		case 'getPersons':
132			Zend_Session::writeClose();
133			header('Content-Type: text/html; charset=UTF-8');
134			$q = Filter::get('q');
135			$i = Filter::get('instance');
136			$tv = new TreeView($i);
137			echo $tv->getPersons($q);
138			break;
139
140		default:
141			http_response_code(404);
142			break;
143		}
144	}
145
146	/**
147	 * @return string
148	 */
149	public function css() {
150		return WT_STATIC_URL . WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
151	}
152
153	/**
154	 * @return string
155	 */
156	public function js() {
157		return WT_STATIC_URL . WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
158	}
159}
160