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