xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision ac5f8ed16774e57c5f89faca528d55bc4a3971e1)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
24cef665d9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
251e7a7a28SGreg Roachuse Illuminate\Support\Str;
266ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
28ca52a408SGreg Roachuse stdClass;
29fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class FamilyTreeNewsModule
338c2e8227SGreg Roach */
3437eb8894SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleBlockTrait;
3749a243cbSGreg Roach
3876692c8bSGreg Roach    /**
3976692c8bSGreg Roach     * A sentence describing what this module does.
4076692c8bSGreg Roach     *
4176692c8bSGreg Roach     * @return string
4276692c8bSGreg Roach     */
4349a243cbSGreg Roach    public function description(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Description of the “News” module */
46bbb76c12SGreg Roach        return I18N::translate('Family news and site announcements.');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
4976692c8bSGreg Roach    /**
5076692c8bSGreg Roach     * Generate the HTML content of this block.
5176692c8bSGreg Roach     *
52e490cd80SGreg Roach     * @param Tree     $tree
5376692c8bSGreg Roach     * @param int      $block_id
545f2ae573SGreg Roach     * @param string   $ctype
55727f238cSGreg Roach     * @param string[] $cfg
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @return string
5876692c8bSGreg Roach     */
595f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
60c1010edaSGreg Roach    {
61cef665d9SGreg Roach        $articles = DB::table('news')
62cef665d9SGreg Roach            ->where('gedcom_id', '=', $tree->id())
63cef665d9SGreg Roach            ->orderByDesc('updated')
64ca52a408SGreg Roach            ->get()
650b5fd0a6SGreg Roach            ->map(static function (stdClass $row): stdClass {
664459dc9aSGreg Roach                $row->updated = Carbon::make($row->updated);
67ca52a408SGreg Roach
68ca52a408SGreg Roach                return $row;
69ca52a408SGreg Roach            });
70d004f62cSGreg Roach
71147e99aaSGreg Roach        $content = view('modules/gedcom_news/list', [
72fe8a65d1SGreg Roach            'articles' => $articles,
73fe8a65d1SGreg Roach            'block_id' => $block_id,
74fe8a65d1SGreg Roach            'limit'    => 5,
75fe8a65d1SGreg Roach        ]);
768c2e8227SGreg Roach
776a8879feSGreg Roach        if ($ctype !== '') {
78147e99aaSGreg Roach            return view('modules/block-template', [
791e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
809c6524dcSGreg Roach                'id'         => $block_id,
819c6524dcSGreg Roach                'config_url' => '',
8249a243cbSGreg Roach                'title'      => $this->title(),
839c6524dcSGreg Roach                'content'    => $content,
849c6524dcSGreg Roach            ]);
858c2e8227SGreg Roach        }
86b2ce94c6SRico Sonntag
87b2ce94c6SRico Sonntag        return $content;
888c2e8227SGreg Roach    }
898c2e8227SGreg Roach
906ccdf4f0SGreg Roach    /**
916ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
926ccdf4f0SGreg Roach     *
936ccdf4f0SGreg Roach     * @return string
946ccdf4f0SGreg Roach     */
956ccdf4f0SGreg Roach    public function title(): string
966ccdf4f0SGreg Roach    {
976ccdf4f0SGreg Roach        /* I18N: Name of a module */
986ccdf4f0SGreg Roach        return I18N::translate('News');
996ccdf4f0SGreg Roach    }
1006ccdf4f0SGreg Roach
1018c2e8227SGreg Roach    /** {@inheritdoc} */
102c1010edaSGreg Roach    public function loadAjax(): bool
103c1010edaSGreg Roach    {
1048c2e8227SGreg Roach        return false;
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach    /** {@inheritdoc} */
108c1010edaSGreg Roach    public function isUserBlock(): bool
109c1010edaSGreg Roach    {
1108c2e8227SGreg Roach        return false;
1118c2e8227SGreg Roach    }
1128c2e8227SGreg Roach
1138c2e8227SGreg Roach    /** {@inheritdoc} */
11463276d8fSGreg Roach    public function isTreeBlock(): bool
115c1010edaSGreg Roach    {
1168c2e8227SGreg Roach        return true;
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
11976692c8bSGreg Roach    /**
120a45f9889SGreg Roach     * Update the configuration for a block.
121a45f9889SGreg Roach     *
1226ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
123a45f9889SGreg Roach     * @param int     $block_id
124a45f9889SGreg Roach     *
125a45f9889SGreg Roach     * @return void
126a45f9889SGreg Roach     */
1276ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
128a45f9889SGreg Roach    {
129a45f9889SGreg Roach    }
130a45f9889SGreg Roach
131a45f9889SGreg Roach    /**
13276692c8bSGreg Roach     * An HTML form to edit block settings
13376692c8bSGreg Roach     *
134e490cd80SGreg Roach     * @param Tree $tree
13576692c8bSGreg Roach     * @param int  $block_id
136a9430be8SGreg Roach     *
137a9430be8SGreg Roach     * @return void
13876692c8bSGreg Roach     */
139e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
140c1010edaSGreg Roach    {
1418c2e8227SGreg Roach    }
142fe8a65d1SGreg Roach
143fe8a65d1SGreg Roach    /**
1446ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
145b6db7c1fSGreg Roach     * @param Tree                   $tree
146fe8a65d1SGreg Roach     *
1476ccdf4f0SGreg Roach     * @return ResponseInterface
148fe8a65d1SGreg Roach     */
1496ccdf4f0SGreg Roach    public function getEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
150c1010edaSGreg Roach    {
151fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
15259f2f229SGreg Roach            throw new AccessDeniedHttpException();
153fe8a65d1SGreg Roach        }
154fe8a65d1SGreg Roach
155*ac5f8ed1SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
156fe8a65d1SGreg Roach
157*ac5f8ed1SGreg Roach        if ($news_id !== '') {
158cef665d9SGreg Roach            $row = DB::table('news')
159cef665d9SGreg Roach                ->where('news_id', '=', $news_id)
160cef665d9SGreg Roach                ->where('gedcom_id', '=', $tree->id())
161cef665d9SGreg Roach                ->first();
162fe8a65d1SGreg Roach        } else {
163fe8a65d1SGreg Roach            $row = (object) [
164fe8a65d1SGreg Roach                'body'    => '',
165fe8a65d1SGreg Roach                'subject' => '',
166fe8a65d1SGreg Roach            ];
167fe8a65d1SGreg Roach        }
168fe8a65d1SGreg Roach
169fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
170fe8a65d1SGreg Roach
171147e99aaSGreg Roach        return $this->viewResponse('modules/gedcom_news/edit', [
172fe8a65d1SGreg Roach            'body'    => $row->body,
173fe8a65d1SGreg Roach            'news_id' => $news_id,
174fe8a65d1SGreg Roach            'subject' => $row->subject,
175fe8a65d1SGreg Roach            'title'   => $title,
176fe8a65d1SGreg Roach        ]);
177fe8a65d1SGreg Roach    }
178fe8a65d1SGreg Roach
179fe8a65d1SGreg Roach    /**
1806ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
181b6db7c1fSGreg Roach     * @param Tree                   $tree
182fe8a65d1SGreg Roach     *
1836ccdf4f0SGreg Roach     * @return ResponseInterface
184fe8a65d1SGreg Roach     */
1856ccdf4f0SGreg Roach    public function postEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
186c1010edaSGreg Roach    {
187fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
18859f2f229SGreg Roach            throw new AccessDeniedHttpException();
189fe8a65d1SGreg Roach        }
190fe8a65d1SGreg Roach
191*ac5f8ed1SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
192*ac5f8ed1SGreg Roach        $subject = $request->getParsedBody()['subject'];
193*ac5f8ed1SGreg Roach        $body    = $request->getParsedBody()['body'];
194fe8a65d1SGreg Roach
195fe8a65d1SGreg Roach        if ($news_id > 0) {
196cef665d9SGreg Roach            DB::table('news')
197cef665d9SGreg Roach                ->where('news_id', '=', $news_id)
198cef665d9SGreg Roach                ->where('gedcom_id', '=', $tree->id())
199cef665d9SGreg Roach                ->update([
200fe8a65d1SGreg Roach                    'body'    => $body,
201cef665d9SGreg Roach                    'subject' => $subject,
202fe8a65d1SGreg Roach                ]);
203fe8a65d1SGreg Roach        } else {
204cef665d9SGreg Roach            DB::table('news')->insert([
205fe8a65d1SGreg Roach                'body'      => $body,
206fe8a65d1SGreg Roach                'subject'   => $subject,
207cef665d9SGreg Roach                'gedcom_id' => $tree->id(),
208fe8a65d1SGreg Roach            ]);
209fe8a65d1SGreg Roach        }
210fe8a65d1SGreg Roach
21104ac9f0fSGreg Roach        $url = route('tree-page', [
212aa6f03bbSGreg Roach            'ged' => $tree->name(),
213fe8a65d1SGreg Roach        ]);
214fe8a65d1SGreg Roach
2156ccdf4f0SGreg Roach        return redirect($url);
216fe8a65d1SGreg Roach    }
217fe8a65d1SGreg Roach
218fe8a65d1SGreg Roach    /**
2196ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
220b6db7c1fSGreg Roach     * @param Tree                   $tree
221fe8a65d1SGreg Roach     *
2226ccdf4f0SGreg Roach     * @return ResponseInterface
223fe8a65d1SGreg Roach     */
2246ccdf4f0SGreg Roach    public function postDeleteNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
225c1010edaSGreg Roach    {
226*ac5f8ed1SGreg Roach        $news_id = $request->getQueryParams()['news_id'];
227fe8a65d1SGreg Roach
228fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
22959f2f229SGreg Roach            throw new AccessDeniedHttpException();
230fe8a65d1SGreg Roach        }
231fe8a65d1SGreg Roach
232cef665d9SGreg Roach        DB::table('news')
233cef665d9SGreg Roach            ->where('news_id', '=', $news_id)
234cef665d9SGreg Roach            ->where('gedcom_id', '=', $tree->id())
235cef665d9SGreg Roach            ->delete();
236fe8a65d1SGreg Roach
23704ac9f0fSGreg Roach        $url = route('tree-page', [
238aa6f03bbSGreg Roach            'ged' => $tree->name(),
239fe8a65d1SGreg Roach        ]);
240fe8a65d1SGreg Roach
2416ccdf4f0SGreg Roach        return redirect($url);
242fe8a65d1SGreg Roach    }
2438c2e8227SGreg Roach}
244