1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2016 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 // NOTE: if we want to set any module-settings, we'll need to move this. 44 Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION); 45 } 46 47 /** 48 * How should this module be labelled on tabs, menus, etc.? 49 * 50 * @return string 51 */ 52 public function getTitle() { 53 return /* I18N: Name of a module */ I18N::translate('News'); 54 } 55 56 /** 57 * A sentence describing what this module does. 58 * 59 * @return string 60 */ 61 public function getDescription() { 62 return /* I18N: Description of the “News” module */ I18N::translate('Family news and site announcements.'); 63 } 64 65 /** 66 * Generate the HTML content of this block. 67 * 68 * @param int $block_id 69 * @param bool $template 70 * @param string[] $cfg 71 * 72 * @return string 73 */ 74 public function getBlock($block_id, $template = true, $cfg = array()) { 75 global $ctype, $WT_TREE; 76 77 switch (Filter::get('action')) { 78 case 'deletenews': 79 $news_id = Filter::get('news_id'); 80 if ($news_id) { 81 Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id)); 82 } 83 break; 84 } 85 86 $more_news = Filter::getInteger('more_news'); 87 $limit = 5 * (1 + $more_news); 88 89 $articles = Database::prepare( 90 "SELECT SQL_CACHE news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) AS updated, subject, body FROM `##news` WHERE gedcom_id = :tree_id ORDER BY updated DESC LIMIT :limit" 91 )->execute(array( 92 'tree_id' => $WT_TREE->getTreeId(), 93 'limit' => $limit, 94 ))->fetchAll(); 95 96 $count = Database::prepare( 97 "SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id" 98 )->execute(array( 99 'tree_id' => $WT_TREE->getTreeId(), 100 ))->fetchOne(); 101 102 $id = $this->getName() . $block_id; 103 $class = $this->getName() . '_block'; 104 $title = $this->getTitle(); 105 $content = ''; 106 107 if (empty($articles)) { 108 $content .= I18N::translate('No news articles have been submitted.'); 109 } 110 111 foreach ($articles as $article) { 112 $content .= '<div class="news_box">'; 113 $content .= '<div class="news_title">' . Filter::escapeHtml($article->subject) . '</div>'; 114 $content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>'; 115 if ($article->body == strip_tags($article->body)) { 116 $article->body = nl2br($article->body, false); 117 } 118 $content .= $article->body; 119 if (Auth::isManager($WT_TREE)) { 120 $content .= '<hr>'; 121 $content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $article->news_id . ', \'_blank\', news_window_specs); return false;">' . I18N::translate('Edit') . '</a>'; 122 $content .= ' | '; 123 $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>'; 124 } 125 $content .= '</div>'; 126 } 127 128 if (Auth::isManager($WT_TREE)) { 129 $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>'; 130 } 131 132 if ($count > $limit) { 133 if (Auth::isManager($WT_TREE)) { 134 $content .= ' | '; 135 } 136 $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>"; 137 } 138 139 if ($template) { 140 return Theme::theme()->formatBlock($id, $title, $class, $content); 141 } else { 142 return $content; 143 } 144 } 145 146 /** {@inheritdoc} */ 147 public function loadAjax() { 148 return false; 149 } 150 151 /** {@inheritdoc} */ 152 public function isUserBlock() { 153 return false; 154 } 155 156 /** {@inheritdoc} */ 157 public function isGedcomBlock() { 158 return true; 159 } 160 161 /** 162 * An HTML form to edit block settings 163 * 164 * @param int $block_id 165 */ 166 public function configureBlock($block_id) { 167 } 168} 169