xref: /webtrees/app/Module/UserJournalModule.php (revision 13abd6f3a37322f885d85df150e105d27ad81f8d)
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\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class UserJournalModule
278c2e8227SGreg Roach */
28e2a378d3SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface {
2976692c8bSGreg Roach	/**
3076692c8bSGreg Roach	 * Create a new module.
3176692c8bSGreg Roach	 *
3276692c8bSGreg Roach	 * @param string $directory Where is this module installed
3376692c8bSGreg Roach	 */
343bfb94b0SGreg Roach	public function __construct($directory) {
353bfb94b0SGreg Roach		parent::__construct($directory);
363bfb94b0SGreg Roach
373bfb94b0SGreg Roach		// Create/update the database tables.
383bfb94b0SGreg Roach		Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3);
393bfb94b0SGreg Roach	}
403bfb94b0SGreg Roach
4176692c8bSGreg Roach	/**
4276692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
4376692c8bSGreg Roach	 *
4476692c8bSGreg Roach	 * @return string
4576692c8bSGreg Roach	 */
468c2e8227SGreg Roach	public function getTitle() {
478c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Journal');
488c2e8227SGreg Roach	}
498c2e8227SGreg Roach
5076692c8bSGreg Roach	/**
5176692c8bSGreg Roach	 * A sentence describing what this module does.
5276692c8bSGreg Roach	 *
5376692c8bSGreg Roach	 * @return string
5476692c8bSGreg Roach	 */
558c2e8227SGreg Roach	public function getDescription() {
568c2e8227SGreg Roach		return /* I18N: Description of the “Journal” module */ I18N::translate('A private area to record notes or keep a journal.');
578c2e8227SGreg Roach	}
588c2e8227SGreg Roach
5976692c8bSGreg Roach	/**
6076692c8bSGreg Roach	 * Generate the HTML content of this block.
6176692c8bSGreg Roach	 *
6276692c8bSGreg Roach	 * @param int      $block_id
6376692c8bSGreg Roach	 * @param bool     $template
64727f238cSGreg Roach	 * @param string[] $cfg
6576692c8bSGreg Roach	 *
6676692c8bSGreg Roach	 * @return string
6776692c8bSGreg Roach	 */
68*13abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
69a3dfb74cSGreg Roach		global $ctype, $WT_TREE;
708c2e8227SGreg Roach
718c2e8227SGreg Roach		switch (Filter::get('action')) {
728c2e8227SGreg Roach		case 'deletenews':
738c2e8227SGreg Roach			$news_id = Filter::getInteger('news_id');
748c2e8227SGreg Roach			if ($news_id) {
75*13abd6f3SGreg Roach				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute([$news_id]);
768c2e8227SGreg Roach			}
778c2e8227SGreg Roach			break;
788c2e8227SGreg Roach		}
79d004f62cSGreg Roach
80d004f62cSGreg Roach		$articles = Database::prepare(
8145d06cf3SGreg 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"
82*13abd6f3SGreg Roach		)->execute([
8345d06cf3SGreg Roach			'offset'  => WT_TIMESTAMP_OFFSET,
8445d06cf3SGreg Roach			'user_id' => Auth::id(),
85*13abd6f3SGreg Roach		])->fetchAll();
868c2e8227SGreg Roach
878c2e8227SGreg Roach		$id      = $this->getName() . $block_id;
888c2e8227SGreg Roach		$class   = $this->getName() . '_block';
89d004f62cSGreg Roach		$title   = $this->getTitle();
908c2e8227SGreg Roach		$content = '';
91d004f62cSGreg Roach
92d004f62cSGreg Roach		if (empty($articles)) {
93fc21c690SGreg Roach			$content .= '<p>' . I18N::translate('You have not created any journal items.') . '</p>';
948c2e8227SGreg Roach		}
95d004f62cSGreg Roach
96d004f62cSGreg Roach		foreach ($articles as $article) {
978c2e8227SGreg Roach			$content .= '<div class="journal_box">';
98d004f62cSGreg Roach			$content .= '<div class="news_title">' . Filter::escapeHtml($article->subject) . '</div>';
99d004f62cSGreg Roach			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
100d004f62cSGreg Roach			if ($article->body == strip_tags($article->body)) {
101d004f62cSGreg Roach				$article->body = nl2br($article->body, false);
1028c2e8227SGreg Roach			}
103d004f62cSGreg Roach			$content .= $article->body;
104d004f62cSGreg Roach			$content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $article->news_id . ', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Edit') . '</a>';
105d004f62cSGreg Roach			$content .= ' | ';
106d004f62cSGreg Roach			$content .= '<a href="index.php?action=deletenews&amp;news_id=' . $article->news_id . '&amp;ctype=' . $ctype . '&amp;ged=' . $WT_TREE->getNameHtml() . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete “%s”?', Filter::escapeHtml($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
107d004f62cSGreg Roach			$content .= '</div><br>';
1088c2e8227SGreg Roach		}
109d004f62cSGreg Roach
110fc21c690SGreg Roach		$content .= '<p><a href="#" onclick="window.open(\'editnews.php?user_id=' . Auth::id() . '\', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Add a journal entry') . '</a></p>';
1118c2e8227SGreg Roach
1128c2e8227SGreg Roach		if ($template) {
1138c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1148c2e8227SGreg Roach		} else {
1158c2e8227SGreg Roach			return $content;
1168c2e8227SGreg Roach		}
1178c2e8227SGreg Roach	}
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach	/** {@inheritdoc} */
1208c2e8227SGreg Roach	public function loadAjax() {
1218c2e8227SGreg Roach		return false;
1228c2e8227SGreg Roach	}
1238c2e8227SGreg Roach
1248c2e8227SGreg Roach	/** {@inheritdoc} */
1258c2e8227SGreg Roach	public function isUserBlock() {
1268c2e8227SGreg Roach		return true;
1278c2e8227SGreg Roach	}
1288c2e8227SGreg Roach
1298c2e8227SGreg Roach	/** {@inheritdoc} */
1308c2e8227SGreg Roach	public function isGedcomBlock() {
1318c2e8227SGreg Roach		return false;
1328c2e8227SGreg Roach	}
1338c2e8227SGreg Roach
13476692c8bSGreg Roach	/**
13576692c8bSGreg Roach	 * An HTML form to edit block settings
13676692c8bSGreg Roach	 *
13776692c8bSGreg Roach	 * @param int $block_id
13876692c8bSGreg Roach	 */
1398c2e8227SGreg Roach	public function configureBlock($block_id) {
1408c2e8227SGreg Roach	}
1418c2e8227SGreg Roach}
142