/** * @return mixed an HTTP response, or */ public function configureAction() { if (null !== ($response = $this->checkAuth(AdminResources::MODULE, 'OpenSearchServerSearch', AccessManager::UPDATE))) { return $response; } // Initialize the potential error message, and the potential exception $error_msg = $ex = null; // Create the Form from the request $configurationForm = new ConfigurationForm($this->getRequest()); try { // Check the form against constraints violations $form = $this->validateForm($configurationForm, "POST"); // Get the form field values $data = $form->getData(); foreach ($data as $name => $value) { if (is_array($value)) { $value = implode(';', $value); } OpenSearchServerConfigQuery::set($name, $value); } //get handle to work with the API $oss_api = OpenSearchServerSearchHelper::getHandler(); //check if index exists, if not, creates it $index = OpensearchserverConfigQuery::read('index_name'); $indexCreated = false; $request = new \OpenSearchServer\Index\Exists(); $request->index($index); $response = $oss_api->submit($request); //index doesn't exist, create it if (!$response->isSuccess()) { $this->createIndex($index); $indexCreated = true; } //check if Analyzer "PriceAnalyzer" exists, create it if it doesn't $analyzerName = 'PriceAnalyzer'; $request = new \OpenSearchServer\Analyzer\Get(); $request->index($index)->name($analyzerName); $response = $oss_api->submit($request); if (!$response->isSuccess()) { $this->createAnalyzer($index, $analyzerName); } //if index has just been created, create its schema if ($indexCreated) { $this->createSchema($index); } //check if query template exists, if not, creates it $queryTemplate = OpensearchserverConfigQuery::read('query_template'); $request = new \OpenSearchServer\SearchTemplate\Get(); $request->index($index)->name($queryTemplate); $response = $oss_api->submit($request); if (!$response->isSuccess()) { $this->createQueryTemplate($index, $queryTemplate); } // Log configuration modification $this->adminLogAppend("opensearchserversearch.configuration.message", AccessManager::UPDATE, sprintf("OpenSearchServer configuration updated")); // Redirect to the success URL, if ($this->getRequest()->get('save_mode') == 'stay') { // If we have to stay on the same page, redisplay the configuration page/ $route = '/admin/module/OpenSearchServerSearch'; } else { // If we have to close the page, go back to the module back-office page. $route = '/admin/modules'; } //$this->getRequest()->getSession()->getFlashBag()->add('notice', $this->getTranslator()->trans('Settings have been saved.')); $this->getRequest()->getSession()->getFlashBag()->add('oss', $this->getTranslator()->trans('Settings have been saved.', [], OpenSearchServerSearch::MODULE_DOMAIN)); $this->redirect(URL::getInstance()->absoluteUrl($route)); exit; } catch (FormValidationException $ex) { // Form cannot be validated. Create the error message using // the BaseAdminController helper method. $error_msg = $this->createStandardFormValidationErrorMessage($ex); } catch (\Exception $ex) { // Any other error $error_msg = $ex->getMessage(); } // At this point, the form has errors, and should be redisplayed. We don not redirect, // just redisplay the same template. // Setup the Form error context, to make error information available in the template. $this->setupFormErrorContext($this->getTranslator()->trans("OpenSearchServer configuration", [], OpenSearchServerSearch::MODULE_DOMAIN), $error_msg, $configurationForm, $ex); // Do not redirect at this point, or the error context will be lost. // Just redisplay the current template. return $this->renderTemplate(); }
protected function buildForm() { $this->formBuilder->add('hostname', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('OpenSearchServer instance URL'), 'data' => OpenSearchServerConfigQuery::read('hostname', 'http://localhost:9090'), 'label_attr' => array('for' => 'hostname', 'help' => $this->trans('URL to access your OpenSearchServer instance. Please include port information.'))))->add('login', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('Login'), 'data' => OpenSearchServerConfigQuery::read('login', ''), 'label_attr' => array('for' => 'login')))->add('apikey', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('API Key'), 'data' => OpenSearchServerConfigQuery::read('apikey', ''), 'label_attr' => array('for' => 'apikey')))->add('index_name', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('Index to use'), 'data' => OpenSearchServerConfigQuery::read('index_name', ''), 'label_attr' => array('for' => 'index_name', 'help' => $this->trans('Index will be automatically created if it does not already exist.'))))->add('query_template', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('Query template'), 'data' => OpenSearchServerConfigQuery::read('query_template', 'search'), 'label_attr' => array('for' => 'query_template', 'help' => $this->trans('Query template to use for searching products. If it does not already exists in index it will be automatically created from a basic template.'))))->add('enable_search', 'checkbox', array('required' => false, 'label' => $this->trans('Enable search with OpenSearchServer'), 'data' => (bool) OpenSearchServerConfigQuery::read('enable_search', ''), 'label_attr' => array('for' => 'enable_search', 'help' => $this->trans('Enable this option when configuration is completed.')))); }