/**
  * creates a form object with a free configurable markup
  *
  * @param ContentController $controller  the calling controller instance
  * @param array             $params      optional parameters
  * @param array             $preferences optional preferences
  * @param bool              $barebone    defines if a form should only be instanciated or be used too
  *
  * @return CustomHtmlForm
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 13.01.2015
  */
 public function __construct($controller, $params = null, $preferences = null, $barebone = false)
 {
     $this->extend('onBeforeConstruct', $controller, $params, $preferences, $barebone);
     global $project;
     $this->barebone = $barebone;
     $this->controller = $controller;
     if (is_array($params)) {
         $this->customParameters = $params;
     }
     // Hook for setting preferences via a method call
     $this->preferences();
     if (is_array($preferences)) {
         foreach ($preferences as $title => $setting) {
             if (!empty($title)) {
                 $this->basePreferences[$title] = $setting;
             }
         }
     }
     $name = $this->getSubmitAction();
     if (!$barebone) {
         $this->getFormFields();
     }
     if ($this->securityTokenEnabled) {
         SecurityToken::enable();
     } else {
         SecurityToken::disable();
     }
     parent::__construct($this->getFormController($controller, $preferences), $name, new FieldList(), new FieldList());
     if (!$barebone) {
         $this->getFormFields();
         $this->fillInFieldValues();
     }
     // Hook for setting preferences via a method call; we need to do this
     // a second time so that the standard Silverstripe mechanism can take
     // influence, too (i.e. _config.php files, init methods, etc).
     $this->preferences();
     if (is_array($preferences)) {
         foreach ($preferences as $title => $setting) {
             if (!empty($title)) {
                 $this->basePreferences[$title] = $setting;
             }
         }
     }
     // Counter for the form class, init or increment
     if (!isset(self::$classInstanceCounter[$this->class])) {
         self::$classInstanceCounter[$this->class] = 0;
     }
     if (!$barebone) {
         self::$classInstanceCounter[$this->class]++;
     }
     // new assignment required, because the controller will be overwritten in the form class
     $this->controller = $controller;
     // create group structure
     if (isset($this->formFields)) {
         $this->fieldGroups['formFields'] = $this->getFormFields();
     } else {
         $this->fieldGroups['formFields'] = array();
     }
     $this->name = str_replace('/', '', $this->class . '_' . $name . '_' . self::$classInstanceCounter[$this->class]);
     $this->jsName = $this->name;
     $this->SSformFields = $this->getForm();
     $this->SSformFields['fields']->setForm($this);
     $this->SSformFields['actions']->setForm($this);
     parent::setFields($this->SSformFields['fields']);
     parent::setActions($this->SSformFields['actions']);
     // define form action
     $this->setFormAction($this->buildFormAction());
     $this->setHTMLID($this->getName());
     /*
      * load and init JS validators
      * form integration via FormAttributes()
      */
     if (!$barebone) {
         $javascriptSnippets = $this->getJavascriptValidatorInitialisation();
         if (!$this->getLoadShoppingCartModules()) {
             SilvercartShoppingCart::setLoadShoppingCartModules(false);
         }
         if ($this->getCreateShoppingCartForms() && class_exists('SilvercartShoppingCart')) {
             SilvercartShoppingCart::setCreateShoppingCartForms(false);
         }
         $this->controller->addJavascriptSnippet($javascriptSnippets['javascriptSnippets']);
         $this->controller->addJavascriptOnloadSnippet($javascriptSnippets['javascriptOnloadSnippets']);
         $this->controller->addJavascriptOnloadSnippet($this->getJavascriptFieldInitialisations());
     }
     // Register the default module directory from mysite/_config.php
     self::registerModule($project);
     $this->extend('onAfterConstruct', $controller, $params, $preferences, $barebone);
 }