/**
  * PHPDOC
  *
  * @param string $type    PHPDOC
  * @param int    $form_id PHPDOC
  *
  * @return void
  *
  * @since PHPDOC
  */
 private function send_form_action_if_required($type, $form_id)
 {
     require_once NELIOAB_MODELS_DIR . '/goals/actions/form-submission-action.php';
     require_once NELIOAB_MODELS_DIR . '/goals/actions/action.php';
     require_once NELIOAB_UTILS_DIR . '/backend.php';
     $kap = NelioABFormSubmissionAction::type_to_kind_and_plugin($type);
     if (!$kap) {
         return;
     }
     $f = 'nelioab_current_id';
     if (!isset($_POST[$f])) {
         return;
     }
     $f = 'nelioab_current_actual_id';
     if (!isset($_POST[$f])) {
         return;
     }
     $f = 'nelioab_userid';
     if (!isset($_POST[$f]) || strlen(trim($_POST[$f])) === 0) {
         return;
     }
     // Constructing FORM EVENT object:
     $page = $_POST['nelioab_current_id'];
     $actual_page = $_POST['nelioab_current_actual_id'];
     $ev = array('kind' => $kap['kind'], 'form' => $form_id, 'plugin' => $kap['plugin'], 'page' => $page, 'actualPage' => $actual_page, 'user' => $_POST['nelioab_userid']);
     $the_theme = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::THEME_ALT_EXP);
     if ($the_theme) {
         $ev['activeTheme'] = $the_theme->get_id();
     }
     $the_css = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::CSS_ALT_EXP);
     if ($the_css) {
         $ev['activeCSS'] = $the_css->get_id();
     }
     $the_widget = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::WIDGET_ALT_EXP);
     if ($the_widget) {
         $ev['activeWidget'] = '' . $the_widget->get_id();
     }
     $the_menu = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::MENU_ALT_EXP);
     if ($the_menu) {
         $ev['activeMenu'] = $the_menu->get_id();
     }
     // Check if there's one experiment at least with a form submission action,
     // which corresponds to the given form, then send the event. Otherwise,
     // get out!
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     $send_form_event = false;
     $running_exps = NelioABExperimentsManager::get_relevant_running_experiments_from_cache();
     foreach ($running_exps as $exp) {
         /** @var NelioABExperiment $exp */
         foreach ($exp->get_goals() as $goal) {
             /** @var NelioABAltExpGoal $goal */
             foreach ($goal->get_actions() as $action) {
                 /** @var NelioABFormSubmissionAction $action */
                 if ($action->get_type() == $type && $action->get_form_id() == $ev['form']) {
                     // If this action uses the form, then we have to check whether
                     // it accepts submissions from anywhere or only from the tested
                     // page.
                     if ($action->accepts_submissions_from_any_page()) {
                         $send_form_event = true;
                         break;
                     }
                     if ($exp->get_originals_id() == $ev['page']) {
                         $send_form_event = true;
                         break;
                     }
                 }
             }
             if ($send_form_event) {
                 break;
             }
         }
         if ($send_form_event) {
             break;
         }
     }
     if (!$send_form_event) {
         return;
     }
     $url = sprintf(NELIOAB_BACKEND_URL . '/site/%s/form', NelioABAccountSettings::get_site_id());
     $data = NelioABBackend::build_json_object_with_credentials($ev);
     $data['timeout'] = 50;
     for ($attemp = 0; $attemp < 5; ++$attemp) {
         try {
             NelioABBackend::remote_post_raw($url, $data);
             break;
         } catch (Exception $e) {
             // If the form submission event could not be sent, it may be that's
             // because there is no more quota available
             if ($e->getCode() == NelioABErrCodes::NO_MORE_QUOTA) {
                 // If that was the case, simply leave
                 break;
             }
             // If there was another error... we just keep trying (attemp) up to 5
             // times.
         }
     }
 }