Skip to content

Commit

Permalink
Mise à jour pour la version beta2 du CMS.
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Sep 7, 2020
1 parent cfdc9f5 commit 23d9fcb
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 178 deletions.
13 changes: 7 additions & 6 deletions Config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
R::useNamespace('SoosyzeExtension\Starterkit\Controller');

R::get('starterkit.index', 'starterkit/index', 'Starterkit@index');
R::get('starterkit.show', 'starterkit/:id', 'Starterkit@show', [':id' => '\d+']);

R::get('starterkit.admin', 'admin/starterkit', 'Starterkit@admin');
R::get('starterkit.show', 'starterkit/:id', 'Starterkit@show', [':id' => '\d']);
R::get('starterkit.create', 'starterkit/item', 'Starterkit@create');
R::post('starterkit.store', 'starterkit/item', 'Starterkit@store');
R::get('starterkit.edit', 'starterkit/:id/edit', 'Starterkit@edit', [':id' => '\d']);
R::post('starterkit.update', 'starterkit/:id/edit', 'Starterkit@update', [':id' => '\d']);
R::post('starterkit.delete', 'starterkit/:id/delete', 'Starterkit@delete', [':id' => '\d']);
R::get('starterkit.create', 'admin/starterkit/create', 'Starterkit@create');
R::post('starterkit.store', 'admin/starterkit/create', 'Starterkit@store');
R::get('starterkit.edit', 'admin/starterkit/:id/edit', 'Starterkit@edit', [':id' => '\d+']);
R::post('starterkit.update', 'admin/starterkit/:id/edit', 'Starterkit@update', [':id' => '\d+']);
R::post('starterkit.delete', 'admin/starterkit/:id/delete', 'Starterkit@delete', [':id' => '\d+']);
31 changes: 13 additions & 18 deletions Config/service.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
{
"starterkit": {
"class": "SoosyzeExtension\\Starterkit\\Services\\Starterkit",
"arguments": [
"@query"
]
"arguments": ["@query"]
},
"starterkit.install": {
"class": "SoosyzeExtension\\Starterkit\\Install",
"hooks": {
"install.user": "hookInstallUser",
"install.menu": "hookInstallMenu"
}
},
"starterkit.hook.config": {
"class": "SoosyzeExtension\\Starterkit\\Services\\HookConfig",
"arguments": [
"@config"
],
"hooks": {
"config.edit.menu": "menu",
"config.edit.starterkit.form.generate": "form",
"config.update.starterkit.validator": "validator",
"config.update.starterkit.before": "before"
"config.edit.menu": "menu"
}
},
"starterkit.hook.user": {
"class": "SoosyzeExtension\\Starterkit\\Services\\HookUser",
"hooks": {
"user.permission.module": "hookPermission",
"route.starterkit.index": "hookStarterkitShow",
"route.starterkit.show": "hookStarterkitShow",
"route.starterkit.create": "hookStarterkitCreated",
"route.starterkit.store": "hookStarterkitCreated",
"route.starterkit.edit": "hookStarterkitEdited",
"route.starterkit.update": "hookStarterkitEdited"
}
},
"starterkit.install": {
"class": "SoosyzeExtension\\Starterkit\\Install",
"hooks": {
"install.user": "hookInstallUser",
"install.menu": "hookInstallMenu"
"route.starterkit.update": "hookStarterkitEdited",
"route.starterkit.delete": "hookStarterkitDelete"
}
}
}
172 changes: 106 additions & 66 deletions Controller/Starterkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace SoosyzeExtension\Starterkit\Controller;

use Soosyze\Components\Form\FormBuilder;
use Psr\Http\Message\ServerRequestInterface;
use Soosyze\Components\Http\Redirect;
use Soosyze\Components\Http\ServerRequest;
use Soosyze\Components\Validator\Validator;
use SoosyzeExtension\Starterkit\Form\FormStarterkit;

class Starterkit extends \Soosyze\Controller
{
Expand All @@ -16,126 +16,166 @@ public function __construct()
$this->pathViews = dirname(__DIR__) . '/Views/';
}

public function index(ServerRequest $req)
public function index(ServerRequestInterface $req)
{
$linkShow = self::router()->getRoute('starterkit.show', [ ':id' => 1 ]);

return self::template()
->view('page', [
'title_main' => t('Starterkit index')
'title_main' => t('Starterkit home page')
])
->make('page.content', 'page-starterkit-index.php', $this->pathViews, [
->make('page.content', 'starterkit/content-starterkit-index.php', $this->pathViews, [
'link_show' => $linkShow
]);
}

public function admin(ServerRequest $req)
public function admin(ServerRequestInterface $req)
{
$linkCreate = self::router()->getRoute('starterkit.create');
$linkEdit = self::router()->getRoute('starterkit.edit', [ ':id' => 1 ]);
$messages = [];
if (isset($_SESSION[ 'messages' ])) {
$messages = $_SESSION[ 'messages' ];
unset($_SESSION[ 'messages' ]);
}

return self::template()
->getTheme('theme_admin')
->view('page', [
'title_main' => t('Starterkit admin'),
'title_main' => t('Administer starterkit'),
'icon' => '<i class="fa fa-puzzle-piece" aria-hidden="true"></i>'
])
->make('page.content', 'page-starterkit-admin.php', $this->pathViews, [
'link_create' => $linkCreate,
'link_edit' => $linkEdit
->view('page.messages', $messages)
->make('page.content', 'starterkit/content-starterkit-admin.php', $this->pathViews, [
'link_create' => self::router()->getRoute('starterkit.create'),
'link_edit' => self::router()->getRoute('starterkit.edit', [
':id' => 1
])
]);
}

public function show($id, ServerRequest $req)
public function show($id, ServerRequestInterface $req)
{
return self::template()
->view('page', [
'title_main' => 'Starterkit content ' . $id,
'title_main' => t('Starterkit :id', [ ':id' => $id ]),
])
->make('page.content', 'page-starterkit-show.php', $this->pathViews, [
->make('page.content', 'starterkit/content-starterkit-show.php', $this->pathViews, [
'id' => $id
]);
}

public function create(ServerRequest $req)
public function create(ServerRequestInterface $req)
{
$action = self::router()->getRoute('starterkit.store');

$form = (new FormBuilder([ 'method' => 'post', 'action' => $action ]))
->group('start-config-fieldset', 'fieldset', function ($form) {
$form->legend('start-config-legend', t('Starterkit config'))
->group('title-group', 'div', function ($form) {
$form->label('title-label', 'Title', [
'for' => 'title'
])
->text('title', [
'class' => 'form-control',
'maxlength' => 255,
'placeholder' => 'Field example',
'required' => 1
]);
}, [ 'class' => 'form-group' ]);
})
->token('starterkit_create')
->submit('submit', t('Save'), [ 'class' => 'btn btn-success' ]);
$values = [];
if (isset($_SESSION[ 'inputs' ])) {
$values = $_SESSION[ 'inputs' ];
unset($_SESSION[ 'inputs' ]);
}

$form = (new FormStarterkit([
'method' => 'post',
'action' => self::router()->getRoute('starterkit.store')
]))
->setValues($values)
->makeFields();

$messages = [];
if (isset($_SESSION[ 'messages' ])) {
$messages = $_SESSION[ 'messages' ];
unset($_SESSION[ 'messages' ]);
}

return self::template()
->getTheme('theme_admin')
->view('page', [
'title_main' => t('Starterkit create')
'title_main' => t('Add Starterkit'),
'icon' => '<i class="fa fa-puzzle-piece" aria-hidden="true"></i>'
])
->make('page.content', 'form-starterkit-create.php', $this->pathViews, [
->view('page.messages', $messages)
->make('page.content', 'starterkit/content-starterkit-form.php', $this->pathViews, [
'form' => $form
]);
}

public function store(ServerRequest $req)
public function store(ServerRequestInterface $req)
{
$route = self::router()->getRoute('starterkit.admin');
$validator = $this->setValidator($req);

if ($validator->isValid()) {
$_SESSION[ 'messages' ][ 'success' ] = [ t('Your starterkit has been saved.') ];

return new Redirect(self::router()->getRoute('starterkit.admin'));
}

return new Redirect($route);
$_SESSION[ 'inputs' ] = $validator->getInputs();
$_SESSION[ 'messages' ][ 'errors' ] = $validator->getKeyErrors();

return new Redirect(self::router()->getRoute('starterkit.create'));
}

public function edit($id, ServerRequest $req)
public function edit($id, ServerRequestInterface $req)
{
$action = self::router()->getRoute('starterkit.edit', [ ':id' => $id ]);

$form = (new FormBuilder([ 'method' => 'post', 'action' => $action ]))
->group('title-group', 'div', function ($form) use ($id) {
$form->label('title-label', 'Title', [
'for' => 'title'
])
->text('title', [
'class' => 'form-control',
'maxlength' => 255,
'placeholder' => 'Field example',
'required' => 1,
'value' => $id
]);
}, [ 'class' => 'form-group' ])
->token('starterkit_edit')
->submit('submit', t('Save'), [ 'class' => 'btn btn-success' ]);
$values = [ 'title' => 'Example title' ];
if (isset($_SESSION[ 'inputs' ])) {
$values = $_SESSION[ 'inputs' ];
unset($_SESSION[ 'inputs' ]);
}

$form = (new FormStarterkit([
'method' => 'post',
'action' => self::router()->getRoute('starterkit.edit', [ ':id' => $id ])
]))
->setValues($values)
->makeFields();

$messages = [];
if (isset($_SESSION[ 'messages' ])) {
$messages = $_SESSION[ 'messages' ];
unset($_SESSION[ 'messages' ]);
}

return self::template()
->getTheme('theme_admin')
->view('page', [
'title_main' => t('Starterkit edit :id', [ ':id' => $id ])
'title_main' => t('Edit :id starterkit', [ ':id' => $id ]),
'icon' => '<i class="fa fa-puzzle-piece" aria-hidden="true"></i>'
])
->make('page.content', 'form-starterkit-edit.php', $this->pathViews, [
->view('page.messages', $messages)
->make('page.content', 'starterkit/content-starterkit-form.php', $this->pathViews, [
'form' => $form
]);
}

public function update($id, ServerRequest $req)
public function update($id, ServerRequestInterface $req)
{
$route = self::router()->getRoute('starterkit.admin');
$validator = $this->setValidator($req);

return new Redirect($route);
if ($validator->isValid()) {
$_SESSION[ 'messages' ][ 'success' ] = [ t('Your starterkit has been saved.') ];

return new Redirect(self::router()->getRoute('starterkit.admin'));
}

$_SESSION[ 'inputs' ] = $validator->getInputs();
$_SESSION[ 'messages' ][ 'errors' ] = $validator->getKeyErrors();

return new Redirect(self::router()->getRoute('starterkit.edit', [ ':id' => $id ]));
}

public function delete($id, ServerRequest $req)
public function delete($id, ServerRequestInterface $req)
{
$route = self::router()->getRoute('starterkit.admin');
return new Redirect(self::router()->getRoute('starterkit.admin'));
}

return new Redirect($route);
private function setValidator(ServerRequestInterface $req)
{
return (new Validator)
->setRules([
'title' => 'required|string|max:255',
'token_starterkit' => 'token'
])
->setLabel([
'title' => t('Title')
])
->setInputs($req->getParsedBody());
}
}
36 changes: 36 additions & 0 deletions Form/FormStarterkit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace SoosyzeExtension\Starterkit\Form;

use Soosyze\Components\Form\FormBuilder;

class FormStarterkit extends FormBuilder
{
protected $values = [ 'title' => '' ];

public function setValues(array $values)
{
$this->values = array_merge($this->values, $values);

return $this;
}

public function makeFields()
{
return $this->group('starterkit-fieldset', 'fieldset', function ($form) {
$form->legend('starterkit-legend', t('Fields starterkit'))
->group('title-group', 'div', function ($form) {
$form->label('title-label', 'Title')
->text('title', [
'class' => 'form-control',
'maxlength' => 255,
'placeholder' => t('Enter title'),
'required' => 1,
'value' => $this->values[ 'title' ]
]);
}, [ 'class' => 'form-group' ]);
})
->token('token_starterkit')
->submit('submit', t('Save'), [ 'class' => 'btn btn-success' ]);
}
}
Loading

0 comments on commit 23d9fcb

Please sign in to comment.