xref: /webtrees/app/Module/UserWelcomeModule.php (revision 0b5fd0a636fa959f5279ee28ebd2f27e921c091e)
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;
251e7a7a28SGreg Roachuse Illuminate\Support\Str;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class UserWelcomeModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass UserWelcomeModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
35961ec755SGreg Roach    /**
364ca7e03cSGreg Roach     * @var ModuleService
374ca7e03cSGreg Roach     */
384ca7e03cSGreg Roach    private $module_service;
394ca7e03cSGreg Roach
404ca7e03cSGreg Roach    /**
414ca7e03cSGreg Roach     * UserWelcomeModule constructor.
424ca7e03cSGreg Roach     *
434ca7e03cSGreg Roach     * @param ModuleService $module_service
444ca7e03cSGreg Roach     */
455bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
465bdbe281SGreg Roach    {
474ca7e03cSGreg Roach        $this->module_service = $module_service;
484ca7e03cSGreg Roach    }
494ca7e03cSGreg Roach
504ca7e03cSGreg Roach    /**
510cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
52961ec755SGreg Roach     *
53961ec755SGreg Roach     * @return string
54961ec755SGreg Roach     */
5549a243cbSGreg Roach    public function title(): string
56c1010edaSGreg Roach    {
57bbb76c12SGreg Roach        /* I18N: Name of a module */
58bbb76c12SGreg Roach        return I18N::translate('My page');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
61961ec755SGreg Roach    /**
62961ec755SGreg Roach     * A sentence describing what this module does.
63961ec755SGreg Roach     *
64961ec755SGreg Roach     * @return string
65961ec755SGreg Roach     */
6649a243cbSGreg Roach    public function description(): string
67c1010edaSGreg Roach    {
68bbb76c12SGreg Roach        /* I18N: Description of the “My page” module */
69bbb76c12SGreg Roach        return I18N::translate('A greeting message and useful links for a user.');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
7276692c8bSGreg Roach    /**
7376692c8bSGreg Roach     * Generate the HTML content of this block.
7476692c8bSGreg Roach     *
75e490cd80SGreg Roach     * @param Tree     $tree
7676692c8bSGreg Roach     * @param int      $block_id
775f2ae573SGreg Roach     * @param string   $ctype
78727f238cSGreg Roach     * @param string[] $cfg
7976692c8bSGreg Roach     *
8076692c8bSGreg Roach     * @return string
8176692c8bSGreg Roach     */
825f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
83c1010edaSGreg Roach    {
84e490cd80SGreg Roach        $gedcomid   = $tree->getUserPreference(Auth::user(), 'gedcomid');
85e490cd80SGreg Roach        $individual = Individual::getInstance($gedcomid, $tree);
86a0ebf380SGreg Roach        $links      = [];
87a0ebf380SGreg Roach
8887cca37cSGreg Roach        $pedigree_chart = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
89*0b5fd0a6SGreg Roach            ->filter(static function (ModuleInterface $module): bool {
9049a243cbSGreg Roach                return $module instanceof PedigreeChartModule;
9149a243cbSGreg Roach            });
9249a243cbSGreg Roach
9349a243cbSGreg Roach        if ($individual instanceof Individual) {
9449a243cbSGreg Roach            if ($pedigree_chart instanceof PedigreeChartModule) {
95a0ebf380SGreg Roach                $links[] = [
96c1010edaSGreg Roach                    'url'   => route('pedigree', [
97c0935879SGreg Roach                        'xref' => $individual->xref(),
98f4afa648SGreg Roach                        'ged'  => $individual->tree()->name(),
99c1010edaSGreg Roach                    ]),
100a0ebf380SGreg Roach                    'title' => I18N::translate('Default chart'),
101a0ebf380SGreg Roach                    'icon'  => 'icon-pedigree',
102a0ebf380SGreg Roach                ];
1033f8a84f7SDavid Drury            }
104a0ebf380SGreg Roach
105a0ebf380SGreg Roach            $links[] = [
106b1f1e4efSGreg Roach                'url'   => $individual->url(),
107a0ebf380SGreg Roach                'title' => I18N::translate('My individual record'),
108a0ebf380SGreg Roach                'icon'  => 'icon-indis',
109a0ebf380SGreg Roach            ];
1108c2e8227SGreg Roach        }
111a0ebf380SGreg Roach
112a0ebf380SGreg Roach        $links[] = [
1134653dc2eSGreg Roach            'url'   => route('my-account', []),
114a0ebf380SGreg Roach            'title' => I18N::translate('My account'),
115a0ebf380SGreg Roach            'icon'  => 'icon-mypage',
116a0ebf380SGreg Roach        ];
117147e99aaSGreg Roach        $content = view('modules/user_welcome/welcome', ['links' => $links]);
1188c2e8227SGreg Roach
119e5a6b4d4SGreg Roach        $real_name = '<span dir="auto">' . e(Auth::user()->realName()) . '</span>';
120334b74ddSGreg Roach
121bbb76c12SGreg Roach        /* I18N: A %s is the user’s name */
122334b74ddSGreg Roach        $title = I18N::translate('Welcome %s', $real_name);
123bbb76c12SGreg Roach
1246a8879feSGreg Roach        if ($ctype !== '') {
125147e99aaSGreg Roach            return view('modules/block-template', [
1261e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
127d034ca3bSGreg Roach                'id'         => $block_id,
1281fa06fe3SGreg Roach                'config_url' => '',
129bbb76c12SGreg Roach                'title'      => $title,
130a0ebf380SGreg Roach                'content'    => $content,
131a0ebf380SGreg Roach            ]);
1328c2e8227SGreg Roach        }
133b2ce94c6SRico Sonntag
134b2ce94c6SRico Sonntag        return $content;
1358c2e8227SGreg Roach    }
1368c2e8227SGreg Roach
1378c2e8227SGreg Roach    /** {@inheritdoc} */
138c1010edaSGreg Roach    public function loadAjax(): bool
139c1010edaSGreg Roach    {
1408c2e8227SGreg Roach        return false;
1418c2e8227SGreg Roach    }
1428c2e8227SGreg Roach
1438c2e8227SGreg Roach    /** {@inheritdoc} */
144c1010edaSGreg Roach    public function isUserBlock(): bool
145c1010edaSGreg Roach    {
1468c2e8227SGreg Roach        return true;
1478c2e8227SGreg Roach    }
1488c2e8227SGreg Roach
1498c2e8227SGreg Roach    /** {@inheritdoc} */
15063276d8fSGreg Roach    public function isTreeBlock(): bool
151c1010edaSGreg Roach    {
1528c2e8227SGreg Roach        return false;
1538c2e8227SGreg Roach    }
1548c2e8227SGreg Roach
15576692c8bSGreg Roach    /**
156a45f9889SGreg Roach     * Update the configuration for a block.
157a45f9889SGreg Roach     *
158a45f9889SGreg Roach     * @param Request $request
159a45f9889SGreg Roach     * @param int     $block_id
160a45f9889SGreg Roach     *
161a45f9889SGreg Roach     * @return void
162a45f9889SGreg Roach     */
163e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
164a45f9889SGreg Roach    {
165a45f9889SGreg Roach    }
166a45f9889SGreg Roach
167a45f9889SGreg Roach    /**
16876692c8bSGreg Roach     * An HTML form to edit block settings
16976692c8bSGreg Roach     *
170e490cd80SGreg Roach     * @param Tree $tree
17176692c8bSGreg Roach     * @param int  $block_id
172a9430be8SGreg Roach     *
173a9430be8SGreg Roach     * @return void
17476692c8bSGreg Roach     */
175e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
176c1010edaSGreg Roach    {
1778c2e8227SGreg Roach    }
1788c2e8227SGreg Roach}
179