xref: /webtrees/app/Module/UserJournalModule.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
18c2e8227SGreg Roach<?php
20e62c4b8SGreg Roachnamespace Fisharebest\Webtrees\Module;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
21*3d7a8a4cSGreg 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 {
298c2e8227SGreg Roach	/** {@inheritdoc} */
303bfb94b0SGreg Roach	public function __construct($directory) {
313bfb94b0SGreg Roach		parent::__construct($directory);
323bfb94b0SGreg Roach
333bfb94b0SGreg Roach		// Create/update the database tables.
343bfb94b0SGreg Roach		Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3);
353bfb94b0SGreg Roach	}
363bfb94b0SGreg Roach
373bfb94b0SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function getTitle() {
398c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Journal');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
428c2e8227SGreg Roach	/** {@inheritdoc} */
438c2e8227SGreg Roach	public function getDescription() {
448c2e8227SGreg Roach		return /* I18N: Description of the “Journal” module */ I18N::translate('A private area to record notes or keep a journal.');
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
478c2e8227SGreg Roach	/** {@inheritdoc} */
488c2e8227SGreg Roach	public function getBlock($block_id, $template = true, $cfg = null) {
498c2e8227SGreg Roach		global $ctype;
508c2e8227SGreg Roach
518c2e8227SGreg Roach		switch (Filter::get('action')) {
528c2e8227SGreg Roach		case 'deletenews':
538c2e8227SGreg Roach			$news_id = Filter::getInteger('news_id');
548c2e8227SGreg Roach			if ($news_id) {
558c2e8227SGreg Roach				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id));
568c2e8227SGreg Roach			}
578c2e8227SGreg Roach			break;
588c2e8227SGreg Roach		}
59e2a378d3SGreg Roach		$block = $this->getBlockSetting($block_id, 'block', '1');
608c2e8227SGreg Roach		if ($cfg) {
618c2e8227SGreg Roach			foreach (array('block') as $name) {
628c2e8227SGreg Roach				if (array_key_exists($name, $cfg)) {
638c2e8227SGreg Roach					$$name = $cfg[$name];
648c2e8227SGreg Roach				}
658c2e8227SGreg Roach			}
668c2e8227SGreg Roach		}
678c2e8227SGreg Roach		$usernews = Database::prepare(
688c2e8227SGreg Roach			"SELECT SQL_CACHE news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) AS updated, subject, body FROM `##news` WHERE user_id = ? ORDER BY updated DESC"
698c2e8227SGreg Roach		)->execute(array(Auth::id()))->fetchAll();
708c2e8227SGreg Roach
718c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
728c2e8227SGreg Roach		$class = $this->getName() . '_block';
738c2e8227SGreg Roach		$title = '';
748c2e8227SGreg Roach		$title .= $this->getTitle();
758c2e8227SGreg Roach		$content = '';
768c2e8227SGreg Roach		if (!$usernews) {
778c2e8227SGreg Roach			$content .= I18N::translate('You have not created any journal items.');
788c2e8227SGreg Roach		}
798c2e8227SGreg Roach		foreach ($usernews as $news) {
808c2e8227SGreg Roach			$content .= '<div class="journal_box">';
818c2e8227SGreg Roach			$content .= '<div class="news_title">' . $news->subject . '</div>';
82*3d7a8a4cSGreg Roach			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($news->updated) . '</div>';
838c2e8227SGreg Roach			if ($news->body == strip_tags($news->body)) {
848c2e8227SGreg Roach				// No HTML?
858c2e8227SGreg Roach				$news->body = nl2br($news->body, false);
868c2e8227SGreg Roach			}
878c2e8227SGreg Roach			$content .= $news->body . '<br><br>';
888c2e8227SGreg Roach			$content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $news->news_id . ', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Edit') . '</a> | ';
898c2e8227SGreg Roach			$content .= '<a href="index.php?action=deletenews&amp;news_id=' . $news->news_id . '&amp;ctype=' . $ctype . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete this journal entry?') . "');\">" . I18N::translate('Delete') . '</a><br>';
908c2e8227SGreg Roach			$content .= "</div><br>";
918c2e8227SGreg Roach		}
928c2e8227SGreg Roach		$content .= '<br><a href="#" onclick="window.open(\'editnews.php?user_id=' . Auth::id() . '\', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Add a new journal entry') . '</a>';
938c2e8227SGreg Roach
948c2e8227SGreg Roach		if ($template) {
958c2e8227SGreg Roach			if ($block) {
968c2e8227SGreg Roach				$class .= ' small_inner_block';
978c2e8227SGreg Roach			}
98cbc1590aSGreg Roach
998c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1008c2e8227SGreg Roach		} else {
1018c2e8227SGreg Roach			return $content;
1028c2e8227SGreg Roach		}
1038c2e8227SGreg Roach	}
1048c2e8227SGreg Roach
1058c2e8227SGreg Roach	/** {@inheritdoc} */
1068c2e8227SGreg Roach	public function loadAjax() {
1078c2e8227SGreg Roach		return false;
1088c2e8227SGreg Roach	}
1098c2e8227SGreg Roach
1108c2e8227SGreg Roach	/** {@inheritdoc} */
1118c2e8227SGreg Roach	public function isUserBlock() {
1128c2e8227SGreg Roach		return true;
1138c2e8227SGreg Roach	}
1148c2e8227SGreg Roach
1158c2e8227SGreg Roach	/** {@inheritdoc} */
1168c2e8227SGreg Roach	public function isGedcomBlock() {
1178c2e8227SGreg Roach		return false;
1188c2e8227SGreg Roach	}
1198c2e8227SGreg Roach
1208c2e8227SGreg Roach	/** {@inheritdoc} */
1218c2e8227SGreg Roach	public function configureBlock($block_id) {
1228c2e8227SGreg Roach	}
1238c2e8227SGreg Roach}
124