/**
  * Signál pro propojení DatasourceColumn s Formátem
  * @param int $datasource
  * @param int $column
  * @param string $format
  */
 public function handleSetDatasourceColumnFormat($datasource, $column, $format)
 {
     //TODO kontrola oprávnění!!!
     $datasource = $this->datasourcesFacade->findDatasource($datasource);
     $this->databasesFacade->openDatabase($datasource->getDbConnection());
     $datasourceColumn = $this->datasourcesFacade->findDatasourceColumn($datasource, $column);
     $datasourceColumnValuesStatistic = $this->databasesFacade->getColumnValuesStatistic($datasource->dbTable, $datasourceColumn->name);
     $format = $this->metaAttributesFacade->findFormat($format);
     $this->metaAttributesFacade->updateFormatFromDatasourceColumn($format, $datasourceColumn, $datasourceColumnValuesStatistic);
     $datasourceColumn->format = $format;
     $this->datasourcesFacade->saveDatasourceColumn($datasourceColumn);
 }
 /**
  * Formulář pro zadání preprocessingu pomocí nominal enumeration
  * @return Form
  */
 protected function createComponentNewNominalEnumerationForm()
 {
     $form = new Form();
     $form->setTranslator($this->translator);
     $form->addText('preprocessingName', 'Preprocessing name:')->setRequired(false)->setAttribute('placeholder', $this->translate('Nominal bins'))->setAttribute('title', $this->translate('You can left this field blank, it will be filled in automatically.'))->addRule(function (TextInput $textInput) {
         $form = $textInput->getForm(true);
         $formValues = $form->getValues(true);
         $textInputValue = $textInput->value;
         //nalezení aktuálního formátu
         $miner = $this->findMinerWithCheckAccess($formValues['miner']);
         $datasourceColumn = $this->findDatasourceColumn($miner->datasource, $formValues['column']);
         $format = $datasourceColumn->format;
         $textInput->setAttribute('placeholder', $this->prepareNominalEnumerationPreprocessingName($format, $formValues));
         if ($textInputValue != '') {
             //check preprocessings
             $existingPreprocessings = $format->preprocessings;
             if (!empty($existingPreprocessings)) {
                 foreach ($existingPreprocessings as $existingPreprocessing) {
                     if ($existingPreprocessing->name == $textInput) {
                         return false;
                     }
                 }
             }
         }
         return true;
     }, 'This preprocessing name already exists. Please select a new one...')->addRule(function (TextInput $input) {
         $values = $input->getForm(true)->getValues(true);
         return count($values['valuesBins']) > 0;
     }, 'You have to input at least one bin!');
     $form->addText('attributeName', 'Create attribute with name:')->setRequired('Input attribute name!')->addRule(Form::PATTERN, 'Attribute name can contain only letters, numbers and _ and has start with a letter.', '[a-zA-Z]{1}\\w*')->addRule(function (TextInput $input) {
         //kontrola, jestli již existuje atribtu se zadaným názvem
         $values = $input->getForm(true)->getValues();
         $miner = $this->findMinerWithCheckAccess($values->miner);
         $attributes = $miner->metasource->attributes;
         if (!empty($attributes)) {
             foreach ($attributes as $attribute) {
                 if ($attribute->name == $input->value) {
                     return false;
                 }
             }
         }
         return true;
     }, 'Attribute with this name already exists!');
     $form->addHidden('column');
     $form->addHidden('miner');
     $form->addHidden('formatType');
     $form->addHidden('formatId');
     /** @var Container $valuesBins */
     $valuesBins = $form->addDynamic('valuesBins', function (Container $valuesBin) {
         $valuesBin->addText('name', 'Bin name:')->setRequired(true)->setRequired('Input bin name!')->addRule(function (TextInput $input) {
             $values = $input->parent->getValues(true);
             return count($values['values']) > 0;
         }, 'Add at least one value!')->addRule(function (TextInput $input) {
             //kontrola, jestli má každý BIN jiný název
             $values = $input->getParent()->getParent()->getValues(true);
             $inputValue = $input->getValue();
             $usesCount = 0;
             if (!empty($values)) {
                 foreach ($values as $value) {
                     if ($value['name'] == $inputValue) {
                         $usesCount++;
                     }
                 }
             }
             return $usesCount <= 1;
         }, 'This name is used for other bin!');
         /** @var Container $intervals */
         $intervals = $valuesBin->addDynamic('values', function (Container $interval) {
             $interval->addText('value')->setAttribute('readonly');
             $interval->addSubmit('remove', 'x')->setAttribute('class', 'removeValue')->setValidationScope([])->onClick[] = function (SubmitButton $submitButton) {
                 $intervals = $submitButton->parent->parent;
                 $intervals->remove($submitButton->parent, TRUE);
             };
         });
         $addValueSubmit = $valuesBin->addSubmit('addValue', 'Add value');
         $value = $valuesBin->addText('value', null, '');
         //TODO dodělat select...
         $value->addConditionOn($addValueSubmit, Form::SUBMITTED)->setRequired('Input value!')->addConditionOn($valuesBin->getForm(true)->getComponent('formatType'), Form::EQUAL, Format::DATATYPE_VALUES)->addRule(function (TextInput $input) {
             $inputValue = $input->getValue();
             $values = $input->getForm(true)->getValues(true);
             $format = $this->metaAttributesFacade->findFormat($values['formatId']);
             $values = $format->values;
             if (!empty($values)) {
                 foreach ($values as $value) {
                     if ($value->value == $inputValue) {
                         return true;
                     }
                 }
             }
             return false;
         }, 'You have to input existing value!')->elseCondition()->addRule(Form::FLOAT, 'You have to input number!')->endCondition();
         $value->addRule(function (TextInput $input) {
             $values = $input->getForm(true)->getValues(true);
             $usedValuesArr = [];
             if (!empty($values['valuesBins'])) {
                 foreach ($values['valuesBins'] as $valuesBin) {
                     if (!empty($valuesBin['values'])) {
                         foreach ($valuesBin['values'] as $value) {
                             $usedValuesArr[] = $value['value'];
                         }
                     }
                 }
             }
             return !in_array($input->value, $usedValuesArr);
         }, 'This value is already used!');
         $addValueSubmit->setValidationScope([$value])->onClick[] = function (SubmitButton $submitButton) use($intervals) {
             $values = $submitButton->getParent()->getValues(true);
             $valueItem = $submitButton->getParent()['values']->createOne();
             $valueItem->setValues(['value' => $values['value']]);
             $submitButton->getParent()->setValues(['value' => '']);
         };
         $valuesBin->addSubmit('remove', 'Remove bin')->setAttribute('class', 'removeBin')->setValidationScope([])->onClick[] = function (SubmitButton $submitButton) {
             $submitButton->getParent()->getParent()->remove($submitButton->getParent(), true);
         };
     }, 0);
     $valuesBins->addSubmit('addBin', 'Add bin')->setValidationScope([])->onClick[] = function (SubmitButton $submitButton) {
         $submitButton->getParent()->createOne();
     };
     $form->addSubmit('submitAll', 'Save preprocessing & create attribute')->onClick[] = function (SubmitButton $submitButton) {
         #region vytvoření preprocessingu
         $values = $submitButton->getForm(true)->getValues(true);
         $miner = $this->findMinerWithCheckAccess($values['miner']);
         $this->minersFacade->checkMinerMetasource($miner);
         //vytvoření preprocessingu
         $datasourceColumn = $this->findDatasourceColumn($miner->datasource, $values['column']);
         $format = $datasourceColumn->format;
         $preprocessing = new Preprocessing();
         $preprocessing->name = $values['preprocessingName'] != '' ? $values['preprocessingName'] : $this->prepareNominalEnumerationPreprocessingName($format, $values);
         $preprocessing->format = $format;
         $preprocessing->user = $this->getCurrentUser();
         $this->preprocessingsFacade->savePreprocessing($preprocessing);
         foreach ($values['valuesBins'] as $valuesBinValues) {
             $valuesBin = new ValuesBin();
             $valuesBin->format = $format;
             $valuesBin->name = $valuesBinValues['name'];
             $this->metaAttributesFacade->saveValuesBin($valuesBin);
             foreach ($valuesBinValues['values'] as $valuesValues) {
                 try {
                     $value = $this->metaAttributesFacade->findValue($format, $valuesValues['value']);
                 } catch (\Exception $e) {
                     $value = new Value();
                     $value->value = $valuesValues['value'];
                     $this->metaAttributesFacade->saveValue($value);
                 }
                 $valuesBin->addToValues($value);
             }
             $this->metaAttributesFacade->saveValuesBin($valuesBin);
             $preprocessing->addToValuesBins($valuesBin);
         }
         $this->preprocessingsFacade->savePreprocessing($preprocessing);
         //vytvoření atributu
         $attribute = new Attribute();
         $attribute->metasource = $miner->metasource;
         $attribute->datasourceColumn = $datasourceColumn;
         $attribute->name = $values['attributeName'];
         $attribute->type = $attribute->datasourceColumn->type;
         $attribute->preprocessing = $preprocessing;
         $this->minersFacade->prepareAttribute($miner, $attribute);
         $this->metasourcesFacade->saveAttribute($attribute);
         $this->minersFacade->checkMinerState($miner, $this->getCurrentUser());
         $this->redirect('reloadUI');
         #endregion vytvoření preprocessingu
     };
     $presenter = $this;
     $form->addSubmit('storno', 'storno')->setValidationScope([])->onClick[] = function (SubmitButton $submitButton) use($presenter) {
         $values = $submitButton->getForm()->getValues();
         $presenter->redirect('addAttribute', array('column' => $values->column, 'miner' => $values->miner));
     };
     return $form;
 }