xref: /webtrees/app/Module/StoriesModule.php (revision 73df4c545c87012519818766e1a8eb02272a59b5)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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\Controller\PageController;
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\Functions\FunctionsPrint;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Menu;
27use Fisharebest\Webtrees\Module;
28use Fisharebest\Webtrees\Tree;
29
30/**
31 * Class StoriesModule
32 */
33class StoriesModule extends AbstractModule implements ModuleTabInterface, ModuleConfigInterface, ModuleMenuInterface {
34	/** {@inheritdoc} */
35	public function getTitle() {
36		return /* I18N: Name of a module */ I18N::translate('Stories');
37	}
38
39	/** {@inheritdoc} */
40	public function getDescription() {
41		return /* I18N: Description of the “Stories” module */ I18N::translate('Add narrative stories to individuals in the family tree.');
42	}
43
44	/**
45	 * This is a general purpose hook, allowing modules to respond to routes
46	 * of the form module.php?mod=FOO&mod_action=BAR
47	 *
48	 * @param string $mod_action
49	 */
50	public function modAction($mod_action) {
51		switch ($mod_action) {
52		case 'admin_edit':
53			$this->edit();
54			break;
55		case 'admin_delete':
56			$this->delete();
57			$this->config();
58			break;
59		case 'admin_config':
60			$this->config();
61			break;
62		case 'show_list':
63			$this->showList();
64			break;
65		default:
66			http_response_code(404);
67		}
68	}
69
70	/** {@inheritdoc} */
71	public function getConfigLink() {
72		return 'module.php?mod=' . $this->getName() . '&amp;mod_action=admin_config';
73	}
74
75	/** {@inheritdoc} */
76	public function defaultTabOrder() {
77		return 55;
78	}
79
80	/** {@inheritdoc} */
81	public function getTabContent() {
82		global $controller, $WT_TREE;
83
84		$block_ids =
85			Database::prepare(
86				"SELECT block_id" .
87				" FROM `##block`" .
88				" WHERE module_name=?" .
89				" AND xref=?" .
90				" AND gedcom_id=?"
91			)->execute(array(
92				$this->getName(),
93				$controller->record->getXref(),
94				$controller->record->getTree()->getTreeId(),
95			))->fetchOneColumn();
96
97		$html = '';
98		foreach ($block_ids as $block_id) {
99			// Only show this block for certain languages
100			$languages = $this->getBlockSetting($block_id, 'languages');
101			if (!$languages || in_array(WT_LOCALE, explode(',', $languages))) {
102				$html .= '<div class="story_title descriptionbox center rela">' . $this->getBlockSetting($block_id, 'title') . '</div>';
103				$html .= '<div class="story_body optionbox">' . $this->getBlockSetting($block_id, 'story_body') . '</div>';
104				if (Auth::isEditor($WT_TREE)) {
105					$html .= '<div class="story_edit"><a href="module.php?mod=' . $this->getName() . '&amp;mod_action=admin_edit&amp;block_id=' . $block_id . '">';
106					$html .= I18N::translate('Edit the story') . '</a></div>';
107				}
108			}
109		}
110		if (Auth::isManager($WT_TREE) && !$html) {
111			$html .= '<div class="news_title center">' . $this->getTitle() . '</div>';
112			$html .= '<div><a href="module.php?mod=' . $this->getName() . '&amp;mod_action=admin_edit&amp;xref=' . $controller->record->getXref() . '">';
113			$html .= I18N::translate('Add a story') . '</a></div><br>';
114		}
115
116		return $html;
117	}
118
119	/** {@inheritdoc} */
120	public function hasTabContent() {
121		return $this->getTabContent() != '';
122	}
123
124	/** {@inheritdoc} */
125	public function isGrayedOut() {
126		global $controller;
127
128		$count_of_stories =
129			Database::prepare(
130				"SELECT COUNT(block_id)" .
131				" FROM `##block`" .
132				" WHERE module_name=?" .
133				" AND xref=?" .
134				" AND gedcom_id=?"
135			)->execute(array(
136				$this->getName(),
137				$controller->record->getXref(),
138				$controller->record->getTree()->getTreeId(),
139			))->fetchOne();
140
141		return $count_of_stories == 0;
142	}
143
144	/** {@inheritdoc} */
145	public function canLoadAjax() {
146		return false;
147	}
148
149	/** {@inheritdoc} */
150	public function getPreLoadContent() {
151		return '';
152	}
153
154	/**
155	 * Show and process a form to edit a story.
156	 */
157	private function edit() {
158		global $WT_TREE;
159
160		if (Auth::isEditor($WT_TREE)) {
161			if (Filter::postBool('save') && Filter::checkCsrf()) {
162				$block_id = Filter::postInteger('block_id');
163				if ($block_id) {
164					Database::prepare(
165						"UPDATE `##block` SET gedcom_id=?, xref=? WHERE block_id=?"
166					)->execute(array(Filter::postInteger('gedcom_id'), Filter::post('xref', WT_REGEX_XREF), $block_id));
167				} else {
168					Database::prepare(
169						"INSERT INTO `##block` (gedcom_id, xref, module_name, block_order) VALUES (?, ?, ?, ?)"
170					)->execute(array(
171						Filter::postInteger('gedcom_id'),
172						Filter::post('xref', WT_REGEX_XREF),
173						$this->getName(),
174						0,
175					));
176					$block_id = Database::getInstance()->lastInsertId();
177				}
178				$this->setBlockSetting($block_id, 'title', Filter::post('title'));
179				$this->setBlockSetting($block_id, 'story_body', Filter::post('story_body'));
180				$languages = Filter::postArray('lang');
181				$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
182				$this->config();
183			} else {
184				$block_id = Filter::getInteger('block_id');
185
186				$controller = new PageController;
187				if ($block_id) {
188					$controller->setPageTitle(I18N::translate('Edit the story'));
189					$title      = $this->getBlockSetting($block_id, 'title');
190					$story_body = $this->getBlockSetting($block_id, 'story_body');
191					$xref       = Database::prepare(
192						"SELECT xref FROM `##block` WHERE block_id=?"
193					)->execute(array($block_id))->fetchOne();
194				} else {
195					$controller->setPageTitle(I18N::translate('Add a story'));
196					$title      = '';
197					$story_body = '';
198					$xref       = Filter::get('xref', WT_REGEX_XREF);
199				}
200				$controller
201					->pageHeader()
202					->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
203					->addInlineJavascript('autocomplete();');
204				if (Module::getModuleByName('ckeditor')) {
205					CkeditorModule::enableEditor($controller);
206				}
207
208				$individual = Individual::getInstance($xref, $WT_TREE);
209
210				?>
211				<ol class="breadcrumb small">
212					<li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
213					<li><a href="admin_modules.php"><?php echo I18N::translate('Module administration'); ?></a></li>
214					<li><a href="module.php?mod=<?php echo $this->getName(); ?>&mod_action=admin_config"><?php echo $this->getTitle(); ?></a></li>
215					<li class="active"><?php echo $controller->getPageTitle(); ?></li>
216				</ol>
217
218				<h1><?php echo $controller->getPageTitle(); ?></h1>
219
220				<form class="form-horizontal" method="post" action="module.php?mod=<?php echo $this->getName(); ?>&amp;mod_action=admin_edit">
221					<?php echo Filter::getCsrf(); ?>
222					<input type="hidden" name="save" value="1">
223					<input type="hidden" name="block_id" value="<?php echo $block_id; ?>">
224					<input type="hidden" name="gedcom_id" value="<?php echo $WT_TREE->getTreeId(); ?>">
225
226					<div class="form-group">
227						<label for="title" class="col-sm-3 control-label">
228							<?php echo I18N::translate('Story title'); ?>
229						</label>
230						<div class="col-sm-9">
231							<input type="text" class="form-control" name="title" id="title" value="<?php echo Filter::escapeHtml($title); ?>">
232						</div>
233					</div>
234
235					<div class="form-group">
236						<label for="story_body" class="col-sm-3 control-label">
237							<?php echo I18N::translate('Story'); ?>
238						</label>
239						<div class="col-sm-9">
240							<textarea name="story_body" id="story_body" class="html-edit form-control" rows="10"><?php echo Filter::escapeHtml($story_body); ?></textarea>
241						</div>
242					</div>
243
244					<div class="form-group">
245						<label for="xref" class="col-sm-3 control-label">
246							<?php echo I18N::translate('Individual'); ?>
247						</label>
248						<div class="col-sm-9">
249							<input data-autocomplete-type="INDI" type="text" name="xref" id="xref" size="4" value="<?php echo $xref; ?>">
250							<?php echo FunctionsPrint::printFindIndividualLink('xref'); ?>
251							<?php if ($individual): ?>
252								<?php echo $individual->formatList('span'); ?>
253							<?php endif; ?>
254						</div>
255					</div>
256
257					<div class="form-group">
258						<label for="xref" class="col-sm-3 control-label">
259							<?php echo I18N::translate('Show this block for which languages'); ?>
260						</label>
261						<div class="col-sm-9">
262							<?php echo FunctionsEdit::editLanguageCheckboxes('lang', explode(',', $this->getBlockSetting($block_id, 'languages'))); ?>
263						</div>
264					</div>
265
266					<div class="form-group">
267						<div class="col-sm-offset-3 col-sm-9">
268							<button type="submit" class="btn btn-primary">
269								<i class="fa fa-check"></i>
270								<?php echo I18N::translate('save'); ?>
271							</button>
272						</div>
273					</div>
274
275				</form>
276				<?php
277			}
278		} else {
279			header('Location: ' . WT_BASE_URL);
280		}
281	}
282
283	/**
284	 * Respond to a request to delete a story.
285	 */
286	private function delete() {
287		global $WT_TREE;
288
289		if (Auth::isEditor($WT_TREE)) {
290			$block_id = Filter::getInteger('block_id');
291
292			Database::prepare(
293				"DELETE FROM `##block_setting` WHERE block_id=?"
294			)->execute(array($block_id));
295
296			Database::prepare(
297				"DELETE FROM `##block` WHERE block_id=?"
298			)->execute(array($block_id));
299		} else {
300			header('Location: ' . WT_BASE_URL);
301			exit;
302		}
303	}
304
305	/**
306	 * The admin view - list, create, edit, delete stories.
307	 */
308	private function config() {
309		global $WT_TREE;
310
311		$controller = new PageController;
312		$controller
313			->restrictAccess(Auth::isAdmin())
314			->setPageTitle($this->getTitle())
315			->pageHeader()
316			->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
317			->addExternalJavascript(WT_DATATABLES_BOOTSTRAP_JS_URL)
318			->addInlineJavascript('
319				jQuery("#story_table").dataTable({
320					' . I18N::datatablesI18N() . ',
321					autoWidth: false,
322					paging: true,
323					pagingType: "full_numbers",
324					lengthChange: true,
325					filter: true,
326					info: true,
327					sorting: [[0,"asc"]],
328					columns: [
329						/* 0-name */ null,
330						/* 1-NAME */ null,
331						/* 2-NAME */ { sortable:false },
332						/* 3-NAME */ { sortable:false }
333					]
334				});
335			');
336
337		$stories = Database::prepare(
338			"SELECT block_id, xref" .
339			" FROM `##block` b" .
340			" WHERE module_name=?" .
341			" AND gedcom_id=?" .
342			" ORDER BY xref"
343		)->execute(array($this->getName(), $WT_TREE->getTreeId()))->fetchAll();
344
345		?>
346		<ol class="breadcrumb small">
347			<li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
348			<li><a href="admin_modules.php"><?php echo I18N::translate('Module administration'); ?></a></li>
349			<li class="active"><?php echo $controller->getPageTitle(); ?></li>
350		</ol>
351
352		<h1><?php echo $controller->getPageTitle(); ?></h1>
353
354		<form class="form form-inline">
355			<label for="ged" class="sr-only">
356				<?php echo I18N::translate('Family tree'); ?>
357			</label>
358			<input type="hidden" name="mod" value="<?php echo  $this->getName(); ?>">
359			<input type="hidden" name="mod_action" value="admin_config">
360			<?php echo FunctionsEdit::selectEditControl('ged', Tree::getNameList(), null, $WT_TREE->getName(), 'class="form-control"'); ?>
361			<input type="submit" class="btn btn-primary" value="<?php echo I18N::translate('show'); ?>">
362		</form>
363
364		<p>
365			<a href="module.php?mod=<?php echo $this->getName(); ?>&amp;mod_action=admin_edit" class="btn btn-default">
366				<i class="fa fa-plus"></i>
367				<?php echo I18N::translate('Add a story'); ?>
368			</a>
369		</p>
370
371		<table class="table table-bordered table-condensed">
372			<thead>
373				<tr>
374					<th><?php echo I18N::translate('Story title'); ?></th>
375					<th><?php echo I18N::translate('Individual'); ?></th>
376					<th><?php echo I18N::translate('Edit'); ?></th>
377					<th><?php echo I18N::translate('Delete'); ?></th>
378				</tr>
379			</thead>
380			<tbody>
381				<?php foreach ($stories as $story): ?>
382				<tr>
383					<td>
384						<?php echo Filter::escapeHtml($this->getBlockSetting($story->block_id, 'title')); ?>
385					</td>
386					<td>
387						<?php $individual = Individual::getInstance($story->xref, $WT_TREE); ?>
388						<?php if ($individual): ?>
389						<a href="<?php echo $individual->getHtmlUrl(); ?>#stories">
390							<?php echo $individual->getFullName(); ?>
391						</a>
392						<?php else: ?>
393							<?php echo $story->xref; ?>
394						<?php endif; ?>
395						</td>
396						<td>
397							<a href="module.php?mod=<?php echo $this->getName(); ?>&amp;mod_action=admin_edit&amp;block_id=<?php echo $story->block_id; ?>">
398								<i class="fa fa-pencil"></i> <?php echo I18N::translate('Edit'); ?>
399							</a>
400						</td>
401						<td>
402							<a
403								href="module.php?mod=<?php echo $this->getName(); ?>&amp;mod_action=admin_delete&amp;block_id=<?php echo $story->block_id; ?>"
404								onclick="return confirm('<?php echo I18N::translate('Are you sure you want to delete “%s”?', Filter::escapeHtml($this->getBlockSetting($story->block_id, 'title'))); ?>');"
405							>
406								<i class="fa fa-trash"></i> <?php echo I18N::translate('Delete'); ?>
407							</a>
408					</td>
409				</tr>
410				<?php endforeach; ?>
411			</tbody>
412		</table>
413		<?php
414	}
415
416	/**
417	 * Show the list of stories
418	 */
419	private function showList() {
420		global $controller, $WT_TREE;
421
422		$controller = new PageController;
423		$controller
424			->setPageTitle($this->getTitle())
425			->pageHeader()
426			->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
427			->addInlineJavascript('
428				jQuery("#story_table").dataTable({
429					dom: \'<"H"pf<"dt-clear">irl>t<"F"pl>\',
430					' . I18N::datatablesI18N() . ',
431					autoWidth: false,
432					paging: true,
433					pagingType: "full_numbers",
434					lengthChange: true,
435					filter: true,
436					info: true,
437					jQueryUI: true,
438					sorting: [[0,"asc"]],
439					columns: [
440						/* 0-name */ null,
441						/* 1-NAME */ null
442					]
443				});
444			');
445
446		$stories = Database::prepare(
447			"SELECT block_id, xref" .
448			" FROM `##block` b" .
449			" WHERE module_name=?" .
450			" AND gedcom_id=?" .
451			" ORDER BY xref"
452		)->execute(array($this->getName(), $WT_TREE->getTreeId()))->fetchAll();
453
454		echo '<h2 class="center">', I18N::translate('Stories'), '</h2>';
455		if (count($stories) > 0) {
456			echo '<table id="story_table" class="width100">';
457			echo '<thead><tr>
458				<th>', I18N::translate('Story title'), '</th>
459				<th>', I18N::translate('Individual'), '</th>
460				</tr></thead>
461				<tbody>';
462			foreach ($stories as $story) {
463				$indi        = Individual::getInstance($story->xref, $WT_TREE);
464				$story_title = $this->getBlockSetting($story->block_id, 'title');
465				$languages   = $this->getBlockSetting($story->block_id, 'languages');
466				if (!$languages || in_array(WT_LOCALE, explode(',', $languages))) {
467					if ($indi) {
468						if ($indi->canShow()) {
469							echo '<tr><td><a href="' . $indi->getHtmlUrl() . '#stories">' . $story_title . '</a></td><td><a href="' . $indi->getHtmlUrl() . '#stories">' . $indi->getFullName() . '</a></td></tr>';
470						}
471					} else {
472						echo '<tr><td>', $story_title, '</td><td class="error">', $story->xref, '</td></tr>';
473					}
474				}
475			}
476			echo '</tbody></table>';
477		}
478	}
479
480	/**
481	 * The user can re-order menus. Until they do, they are shown in this order.
482	 *
483	 * @return int
484	 */
485	public function defaultMenuOrder() {
486		return 30;
487	}
488
489	/**
490	 * What is the default access level for this module?
491	 *
492	 * Some modules are aimed at admins or managers, and are not generally shown to users.
493	 *
494	 * @return int
495	 */
496	public function defaultAccessLevel() {
497		return Auth::PRIV_HIDE;
498	}
499
500	/**
501	 * A menu, to be added to the main application menu.
502	 *
503	 * @return Menu|null
504	 */
505	public function getMenu() {
506		$menu = new Menu($this->getTitle(), 'module.php?mod=' . $this->getName() . '&amp;mod_action=show_list', 'menu-story');
507
508		return $menu;
509	}
510}
511