/**
  * Formulář pro vytvoření nového formátu
  * @return Form
  */
 protected function createComponentNewFormatForm()
 {
     $form = new Form();
     $form->addHidden('datasource');
     $form->addHidden('column');
     $metaAttribute = $form->addHidden('metaAttribute');
     $form->addText('metaAttributeName', 'Meta-attribute:')->setAttribute('readonly', 'readonly')->setAttribute('class', 'normalWidth');
     $formatName = $form->addText('formatName', 'Format name:')->setRequired()->setAttribute('class', 'normalWidth');
     $formatName->addRule(Form::MIN_LENGTH, 'Min length of format name is %s characters!', 3);
     $formatName->addRule(function (TextInput $control) use($metaAttribute) {
         try {
             $format = $this->metaAttributesFacade->findFormatByName($metaAttribute->value, $control->value);
             ///XXX
             if ($format instanceof Format) {
                 return false;
             }
         } catch (\Exception $e) {
             /*chybu ignorujeme (nenalezený metaatribut je OK)*/
         }
         return true;
     }, 'Format with this name already exists!');
     $form->addCheckbox('formatShared', 'Create shared (standard) format');
     $form->addSelect('formatType', 'Values range:', array('interval' => 'Continuous values (interval)', 'values' => 'Distinct values (enumeration)'))->setAttribute('class', 'normalWidth')->setDefaultValue('values');
     $submit = $form->addSubmit('create', 'Create format');
     $submit->setValidationScope(array($formatName));
     $submit->onClick[] = function (SubmitButton $button) {
         $values = $button->form->values;
         try {
             $datasourceColumn = $this->datasourcesFacade->findDatasourceColumn($values->datasource, $values->column);
             $metaAttribute = $this->metaAttributesFacade->findMetaAttribute($values->metaAttribute);
             $datasource = $this->datasourcesFacade->findDatasource($values->datasource);
             $this->databasesFacade->openDatabase($datasource->getDbConnection());
             $datasourceColumnValuesStatistic = $this->databasesFacade->getColumnValuesStatistic($datasource->dbTable, $datasourceColumn->name);
             $format = $this->metaAttributesFacade->createFormatFromDatasourceColumn($metaAttribute, $values->formatName, $datasourceColumn, $datasourceColumnValuesStatistic, @$values->formatType, @$values->formatShared);
             $datasourceColumn->format = $format;
             $this->datasourcesFacade->saveDatasourceColumn($datasourceColumn);
         } catch (\Exception $e) {
             $this->flashMessage($this->translator->translate('Format creation failed.'));
         }
         $this->redirect('Close');
     };
     $storno = $form->addSubmit('storno', 'Storno');
     $storno->setValidationScope(array());
     $storno->onClick[] = function (SubmitButton $button) {
         $values = $button->form->values;
         $this->redirect('SelectFormat', array('datasource' => $values->datasource, 'column' => $values->column, 'metaAttribute' => $values->metaAttribute));
     };
     $form->onError[] = function (Form $form) {
         $values = $form->values;
         $this->handleNewFormat($values->datasource, $values->column, $values->metaAttribute);
     };
     return $form;
 }
 /**
  * Funkce pro nalezení datového zdroje s kontrolou oprávnění přístupu
  * @param int $datasourceId
  * @throws BadRequestException
  * @return Datasource
  */
 private function findDatasourceWithCheckAccess($datasourceId)
 {
     try {
         $datasource = $this->datasourcesFacade->findDatasource($datasourceId);
         if (!$this->datasourcesFacade->checkDatasourceAccess($datasource, $this->getCurrentUser())) {
             throw new BadRequestException("You are not authorized to use the selected datasource!");
         }
     } catch (\Exception $e) {
         throw new BadRequestException("Requested datasource was not found or is not accessible!");
     }
     return $datasource;
 }
 /**
  * @return Form
  * @throws \Exception
  */
 public function createComponentNewMinerForm()
 {
     $form = new Form();
     $form->setTranslator($this->translator);
     $form->addHidden('datasource');
     $minersFacade = $this->minersFacade;
     $currentUserId = $this->user->id;
     $form->addText('datasourceName', 'Datasource:')->setAttribute('class', 'noBorder normalWidth')->setAttribute('readonly');
     $form->addText('name', 'Miner name:')->setRequired('Input the miner name!')->setAttribute('autofocus')->setAttribute('class', 'normalWidth')->addRule(Form::MAX_LENGTH, 'Max length of miner name is %s characters!', 100)->addRule(Form::MIN_LENGTH, 'Min length of miner name is %s characters!', 3)->addRule(function (TextInput $control) use($currentUserId, $minersFacade) {
         try {
             $miner = $minersFacade->findMinerByName($currentUserId, $control->value);
             if ($miner instanceof Miner) {
                 return false;
             }
         } catch (\Exception $e) {
             /*chybu ignorujeme (nenalezený miner je OK)*/
         }
         return true;
     }, 'Miner with this name already exists!');
     $availableMinerTypes = $this->minersFacade->getAvailableMinerTypes();
     if (empty($availableMinerTypes)) {
         throw new \Exception('No miner type found! Please check the configuration...');
     }
     $form->addSelect('type', 'Miner type:', $availableMinerTypes)->setAttribute('class', 'normalWidth')->setDefaultValue(Miner::DEFAULT_TYPE);
     $form->addSubmit('submit', 'Create miner...')->onClick[] = function (SubmitButton $submitButton) {
         /** @var Form $form */
         $form = $submitButton->form;
         $values = $form->getValues();
         $miner = new Miner();
         $miner->user = $this->usersFacade->findUser($this->user->id);
         $miner->name = $values['name'];
         $miner->datasource = $this->datasourcesFacade->findDatasource($values['datasource']);
         $miner->created = new DateTime();
         $miner->type = $values['type'];
         $this->minersFacade->saveMiner($miner);
         $this->redirect('Data:openMiner', array('id' => $miner->minerId));
     };
     $form->addSubmit('storno', 'storno')->setValidationScope(array())->onClick[] = function (SubmitButton $button) {
         /** @var Presenter $presenter */
         $presenter = $button->form->getParent();
         $presenter->redirect('Data:newMiner');
     };
     return $form;
 }
 /**
  * Akce pro vyhodnocení klasifikace
  * @SWG\Get(
  *   tags={"Evaluation"},
  *   path="/evaluation/classification",
  *   summary="Evaluate classification model",
  *   produces={"application/json","application/xml"},
  *   security={{"apiKey":{}},{"apiKeyHeader":{}}},
  *   @SWG\Parameter(
  *     name="scorer",
  *     description="Scorer type",
  *     required=true,
  *     type="string",
  *     in="query",
  *     enum={"easyMinerScorer","modelTester"}
  *   ),
  *   @SWG\Parameter(
  *     name="task",
  *     description="Task ID",
  *     required=false,
  *     type="integer",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="ruleSet",
  *     description="Rule set ID",
  *     required=false,
  *     type="integer",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="datasource",
  *     description="Datasource ID (if not specified, task datasource will be used)",
  *     required=false,
  *     type="integer",
  *     in="query"
  *   ),
  *   @SWG\Response(
  *     response=200,
  *     description="Evaluation result",
  *     @SWG\Schema(
  *       ref="#/definitions/ScoringResultResponse"
  *     )
  *   ),
  *   @SWG\Response(
  *     response=400,
  *     description="Invalid API key supplied",
  *     @SWG\Schema(ref="#/definitions/StatusResponse")
  *   )
  * )
  *
  * @throws BadRequestException
  * @throws NotImplementedException
  */
 public function actionClassification()
 {
     $this->setXmlMapperElements('classification');
     $inputData = $this->getInput()->getData();
     /** @var IScorerDriver $scorerDriver */
     if (empty($inputData['scorer'])) {
         $scorerDriver = $this->scorerDriverFactory->getDefaultScorerInstance();
     } else {
         $scorerDriver = $this->scorerDriverFactory->getScorerInstance($inputData['scorer']);
     }
     if (!empty($inputData['datasource'])) {
         try {
             $datasource = $this->datasourcesFacade->findDatasource(@$inputData['datasource']);
             if (!$this->datasourcesFacade->checkDatasourceAccess($datasource, $this->getCurrentUser())) {
                 throw new \Exception();
             }
         } catch (\Exception $e) {
             throw new BadRequestException("Requested data source was not found!");
         }
     } elseif (!empty($inputData['task'])) {
         $task = $this->findTaskWithCheckAccess($inputData['task']);
         $datasource = $task->miner->datasource;
     } else {
         throw new BadRequestException("Data source was not specified!");
     }
     if (!empty($inputData['task'])) {
         if (empty($task)) {
             $task = $this->findTaskWithCheckAccess($inputData['task']);
         }
         $result = $scorerDriver->evaluateTask($task, $datasource)->getCorrectIncorrectDataArr();
         $result['task'] = $task->getDataArr(false);
     } elseif (!empty($inputData['ruleSet'])) {
         $ruleSet = $this->ruleSetsFacade->findRuleSet($inputData['ruleSet']);
         //TODO kontrola oprávnění k rule setu
         $result = $scorerDriver->evaluateRuleSet($ruleSet, $datasource)->getDataArr();
         $result['ruleSet'] = $ruleSet->getDataArr();
     } else {
         throw new BadRequestException("No task or rule set found!");
     }
     $this->resource = $result;
     $this->sendResource();
 }
 /**
  * Funkce pro kontrolu vstupů pro vytvoření nového mineru
  */
 public function validateCreate()
 {
     $currentUser = $this->getCurrentUser();
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('name')->addRule(IValidator::REQUIRED, 'Name is required!');
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('type')->addRule(IValidator::REQUIRED, 'Miner type is required!');
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('datasourceId')->addRule(IValidator::REQUIRED, 'Datasource ID is required!')->addRule(IValidator::CALLBACK, 'Requested datasource was not found, or is not accessible!', function ($value) use($currentUser) {
         try {
             $this->datasource = $this->datasourcesFacade->findDatasource($value);
             if ($this->datasourcesFacade->checkDatasourceAccess($this->datasource, $currentUser)) {
                 //kontrola dostupnosti kompatibilního mineru s vybraným datasource
                 $availableMinerTypes = $this->minersFacade->getAvailableMinerTypes($this->datasource->type);
                 return !empty($availableMinerTypes[$this->getInput()->getData()['type']]);
             } else {
                 return false;
             }
         } catch (\Exception $e) {
             return false;
         }
     });
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('ruleSetId')->addRule(IValidator::CALLBACK, 'Requested rule set was not found, or is not accessible!', function ($value) use($currentUser) {
         if (empty($value)) {
             return true;
         }
         try {
             $this->ruleSet = $this->ruleSetsFacade->findRuleSet($value);
             $this->ruleSetsFacade->checkRuleSetAccess($this->ruleSet, $currentUser);
             return true;
         } catch (\Exception $e) {
             return false;
         }
     });
 }