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 FamilyTreeNewsModule 278c2e8227SGreg Roach */ 28e2a378d3SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface { 297fafef87SGreg Roach // How to update the database schema for this module 307fafef87SGreg Roach const SCHEMA_TARGET_VERSION = 3; 317fafef87SGreg Roach const SCHEMA_SETTING_NAME = 'NB_SCHEMA_VERSION'; 327fafef87SGreg Roach const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema'; 337fafef87SGreg Roach 3476692c8bSGreg Roach /** 3576692c8bSGreg Roach * Create a new module. 3676692c8bSGreg Roach * 3776692c8bSGreg Roach * @param string $directory Where is this module installed 3876692c8bSGreg Roach */ 393bfb94b0SGreg Roach public function __construct($directory) { 403bfb94b0SGreg Roach parent::__construct($directory); 413bfb94b0SGreg Roach 423bfb94b0SGreg Roach // Create/update the database tables. 437fafef87SGreg Roach // NOTE: if we want to set any module-settings, we'll need to move this. 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 */ 7476692c8bSGreg Roach public function getBlock($block_id, $template = true, $cfg = array()) { 754b9ff166SGreg Roach global $ctype, $WT_TREE; 768c2e8227SGreg Roach 778c2e8227SGreg Roach switch (Filter::get('action')) { 788c2e8227SGreg Roach case 'deletenews': 798c2e8227SGreg Roach $news_id = Filter::get('news_id'); 808c2e8227SGreg Roach if ($news_id) { 818c2e8227SGreg Roach Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id)); 828c2e8227SGreg Roach } 838c2e8227SGreg Roach break; 848c2e8227SGreg Roach } 858c2e8227SGreg Roach 86d004f62cSGreg Roach $more_news = Filter::getInteger('more_news'); 87d004f62cSGreg Roach $limit = 5 * (1 + $more_news); 88d004f62cSGreg Roach 89d004f62cSGreg Roach $articles = Database::prepare( 90*45d06cf3SGreg 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" 91d004f62cSGreg Roach )->execute(array( 92*45d06cf3SGreg Roach 'offset' => WT_TIMESTAMP_OFFSET, 93d004f62cSGreg Roach 'tree_id' => $WT_TREE->getTreeId(), 94d004f62cSGreg Roach 'limit' => $limit, 95d004f62cSGreg Roach ))->fetchAll(); 96d004f62cSGreg Roach 97d004f62cSGreg Roach $count = Database::prepare( 98d004f62cSGreg Roach "SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id" 99d004f62cSGreg Roach )->execute(array( 100d004f62cSGreg Roach 'tree_id' => $WT_TREE->getTreeId(), 101d004f62cSGreg Roach ))->fetchOne(); 1028c2e8227SGreg Roach 1038c2e8227SGreg Roach $id = $this->getName() . $block_id; 1048c2e8227SGreg Roach $class = $this->getName() . '_block'; 105d004f62cSGreg Roach $title = $this->getTitle(); 1068c2e8227SGreg Roach $content = ''; 107d004f62cSGreg Roach 108d004f62cSGreg Roach if (empty($articles)) { 109d004f62cSGreg Roach $content .= I18N::translate('No news articles have been submitted.'); 1108c2e8227SGreg Roach } 111d004f62cSGreg Roach 112d004f62cSGreg Roach foreach ($articles as $article) { 113d004f62cSGreg Roach $content .= '<div class="news_box">'; 114d004f62cSGreg Roach $content .= '<div class="news_title">' . Filter::escapeHtml($article->subject) . '</div>'; 115d004f62cSGreg Roach $content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>'; 116d004f62cSGreg Roach if ($article->body == strip_tags($article->body)) { 117d004f62cSGreg Roach $article->body = nl2br($article->body, false); 1188c2e8227SGreg Roach } 119d004f62cSGreg Roach $content .= $article->body; 1204b9ff166SGreg Roach if (Auth::isManager($WT_TREE)) { 121d004f62cSGreg Roach $content .= '<hr>'; 122d004f62cSGreg Roach $content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $article->news_id . ', \'_blank\', news_window_specs); return false;">' . I18N::translate('Edit') . '</a>'; 123d004f62cSGreg Roach $content .= ' | '; 124d004f62cSGreg Roach $content .= '<a href="index.php?action=deletenews&news_id=' . $article->news_id . '&ctype=' . $ctype . '&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>'; 1258c2e8227SGreg Roach } 1268c2e8227SGreg Roach $content .= '</div>'; 1278c2e8227SGreg Roach } 128d004f62cSGreg Roach 1294b9ff166SGreg Roach if (Auth::isManager($WT_TREE)) { 130d004f62cSGreg Roach $content .= '<a href="#" onclick="window.open(\'editnews.php?gedcom_id=' . $WT_TREE->getTreeId() . '\', \'_blank\', news_window_specs); return false;">' . I18N::translate('Add a news article') . '</a>'; 1318c2e8227SGreg Roach } 132d004f62cSGreg Roach 133d004f62cSGreg Roach if ($count > $limit) { 134d004f62cSGreg Roach if (Auth::isManager($WT_TREE)) { 135d004f62cSGreg Roach $content .= ' | '; 1368c2e8227SGreg Roach } 1377eaa0127SGreg Roach $content .= '<a href="#" onclick="jQuery(\'#' . $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>"; 1388c2e8227SGreg Roach } 1398c2e8227SGreg Roach 1408c2e8227SGreg Roach if ($template) { 1418c2e8227SGreg Roach return Theme::theme()->formatBlock($id, $title, $class, $content); 1428c2e8227SGreg Roach } else { 1438c2e8227SGreg Roach return $content; 1448c2e8227SGreg Roach } 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 1478c2e8227SGreg Roach /** {@inheritdoc} */ 1488c2e8227SGreg Roach public function loadAjax() { 1498c2e8227SGreg Roach return false; 1508c2e8227SGreg Roach } 1518c2e8227SGreg Roach 1528c2e8227SGreg Roach /** {@inheritdoc} */ 1538c2e8227SGreg Roach public function isUserBlock() { 1548c2e8227SGreg Roach return false; 1558c2e8227SGreg Roach } 1568c2e8227SGreg Roach 1578c2e8227SGreg Roach /** {@inheritdoc} */ 1588c2e8227SGreg Roach public function isGedcomBlock() { 1598c2e8227SGreg Roach return true; 1608c2e8227SGreg Roach } 1618c2e8227SGreg Roach 16276692c8bSGreg Roach /** 16376692c8bSGreg Roach * An HTML form to edit block settings 16476692c8bSGreg Roach * 16576692c8bSGreg Roach * @param int $block_id 16676692c8bSGreg Roach */ 1678c2e8227SGreg Roach public function configureBlock($block_id) { 1688c2e8227SGreg Roach } 1698c2e8227SGreg Roach} 170