Skip to content

Commit

Permalink
Implement app.contact_statistics view
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Aug 24, 2021
1 parent 7fdb4b6 commit 15a1ccc
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 2 deletions.
4 changes: 4 additions & 0 deletions assets/admin/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
// Add project specific javascript code here:
import {viewRegistry} from 'sulu-admin-bundle/containers';
import ContactStatistics from "./views/ContactStatistics";

viewRegistry.add('app.contact_statistics', ContactStatistics);
76 changes: 76 additions & 0 deletions assets/admin/views/ContactStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';
import {observer} from 'mobx-react';
import {action, observable} from 'mobx';
import {Loader, Button} from 'sulu-admin-bundle/components';
import {withToolbar} from 'sulu-admin-bundle/containers';
import {translate} from 'sulu-admin-bundle/utils';
import {ResourceRequester} from 'sulu-admin-bundle/services';

@observer
class ContactStatistics extends React.Component {
@observable loading = false;
@observable contactCount = undefined;

componentDidMount() {
this.loadData();
}

@action loadData = () => {
this.loading = true;
this.contactCount = undefined;
const contactResourceKey = this.props.router.route.options.contactResourceKey;

return ResourceRequester.getList(contactResourceKey)
.then(action((response) => {
this.contactCount = response.total;
}))
.catch((e) => {
console.error('Error while loading contact statistics from server.', e);
})
.finally(action(() => {
this.loading = false;
}));
}

navigateToContactList = () => {
const contactListView = this.props.router.route.options.contactListView;

this.props.router.navigate(contactListView);
}

render() {
if (this.loading) {
return <Loader/>;
}

return (
<div>
<div style={{marginBottom: 20}}>
{translate('app.contact_statistics_count_text', {contactCount: this.contactCount})}
</div>
<Button
onClick={this.navigateToContactList}
skin="link"
>
{translate('app.contact_statistics_link_text')}
</Button>
</div>
);
}
}

export default withToolbar(ContactStatistics, function () {
return {
items: [
{
type: 'button',
label: translate('app.refresh_statistics'),
icon: 'su-sync',
disabled: this.loading,
onClick: () => {
this.loadData();
},
}
]
};
});
62 changes: 62 additions & 0 deletions src/Admin/ContactStatisticsAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace App\Admin;

use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Bundle\ContactBundle\Admin\ContactAdmin;
use Sulu\Component\Security\Authorization\PermissionTypes;
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;

class ContactStatisticsAdmin extends Admin
{
const CONTACT_STATISTICS_VIEW = 'app.contact_statistics';

private ViewBuilderFactoryInterface $viewBuilderFactory;
private SecurityCheckerInterface $securityChecker;

public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory,
SecurityCheckerInterface $securityChecker
) {
$this->viewBuilderFactory = $viewBuilderFactory;
$this->securityChecker = $securityChecker;
}

public function configureNavigationItems(NavigationItemCollection $navigationItemCollection): void
{
if (!$navigationItemCollection->has('sulu_contact.contacts')) {
return;
}

if ($this->securityChecker->hasPermission(ContactAdmin::CONTACT_SECURITY_CONTEXT, PermissionTypes::VIEW)) {
$contactStatisticsNavigationItem = new NavigationItem('app.contact_statistics');
$contactStatisticsNavigationItem->setPosition(30);
$contactStatisticsNavigationItem->setView(static::CONTACT_STATISTICS_VIEW);

$contactsNavigationItem = $navigationItemCollection->get('sulu_contact.contacts');
$contactsNavigationItem->addChild($contactStatisticsNavigationItem);
}
}

public function configureViews(ViewCollection $viewCollection): void
{
if ($this->securityChecker->hasPermission(ContactAdmin::CONTACT_SECURITY_CONTEXT, PermissionTypes::VIEW)) {
$viewCollection->add(
$this->viewBuilderFactory->createViewBuilder(self::CONTACT_STATISTICS_VIEW, '/contact-statistics', 'app.contact_statistics')
->setOption('contactListView', ContactAdmin::CONTACT_LIST_VIEW)
->setOption('contactResourceKey', 'contacts')
);
}
}

public static function getPriority(): int
{
return ContactAdmin::getPriority() - 1;
}
}
6 changes: 5 additions & 1 deletion translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"app.album_selection_label": "{count} {count, plural, =1 {Album} other {Alben}} ausgewählt",
"app.select_albums": "Alben auswählen",
"app.no_album_selected": "Kein Album ausgewählt",
"app.select_album": "Album auswählen"
"app.select_album": "Album auswählen",
"app.contact_statistics": "Statistiken",
"app.refresh_statistics": "Stastiken aktualisieren",
"app.contact_statistics_count_text": "Die Applikation enthält {contactCount} Kontakte.",
"app.contact_statistics_link_text": "Navigiere zur Kontaktliste"
}
6 changes: 5 additions & 1 deletion translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"app.album_selection_label": "{count} {count, plural, =1 {album} other {albums}} selected",
"app.select_albums": "Select albums",
"app.no_album_selected": "No album selected",
"app.select_album": "Select album"
"app.select_album": "Select album",
"app.contact_statistics": "Statistics",
"app.refresh_statistics": "Refresh Statistics",
"app.contact_statistics_count_text": "The application includes {contactCount} contacts at the moment.",
"app.contact_statistics_link_text": "Navigate to the contact list"
}

0 comments on commit 15a1ccc

Please sign in to comment.