Skip to content

Commit

Permalink
Add sample single page to manage config value
Browse files Browse the repository at this point in the history
Fixed #4
  • Loading branch information
hissy committed Jun 23, 2022
1 parent 989a2f0 commit 78bdf73
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Controller extends Package
*
* @var string
*/
protected $pkgVersion = '0.0.4';
protected $pkgVersion = '0.0.5';

/**
* @see https://documentation.concretecms.org/developers/packages/adding-custom-code-to-packages
Expand Down
17 changes: 17 additions & 0 deletions controllers/single_page/dashboard/system/boilerplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Concrete\Package\V9PackageBoilerplate\Controller\SinglePage\Dashboard\System;

use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;

class Boilerplate extends DashboardPageController
{
public function view()
{
/** @var ResolverManagerInterface $resolver */
$resolver = $this->app->make(ResolverManagerInterface::class);

return $this->buildRedirect($resolver->resolve(['/dashboard/system/boilerplate/config']));
}
}
56 changes: 56 additions & 0 deletions controllers/single_page/dashboard/system/boilerplate/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Concrete\Package\V9PackageBoilerplate\Controller\SinglePage\Dashboard\System\Boilerplate;

use Concrete\Core\Config\Repository\Liaison;
use Concrete\Core\Package\PackageService;
use Concrete\Core\Page\Controller\DashboardPageController;
use Symfony\Component\HttpFoundation\RedirectResponse;

class Config extends DashboardPageController
{
public function view()
{
$config = $this->getConfig();
if ($config) {
$example_value = $config->get('app.example');
$this->set('example_value', $example_value);
}
}

public function submit(): ?RedirectResponse
{
if (!$this->token->validate('update_config')) {
$this->error->add($this->token->getErrorMessage());
}

$example_value = trim($this->post('example_key'));
if (empty($example_value)) {
$this->error->add(t('The key is required.'));
}

if (!$this->error->has()) {
$config = $this->getConfig();
if ($config) {
$config->save('app.example', $example_value);
$this->flash('success', t('The settings has been successfully updated.'));
}

return $this->buildRedirect([$this->getPageObject()]);
}

return null;
}

protected function getConfig(): ?Liaison
{
/** @var PackageService $packageService */
$packageService = $this->app->make(PackageService::class);
$package = $packageService->getClass('v9_package_boilerplate');
if ($package) {
return $package->getFileConfig();
}

return null;
}
}
12 changes: 12 additions & 0 deletions install/singlepages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,17 @@
</attributekey>
</attributes>
</page>
<page name="Boilerplate"
path="/dashboard/system/boilerplate"
filename="/dashboard/system/boilerplate/view.php"
pagetype=""
description=""
package="v9_package_boilerplate"/>
<page name="Config"
path="/dashboard/system/boilerplate/config"
filename="/dashboard/system/boilerplate/config.php"
pagetype=""
description=""
package="v9_package_boilerplate"/>
</singlepages>
</concrete5-cif>
26 changes: 26 additions & 0 deletions single_pages/dashboard/system/boilerplate/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
defined('C5_EXECUTE') or die('Access Denied.');

/** @var \Concrete\Core\Page\View\PageView $view */
/** @var \Concrete\Core\Form\Service\Form $form */
/** @var \Concrete\Core\Validation\CSRF\Token $token */

$example_value = $example_value ?? null;
?>
<form action="<?= $view->action('submit') ?>" method="post">
<?php $token->output('update_config'); ?>

<div class="form-group">
<?= $form->label('example_key', t('Example Config Value')) ?>
<?= $form->text('example_key', $example_value) ?>
<p class="help-block"><?= t('This is an example config value and not used in anywhere.') ?></p>
</div>

<div class="ccm-dashboard-form-actions-wrapper">
<div class="ccm-dashboard-form-actions">
<button type="submit" class="btn btn-primary float-end">
<?php echo t('Save') ?>
</button>
</div>
</div>
</form>
2 changes: 2 additions & 0 deletions single_pages/dashboard/system/boilerplate/view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
defined('C5_EXECUTE') or die('Access Denied.');

0 comments on commit 78bdf73

Please sign in to comment.