xref: /webtrees/app/Module/UserJournalModule.php (revision 0cfd6963ac65bd7fe86283b801b4f23d665c6004)
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;
24ec589cf2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
25ca52a408SGreg Roachuse stdClass;
26fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
27fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request;
28fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response;
29fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class UserJournalModule
338c2e8227SGreg Roach */
3437eb8894SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleBlockTrait;
3749a243cbSGreg Roach
3876692c8bSGreg Roach    /**
39*0cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4076692c8bSGreg Roach     *
4176692c8bSGreg Roach     * @return string
4276692c8bSGreg Roach     */
4349a243cbSGreg Roach    public function title(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Name of a module */
46bbb76c12SGreg Roach        return I18N::translate('Journal');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
4976692c8bSGreg Roach    /**
5076692c8bSGreg Roach     * A sentence describing what this module does.
5176692c8bSGreg Roach     *
5276692c8bSGreg Roach     * @return string
5376692c8bSGreg Roach     */
5449a243cbSGreg Roach    public function description(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Description of the “Journal” module */
57bbb76c12SGreg Roach        return I18N::translate('A private area to record notes or keep a journal.');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
6076692c8bSGreg Roach    /**
6176692c8bSGreg Roach     * Generate the HTML content of this block.
6276692c8bSGreg Roach     *
63e490cd80SGreg Roach     * @param Tree     $tree
6476692c8bSGreg Roach     * @param int      $block_id
655f2ae573SGreg Roach     * @param string   $ctype
66727f238cSGreg Roach     * @param string[] $cfg
6776692c8bSGreg Roach     *
6876692c8bSGreg Roach     * @return string
6976692c8bSGreg Roach     */
705f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
71c1010edaSGreg Roach    {
72ec589cf2SGreg Roach        $articles = DB::table('news')
73ec589cf2SGreg Roach            ->where('user_id', '=', Auth::id())
74ec589cf2SGreg Roach            ->orderByDesc('updated')
75ca52a408SGreg Roach            ->get()
76ca52a408SGreg Roach            ->map(function (stdClass $row): stdClass {
774459dc9aSGreg Roach                $row->updated = Carbon::make($row->updated);
78ca52a408SGreg Roach
79ca52a408SGreg Roach                return $row;
80ca52a408SGreg Roach            });
81ec589cf2SGreg Roach
82147e99aaSGreg Roach        $content = view('modules/user_blog/list', [
83fe8a65d1SGreg Roach            'articles' => $articles,
84fe8a65d1SGreg Roach            'block_id' => $block_id,
85fe8a65d1SGreg Roach            'limit'    => 5,
86fe8a65d1SGreg Roach        ]);
878c2e8227SGreg Roach
886a8879feSGreg Roach        if ($ctype !== '') {
89147e99aaSGreg Roach            return view('modules/block-template', [
9026684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
919c6524dcSGreg Roach                'id'         => $block_id,
929c6524dcSGreg Roach                'config_url' => '',
9349a243cbSGreg Roach                'title'      => $this->title(),
949c6524dcSGreg Roach                'content'    => $content,
959c6524dcSGreg Roach            ]);
968c2e8227SGreg Roach        }
97b2ce94c6SRico Sonntag
98b2ce94c6SRico Sonntag        return $content;
998c2e8227SGreg Roach    }
1008c2e8227SGreg 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 true;
1118c2e8227SGreg Roach    }
1128c2e8227SGreg Roach
1138c2e8227SGreg Roach    /** {@inheritdoc} */
11463276d8fSGreg Roach    public function isTreeBlock(): bool
115c1010edaSGreg Roach    {
1168c2e8227SGreg Roach        return false;
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
11976692c8bSGreg Roach    /**
120a45f9889SGreg Roach     * Update the configuration for a block.
121a45f9889SGreg Roach     *
122a45f9889SGreg Roach     * @param Request $request
123a45f9889SGreg Roach     * @param int     $block_id
124a45f9889SGreg Roach     *
125a45f9889SGreg Roach     * @return void
126a45f9889SGreg Roach     */
127a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
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     */
139a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
140c1010edaSGreg Roach    {
1418c2e8227SGreg Roach    }
142fe8a65d1SGreg Roach
143fe8a65d1SGreg Roach    /**
144fe8a65d1SGreg Roach     * @param Request $request
145fe8a65d1SGreg Roach     *
146fe8a65d1SGreg Roach     * @return Response
147fe8a65d1SGreg Roach     */
148c1010edaSGreg Roach    public function getEditJournalAction(Request $request): Response
149c1010edaSGreg Roach    {
150fe8a65d1SGreg Roach        if (!Auth::check()) {
15159f2f229SGreg Roach            throw new AccessDeniedHttpException();
152fe8a65d1SGreg Roach        }
153fe8a65d1SGreg Roach
154fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
155fe8a65d1SGreg Roach
156fe8a65d1SGreg Roach        if ($news_id > 0) {
157ec589cf2SGreg Roach            $row = DB::table('news')
158ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
159ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
160ec589cf2SGreg Roach                ->first();
161fe8a65d1SGreg Roach        } else {
162fe8a65d1SGreg Roach            $row = (object) [
163fe8a65d1SGreg Roach                'body'    => '',
164fe8a65d1SGreg Roach                'subject' => '',
165fe8a65d1SGreg Roach            ];
166fe8a65d1SGreg Roach        }
167fe8a65d1SGreg Roach
168fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
169fe8a65d1SGreg Roach
170147e99aaSGreg Roach        return $this->viewResponse('modules/user_blog/edit', [
171fe8a65d1SGreg Roach            'body'    => $row->body,
172fe8a65d1SGreg Roach            'news_id' => $news_id,
173fe8a65d1SGreg Roach            'subject' => $row->subject,
174fe8a65d1SGreg Roach            'title'   => $title,
175fe8a65d1SGreg Roach        ]);
176fe8a65d1SGreg Roach    }
177fe8a65d1SGreg Roach
178fe8a65d1SGreg Roach    /**
179fe8a65d1SGreg Roach     * @param Request $request
180b6db7c1fSGreg Roach     * @param Tree    $tree
181fe8a65d1SGreg Roach     *
182b43958a0SGreg Roach     * @return RedirectResponse
183fe8a65d1SGreg Roach     */
184b6db7c1fSGreg Roach    public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse
185c1010edaSGreg Roach    {
186fe8a65d1SGreg Roach        if (!Auth::check()) {
18759f2f229SGreg Roach            throw new AccessDeniedHttpException();
188fe8a65d1SGreg Roach        }
189fe8a65d1SGreg Roach
190fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
191fe8a65d1SGreg Roach        $subject = $request->get('subject');
192fe8a65d1SGreg Roach        $body    = $request->get('body');
193fe8a65d1SGreg Roach
194fe8a65d1SGreg Roach        if ($news_id > 0) {
195ec589cf2SGreg Roach            DB::table('news')
196ec589cf2SGreg Roach                ->where('news_id', '=', $news_id)
197ec589cf2SGreg Roach                ->where('user_id', '=', Auth::id())
198ec589cf2SGreg Roach                ->update([
199fe8a65d1SGreg Roach                    'body'    => $body,
200ec589cf2SGreg Roach                    'subject' => $subject,
201fe8a65d1SGreg Roach                ]);
202fe8a65d1SGreg Roach        } else {
203ec589cf2SGreg Roach            DB::table('news')->insert([
204fe8a65d1SGreg Roach                'body'    => $body,
205fe8a65d1SGreg Roach                'subject' => $subject,
206fe8a65d1SGreg Roach                'user_id' => Auth::id(),
207fe8a65d1SGreg Roach            ]);
208fe8a65d1SGreg Roach        }
209fe8a65d1SGreg Roach
210fe8a65d1SGreg Roach        $url = route('user-page', [
211aa6f03bbSGreg Roach            'ged' => $tree->name(),
212fe8a65d1SGreg Roach        ]);
213fe8a65d1SGreg Roach
214fe8a65d1SGreg Roach        return new RedirectResponse($url);
215fe8a65d1SGreg Roach    }
216fe8a65d1SGreg Roach
217fe8a65d1SGreg Roach    /**
218fe8a65d1SGreg Roach     * @param Request $request
219b6db7c1fSGreg Roach     * @param Tree    $tree
220fe8a65d1SGreg Roach     *
221b43958a0SGreg Roach     * @return RedirectResponse
222fe8a65d1SGreg Roach     */
223b6db7c1fSGreg Roach    public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse
224c1010edaSGreg Roach    {
225fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
226fe8a65d1SGreg Roach
227ec589cf2SGreg Roach        DB::table('news')
228ec589cf2SGreg Roach            ->where('news_id', '=', $news_id)
229ec589cf2SGreg Roach            ->where('user_id', '=', Auth::id())
230ec589cf2SGreg Roach            ->delete();
231fe8a65d1SGreg Roach
232fe8a65d1SGreg Roach        $url = route('user-page', [
233aa6f03bbSGreg Roach            'ged' => $tree->name(),
234fe8a65d1SGreg Roach        ]);
235fe8a65d1SGreg Roach
236fe8a65d1SGreg Roach        return new RedirectResponse($url);
237fe8a65d1SGreg Roach    }
2388c2e8227SGreg Roach}
239