xref: /webtrees/app/Module/UserJournalModule.php (revision d53324c994737e50e57b9e0bc691b4f742b400b2)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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\Database;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
229c6524dcSGreg Roachuse Fisharebest\Webtrees\View;
238c2e8227SGreg Roach
248c2e8227SGreg Roach/**
258c2e8227SGreg Roach * Class UserJournalModule
268c2e8227SGreg Roach */
27e2a378d3SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface {
2876692c8bSGreg Roach	/**
2976692c8bSGreg Roach	 * Create a new module.
3076692c8bSGreg Roach	 *
3176692c8bSGreg Roach	 * @param string $directory Where is this module installed
3276692c8bSGreg Roach	 */
333bfb94b0SGreg Roach	public function __construct($directory) {
343bfb94b0SGreg Roach		parent::__construct($directory);
353bfb94b0SGreg Roach
363bfb94b0SGreg Roach		// Create/update the database tables.
373bfb94b0SGreg Roach		Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3);
383bfb94b0SGreg Roach	}
393bfb94b0SGreg Roach
4076692c8bSGreg Roach	/**
4176692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
4276692c8bSGreg Roach	 *
4376692c8bSGreg Roach	 * @return string
4476692c8bSGreg Roach	 */
458c2e8227SGreg Roach	public function getTitle() {
468c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Journal');
478c2e8227SGreg Roach	}
488c2e8227SGreg Roach
4976692c8bSGreg Roach	/**
5076692c8bSGreg Roach	 * A sentence describing what this module does.
5176692c8bSGreg Roach	 *
5276692c8bSGreg Roach	 * @return string
5376692c8bSGreg Roach	 */
548c2e8227SGreg Roach	public function getDescription() {
558c2e8227SGreg Roach		return /* I18N: Description of the “Journal” module */ I18N::translate('A private area to record notes or keep a journal.');
568c2e8227SGreg Roach	}
578c2e8227SGreg Roach
5876692c8bSGreg Roach	/**
5976692c8bSGreg Roach	 * Generate the HTML content of this block.
6076692c8bSGreg Roach	 *
6176692c8bSGreg Roach	 * @param int      $block_id
6276692c8bSGreg Roach	 * @param bool     $template
63727f238cSGreg Roach	 * @param string[] $cfg
6476692c8bSGreg Roach	 *
6576692c8bSGreg Roach	 * @return string
6676692c8bSGreg Roach	 */
67a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
68a3dfb74cSGreg Roach		global $ctype, $WT_TREE;
698c2e8227SGreg Roach
70d004f62cSGreg Roach		$articles = Database::prepare(
7145d06cf3SGreg Roach			"SELECT SQL_CACHE news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) + :offset AS updated, subject, body FROM `##news` WHERE user_id = :user_id ORDER BY updated DESC"
7213abd6f3SGreg Roach		)->execute([
7345d06cf3SGreg Roach			'offset'  => WT_TIMESTAMP_OFFSET,
7445d06cf3SGreg Roach			'user_id' => Auth::id(),
7513abd6f3SGreg Roach		])->fetchAll();
768c2e8227SGreg Roach
778c2e8227SGreg Roach		$content = '';
78d004f62cSGreg Roach
79d004f62cSGreg Roach		if (empty($articles)) {
80fc21c690SGreg Roach			$content .= '<p>' . I18N::translate('You have not created any journal items.') . '</p>';
818c2e8227SGreg Roach		}
82d004f62cSGreg Roach
83d004f62cSGreg Roach		foreach ($articles as $article) {
848c2e8227SGreg Roach			$content .= '<div class="journal_box">';
85*d53324c9SGreg Roach			$content .= '<div class="news_title">' . e($article->subject) . '</div>';
86d004f62cSGreg Roach			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
87d004f62cSGreg Roach			if ($article->body == strip_tags($article->body)) {
88d004f62cSGreg Roach				$article->body = nl2br($article->body, false);
898c2e8227SGreg Roach			}
90d004f62cSGreg Roach			$content .= $article->body;
9115d603e7SGreg Roach			$content .= '<a href="editnews.php?news_id=' . $article->news_id . '&amp;ctype=user&amp;ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>';
92d004f62cSGreg Roach			$content .= ' | ';
93*d53324c9SGreg Roach			$content .= '<a href="editnews.php?action=delete&amp;news_id=' . $article->news_id . '&amp;ctype=user&amp;ged=' . $WT_TREE->getNameHtml() . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete “%s”?', e($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
94d004f62cSGreg Roach			$content .= '</div><br>';
958c2e8227SGreg Roach		}
96d004f62cSGreg Roach
9715d603e7SGreg Roach		$content .= '<p><a href="editnews.php?ctype=user&amp;ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a journal entry') . '</a></p>';
988c2e8227SGreg Roach
998c2e8227SGreg Roach		if ($template) {
1009c6524dcSGreg Roach			return View::make('blocks/template', [
1019c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1029c6524dcSGreg Roach				'id'         => $block_id,
1039c6524dcSGreg Roach				'config_url' => '',
1049c6524dcSGreg Roach				'title'      => $this->getTitle(),
1059c6524dcSGreg Roach				'content'    => $content,
1069c6524dcSGreg Roach			]);
1078c2e8227SGreg Roach		} else {
1088c2e8227SGreg Roach			return $content;
1098c2e8227SGreg Roach		}
1108c2e8227SGreg Roach	}
1118c2e8227SGreg Roach
1128c2e8227SGreg Roach	/** {@inheritdoc} */
113a9430be8SGreg Roach	public function loadAjax(): bool {
1148c2e8227SGreg Roach		return false;
1158c2e8227SGreg Roach	}
1168c2e8227SGreg Roach
1178c2e8227SGreg Roach	/** {@inheritdoc} */
118a9430be8SGreg Roach	public function isUserBlock(): bool {
1198c2e8227SGreg Roach		return true;
1208c2e8227SGreg Roach	}
1218c2e8227SGreg Roach
1228c2e8227SGreg Roach	/** {@inheritdoc} */
123a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1248c2e8227SGreg Roach		return false;
1258c2e8227SGreg Roach	}
1268c2e8227SGreg Roach
12776692c8bSGreg Roach	/**
12876692c8bSGreg Roach	 * An HTML form to edit block settings
12976692c8bSGreg Roach	 *
13076692c8bSGreg Roach	 * @param int $block_id
131a9430be8SGreg Roach	 *
132a9430be8SGreg Roach	 * @return void
13376692c8bSGreg Roach	 */
134be9a728cSGreg Roach	public function configureBlock($block_id) {
1358c2e8227SGreg Roach	}
1368c2e8227SGreg Roach}
137