integrate form into admin part

This commit is contained in:
Manuel Raynaud
2013-06-25 14:30:35 +02:00
parent 1fc1482ac4
commit 7bc1b4f513
5 changed files with 73 additions and 18 deletions

View File

@@ -1,19 +1,55 @@
<?php <?php
/** /*************************************************************************************/
* Created by JetBrains PhpStorm. /* */
* User: manu /* Thelia */
* Date: 20/06/13 /* */
* Time: 12:05 /* Copyright (c) OpenStudio */
* To change this template use File | Settings | File Templates. /* email : info@thelia.net */
*/ /* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Admin\Controller; namespace Thelia\Admin\Controller;
use Thelia\Form\AdminLogin;
class AdminController extends BaseAdminController { class AdminController extends BaseAdminController {
public function indexAction() public function indexAction()
{ {
return $this->render("login.html");
$form = $this->getFormBuilder();
$adminLogin = new AdminLogin();
$form = $adminLogin->buildForm($form, array())->getForm();
$request = $this->getRequest();
if($request->isMethod("POST")) {
$form->bind($request);
if($form->isValid()) {
//TODO
} else {
//TODO
}
}
return $this->render("login.html", array(
"form" => $form->createView()
));
} }
} }

View File

@@ -24,6 +24,10 @@ namespace Thelia\Admin\Controller;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Thelia\Form\BaseForm;
use Thelia\Model\ConfigQuery;
/** /**
* *
@@ -44,7 +48,7 @@ class BaseAdminController extends ContainerAware
*/ */
public function render($templateName, $args = array()) public function render($templateName, $args = array())
{ {
$args = array('lang' => 'fr'); $args = array_merge($args, array('lang' => 'fr'));
$response = new Response(); $response = new Response();
@@ -53,7 +57,7 @@ class BaseAdminController extends ContainerAware
public function renderRaw($templateName, $args = array()) public function renderRaw($templateName, $args = array())
{ {
$args = array('lang' => 'fr'); $args = array_merge($args, array('lang' => 'fr'));
return $this->getParser()->render($templateName, $args); return $this->getParser()->render($templateName, $args);
} }
@@ -76,5 +80,15 @@ class BaseAdminController extends ContainerAware
return $parser; return $parser;
} }
public function getFormFactory()
{
return BaseForm::getFormFactory($this->getRequest(), ConfigQuery::read("form.secret.admin", md5(__DIR__)));
}
public function getFormBuilder()
{
return $this->getFormFactory()->createBuilder("form");
}
} }

View File

@@ -88,7 +88,7 @@ class Form implements SmartyPluginInterface
$template->assign("options", $form->vars); $template->assign("options", $form->vars);
$template->assign("name", $form->vars["name"]); $template->assign("name", $form->vars["full_name"]);
$template->assign("value", $form->vars["value"]); $template->assign("value", $form->vars["value"]);
$template->assign("label", $form->vars["label"]); $template->assign("label", $form->vars["label"]);
$attr = array(); $attr = array();
@@ -119,7 +119,7 @@ class Form implements SmartyPluginInterface
foreach ($form->getIterator() as $row) { foreach ($form->getIterator() as $row) {
if ($this->isHidden($row) && $row->isRendered() === false) { if ($this->isHidden($row) && $row->isRendered() === false) {
$return .= sprintf($field, $row->vars["name"], $row->vars["value"]); $return .= sprintf($field, $row->vars["full_name"], $row->vars["value"]);
} }
} }
@@ -136,7 +136,7 @@ class Form implements SmartyPluginInterface
$form = $params["form"]; $form = $params["form"];
if (! $form instanceof \Symfony\Component\Form\FormView) { if (! $form instanceof \Symfony\Component\Form\FormView) {
throw new \InvalidArgumentException("form parameter in form_field block must be an instance of throw new \InvalidArgumentException("form parameter in form_enctype function must be an instance of
Symfony\Component\Form\FormView"); Symfony\Component\Form\FormView");
} }

View File

@@ -36,7 +36,7 @@ class BaseForm {
* @param Request $request * @param Request $request
* @return \Symfony\Component\Form\FormFactoryInterface * @return \Symfony\Component\Form\FormFactoryInterface
*/ */
public static function getFormFactory(Request $request) public static function getFormFactory(Request $request, $secret = null)
{ {
$form = Forms::createFormFactoryBuilder() $form = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension()) ->addExtension(new HttpFoundationExtension())
@@ -44,7 +44,7 @@ class BaseForm {
new CsrfExtension( new CsrfExtension(
new SessionCsrfProvider( new SessionCsrfProvider(
$request->getSession(), $request->getSession(),
ConfigQuery::read("form.secret", md5(__DIR__)) $secret ?: ConfigQuery::read("form.secret", md5(__DIR__))
) )
) )
)->getFormFactory(); )->getFormFactory();

View File

@@ -15,9 +15,14 @@
<div class="hero-unit"> <div class="hero-unit">
<h1>{intl l='Thelia Back Office'}</h1> <h1>{intl l='Thelia Back Office'}</h1>
<form action="/admin/login" method="post" class="well form-inline"> <form action="/admin" method="post" class="well form-inline" {form_enctype form=$form}>
<input type="text" class="input" placeholder="{intl l='E-mail address'}" name="username" /> {form_field_hidden form=$form}
<input type="password" class="input" placeholder="{intl l='Password'}" name="password" /> {form_field form=$form.username}
<input type="text" class="input" placeholder="{intl l='E-mail address'}" name="{$name}" />
{/form_field}
{form_field form=$form.password}
<input type="password" class="input" placeholder="{intl l='Password'}" name="{$name}" />
{/form_field}
<label class="checkbox"> <input type="checkbox" name="remember" value="yes"> {intl l='Remember me'}</label> <label class="checkbox"> <input type="checkbox" name="remember" value="yes"> {intl l='Remember me'}</label>