Skip to content

Commit

Permalink
User Edit improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
luke- committed Dec 2, 2022
1 parent 7e9b7b8 commit 630a3e8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
2 changes: 1 addition & 1 deletion resources/views/user/edit.php
Expand Up @@ -39,7 +39,7 @@

<?= Html::form()->post($urlGenerator->generate('user-edit', ['dn' => $userForm->user->getDn()]))->csrf($csrf)->open() ?>

<?php foreach ($fieldSet as $row): ?>
<?php foreach ($userForm->getFormSchema()->getRows() as $row): ?>
<div class="row">
<?php foreach ($row as $fieldName => $fieldLabel): ?>
<div class="col-sm">
Expand Down
20 changes: 19 additions & 1 deletion resources/views/user/list.php
Expand Up @@ -12,6 +12,7 @@
*/

use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Button;

$this->setTitle($applicationParameters->getName());
?>
Expand All @@ -24,7 +25,24 @@
<p class="lead">
Overview of all users
</p>
<br>
<br/>

<!--
<form class="row g-3 alert alert-dark" style="padding-top:0px;"
action="#" method="post">
<div class="col-auto" style="width:300px">
<label for="test">Organizational Unit</label>
<?= Html::select('test')
->optionsData(['foo' => 'bar', 'bla' => 'fuu'])
->addClass('form-control')
?>
</div>
<div class="col-auto">
<?= Button::submit('Add')->addClass('btn btn-primary')->addAttributes(['style' => 'margin-top:24px']); ?>
</div>
</form>
-->

<table class="table table-striped" data-search="true" data-toggle="table" data-pagination="true"
data-page-size="100" data-sortable="true" data-click-to-select="true">
<thead>
Expand Down
1 change: 0 additions & 1 deletion src/User/UserController.php
Expand Up @@ -82,7 +82,6 @@ public function edit(ServerRequestInterface $request, WebControllerService $webS
'dn' => $userForm->user->getDn(),
'parentDNs' => $this->ldapService->getOrganizationalUnits(),
'userForm' => $userForm,
'fieldSet' => $this->applicationParameters->getUserEditFields()
]);
}

Expand Down
29 changes: 19 additions & 10 deletions src/User/UserForm.php
Expand Up @@ -3,6 +3,7 @@
namespace Balemy\LdapCommander\User;

use Balemy\LdapCommander\ApplicationParameters;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Form\FormModel;


Expand All @@ -12,12 +13,15 @@ class UserForm extends FormModel
private array $_attrs = [];

private array $internalAttrs = ['parentDn'];
private UserFormSchema $formSchema;


public function __construct(private ApplicationParameters $applicationParameters)
public function __construct(ApplicationParameters $applicationParameters)
{
parent::__construct();
$this->formSchema = new UserFormSchema($applicationParameters);
$this->user = new User();

parent::__construct();
}

public function setUser(User $user): void
Expand All @@ -36,6 +40,14 @@ public function getAttributeCastValue(string $attribute): mixed
return $this->_attrs[$attribute] ?? '';
}

/**
* @return UserFormSchema
*/
public function getFormSchema(): UserFormSchema
{
return $this->formSchema;
}


private function getEntryValue(string $name): string
{
Expand All @@ -53,9 +65,10 @@ private function getEntryValue(string $name): string

public function getAttributeLabels(): array
{
return [
/** @var string[] */
return array_merge([
'parentDn' => 'Organizational Unit'
];
], $this->formSchema->getFields());
}

/**
Expand All @@ -65,12 +78,8 @@ public function getAttributeLabels(): array
protected function collectAttributes(): array
{
$fields = ['parentDn' => 'string'];
$rows = $this->applicationParameters->getUserEditFields();
/** @var string[] $row */
foreach ($rows as $row) {
foreach (array_keys($row) as $fieldKey) {
$fields[$fieldKey] = 'string';
}
foreach (array_keys($this->formSchema->getFields()) as $attribute) {
$fields[$attribute] = 'string';
}
return $fields;
}
Expand Down
37 changes: 37 additions & 0 deletions src/User/UserFormSchema.php
@@ -0,0 +1,37 @@
<?php

namespace Balemy\LdapCommander\User;

use Balemy\LdapCommander\ApplicationParameters;
use Yiisoft\Form\FormModel;


final class UserFormSchema
{

private array $_fields = [];

private array $_rows;

public function __construct(private readonly ApplicationParameters $applicationParameters)
{
$this->_rows = $this->applicationParameters->getUserEditFields();

/** @var string[] $row */
foreach ($this->_rows as $row) {
foreach ($row as $fieldKey => $fieldLabel) {
$this->_fields[$fieldKey] = $fieldLabel;
}
}
}

public function getRows(): array
{
return $this->_rows;
}

public function getFields(): array
{
return $this->_fields;
}
}

0 comments on commit 630a3e8

Please sign in to comment.