xref: /webtrees/app/Module/UserWelcomeModule.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
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;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
22a0ebf380SGreg Roachuse Fisharebest\Webtrees\Individual;
234ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class UserWelcomeModule
298c2e8227SGreg Roach */
3037eb8894SGreg Roachclass UserWelcomeModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
34961ec755SGreg Roach    /**
354ca7e03cSGreg Roach     * @var ModuleService
364ca7e03cSGreg Roach     */
374ca7e03cSGreg Roach    private $module_service;
384ca7e03cSGreg Roach
394ca7e03cSGreg Roach    /**
404ca7e03cSGreg Roach     * UserWelcomeModule constructor.
414ca7e03cSGreg Roach     *
424ca7e03cSGreg Roach     * @param ModuleService $module_service
434ca7e03cSGreg Roach     */
445bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
455bdbe281SGreg Roach    {
464ca7e03cSGreg Roach        $this->module_service = $module_service;
474ca7e03cSGreg Roach    }
484ca7e03cSGreg Roach
494ca7e03cSGreg Roach    /**
500cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function title(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Name of a module */
57bbb76c12SGreg Roach        return I18N::translate('My page');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
60961ec755SGreg Roach    /**
61961ec755SGreg Roach     * A sentence describing what this module does.
62961ec755SGreg Roach     *
63961ec755SGreg Roach     * @return string
64961ec755SGreg Roach     */
6549a243cbSGreg Roach    public function description(): string
66c1010edaSGreg Roach    {
67bbb76c12SGreg Roach        /* I18N: Description of the “My page” module */
68bbb76c12SGreg Roach        return I18N::translate('A greeting message and useful links for a user.');
698c2e8227SGreg Roach    }
708c2e8227SGreg Roach
7176692c8bSGreg Roach    /**
7276692c8bSGreg Roach     * Generate the HTML content of this block.
7376692c8bSGreg Roach     *
74e490cd80SGreg Roach     * @param Tree     $tree
7576692c8bSGreg Roach     * @param int      $block_id
765f2ae573SGreg Roach     * @param string   $ctype
77727f238cSGreg Roach     * @param string[] $cfg
7876692c8bSGreg Roach     *
7976692c8bSGreg Roach     * @return string
8076692c8bSGreg Roach     */
815f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
82c1010edaSGreg Roach    {
83e490cd80SGreg Roach        $gedcomid   = $tree->getUserPreference(Auth::user(), 'gedcomid');
84e490cd80SGreg Roach        $individual = Individual::getInstance($gedcomid, $tree);
85a0ebf380SGreg Roach        $links      = [];
86a0ebf380SGreg Roach
8787cca37cSGreg Roach        $pedigree_chart = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
8849a243cbSGreg Roach            ->filter(function (ModuleInterface $module): bool {
8949a243cbSGreg Roach                return $module instanceof PedigreeChartModule;
9049a243cbSGreg Roach            });
9149a243cbSGreg Roach
9249a243cbSGreg Roach        if ($individual instanceof Individual) {
9349a243cbSGreg Roach            if ($pedigree_chart instanceof PedigreeChartModule) {
94a0ebf380SGreg Roach                $links[] = [
95c1010edaSGreg Roach                    'url'   => route('pedigree', [
96c0935879SGreg Roach                        'xref' => $individual->xref(),
97f4afa648SGreg Roach                        'ged'  => $individual->tree()->name(),
98c1010edaSGreg Roach                    ]),
99a0ebf380SGreg Roach                    'title' => I18N::translate('Default chart'),
100a0ebf380SGreg Roach                    'icon'  => 'icon-pedigree',
101a0ebf380SGreg Roach                ];
1023f8a84f7SDavid Drury            }
103a0ebf380SGreg Roach
104a0ebf380SGreg Roach            $links[] = [
105b1f1e4efSGreg Roach                'url'   => $individual->url(),
106a0ebf380SGreg Roach                'title' => I18N::translate('My individual record'),
107a0ebf380SGreg Roach                'icon'  => 'icon-indis',
108a0ebf380SGreg Roach            ];
1098c2e8227SGreg Roach        }
110a0ebf380SGreg Roach
111a0ebf380SGreg Roach        $links[] = [
1124653dc2eSGreg Roach            'url'   => route('my-account', []),
113a0ebf380SGreg Roach            'title' => I18N::translate('My account'),
114a0ebf380SGreg Roach            'icon'  => 'icon-mypage',
115a0ebf380SGreg Roach        ];
116147e99aaSGreg Roach        $content = view('modules/user_welcome/welcome', ['links' => $links]);
1178c2e8227SGreg Roach
118e5a6b4d4SGreg Roach        $real_name = '<span dir="auto">' . e(Auth::user()->realName()) . '</span>';
119334b74ddSGreg Roach
120bbb76c12SGreg Roach        /* I18N: A %s is the user’s name */
121334b74ddSGreg Roach        $title = I18N::translate('Welcome %s', $real_name);
122bbb76c12SGreg Roach
1236a8879feSGreg Roach        if ($ctype !== '') {
124147e99aaSGreg Roach            return view('modules/block-template', [
12526684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
126d034ca3bSGreg Roach                'id'         => $block_id,
1271fa06fe3SGreg Roach                'config_url' => '',
128bbb76c12SGreg Roach                'title'      => $title,
129a0ebf380SGreg Roach                'content'    => $content,
130a0ebf380SGreg Roach            ]);
1318c2e8227SGreg Roach        }
132b2ce94c6SRico Sonntag
133b2ce94c6SRico Sonntag        return $content;
1348c2e8227SGreg Roach    }
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach    /** {@inheritdoc} */
137c1010edaSGreg Roach    public function loadAjax(): bool
138c1010edaSGreg Roach    {
1398c2e8227SGreg Roach        return false;
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach    /** {@inheritdoc} */
143c1010edaSGreg Roach    public function isUserBlock(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return true;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach    /** {@inheritdoc} */
14963276d8fSGreg Roach    public function isTreeBlock(): bool
150c1010edaSGreg Roach    {
1518c2e8227SGreg Roach        return false;
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
15476692c8bSGreg Roach    /**
155a45f9889SGreg Roach     * Update the configuration for a block.
156a45f9889SGreg Roach     *
157a45f9889SGreg Roach     * @param Request $request
158a45f9889SGreg Roach     * @param int     $block_id
159a45f9889SGreg Roach     *
160a45f9889SGreg Roach     * @return void
161a45f9889SGreg Roach     */
162*e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
163a45f9889SGreg Roach    {
164a45f9889SGreg Roach    }
165a45f9889SGreg Roach
166a45f9889SGreg Roach    /**
16776692c8bSGreg Roach     * An HTML form to edit block settings
16876692c8bSGreg Roach     *
169e490cd80SGreg Roach     * @param Tree $tree
17076692c8bSGreg Roach     * @param int  $block_id
171a9430be8SGreg Roach     *
172a9430be8SGreg Roach     * @return void
17376692c8bSGreg Roach     */
174*e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
175c1010edaSGreg Roach    {
1768c2e8227SGreg Roach    }
1778c2e8227SGreg Roach}
178