public function remove()
 {
     // 1. We remove the experiment itself
     parent::remove();
     // 2. And we now remove all the alternative widgets
     require_once NELIOAB_EXP_CONTROLLERS_DIR . '/widget-experiment-controller.php';
     NelioABWidgetExpAdminController::clean_widgets_in_experiment($this->get_id());
 }
 public function save()
 {
     $exp_id = parent::save();
     // 2. UPDATE THE ALTERNATIVES
     // -------------------------------------------------------------------------
     // 2.1 REUSE ALL APPSPOT ALTERNATIVES
     $i = 0;
     while ($i < count($this->appspot_ids) && $i < count($this->selected_themes)) {
         $theme = $this->selected_themes[$i];
         $body = array('name' => $theme->name, 'value' => $theme->value);
         $url = sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/update', $this->appspot_ids[$i]);
         NelioABBackend::remote_post($url, $body);
         $i++;
     }
     // 2.2 CREATE NEW APPSPOT ALTERNATIVES (IF REQUIRED)
     while ($i < count($this->selected_themes)) {
         $theme = $this->selected_themes[$i];
         $body = array('name' => $theme->name, 'value' => $theme->value, 'kind' => NelioABExperiment::THEME_ALT_EXP_STR);
         try {
             $result = NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/exp/global/%s/alternative', $exp_id), $body);
             array_push($this->appspot_ids, $result);
         } catch (Exception $e) {
         }
         $i++;
     }
     // 2.3 REMOVE UNUSED APPSPOT ALTERNATIVES (IF REQUIRED)
     $last_valid = $i;
     while ($i < count($this->appspot_ids)) {
         $id = $this->appspot_ids[$i];
         $url = sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/delete', $id);
         NelioABBackend::remote_post($url);
         $i++;
     }
     $aux = $this->appspot_ids;
     $this->appspot_ids = array();
     for ($i = 0; $i < $last_valid; ++$i) {
         array_push($this->appspot_ids, $aux[$i]);
     }
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     NelioABExperimentsManager::update_experiment($this);
 }
 public function save()
 {
     // 0. WE CHECK WHETHER THE EXPERIMENT IS COMPLETELY NEW OR NOT
     // -------------------------------------------------------------------------
     $is_new = $this->get_id() < 0;
     // 1. SAVE THE EXPERIMENT AND ITS GOALS
     // -------------------------------------------------------------------------
     $exp_id = parent::save();
     // 2. UPDATE THE ALTERNATIVES
     // -------------------------------------------------------------------------
     // 2.0. FIRST OF ALL, WE CREATE A FAKE ORIGINAL FOR NEW EXPERIMENTS
     /** @var NelioABAlternative $original */
     $original = $this->get_original();
     if ($is_new && $original->get_id() < 0) {
         $body = array('name' => $original->get_name(), 'content' => '', 'kind' => $this->get_textual_type());
         try {
             /** @var int $result */
             $result = NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/exp/global/%s/alternative', $exp_id), $body);
             $original->set_id($result);
         } catch (Exception $e) {
         }
     }
     // 2.1. UPDATE CHANGES ON ALREADY EXISTING APPSPOT ALTERNATIVES
     foreach ($this->get_appspot_alternatives() as $alt) {
         /** @var NelioABAlternative $alt */
         if ($alt->was_removed() || !$alt->is_dirty()) {
             continue;
         }
         $body = array('name' => $alt->get_name());
         NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/update', $alt->get_id()), $body);
     }
     // 2.2. REMOVE FROM APPSPOT THE REMOVED ALTERNATIVES
     foreach ($this->get_appspot_alternatives() as $alt) {
         /** @var NelioABAlternative $alt */
         if (!$alt->was_removed()) {
             continue;
         }
         $url = sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/delete', $alt->get_id());
         NelioABBackend::remote_post($url);
     }
     // 2.3. CREATE LOCAL ALTERNATIVES IN APPSPOT
     $this->new_ids = array();
     foreach ($this->get_local_alternatives() as $alt) {
         /** @var NelioABAlternative $alt */
         if ($alt->was_removed()) {
             continue;
         }
         $body = array('name' => $alt->get_name(), 'content' => '', 'kind' => $this->get_textual_type());
         try {
             /** @var array $result */
             $result = NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/exp/global/%s/alternative', $exp_id), $body);
             /** @var object $result */
             $result = json_decode($result['body']);
             $this->new_ids[$alt->get_id()] = $result->key->id;
         } catch (Exception $e) {
         }
     }
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     NelioABExperimentsManager::update_experiment($this);
 }
 public function duplicate($new_name)
 {
     $id = parent::duplicate($new_name);
     if (-1 == $id) {
         return $id;
     }
     require_once NELIOAB_EXP_CONTROLLERS_DIR . '/menu-experiment-controller.php';
     $controller = NelioABMenuExpAdminController::get_instance();
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     /** @var NelioABMenuAlternativeExperiment $exp */
     $exp = NelioABExperimentsManager::get_experiment_by_id($id, $this->get_type());
     $alts = 0;
     $controller->begin();
     foreach ($exp->get_alternatives() as $alt) {
         /** @var NelioABAlternative $alt */
         $menu_id = $controller->duplicate_menu_and_create_alternative($alt->get_value(), $exp->get_id());
         $body = array('value' => $menu_id);
         try {
             NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/update', $alt->get_id()), $body);
             $alts++;
         } catch (Exception $e) {
         }
     }
     $controller->commit();
     if (0 == $alts) {
         $exp->set_status(NelioABExperiment::STATUS_DRAFT);
     }
     $exp->save();
     return $exp->get_id();
 }
Ejemplo n.º 5
0
 /**
  * Adds a Global Alternative Experiment in the appropriate property.
  *
  * @param NelioABGlobalAlternativeExperiment $exp The Alternative Experiment to be added.
  * @param int $alt_index The index of the alternative that this visitor is supposed to see.
  *            If 0, the original version is added; if 1, the first
  *            alternative; if 2, the second; and so on.
  *
  * @since 4.0.0
  */
 private static function add_global_exp($exp, $alt_index)
 {
     $alt = $exp->get_original();
     if ($alt_index > 0) {
         --$alt_index;
         $alts = $exp->get_alternatives();
         if ($alt_index < count($alts)) {
             $alt = $alts[$alt_index];
         }
     }
     switch ($exp->get_type()) {
         case NelioABExperiment::CSS_ALT_EXP:
             self::$css_info = array('exp' => $exp, 'alt' => $alt);
             break;
         case NelioABExperiment::MENU_ALT_EXP:
             self::$menu_info = array('exp' => $exp, 'alt' => $alt);
             break;
         case NelioABExperiment::THEME_ALT_EXP:
             self::$theme_info = array('exp' => $exp, 'alt' => $alt);
             break;
         case NelioABExperiment::WIDGET_ALT_EXP:
             self::$widget_info = array('exp' => $exp, 'alt' => $alt);
             break;
     }
 }