protected function do_build()
 {
     $title = __('Edit Experiment', 'nelioab');
     // Check settings
     require_once NELIOAB_ADMIN_DIR . '/error-controller.php';
     $error = NelioABErrorController::build_error_page_on_invalid_settings();
     if ($error) {
         return;
     }
     // We recover the experiment (if any)
     // ----------------------------------------------
     global $nelioab_admin_controller;
     $experiment = NULL;
     $other_names = array();
     if (!empty($nelioab_admin_controller->data)) {
         $experiment = $nelioab_admin_controller->data;
     } else {
         $experiment = new NelioABMenuAlternativeExperiment(-time());
         $experiment->clear();
     }
     // ...and we also recover other experiment names (if any)
     if (isset($_POST['other_names'])) {
         $other_names = json_decode(urldecode($_POST['other_names']));
     } else {
         foreach (NelioABExperimentsManager::get_experiments() as $aux) {
             if ($aux->get_id() != $experiment->get_id()) {
                 array_push($other_names, $aux->get_name());
             }
         }
     }
     // If everything is OK, we keep going!
     // ---------------------------------------------------
     // Creating the view
     $view = $this->create_view();
     // Experiment information
     $view->set_basic_info($experiment->get_id(), $experiment->get_name(), $experiment->get_description(), $experiment->get_finalization_mode(), $experiment->get_finalization_value());
     // Experiment alternatives
     $ori = $experiment->get_original();
     $view->set_original($ori->get_id(), $ori->get_value());
     $view->set_alternatives($experiment->get_json4js_alternatives());
     // Goals
     $goals = $experiment->get_goals();
     foreach ($goals as $goal) {
         $view->add_goal($goal->json4js());
     }
     if (count($goals) == 0) {
         $new_goal = new NelioABAltExpGoal($experiment);
         $new_goal->set_name(__('Default', 'nelioab'));
         $view->add_goal($new_goal->json4js());
     }
     return $view;
 }
 public static function load($id)
 {
     $json_data = NelioABBackend::remote_get(NELIOAB_BACKEND_URL . '/exp/global/' . $id);
     $json_data = json_decode($json_data['body']);
     $exp = new NelioABMenuAlternativeExperiment($json_data->key->id);
     $exp->set_type_using_text($json_data->kind);
     $exp->set_name($json_data->name);
     if (isset($json_data->description)) {
         $exp->set_description($json_data->description);
     }
     $exp->set_status($json_data->status);
     $exp->set_finalization_mode($json_data->finalizationMode);
     if (isset($json_data->finalizationModeValue)) {
         $exp->set_finalization_value($json_data->finalizationModeValue);
     }
     if (isset($json_data->start)) {
         $exp->set_start_date($json_data->start);
     }
     if (isset($json_data->finalization)) {
         $exp->set_end_date($json_data->finalization);
     }
     if (isset($json_data->goals)) {
         NelioABExperiment::load_goals_from_json($exp, $json_data->goals);
     }
     $alternatives = array();
     if (isset($json_data->alternatives)) {
         foreach ($json_data->alternatives as $json_alt) {
             $alt = new NelioABAlternative($json_alt->key->id);
             $alt->set_name($json_alt->name);
             if (isset($json_alt->value)) {
                 $alt->set_value($json_alt->value);
             } else {
                 $alt->set_value('');
             }
             array_push($alternatives, $alt);
         }
     }
     $exp->set_appspot_alternatives($alternatives);
     return $exp;
 }
 /**
  * Loads the experiment from AppEngine.
  *
  * @param int $id   the ID of the experiment we want to retrieve.
  * @param int $type the type of the experiment we want to retrieve.
  *
  * @return NelioABExperiment the experiment whose ID is `$id` and whose type is `$type`.
  *                           Note that the result will be an instance of
  *                           a concrete subclass of `NelioABExperiment`.
  *
  * @throws Exception `EXPERIMENT_ID_NOT_FOUND`
  *                   This exception is thrown if the experiment was not
  *                   found in AppEngine.
  *
  * @see NelioABExperiment::load
  *
  * @since 3.4.0
  */
 private static function load_experiment_by_id($id, $type)
 {
     require_once NELIOAB_MODELS_DIR . '/goals/goals-manager.php';
     /** @var NelioABExperiment $exp */
     switch ($type) {
         case NelioABExperiment::POST_ALT_EXP:
         case NelioABExperiment::PAGE_ALT_EXP:
         case NelioABExperiment::PAGE_OR_POST_ALT_EXP:
         case NelioABExperiment::CPT_ALT_EXP:
             $exp = NelioABPostAlternativeExperiment::load($id);
             break;
         case NelioABExperiment::HEADLINE_ALT_EXP:
             $exp = NelioABHeadlineAlternativeExperiment::load($id);
             break;
         case NelioABExperiment::WC_PRODUCT_SUMMARY_ALT_EXP:
             $exp = NelioABProductSummaryAlternativeExperiment::load($id);
             break;
         case NelioABExperiment::THEME_ALT_EXP:
             $exp = NelioABThemeAlternativeExperiment::load($id);
             break;
         case NelioABExperiment::CSS_ALT_EXP:
             $exp = NelioABCssAlternativeExperiment::load($id);
             break;
         case NelioABExperiment::WIDGET_ALT_EXP:
             $exp = NelioABWidgetAlternativeExperiment::load($id);
             break;
         case NelioABExperiment::MENU_ALT_EXP:
             $exp = NelioABMenuAlternativeExperiment::load($id);
             break;
         case NelioABExperiment::HEATMAP_EXP:
             $exp = NelioABHeatmapExperiment::load($id);
             break;
         default:
             $err = NelioABErrCodes::EXPERIMENT_ID_NOT_FOUND;
             throw new Exception(NelioABErrCodes::to_string($err), $err);
     }
     $exp->mark_as_fully_loaded();
     return $exp;
 }