/** * Formulář pro zadání preprocessingu pomocí interval enumeration * @return Form */ protected function createComponentNewEquidistantIntervalsForm() { $form = new Form(); $form->setTranslator($this->translator); $form->addHidden('column'); $form->addHidden('miner'); $form->addHidden('minLeftMargin'); $form->addHidden('minLeftClosure'); $form->addHidden('maxRightMargin'); $form->addHidden('maxRightClosure'); $form->addText('preprocessingName', 'Preprocessing name:')->setRequired(false)->setAttribute('placeholder', $this->translate('Equidistant intervals'))->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->prepareEquidistantPreprocessingName($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...')->setAttribute('class', 'normalWidth'); $form->addText('attributeName', 'Create attribute with name:')->setAttribute('class', 'normalWidth')->addRule(Form::PATTERN, 'Attribute name can contain only letters, numbers and _ and has start with a letter.', '[a-zA-Z]{1}\\w*')->setRequired('Input attribute name!')->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->addText('equidistantLeftMargin', 'Equidistant intervals from:')->setRequired('You have to input number!')->addRule(Form::FLOAT, 'You have to input number!')->addRule(function (TextInput $textInput) { $values = $textInput->getForm(true)->getValues(true); if ($values['equidistantLeftMargin'] < $values['minLeftMargin'] || $values['equidistantLeftMargin'] >= $values['maxRightMargin']) { return false; } return true; }, 'Start value cannot be out of the data range!'); $form->addText('equidistantRightMargin', 'Equidistant intervals to:')->setRequired('You have to input number!')->addRule(Form::FLOAT, 'You have to input number!')->addRule(function (TextInput $textInput) { $values = $textInput->getForm(true)->getValues(true); if ($values['equidistantRightMargin'] > $values['maxRightMargin'] || $values['equidistantRightMargin'] <= $values['minLeftMargin']) { return false; } return true; }, 'End value cannot be out of the data range!')->addRule(function (TextInput $textInput) { $values = $textInput->getForm(true)->getValues(true); if ($values['equidistantLeftMargin'] >= $values['equidistantRightMargin']) { return false; } return true; }, 'Start value has to be lower than end value!'); $form->addText('equidistantIntervalsCount', 'Intervals count:')->setRequired('You have to input an integer value bigger than 2!')->addRule(Form::INTEGER, 'You have to input an integer value bigger than 2!')->addRule(Form::MIN, 'You have to input an integer value bigger than 2!', 2); $form->addSubmit('submit', 'Save preprocessing & create attribute')->onClick[] = function (SubmitButton $submitButton) { #region vytvoření preprocessingu $values = $submitButton->getForm(true)->getValues(true); //nalezení příslušného formátu $miner = $this->findMinerWithCheckAccess($values['miner']); $this->minersFacade->checkMinerMetasource($miner); $datasourceColumn = $this->findDatasourceColumn($miner->datasource, $values['column']); $format = $datasourceColumn->format; //vytvoření preprocessingu $preprocessing = new Preprocessing(); $preprocessing->name = $values['preprocessingName'] != '' ? $values['preprocessingName'] : $this->prepareEquidistantPreprocessingName($format, $values); $preprocessing->format = $format; $preprocessing->user = $this->getCurrentUser(); $this->preprocessingsFacade->savePreprocessing($preprocessing); //vytvoření jednotlivých intervalů v podobě samostatných binů $intervalsCount = $values['equidistantIntervalsCount']; $step = $values['equidistantRightMargin'] - $values['equidistantLeftMargin']; $step = $step / $intervalsCount; if (round($step, 3) > 0) { $step = round($step, 3); } $firstStart = $values['equidistantLeftMargin']; $endMax = $values['equidistantRightMargin']; $remainingIntervals = $intervalsCount; $lastEnd = $values['equidistantLeftMargin']; while ($remainingIntervals > 0 && $lastEnd < $endMax) { $interval = new Interval(); $interval->leftMargin = $lastEnd; if ($lastEnd == $firstStart) { $interval->leftClosure = $values['minLeftClosure']; } else { $interval->leftClosure = Interval::CLOSURE_CLOSED; } $lastEnd = min($endMax, $lastEnd + $step); if ($remainingIntervals == 1) { //dogenerování intervalu až do konce $lastEnd = $endMax; } $interval->rightMargin = $lastEnd; if ($lastEnd == $endMax) { $interval->rightClosure = $values['maxRightClosure']; } else { $interval->rightClosure = Interval::CLOSURE_OPEN; } $valuesBin = new ValuesBin(); $valuesBin->format = $format; $valuesBin->name = $interval->__toString(); $this->metaAttributesFacade->saveValuesBin($valuesBin); $this->metaAttributesFacade->saveInterval($interval); $valuesBin->addToIntervals($interval); $this->metaAttributesFacade->saveValuesBin($valuesBin); $preprocessing->addToValuesBins($valuesBin); $remainingIntervals--; } $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; }