Skip to content

Commit

Permalink
OXDEV-8462 Add admin controller example
Browse files Browse the repository at this point in the history
  • Loading branch information
TitaKoleva committed Jul 15, 2024
1 parent 24dd725 commit 7297ff3
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 1 deletion.
10 changes: 10 additions & 0 deletions menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="ISO-8859-15"?>
<OX>
<OXMENU id="NAVIGATION_ESHOPADMIN">
<MAINMENU id="mxuadmin">
<SUBMENU id="mxusers">
<TAB id="tbcluser_greetings" cl="oemt_admin_greeting" />
</SUBMENU>
</MAINMENU>
</OXMENU>
</OX>
3 changes: 2 additions & 1 deletion metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
\OxidEsales\Eshop\Application\Model\User::class => \OxidEsales\ModuleTemplate\Extension\Model\User::class,
],
'controllers' => [
'oemtgreeting' => \OxidEsales\ModuleTemplate\Greeting\Controller\GreetingController::class
'oemtgreeting' => \OxidEsales\ModuleTemplate\Greeting\Controller\GreetingController::class,
'oemt_admin_greeting' => \OxidEsales\ModuleTemplate\Greeting\Controller\Admin\GreetingAdminController::class,
],
'events' => [
'onActivate' => '\OxidEsales\ModuleTemplate\Core\ModuleEvents::onActivate',
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ final class Module
public const OEMT_COUNTER_TEMPLATE_VARNAME = 'oemt_greeting_counter';

public const DEFAULT_PERSONAL_GREETING_LANGUAGE_CONST = 'OEMODULETEMPLATE_GREETING_GENERIC';

public const OEMT_ADMIN_GREETING_TEMPLATE_VARNAME = 'greeting_message';
}
29 changes: 29 additions & 0 deletions src/Greeting/Controller/Admin/GreetingAdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\Greeting\Controller\Admin;

use OxidEsales\ModuleTemplate\Core\Module as ModuleCore;
use OxidEsales\ModuleTemplate\Extension\Model\User;
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;

class GreetingAdminController extends AdminController
{
protected $_sThisTemplate = '@oe_moduletemplate/admin/user_greetings';

public function render()
{
$oUser = oxNew(User::class);
if ($oUser->load($this->getEditObjectId())) {
$this->addTplParam(ModuleCore::OEMT_ADMIN_GREETING_TEMPLATE_VARNAME, $oUser->getPersonalGreeting());
}

return parent::render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\Tests\Integration\Controller\Admin;

use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
use OxidEsales\ModuleTemplate\Greeting\Controller\Admin\GreetingAdminController;
use OxidEsales\ModuleTemplate\Core\Module as ModuleCore;
use OxidEsales\ModuleTemplate\Tests\Integration\IntegrationTestCase;

/*
* We want to test controller behavior going 'full way'.
* No mocks, we go straight to the database (full integration)).
*/
final class GreetingAdminControllerTest extends IntegrationTestCase
{
public const TEST_USER_ID = '_testuser';

public const TEST_GREETING = 'Hello there!';

public function testRender(): void
{
$this->createTestUser();

$controller = oxNew(GreetingAdminController::class);
$controller->setEditObjectId(self::TEST_USER_ID);

$this->assertSame('@oe_moduletemplate/admin/user_greetings', $controller->render());

$viewData = $controller->getViewData();

$this->assertSame(self::TEST_GREETING, $viewData[ModuleCore::OEMT_ADMIN_GREETING_TEMPLATE_VARNAME]);
}

private function createTestUser(): void
{
$user = oxNew(EshopModelUser::class);
$user->assign(
[
'oxid' => self::TEST_USER_ID,
'oemtgreeting' => self::TEST_GREETING,
]
);
$user->save();
}
}
7 changes: 7 additions & 0 deletions views/admin_twig/de/module_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

$aLang = [
'charset' => 'UTF-8',
'tbcluser_greetings' => 'Greetings',

'OEMODULETEMPLATE_GREETING_TITLE' => 'Admin controller example',
'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ',
'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!',

# Module settings
'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Einstellungen',
'SHOP_MODULE_oemoduletemplate_GreetingMode' => 'Begrüßungsmodus',
'SHOP_MODULE_oemoduletemplate_GreetingMode_generic' => 'höflich',
Expand Down
7 changes: 7 additions & 0 deletions views/admin_twig/en/module_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

$aLang = [
'charset' => 'UTF-8',
'tbcluser_greetings' => 'Greetings',

'OEMODULETEMPLATE_GREETING_TITLE' => 'Admin controller example',
'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ',
'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!',

# Module settings
'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Settings',
'SHOP_MODULE_oemoduletemplate_GreetingMode' => 'Greeting mode',
'SHOP_MODULE_oemoduletemplate_GreetingMode_generic' => 'generic',
Expand Down
18 changes: 18 additions & 0 deletions views/twig/admin/user_greetings.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% include "headitem.html.twig" with {title: "GENERAL_ADMIN_TITLE"|translate} %}

<form action="{{ oViewConf.getSelfLink() }}" method="post">
{{ oViewConf.getHiddenSid()|raw }}
<input type="hidden" name="oxid" value="{{ oxid }}">
<input type="hidden" name="cl" value="oemt_admin_greeting">
</form>

<h1 class="page-header">{{ translate({ ident: "OEMODULETEMPLATE_GREETING_TITLE" }) }}</h1>

{% if greeting_message %}
<div>{{ translate({ ident: "OEMODULETEMPLATE_GREETING_MESSAGE_TEXT" }) }} {{ greeting_message }}</div>
{% else %}
<div>{{ translate({ ident: "OEMODULETEMPLATE_NO_GREETING_TEXT" }) }}</div>
{% endif %}

{% include "bottomnaviitem.html.twig" %}
{% include "bottomitem.html.twig" %}

0 comments on commit 7297ff3

Please sign in to comment.