Skip to content

Commit

Permalink
Backend classes for core view buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jul 22, 2024
1 parent e53459c commit 445248d
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Panel/Ui/Buttons/LanguagesButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Toolkit\Str;

/**
* View button to switch content translation languages
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class LanguagesButton extends ViewButton
{
protected App $kirby;

public function __construct(
) {
$this->kirby = App::instance();

parent::__construct(
component: 'k-view-languages-button',
class: 'k-view-languages-button',
icon: 'translate',
options: $this->options(),
responsive: 'text',
text: Str::upper($this->kirby->language()?->code())
);
}

protected function option(Language $language): array
{
return [
'text' => $language->name(),
'code' => $language->code(),
'current' => $language->code() === $this->kirby->language()->code(),
];
}

protected function options(): array
{
$languages = $this->kirby->languages();
$options = [];

if ($this->kirby->multilang() === false) {
return $options;
}

// add the primary/default language first
if ($default = $languages->default()) {
$options[] = $this->option($default);
$options[] = '-';
$languages = $languages->not($default);
}

// add all secondary languages after the separator
foreach ($languages as $language) {
$options[] = $this->option($language);
}

return $options;
}

public function render(): array|null
{
if ($this->kirby->multilang() === false) {
return null;
}

return parent::render();
}
}
48 changes: 48 additions & 0 deletions src/Panel/Ui/Buttons/PageStatusButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\Page;
use Kirby\Toolkit\I18n;

/**
* Status view button for pages
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class PageStatusButton extends ViewButton
{
public function __construct(
Page $page
) {
$status = $page->status();
$blueprint = $page->blueprint()->status()[$status] ?? null;
$disabled = $page->permissions()->cannot('changeStatus');
$title = I18n::translate('page.status') . ': ' . I18n::translate('page.status.' . $status);

if ($disabled === true) {
$title .= ' (' . I18n::translate('disabled') . ')';
}

parent::__construct(
class: 'k-view-status-button k-page-status-button',
dialog: $page->panel()->url(true) . '/changeStatus',
disabled: $disabled,
icon: 'status-' . $status,
style: '--icon-size: 15px',
text: $blueprint['label'] ?? $status,
title: $title,
theme: match($status) {
'draft' => 'negative-icon',
'unlisted' => 'info-icon',
'listed' => 'positive-icon'
}
);
}
}
31 changes: 31 additions & 0 deletions src/Panel/Ui/Buttons/PreviewButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Toolkit\I18n;

/**
* Preview view button
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class PreviewButton extends ViewButton
{
public function __construct(
public string|null $link
) {
parent::__construct(
class: 'k-view-preview-button',
icon: 'open',
link: $link,
target: '_blank',
title: I18n::translate('open')
);
}
}
32 changes: 32 additions & 0 deletions src/Panel/Ui/Buttons/SettingsButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\ModelWithContent;
use Kirby\Toolkit\I18n;

/**
* Settings view button for models
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class SettingsButton extends ViewButton
{
public function __construct(
ModelWithContent $model
) {
parent::__construct(
component: 'k-view-settings-button',
class: 'k-view-settings-button',
icon: 'cog',
options: $model->panel()->url(true),
title: I18n::translate('settings'),
);
}
}
59 changes: 59 additions & 0 deletions tests/Panel/Ui/Buttons/LanguagesButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Panel\Areas\AreaTestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\LanguagesButton
* @covers ::__construct
*/
class LanguagesButtonTest extends AreaTestCase
{
/**
* @covers ::render
*/
public function testSingleLang()
{
$button = new LanguagesButton();
$this->assertNull($button->render());
}

/**
* @covers ::option
* @covers ::options
* @covers ::props
* @covers ::render
*/
public function tesMultiLang()
{
$this->enableMultilang();
$this->installLanguages();

$button = new LanguagesButton();
$this->assertSame('k-view-languages-button', $button->component);
$this->assertSame('k-view-languages-button', $button->class);
$this->assertSame('translate', $button->icon);
$this->assertSame([
[
'text' => 'English',
'code' => 'en',
'current' => true
],
'-',
[
'text' => 'Deutsch',
'code' => 'de',
'current' => false
]
], $button->options);
$this->assertSame('text', $button->responsive);
$this->assertSame('EN', $button->text);

$render = $button->render();
$this->assertIsArray($render);
$this->assertSame('k-view-languages-button', $render['component']);
$this->assertIsArray($render['props']);
$this->assertIsArray($render['props']['options']);
}
}
50 changes: 50 additions & 0 deletions tests/Panel/Ui/Buttons/PageStatusButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Cms\Page;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\PageStatusButton
*/
class PageStatusButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testButtonDraftDisabled()
{

$page = new Page(['slug' => 'test', 'isDraft' => true]);
$button = new PageStatusButton($page);

$this->assertSame('k-view-button', $button->component);
$this->assertSame('k-view-status-button k-page-status-button', $button->class);
$this->assertSame('/pages/test/changeStatus', $button->dialog);
$this->assertTrue($button->disabled);
$this->assertSame('status-draft', $button->icon);
$this->assertTrue($button->responsive);
$this->assertSame('Draft', $button->text);
$this->assertSame('Status: Draft (Disabled)', $button->title);
$this->assertSame('negative-icon', $button->theme);
}

/**
* @covers ::__construct
*/
public function testButtonUnlisted()
{
App::instance()->impersonate('kirby');
$page = new Page(['slug' => 'test']);
$button = new PageStatusButton($page);

$this->assertFalse($button->disabled);
$this->assertSame('status-unlisted', $button->icon);
$this->assertTrue($button->responsive);
$this->assertSame('Unlisted', $button->text);
$this->assertSame('Status: Unlisted', $button->title);
$this->assertSame('info-icon', $button->theme);
}
}
27 changes: 27 additions & 0 deletions tests/Panel/Ui/Buttons/PreviewButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\PreviewButton
*/
class PreviewButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testButton()
{
$button = new PreviewButton(link: 'https://getkirby.com');

$this->assertSame('k-view-button', $button->component);
$this->assertSame('k-view-preview-button', $button->class);
$this->assertSame('open', $button->icon);
$this->assertSame('https://getkirby.com', $button->link);
$this->assertSame('_blank', $button->target);
$this->assertSame('Open', $button->title);
}

}
27 changes: 27 additions & 0 deletions tests/Panel/Ui/Buttons/SettingsButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\Page;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\SettingsButton
*/
class SettingsButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testButton()
{
$page = new Page(['slug' => 'test']);
$button = new SettingsButton(model: $page);

$this->assertSame('k-view-settings-button', $button->component);
$this->assertSame('k-view-settings-button', $button->class);
$this->assertSame('cog', $button->icon);
$this->assertSame('/pages/test', $button->options);
$this->assertSame('Settings', $button->title);
}
}

0 comments on commit 445248d

Please sign in to comment.