private function delete_event($delete_all_serie_events = false)
 {
     $events_list = CalendarService::get_serie_events($this->event->get_content()->get_id());
     if ($delete_all_serie_events) {
         foreach ($events_list as $event) {
             //Delete event comments
             CommentsService::delete_comments_topic_module('calendar', $event->get_id());
             //Delete participants
             CalendarService::delete_all_participants($event->get_id());
         }
         CalendarService::delete_all_serie_events($this->event->get_content()->get_id());
         PersistenceContext::get_querier()->delete(DB_TABLE_EVENTS, 'WHERE module = :module AND id_in_module = :id', array('module' => 'calendar', 'id' => !$this->event->get_parent_id() ? $this->event->get_id() : $this->event->get_parent_id()));
     } else {
         if (!$this->event->belongs_to_a_serie() || count($events_list) == 1) {
             CalendarService::delete_event_content('WHERE id = :id', array('id' => $this->event->get_parent_id()));
         }
         //Delete event
         CalendarService::delete_event('WHERE id_event = :id', array('id' => $this->event->get_id()));
         if (!$this->event->get_parent_id()) {
             PersistenceContext::get_querier()->delete(DB_TABLE_EVENTS, 'WHERE module=:module AND id_in_module=:id', array('module' => 'calendar', 'id' => $this->event->get_id()));
         }
         //Delete event comments
         CommentsService::delete_comments_topic_module('calendar', $this->event->get_id());
         //Delete participants
         CalendarService::delete_all_participants($this->event->get_id());
     }
     Feed::clear_cache('calendar');
     CalendarCurrentMonthEventsCache::invalidate();
 }
 private function save()
 {
     $event = $this->get_event();
     $event_content = $event->get_content();
     $event_content->set_title($this->form->get_value('title'));
     $event_content->set_rewrited_title(Url::encode_rewrite($this->form->get_value('title')));
     $event_content->set_category_id($this->form->get_value('category_id')->get_raw_value());
     $event_content->set_contents($this->form->get_value('contents'));
     $event_content->set_location($this->form->get_value('location'));
     if (!$this->is_contributor_member() && $this->form->get_value('approved')) {
         $event_content->approve();
     } else {
         $event_content->unapprove();
     }
     if ($this->form->get_value('registration_authorized')) {
         $event_content->authorize_registration();
         $event_content->set_max_registered_members($this->form->get_value('max_registered_members'));
         if ($this->form->get_value('last_registration_date_enabled')) {
             $event_content->enable_last_registration_date();
             $event_content->set_last_registration_date($this->form->get_value('last_registration_date'));
         } else {
             $event_content->disable_last_registration_date();
             $event_content->set_last_registration_date(null);
         }
         $event_content->set_register_authorizations($this->form->get_value('register_authorizations', $event_content->get_register_authorizations())->build_auth_array());
     } else {
         $event_content->unauthorize_registration();
     }
     $event_content->set_repeat_type($this->form->get_value('repeat_type')->get_raw_value());
     if ($event_content->get_repeat_type() != CalendarEventContent::NEVER) {
         $event_content->set_repeat_number($this->form->get_value('repeat_number'));
     }
     $event->set_start_date($this->form->get_value('start_date'));
     $event->set_end_date($this->form->get_value('end_date'));
     if ($event->get_id() === null) {
         $id_content = CalendarService::add_event_content($event_content);
         $event_content->set_id($id_content);
         $event->set_content($event_content);
         $id_event = CalendarService::add_event($event);
         if ($event->get_content()->is_repeatable()) {
             $new_start_date = $event->get_start_date();
             $new_end_date = $event->get_end_date();
             for ($i = 1; $i <= $event->get_content()->get_repeat_number(); $i++) {
                 $e = new CalendarEvent();
                 $e->set_content($event->get_content());
                 $e->set_parent_id($id_event);
                 $e = $this->set_event_start_and_end_date($e, $new_start_date, $new_end_date);
                 CalendarService::add_event($e);
                 $new_start_date = $e->get_start_date();
                 $new_end_date = $e->get_end_date();
             }
         }
     } else {
         CalendarService::update_event_content($event_content);
         $id_event = CalendarService::update_event($event);
         if ($event->get_content()->is_repeatable() || $event_content->is_repeatable() && ($event->get_content()->get_repeat_number() != $event_content->get_repeat_number() || $event->get_content()->get_repeat_type() != $event_content->get_repeat_type())) {
             $events_list = CalendarService::get_serie_events($event_content->get_id());
             $new_start_date = $event->get_start_date();
             $new_end_date = $event->get_end_date();
             $i = 0;
             foreach ($events_list as $id => $e) {
                 if ($id != $id_event) {
                     $e->set_content($event_content);
                     $e = $this->set_event_start_and_end_date($e, $new_start_date, $new_end_date);
                     if ($i <= $event_content->get_repeat_number()) {
                         CalendarService::update_event($e);
                     } else {
                         CalendarService::delete_event('WHERE id_event = :id', array('id' => $e->get_id()));
                     }
                     $new_start_date = $e->get_start_date();
                     $new_end_date = $e->get_end_date();
                 }
                 $i++;
             }
             if ($i < $event_content->get_repeat_number()) {
                 for ($j = $i; $j <= $event_content->get_repeat_number(); $j++) {
                     $e = new CalendarEvent();
                     $e->set_content($event_content);
                     $e->set_parent_id($id_event);
                     $e = $this->set_event_start_and_end_date($e, $new_start_date, $new_end_date);
                     CalendarService::add_event($e);
                     $new_start_date = $e->get_start_date();
                     $new_end_date = $e->get_end_date();
                 }
             }
         }
     }
     $this->contribution_actions($event, $id_event);
     Feed::clear_cache('calendar');
     CalendarCurrentMonthEventsCache::invalidate();
 }