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\Functions\FunctionsPrint; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Theme; 25 26/** 27 * Class FamilyTreeNewsModule 28 */ 29class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface { 30 // How to update the database schema for this module 31 const SCHEMA_TARGET_VERSION = 3; 32 const SCHEMA_SETTING_NAME = 'NB_SCHEMA_VERSION'; 33 const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema'; 34 35 /** 36 * Create a new module. 37 * 38 * @param string $directory Where is this module installed 39 */ 40 public function __construct($directory) { 41 parent::__construct($directory); 42 43 // Create/update the database tables. 44 // NOTE: if we want to set any module-settings, we'll need to move this. 45 Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION); 46 } 47 48 /** 49 * How should this module be labelled on tabs, menus, etc.? 50 * 51 * @return string 52 */ 53 public function getTitle() { 54 return /* I18N: Name of a module */ I18N::translate('News'); 55 } 56 57 /** 58 * A sentence describing what this module does. 59 * 60 * @return string 61 */ 62 public function getDescription() { 63 return /* I18N: Description of the “GEDCOM News” module */ I18N::translate('Family news and site announcements.'); 64 } 65 66 /** 67 * Generate the HTML content of this block. 68 * 69 * @param int $block_id 70 * @param bool $template 71 * @param string[] $cfg 72 * 73 * @return string 74 */ 75 public function getBlock($block_id, $template = true, $cfg = array()) { 76 global $ctype, $WT_TREE; 77 78 switch (Filter::get('action')) { 79 case 'deletenews': 80 $news_id = Filter::get('news_id'); 81 if ($news_id) { 82 Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id)); 83 } 84 break; 85 } 86 87 if (isset($_REQUEST['gedcom_news_archive'])) { 88 $limit = 'nolimit'; 89 $flag = '0'; 90 } else { 91 $flag = $this->getBlockSetting($block_id, 'flag', 0); 92 if ($flag === '0') { 93 $limit = 'nolimit'; 94 } else { 95 $limit = $this->getBlockSetting($block_id, 'limit', 'nolimit'); 96 } 97 } 98 foreach (array('limit', 'flag') as $name) { 99 if (array_key_exists($name, $cfg)) { 100 $$name = $cfg[$name]; 101 } 102 } 103 $usernews = Database::prepare( 104 "SELECT SQL_CACHE news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) AS updated, subject, body FROM `##news` WHERE gedcom_id=? ORDER BY updated DESC" 105 )->execute(array($WT_TREE->getTreeId()))->fetchAll(); 106 107 $id = $this->getName() . $block_id; 108 $class = $this->getName() . '_block'; 109 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 110 $title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 111 } else { 112 $title = ''; 113 } 114 $title .= $this->getTitle(); 115 116 $content = ''; 117 if (count($usernews) == 0) { 118 $content .= I18N::translate('No news articles have been submitted.') . '<br>'; 119 } 120 $c = 0; 121 foreach ($usernews as $news) { 122 if ($limit == 'count') { 123 if ($c >= $flag) { 124 break; 125 } 126 $c++; 127 } 128 if ($limit == 'date') { 129 if ((int) ((WT_TIMESTAMP - $news->updated) / 86400) > $flag) { 130 break; 131 } 132 } 133 $content .= '<div class="news_box" id="article' . $news->news_id . '">'; 134 $content .= '<div class="news_title">' . Filter::escapeHtml($news->subject) . '</div>'; 135 $content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($news->updated) . '</div>'; 136 if ($news->body == strip_tags($news->body)) { 137 $news->body = nl2br($news->body, false); 138 } 139 $content .= $news->body; 140 // Print Admin options for this News item 141 if (Auth::isManager($WT_TREE)) { 142 $content .= '<hr>' . '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $news->news_id . ', \'_blank\', news_window_specs); return false;">' . I18N::translate('Edit') . '</a> | ' . '<a href="index.php?action=deletenews&news_id=' . $news->news_id . '&ctype=' . $ctype . '&ged=' . $WT_TREE->getNameHtml() . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete “%s”?', Filter::escapeHtml($news->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>'; 143 } 144 $content .= '</div>'; 145 } 146 $printedAddLink = false; 147 if (Auth::isManager($WT_TREE)) { 148 $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>"; 149 $printedAddLink = true; 150 } 151 if ($limit == 'date' || $limit == 'count') { 152 if ($printedAddLink) { 153 $content .= ' | '; 154 } 155 $content .= '<a href="index.php?gedcom_news_archive=yes&ctype=' . $ctype . '&ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('View the archive') . "</a>"; 156 $content .= FunctionsPrint::helpLink('gedcom_news_archive') . '<br>'; 157 } 158 159 if ($template) { 160 return Theme::theme()->formatBlock($id, $title, $class, $content); 161 } else { 162 return $content; 163 } 164 } 165 166 /** {@inheritdoc} */ 167 public function loadAjax() { 168 return false; 169 } 170 171 /** {@inheritdoc} */ 172 public function isUserBlock() { 173 return false; 174 } 175 176 /** {@inheritdoc} */ 177 public function isGedcomBlock() { 178 return true; 179 } 180 181 /** 182 * An HTML form to edit block settings 183 * 184 * @param int $block_id 185 */ 186 public function configureBlock($block_id) { 187 if (Filter::postBool('save') && Filter::checkCsrf()) { 188 $this->setBlockSetting($block_id, 'limit', Filter::post('limit')); 189 $this->setBlockSetting($block_id, 'flag', Filter::post('flag')); 190 } 191 192 $limit = $this->getBlockSetting($block_id, 'limit', 'nolimit'); 193 $flag = $this->getBlockSetting($block_id, 'flag', 0); 194 195 echo 196 '<tr><td class="descriptionbox wrap width33">', 197 /* I18N: Limit display by [age/number] */ I18N::translate('Limit display by'), 198 '</td><td class="optionbox"><select name="limit"><option value="nolimit" ', 199 ($limit == 'nolimit' ? 'selected' : '') . ">", 200 I18N::translate('No limit') . "</option>", 201 '<option value="date" ' . ($limit == 'date' ? 'selected' : '') . ">" . I18N::translate('Age of item') . "</option>", 202 '<option value="count" ' . ($limit == 'count' ? 'selected' : '') . ">" . I18N::translate('Number of items') . "</option>", 203 '</select></td></tr>'; 204 205 echo '<tr><td class="descriptionbox wrap width33">'; 206 echo I18N::translate('Limit'); 207 echo '</td><td class="optionbox"><input type="text" name="flag" size="4" maxlength="4" value="' . $flag . '"></td></tr>'; 208 } 209} 210