1<?php 2namespace Fisharebest\Webtrees\Module; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Theme; 23 24/** 25 * Class FamilyTreeNewsModule 26 */ 27class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface { 28 /** {@inheritdoc} */ 29 public function __construct($directory) { 30 parent::__construct($directory); 31 32 // Create/update the database tables. 33 Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3); 34 } 35 36 /** {@inheritdoc} */ 37 public function getTitle() { 38 return /* I18N: Name of a module */ I18N::translate('News'); 39 } 40 41 /** {@inheritdoc} */ 42 public function getDescription() { 43 return /* I18N: Description of the “GEDCOM News” module */ I18N::translate('Family news and site announcements.'); 44 } 45 46 /** {@inheritdoc} */ 47 public function getBlock($block_id, $template = true, $cfg = null) { 48 global $ctype, $WT_TREE; 49 50 switch (Filter::get('action')) { 51 case 'deletenews': 52 $news_id = Filter::get('news_id'); 53 if ($news_id) { 54 Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id)); 55 } 56 break; 57 } 58 59 if (isset($_REQUEST['gedcom_news_archive'])) { 60 $limit = 'nolimit'; 61 $flag = '0'; 62 } else { 63 $flag = $this->getBlockSetting($block_id, 'flag', 0); 64 if ($flag === '0') { 65 $limit = 'nolimit'; 66 } else { 67 $limit = $this->getBlockSetting($block_id, 'limit', 'nolimit'); 68 } 69 } 70 if ($cfg) { 71 foreach (array('limit', 'flag') as $name) { 72 if (array_key_exists($name, $cfg)) { 73 $$name = $cfg[$name]; 74 } 75 } 76 } 77 $usernews = Database::prepare( 78 "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" 79 )->execute(array($WT_TREE->getTreeId()))->fetchAll(); 80 81 $id = $this->getName() . $block_id; 82 $class = $this->getName() . '_block'; 83 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 84 $title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 85 } else { 86 $title = ''; 87 } 88 $title .= $this->getTitle(); 89 90 $content = ''; 91 if (count($usernews) == 0) { 92 $content .= I18N::translate('No news articles have been submitted.') . '<br>'; 93 } 94 $c = 0; 95 foreach ($usernews as $news) { 96 if ($limit == 'count') { 97 if ($c >= $flag) { 98 break; 99 } 100 $c++; 101 } 102 if ($limit == 'date') { 103 if ((int) ((WT_TIMESTAMP - $news->updated) / 86400) > $flag) { 104 break; 105 } 106 } 107 $content .= '<div class="news_box" id="article' . $news->news_id . '">'; 108 $content .= '<div class="news_title">' . Filter::escapeHtml($news->subject) . '</div>'; 109 $content .= '<div class="news_date">' . format_timestamp($news->updated) . '</div>'; 110 if ($news->body == strip_tags($news->body)) { 111 $news->body = nl2br($news->body, false); 112 } 113 $content .= $news->body; 114 // Print Admin options for this News item 115 if (Auth::isManager($WT_TREE)) { 116 $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 . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete this news article?') . "');\">" . I18N::translate('Delete') . '</a><br>'; 117 } 118 $content .= '</div>'; 119 } 120 $printedAddLink = false; 121 if (Auth::isManager($WT_TREE)) { 122 $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>"; 123 $printedAddLink = true; 124 } 125 if ($limit == 'date' || $limit == 'count') { 126 if ($printedAddLink) { 127 $content .= ' | '; 128 } 129 $content .= '<a href="index.php?gedcom_news_archive=yes&ctype=' . $ctype . '">' . I18N::translate('View archive') . "</a>"; 130 $content .= help_link('gedcom_news_archive') . '<br>'; 131 } 132 133 if ($template) { 134 return Theme::theme()->formatBlock($id, $title, $class, $content); 135 } else { 136 return $content; 137 } 138 } 139 140 /** {@inheritdoc} */ 141 public function loadAjax() { 142 return false; 143 } 144 145 /** {@inheritdoc} */ 146 public function isUserBlock() { 147 return false; 148 } 149 150 /** {@inheritdoc} */ 151 public function isGedcomBlock() { 152 return true; 153 } 154 155 /** {@inheritdoc} */ 156 public function configureBlock($block_id) { 157 if (Filter::postBool('save') && Filter::checkCsrf()) { 158 $this->setBlockSetting($block_id, 'limit', Filter::post('limit')); 159 $this->setBlockSetting($block_id, 'flag', Filter::post('flag')); 160 } 161 162 $limit = $this->getBlockSetting($block_id, 'limit', 'nolimit'); 163 $flag = $this->getBlockSetting($block_id, 'flag', 0); 164 165 echo 166 '<tr><td class="descriptionbox wrap width33">', 167 /* I18N: Limit display by [age/number] */ I18N::translate('Limit display by'), 168 '</td><td class="optionbox"><select name="limit"><option value="nolimit" ', 169 ($limit == 'nolimit' ? 'selected' : '') . ">", 170 I18N::translate('No limit') . "</option>", 171 '<option value="date" ' . ($limit == 'date' ? 'selected' : '') . ">" . I18N::translate('Age of item') . "</option>", 172 '<option value="count" ' . ($limit == 'count' ? 'selected' : '') . ">" . I18N::translate('Number of items') . "</option>", 173 '</select></td></tr>'; 174 175 echo '<tr><td class="descriptionbox wrap width33">'; 176 echo I18N::translate('Limit'); 177 echo '</td><td class="optionbox"><input type="text" name="flag" size="4" maxlength="4" value="' . $flag . '"></td></tr>'; 178 } 179} 180