xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 147e99aae08229316226c46b32d24d4f8bce04e0)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
21fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
22fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
23fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request;
24fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response;
25fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class FamilyTreeNewsModule
298c2e8227SGreg Roach */
30e2a378d3SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface {
317fafef87SGreg Roach	// How to update the database schema for this module
327fafef87SGreg Roach	const SCHEMA_TARGET_VERSION   = 3;
337fafef87SGreg Roach	const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
347fafef87SGreg Roach	const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
357fafef87SGreg Roach
3676692c8bSGreg Roach	/**
3776692c8bSGreg Roach	 * Create a new module.
3876692c8bSGreg Roach	 *
3976692c8bSGreg Roach	 * @param string $directory Where is this module installed
4076692c8bSGreg Roach	 */
413bfb94b0SGreg Roach	public function __construct($directory) {
423bfb94b0SGreg Roach		parent::__construct($directory);
433bfb94b0SGreg Roach
443bfb94b0SGreg Roach		// Create/update the database tables.
457fafef87SGreg Roach		Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
463bfb94b0SGreg Roach	}
473bfb94b0SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @return string
5276692c8bSGreg Roach	 */
538c2e8227SGreg Roach	public function getTitle() {
548c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('News');
558c2e8227SGreg Roach	}
568c2e8227SGreg Roach
5776692c8bSGreg Roach	/**
5876692c8bSGreg Roach	 * A sentence describing what this module does.
5976692c8bSGreg Roach	 *
6076692c8bSGreg Roach	 * @return string
6176692c8bSGreg Roach	 */
628c2e8227SGreg Roach	public function getDescription() {
63d004f62cSGreg Roach		return /* I18N: Description of the “News” module */ I18N::translate('Family news and site announcements.');
648c2e8227SGreg Roach	}
658c2e8227SGreg Roach
6676692c8bSGreg Roach	/**
6776692c8bSGreg Roach	 * Generate the HTML content of this block.
6876692c8bSGreg Roach	 *
69e490cd80SGreg Roach	 * @param Tree     $tree
7076692c8bSGreg Roach	 * @param int      $block_id
7176692c8bSGreg Roach	 * @param bool     $template
72727f238cSGreg Roach	 * @param string[] $cfg
7376692c8bSGreg Roach	 *
7476692c8bSGreg Roach	 * @return string
7576692c8bSGreg Roach	 */
76e490cd80SGreg Roach	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
77d004f62cSGreg Roach		$articles = Database::prepare(
78e5588fb0SGreg Roach			"SELECT 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"
7913abd6f3SGreg Roach		)->execute([
8045d06cf3SGreg Roach			'offset'  => WT_TIMESTAMP_OFFSET,
81e490cd80SGreg Roach			'tree_id' => $tree->getTreeId(),
8213abd6f3SGreg Roach		])->fetchAll();
83d004f62cSGreg Roach
84*147e99aaSGreg Roach		$content = view('modules/gedcom_news/list', [
85fe8a65d1SGreg Roach			'articles' => $articles,
86fe8a65d1SGreg Roach			'block_id' => $block_id,
87fe8a65d1SGreg Roach			'limit'    => 5,
88fe8a65d1SGreg Roach		]);
898c2e8227SGreg Roach
908c2e8227SGreg Roach		if ($template) {
91*147e99aaSGreg Roach			return view('modules/block-template', [
929c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
939c6524dcSGreg Roach				'id'         => $block_id,
949c6524dcSGreg Roach				'config_url' => '',
959c6524dcSGreg Roach				'title'      => $this->getTitle(),
969c6524dcSGreg Roach				'content'    => $content,
979c6524dcSGreg Roach			]);
988c2e8227SGreg Roach		} else {
998c2e8227SGreg Roach			return $content;
1008c2e8227SGreg Roach		}
1018c2e8227SGreg Roach	}
1028c2e8227SGreg Roach
1038c2e8227SGreg Roach	/** {@inheritdoc} */
104a9430be8SGreg Roach	public function loadAjax(): bool {
1058c2e8227SGreg Roach		return false;
1068c2e8227SGreg Roach	}
1078c2e8227SGreg Roach
1088c2e8227SGreg Roach	/** {@inheritdoc} */
109a9430be8SGreg Roach	public function isUserBlock(): bool {
1108c2e8227SGreg Roach		return false;
1118c2e8227SGreg Roach	}
1128c2e8227SGreg Roach
1138c2e8227SGreg Roach	/** {@inheritdoc} */
114a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1158c2e8227SGreg Roach		return true;
1168c2e8227SGreg Roach	}
1178c2e8227SGreg Roach
11876692c8bSGreg Roach	/**
11976692c8bSGreg Roach	 * An HTML form to edit block settings
12076692c8bSGreg Roach	 *
121e490cd80SGreg Roach	 * @param Tree $tree
12276692c8bSGreg Roach	 * @param int  $block_id
123a9430be8SGreg Roach	 *
124a9430be8SGreg Roach	 * @return void
12576692c8bSGreg Roach	 */
126e490cd80SGreg Roach	public function configureBlock(Tree $tree, int $block_id) {
1278c2e8227SGreg Roach	}
128fe8a65d1SGreg Roach
129fe8a65d1SGreg Roach	/**
130fe8a65d1SGreg Roach	 * @param Request $request
131fe8a65d1SGreg Roach	 *
132fe8a65d1SGreg Roach	 * @return Response
133fe8a65d1SGreg Roach	 */
134fe8a65d1SGreg Roach	public function getEditNewsAction(Request $request): Response {
135fe8a65d1SGreg Roach		/** @var Tree $tree */
136fe8a65d1SGreg Roach		$tree = $request->attributes->get('tree');
137fe8a65d1SGreg Roach
138fe8a65d1SGreg Roach		if (!Auth::isManager($tree)) {
139fe8a65d1SGreg Roach			throw new AccessDeniedHttpException;
140fe8a65d1SGreg Roach		}
141fe8a65d1SGreg Roach
142fe8a65d1SGreg Roach		$news_id = $request->get('news_id');
143fe8a65d1SGreg Roach
144fe8a65d1SGreg Roach		if ($news_id > 0) {
145fe8a65d1SGreg Roach			$row = Database::prepare(
146fe8a65d1SGreg Roach				"SELECT subject, body FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id"
147fe8a65d1SGreg Roach			)->execute([
148fe8a65d1SGreg Roach				'news_id' => $news_id,
149fe8a65d1SGreg Roach				'tree_id' => $tree->getTreeId(),
150fe8a65d1SGreg Roach			])->fetchOneRow();
151fe8a65d1SGreg Roach		} else {
152fe8a65d1SGreg Roach			$row = (object) [
153fe8a65d1SGreg Roach				'body'    => '',
154fe8a65d1SGreg Roach				'subject' => '',
155fe8a65d1SGreg Roach			];
156fe8a65d1SGreg Roach		}
157fe8a65d1SGreg Roach
158fe8a65d1SGreg Roach		$title = I18N::translate('Add/edit a journal/news entry');
159fe8a65d1SGreg Roach
160*147e99aaSGreg Roach		return $this->viewResponse('modules/gedcom_news/edit', [
161fe8a65d1SGreg Roach			'body'    => $row->body,
162fe8a65d1SGreg Roach			'news_id' => $news_id,
163fe8a65d1SGreg Roach			'subject' => $row->subject,
164fe8a65d1SGreg Roach			'title'   => $title,
165fe8a65d1SGreg Roach		]);
166fe8a65d1SGreg Roach	}
167fe8a65d1SGreg Roach
168fe8a65d1SGreg Roach	/**
169fe8a65d1SGreg Roach	 * @param Request $request
170fe8a65d1SGreg Roach	 *
171b43958a0SGreg Roach	 * @return RedirectResponse
172fe8a65d1SGreg Roach	 */
173fe8a65d1SGreg Roach	public function postEditNewsAction(Request $request): RedirectResponse {
174fe8a65d1SGreg Roach		/** @var Tree $tree */
175fe8a65d1SGreg Roach		$tree = $request->attributes->get('tree');
176fe8a65d1SGreg Roach
177fe8a65d1SGreg Roach		if (!Auth::isManager($tree)) {
178fe8a65d1SGreg Roach			throw new AccessDeniedHttpException;
179fe8a65d1SGreg Roach		}
180fe8a65d1SGreg Roach
181fe8a65d1SGreg Roach		$news_id = $request->get('news_id');
182fe8a65d1SGreg Roach		$subject = $request->get('subject');
183fe8a65d1SGreg Roach		$body    = $request->get('body');
184fe8a65d1SGreg Roach
185fe8a65d1SGreg Roach		if ($news_id > 0) {
186fe8a65d1SGreg Roach			Database::prepare(
187fe8a65d1SGreg Roach				"UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" .
188fe8a65d1SGreg Roach				" WHERE news_id = :news_id AND gedcom_id = :tree_id"
189fe8a65d1SGreg Roach			)->execute([
190fe8a65d1SGreg Roach				'subject' => $subject,
191fe8a65d1SGreg Roach				'body'    => $body,
192fe8a65d1SGreg Roach				'news_id' => $news_id,
193fe8a65d1SGreg Roach				'tree_id' => $tree->getTreeId(),
194fe8a65d1SGreg Roach			]);
195fe8a65d1SGreg Roach		} else {
196fe8a65d1SGreg Roach			Database::prepare(
197fe8a65d1SGreg Roach				"INSERT INTO `##news` (gedcom_id, subject, body, updated) VALUES (:tree_id, :subject ,:body, CURRENT_TIMESTAMP)"
198fe8a65d1SGreg Roach			)->execute([
199fe8a65d1SGreg Roach				'body'    => $body,
200fe8a65d1SGreg Roach				'subject' => $subject,
201fe8a65d1SGreg Roach				'tree_id' => $tree->getTreeId(),
202fe8a65d1SGreg Roach			]);
203fe8a65d1SGreg Roach		}
204fe8a65d1SGreg Roach
20504ac9f0fSGreg Roach		$url = route('tree-page', [
206fe8a65d1SGreg Roach			'ged' => $tree->getName(),
207fe8a65d1SGreg Roach		]);
208fe8a65d1SGreg Roach
209fe8a65d1SGreg Roach		return new RedirectResponse($url);
210fe8a65d1SGreg Roach	}
211fe8a65d1SGreg Roach
212fe8a65d1SGreg Roach	/**
213fe8a65d1SGreg Roach	 * @param Request $request
214fe8a65d1SGreg Roach	 *
215b43958a0SGreg Roach	 * @return RedirectResponse
216fe8a65d1SGreg Roach	 */
217fe8a65d1SGreg Roach	public function postDeleteNewsAction(Request $request): RedirectResponse {
218fe8a65d1SGreg Roach		/** @var Tree $tree */
219fe8a65d1SGreg Roach		$tree = $request->attributes->get('tree');
220fe8a65d1SGreg Roach
221fe8a65d1SGreg Roach		$news_id = $request->get('news_id');
222fe8a65d1SGreg Roach
223fe8a65d1SGreg Roach		if (!Auth::isManager($tree)) {
224fe8a65d1SGreg Roach			throw new AccessDeniedHttpException;
225fe8a65d1SGreg Roach		}
226fe8a65d1SGreg Roach
227fe8a65d1SGreg Roach		Database::prepare(
228fe8a65d1SGreg Roach			"DELETE FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id"
229fe8a65d1SGreg Roach		)->execute([
230fe8a65d1SGreg Roach			'news_id' => $news_id,
231fe8a65d1SGreg Roach			'tree_id' => $tree->getTreeId(),
232fe8a65d1SGreg Roach		]);
233fe8a65d1SGreg Roach
23404ac9f0fSGreg Roach		$url = route('tree-page', [
235fe8a65d1SGreg Roach			'ged' => $tree->getName(),
236fe8a65d1SGreg Roach		]);
237fe8a65d1SGreg Roach
238fe8a65d1SGreg Roach		return new RedirectResponse($url);
239fe8a65d1SGreg Roach	}
2408c2e8227SGreg Roach}
241