Fix comment

This commit is contained in:
Franck Allimant
2014-04-18 00:03:27 +02:00
parent a3883ef1b6
commit 4b18c4ffba

View File

@@ -1,246 +1,246 @@
<?php <?php
/*************************************************************************************/ /*************************************************************************************/
/* */ /* */
/* Thelia */ /* Thelia */
/* */ /* */
/* Copyright (c) OpenStudio */ /* Copyright (c) OpenStudio */
/* email : info@thelia.net */ /* email : info@thelia.net */
/* web : http://www.thelia.net */ /* web : http://www.thelia.net */
/* */ /* */
/* This program is free software; you can redistribute it and/or modify */ /* 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 */ /* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */ /* the Free Software Foundation; either version 3 of the License */
/* */ /* */
/* This program is distributed in the hope that it will be useful, */ /* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */ /* GNU General Public License for more details. */
/* */ /* */
/* You should have received a copy of the GNU General Public License */ /* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */ /* */
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Form; namespace Thelia\Form;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Forms; use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension; use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider; use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Validation;
use Thelia\Core\Translation\Translator; use Thelia\Core\Translation\Translator;
use Thelia\Model\ConfigQuery; use Thelia\Model\ConfigQuery;
use Thelia\Tools\URL; use Thelia\Tools\URL;
/** /**
* Base form class for creating form objects * Base form class for creating form objects
* *
* Class BaseForm * Class BaseForm
* @package Thelia\Form * @package Thelia\Form
* @author Manuel Raynaud <mraynaud@openstudio.fr> * @author Manuel Raynaud <mraynaud@openstudio.fr>
*/ */
abstract class BaseForm abstract class BaseForm
{ {
/** /**
* @var \Symfony\Component\Form\FormFactoryInterface * @var \Symfony\Component\Form\FormFactoryInterface
*/ */
protected $formBuilder; protected $formBuilder;
/** /**
* @var \Symfony\Component\Form\Form * @var \Symfony\Component\Form\Form
*/ */
protected $form; protected $form;
protected $request; protected $request;
private $view = null; private $view = null;
/** /**
* true if the form has an error, false otherwise. * true if the form has an error, false otherwise.
* @var boolean * @var boolean
*/ */
private $has_error = false; private $has_error = false;
/** /**
* The form error message. * The form error message.
* @var string * @var string
*/ */
private $error_message = ''; private $error_message = '';
public function __construct(Request $request, $type= "form", $data = array(), $options = array()) public function __construct(Request $request, $type= "form", $data = array(), $options = array())
{ {
$this->request = $request; $this->request = $request;
$validator = Validation::createValidatorBuilder(); $validator = Validation::createValidatorBuilder();
if (!isset($options["attr"]["name"])) { if (!isset($options["attr"]["name"])) {
$options["attr"]["thelia_name"] = $this->getName(); $options["attr"]["thelia_name"] = $this->getName();
} }
$builder = Forms::createFormFactoryBuilder() $builder = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension()); ->addExtension(new HttpFoundationExtension());
if (!isset($options["csrf_protection"]) || $options["csrf_protection"] !== false) { if (!isset($options["csrf_protection"]) || $options["csrf_protection"] !== false) {
$builder->addExtension( $builder->addExtension(
new CsrfExtension( new CsrfExtension(
new SessionCsrfProvider( new SessionCsrfProvider(
$request->getSession(), $request->getSession(),
isset($options["secret"]) ? $options["secret"] : ConfigQuery::read("form.secret", md5(__DIR__)) isset($options["secret"]) ? $options["secret"] : ConfigQuery::read("form.secret", md5(__DIR__))
) )
) )
); );
} }
$translator = Translator::getInstance(); $translator = Translator::getInstance();
$validator $validator
->setTranslationDomain('validators') ->setTranslationDomain('validators')
->setTranslator($translator); ->setTranslator($translator);
$this->formBuilder = $builder $this->formBuilder = $builder
->addExtension(new ValidatorExtension($validator->getValidator())) ->addExtension(new ValidatorExtension($validator->getValidator()))
->getFormFactory() ->getFormFactory()
->createNamedBuilder($this->getName(), $type, $data, $this->cleanOptions($options)); ->createNamedBuilder($this->getName(), $type, $data, $this->cleanOptions($options));
; ;
$this->buildForm(); $this->buildForm();
// If not already set, define the success_url field // If not already set, define the success_url field
if (! $this->formBuilder->has('success_url')) { if (! $this->formBuilder->has('success_url')) {
$this->formBuilder->add("success_url", "text"); $this->formBuilder->add("success_url", "text");
} }
if (! $this->formBuilder->has('error_message')) { if (! $this->formBuilder->has('error_message')) {
$this->formBuilder->add("error_message", "text"); $this->formBuilder->add("error_message", "text");
} }
$this->form = $this->formBuilder->getForm(); $this->form = $this->formBuilder->getForm();
} }
public function getRequest() public function getRequest()
{ {
return $this->request; return $this->request;
} }
protected function cleanOptions($options) protected function cleanOptions($options)
{ {
unset($options["csrf_protection"]); unset($options["csrf_protection"]);
return $options; return $options;
} }
/** /**
* Returns the absolute URL to redirect the user to if the form is successfully processed. * Returns the absolute URL to redirect the user to if the form is successfully processed.
* *
* @param string $default the default URL. If not given, the configured base URL is used. * @param string $default the default URL. If not given, the configured base URL is used.
* *
* @return string an absolute URL * @return string an absolute URL
*/ */
public function getSuccessUrl($default = null) public function getSuccessUrl($default = null)
{ {
$successUrl = $this->form->get('success_url')->getData(); $successUrl = $this->form->get('success_url')->getData();
if (empty($successUrl)) { if (empty($successUrl)) {
if ($default === null) $default = ConfigQuery::read('base_url', '/'); if ($default === null) $default = ConfigQuery::read('base_url', '/');
$successUrl = $default; $successUrl = $default;
} }
return URL::getInstance()->absoluteUrl($successUrl); return URL::getInstance()->absoluteUrl($successUrl);
} }
public function createView() public function createView()
{ {
$this->view = $this->form->createView(); $this->view = $this->form->createView();
return $this; return $this;
} }
public function getView() public function getView()
{ {
if ($this->view === null) throw new \LogicException("View was not created. Please call BaseForm::createView() first."); if ($this->view === null) throw new \LogicException("View was not created. Please call BaseForm::createView() first.");
return $this->view; return $this->view;
} }
// -- Error and errro message ---------------------------------------------- // -- Error and errro message ----------------------------------------------
/** /**
* Set the error status of the form. * Set the error status of the form.
* *
* @param boolean $has_error * @param boolean $has_error
*/ */
public function setError($has_error = true) public function setError($has_error = true)
{ {
$this->has_error = $has_error; $this->has_error = $has_error;
return $this; return $this;
} }
/** /**
* Get the cuirrent error status of the form. * Get the cuirrent error status of the form.
* *
* @return boolean * @return boolean
*/ */
public function hasError() public function hasError()
{ {
return $this->has_error; return $this->has_error;
} }
/** /**
* Set the error message related to global form error * Set the error message related to global form error
* *
* @param unknown $message * @param string $message
*/ */
public function setErrorMessage($message) public function setErrorMessage($message)
{ {
$this->setError(true); $this->setError(true);
$this->error_message = $message; $this->error_message = $message;
return $this; return $this;
} }
/** /**
* Get the form error message. * Get the form error message.
* *
* @return string * @return string
*/ */
public function getErrorMessage() public function getErrorMessage()
{ {
return $this->error_message; return $this->error_message;
} }
/** /**
* @return \Symfony\Component\Form\Form * @return \Symfony\Component\Form\Form
*/ */
public function getForm() public function getForm()
{ {
return $this->form; return $this->form;
} }
/** /**
* *
* in this function you add all the fields you need for your Form. * in this function you add all the fields you need for your Form.
* Form this you have to call add method on $this->formBuilder attribute : * Form this you have to call add method on $this->formBuilder attribute :
* *
* $this->formBuilder->add("name", "text") * $this->formBuilder->add("name", "text")
* ->add("email", "email", array( * ->add("email", "email", array(
* "attr" => array( * "attr" => array(
* "class" => "field" * "class" => "field"
* ), * ),
* "label" => "email", * "label" => "email",
* "constraints" => array( * "constraints" => array(
* new \Symfony\Component\Validator\Constraints\NotBlank() * new \Symfony\Component\Validator\Constraints\NotBlank()
* ) * )
* ) * )
* ) * )
* ->add('age', 'integer'); * ->add('age', 'integer');
* *
* @return null * @return null
*/ */
abstract protected function buildForm(); abstract protected function buildForm();
/** /**
* @return string the name of you form. This name must be unique * @return string the name of you form. This name must be unique
*/ */
abstract public function getName(); abstract public function getName();
} }