Skip to content

Commit

Permalink
v.5 update
Browse files Browse the repository at this point in the history
Improvements added from phalcon#165 by @niden;
Was resetting to default select selection - added selected option to the select after a "search" and "submit"
  • Loading branch information
shenyman authored Jan 28, 2023
1 parent bc36edc commit f4f77ff
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/Controllers/PermissionsController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Vökuró.
Expand All @@ -10,12 +9,14 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Vokuro\Controllers;

use Vokuro\Models\Permissions;
use Vokuro\Models\Profiles;
use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Select;

use const PHP_EOL;

/**
* View and define permissions for the various profile levels.
Expand All @@ -30,12 +31,17 @@ public function indexAction(): void
{
$this->view->setTemplateBefore('private');

//default select option value
$selectedOption = 0;
if ($this->request->isPost()) {
$profile = Profiles::findFirstById($this->request->getPost('profileId'));
$selectedOption = $this->request->getPost('profileId');
$profile = Profiles::findFirstById($selectedOption);
if ($profile) {
if ($this->request->hasPost('permissions') && $this->request->hasPost('submit')) {
// Deletes the current permissions
$profile->getPermissions()->delete();
$profile->getPermissions()
->delete()
;

// Save the new permissions
foreach ($this->request->getPost('permissions') as $permission) {
Expand Down Expand Up @@ -69,30 +75,23 @@ public function indexAction(): void
],
]);

$escaper = new Escaper();
$helper = new Select($escaper);

$options = [
'id' => 'profileId',
'name' => 'profileId',
'class' => 'form-control mr-sm-2'
];

$profilesSelect = $helper(' ', PHP_EOL, $options);
$profilesSelect
->addPlaceholder(
'...',
"0",
[],
true,
)
->selected(strval($profile->id))

$select = $this
->tag
->inputSelect(' ', PHP_EOL, $options)
->addPlaceholder('...', '', [], true)
->selected((string) $selectedOption)
;

foreach ($profiles as $profile) {
$profilesSelect->add($profile->name, strval($profile->id));
$select->add($profile->name, (string) $profile->id);
}

$this->view->setVar('profilesSelect', $profilesSelect);
$this->view->setVar('profilesSelect', $select);
}
}

0 comments on commit f4f77ff

Please sign in to comment.