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; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate; 22047f239bSGreg Roachuse Fisharebest\Webtrees\Html; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 249c6524dcSGreg Roachuse Fisharebest\Webtrees\View; 258c2e8227SGreg Roach 268c2e8227SGreg Roach/** 278c2e8227SGreg Roach * Class FamilyTreeNewsModule 288c2e8227SGreg Roach */ 29e2a378d3SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface { 307fafef87SGreg Roach // How to update the database schema for this module 317fafef87SGreg Roach const SCHEMA_TARGET_VERSION = 3; 327fafef87SGreg Roach const SCHEMA_SETTING_NAME = 'NB_SCHEMA_VERSION'; 337fafef87SGreg Roach const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema'; 347fafef87SGreg Roach 3576692c8bSGreg Roach /** 3676692c8bSGreg Roach * Create a new module. 3776692c8bSGreg Roach * 3876692c8bSGreg Roach * @param string $directory Where is this module installed 3976692c8bSGreg Roach */ 403bfb94b0SGreg Roach public function __construct($directory) { 413bfb94b0SGreg Roach parent::__construct($directory); 423bfb94b0SGreg Roach 433bfb94b0SGreg Roach // Create/update the database tables. 447fafef87SGreg Roach Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION); 453bfb94b0SGreg Roach } 463bfb94b0SGreg Roach 4776692c8bSGreg Roach /** 4876692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 4976692c8bSGreg Roach * 5076692c8bSGreg Roach * @return string 5176692c8bSGreg Roach */ 528c2e8227SGreg Roach public function getTitle() { 538c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('News'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 5676692c8bSGreg Roach /** 5776692c8bSGreg Roach * A sentence describing what this module does. 5876692c8bSGreg Roach * 5976692c8bSGreg Roach * @return string 6076692c8bSGreg Roach */ 618c2e8227SGreg Roach public function getDescription() { 62d004f62cSGreg Roach return /* I18N: Description of the “News” module */ I18N::translate('Family news and site announcements.'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 6576692c8bSGreg Roach /** 6676692c8bSGreg Roach * Generate the HTML content of this block. 6776692c8bSGreg Roach * 6876692c8bSGreg Roach * @param int $block_id 6976692c8bSGreg Roach * @param bool $template 70727f238cSGreg Roach * @param string[] $cfg 7176692c8bSGreg Roach * 7276692c8bSGreg Roach * @return string 7376692c8bSGreg Roach */ 74a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 754b9ff166SGreg Roach global $ctype, $WT_TREE; 768c2e8227SGreg Roach 77d004f62cSGreg Roach $more_news = Filter::getInteger('more_news'); 78d004f62cSGreg Roach $limit = 5 * (1 + $more_news); 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 gedcom_id = :tree_id ORDER BY updated DESC LIMIT :limit" 8213abd6f3SGreg Roach )->execute([ 8345d06cf3SGreg Roach 'offset' => WT_TIMESTAMP_OFFSET, 84d004f62cSGreg Roach 'tree_id' => $WT_TREE->getTreeId(), 85d004f62cSGreg Roach 'limit' => $limit, 8613abd6f3SGreg Roach ])->fetchAll(); 87d004f62cSGreg Roach 88d004f62cSGreg Roach $count = Database::prepare( 89d004f62cSGreg Roach "SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id" 9013abd6f3SGreg Roach )->execute([ 91d004f62cSGreg Roach 'tree_id' => $WT_TREE->getTreeId(), 9213abd6f3SGreg Roach ])->fetchOne(); 938c2e8227SGreg Roach 948c2e8227SGreg Roach $id = $this->getName() . $block_id; 958c2e8227SGreg Roach $class = $this->getName() . '_block'; 96d004f62cSGreg Roach $title = $this->getTitle(); 978c2e8227SGreg Roach $content = ''; 98d004f62cSGreg Roach 99d004f62cSGreg Roach if (empty($articles)) { 100d004f62cSGreg Roach $content .= I18N::translate('No news articles have been submitted.'); 1018c2e8227SGreg Roach } 102d004f62cSGreg Roach 103d004f62cSGreg Roach foreach ($articles as $article) { 104d004f62cSGreg Roach $content .= '<div class="news_box">'; 105cc5ab399SGreg Roach $content .= '<div class="news_title">' . Html::escape($article->subject) . '</div>'; 106d004f62cSGreg Roach $content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>'; 107d004f62cSGreg Roach if ($article->body == strip_tags($article->body)) { 108d004f62cSGreg Roach $article->body = nl2br($article->body, false); 1098c2e8227SGreg Roach } 110d004f62cSGreg Roach $content .= $article->body; 1114b9ff166SGreg Roach if (Auth::isManager($WT_TREE)) { 112d004f62cSGreg Roach $content .= '<hr>'; 11315d603e7SGreg Roach $content .= '<a href="editnews.php?news_id=' . $article->news_id . '&ctype=gedcom&ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>'; 114d004f62cSGreg Roach $content .= ' | '; 115cc5ab399SGreg Roach $content .= '<a href="editnews.php?action=delete&news_id=' . $article->news_id . '&ctype=gedcom&ged=' . $WT_TREE->getNameHtml() . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete “%s”?', Html::escape($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>'; 1168c2e8227SGreg Roach } 1178c2e8227SGreg Roach $content .= '</div>'; 1188c2e8227SGreg Roach } 119d004f62cSGreg Roach 1204b9ff166SGreg Roach if (Auth::isManager($WT_TREE)) { 12115d603e7SGreg Roach $content .= '<p><a href="editnews.php?ctype=gedcom&ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a news article') . '</a></p>'; 1228c2e8227SGreg Roach } 123d004f62cSGreg Roach 124d004f62cSGreg Roach if ($count > $limit) { 125d004f62cSGreg Roach if (Auth::isManager($WT_TREE)) { 126d004f62cSGreg Roach $content .= ' | '; 1278c2e8227SGreg Roach } 128564ae2d7SGreg Roach $content .= '<a href="#" onclick="$(\'#' . $id . '\').load(\'index.php?ctype=gedcom&ged=' . $WT_TREE->getNameUrl() . '&block_id=' . $block_id . '&action=ajax&more_news=' . ($more_news + 1) . '\'); return false;">' . I18N::translate('More news articles') . '</a>'; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 1318c2e8227SGreg Roach if ($template) { 1329c6524dcSGreg Roach return View::make('blocks/template', [ 1339c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1349c6524dcSGreg Roach 'id' => $block_id, 1359c6524dcSGreg Roach 'config_url' => '', 1369c6524dcSGreg Roach 'title' => $this->getTitle(), 1379c6524dcSGreg Roach 'content' => $content, 1389c6524dcSGreg Roach ]); 1398c2e8227SGreg Roach } else { 1408c2e8227SGreg Roach return $content; 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach } 1438c2e8227SGreg Roach 1448c2e8227SGreg Roach /** {@inheritdoc} */ 145a9430be8SGreg Roach public function loadAjax(): bool { 1468c2e8227SGreg Roach return false; 1478c2e8227SGreg Roach } 1488c2e8227SGreg Roach 1498c2e8227SGreg Roach /** {@inheritdoc} */ 150a9430be8SGreg Roach public function isUserBlock(): bool { 1518c2e8227SGreg Roach return false; 1528c2e8227SGreg Roach } 1538c2e8227SGreg Roach 1548c2e8227SGreg Roach /** {@inheritdoc} */ 155a9430be8SGreg Roach public function isGedcomBlock(): bool { 1568c2e8227SGreg Roach return true; 1578c2e8227SGreg Roach } 1588c2e8227SGreg Roach 15976692c8bSGreg Roach /** 16076692c8bSGreg Roach * An HTML form to edit block settings 16176692c8bSGreg Roach * 16276692c8bSGreg Roach * @param int $block_id 163a9430be8SGreg Roach * 164a9430be8SGreg Roach * @return void 16576692c8bSGreg Roach */ 166*be9a728cSGreg Roach public function configureBlock($block_id) { 1678c2e8227SGreg Roach } 1688c2e8227SGreg Roach} 169