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