1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\Functions\FunctionsDate; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Theme; 24 25/** 26 * Class FamilyTreeNewsModule 27 */ 28class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface { 29 // How to update the database schema for this module 30 const SCHEMA_TARGET_VERSION = 3; 31 const SCHEMA_SETTING_NAME = 'NB_SCHEMA_VERSION'; 32 const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema'; 33 34 /** 35 * Create a new module. 36 * 37 * @param string $directory Where is this module installed 38 */ 39 public function __construct($directory) { 40 parent::__construct($directory); 41 42 // Create/update the database tables. 43 Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION); 44 } 45 46 /** 47 * How should this module be labelled on tabs, menus, etc.? 48 * 49 * @return string 50 */ 51 public function getTitle() { 52 return /* I18N: Name of a module */ I18N::translate('News'); 53 } 54 55 /** 56 * A sentence describing what this module does. 57 * 58 * @return string 59 */ 60 public function getDescription() { 61 return /* I18N: Description of the “News” module */ I18N::translate('Family news and site announcements.'); 62 } 63 64 /** 65 * Generate the HTML content of this block. 66 * 67 * @param int $block_id 68 * @param bool $template 69 * @param string[] $cfg 70 * 71 * @return string 72 */ 73 public function getBlock($block_id, $template = true, $cfg = []) { 74 global $ctype, $WT_TREE; 75 76 $more_news = Filter::getInteger('more_news'); 77 $limit = 5 * (1 + $more_news); 78 79 $articles = Database::prepare( 80 "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" 81 )->execute([ 82 'offset' => WT_TIMESTAMP_OFFSET, 83 'tree_id' => $WT_TREE->getTreeId(), 84 'limit' => $limit, 85 ])->fetchAll(); 86 87 $count = Database::prepare( 88 "SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id" 89 )->execute([ 90 'tree_id' => $WT_TREE->getTreeId(), 91 ])->fetchOne(); 92 93 $id = $this->getName() . $block_id; 94 $class = $this->getName() . '_block'; 95 $title = $this->getTitle(); 96 $content = ''; 97 98 if (empty($articles)) { 99 $content .= I18N::translate('No news articles have been submitted.'); 100 } 101 102 foreach ($articles as $article) { 103 $content .= '<div class="news_box">'; 104 $content .= '<div class="news_title">' . Filter::escapeHtml($article->subject) . '</div>'; 105 $content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>'; 106 if ($article->body == strip_tags($article->body)) { 107 $article->body = nl2br($article->body, false); 108 } 109 $content .= $article->body; 110 if (Auth::isManager($WT_TREE)) { 111 $content .= '<hr>'; 112 $content .= '<a href="editnews.php?news_id=' . $article->news_id . '&ctype=gedcom&ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>'; 113 $content .= ' | '; 114 $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”?', Filter::escapeHtml($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>'; 115 } 116 $content .= '</div>'; 117 } 118 119 if (Auth::isManager($WT_TREE)) { 120 $content .= '<p><a href="editnews.php?ctype=gedcom&ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a news article') . '</a></p>'; 121 } 122 123 if ($count > $limit) { 124 if (Auth::isManager($WT_TREE)) { 125 $content .= ' | '; 126 } 127 $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>'; 128 } 129 130 if ($template) { 131 return Theme::theme()->formatBlock($id, $title, $class, $content); 132 } else { 133 return $content; 134 } 135 } 136 137 /** {@inheritdoc} */ 138 public function loadAjax() { 139 return false; 140 } 141 142 /** {@inheritdoc} */ 143 public function isUserBlock() { 144 return false; 145 } 146 147 /** {@inheritdoc} */ 148 public function isGedcomBlock() { 149 return true; 150 } 151 152 /** 153 * An HTML form to edit block settings 154 * 155 * @param int $block_id 156 */ 157 public function configureBlock($block_id) { 158 } 159} 160