public function createWithMetaData(EventModel $event, SiteModel $site, EventEditMetaDataModel $eventEditMetaDataModel, GroupModel $group = null, $additionalGroups = null, ImportedEventModel $importedEvent = null, $tags = array(), $medias = array())
 {
     global $DB;
     try {
         $DB->beginTransaction();
         $stat = $DB->prepare("SELECT max(slug) AS c FROM event_information WHERE site_id=:site_id");
         $stat->execute(array('site_id' => $site->getId()));
         $data = $stat->fetch();
         $event->setSlug($data['c'] + 1);
         $stat = $DB->prepare("INSERT INTO event_information (site_id, slug, summary,description,start_at,end_at," . " created_at, event_recur_set_id,venue_id,country_id,timezone, " . " url, ticket_url, is_physical, is_virtual, area_id, approved_at, is_deleted, is_cancelled, custom_fields) " . " VALUES (:site_id, :slug, :summary, :description, :start_at, :end_at, " . " :created_at, :event_recur_set_id,:venue_id,:country_id,:timezone, " . " :url, :ticket_url, :is_physical, :is_virtual, :area_id, :approved_at, '0', '0', :custom_fields) RETURNING id");
         $stat->execute(array('site_id' => $site->getId(), 'slug' => $event->getSlug(), 'summary' => substr($event->getSummary(), 0, VARCHAR_COLUMN_LENGTH_USED), 'description' => $event->getDescription(), 'start_at' => $event->getStartAtInUTC()->format("Y-m-d H:i:s"), 'end_at' => $event->getEndAtInUTC()->format("Y-m-d H:i:s"), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase(), 'event_recur_set_id' => $event->getEventRecurSetId(), 'country_id' => $event->getCountryId(), 'venue_id' => $event->getVenueId(), 'area_id' => $event->getVenueId() ? null : $event->getAreaId(), 'timezone' => $event->getTimezone(), 'url' => substr($event->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'ticket_url' => substr($event->getTicketUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'is_physical' => $event->getIsPhysical() ? 1 : 0, 'is_virtual' => $event->getIsVirtual() ? 1 : 0, 'custom_fields' => json_encode($event->getCustomFields())));
         $data = $stat->fetch();
         $event->setId($data['id']);
         $stat = $DB->prepare("INSERT INTO event_history (event_id, summary, description,start_at, end_at, " . " user_account_id  , created_at,venue_id,country_id,timezone," . " url, ticket_url, is_physical, is_virtual, area_id, is_new, approved_at, is_deleted, is_cancelled, edit_comment, custom_fields) VALUES " . " (:event_id, :summary, :description, :start_at, :end_at, " . " :user_account_id  , :created_at,:venue_id,:country_id,:timezone," . " :url, :ticket_url, :is_physical, :is_virtual, :area_id, '1', :approved_at, '0', '0', :edit_comment, :custom_fields)");
         $stat->execute(array('event_id' => $event->getId(), 'summary' => substr($event->getSummary(), 0, VARCHAR_COLUMN_LENGTH_USED), 'description' => $event->getDescription(), 'start_at' => $event->getStartAtInUTC()->format("Y-m-d H:i:s"), 'end_at' => $event->getEndAtInUTC()->format("Y-m-d H:i:s"), 'user_account_id' => $eventEditMetaDataModel->getUserAccount() ? $eventEditMetaDataModel->getUserAccount()->getId() : null, 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase(), 'country_id' => $event->getCountryId(), 'venue_id' => $event->getVenueId(), 'area_id' => $event->getVenueId() ? null : $event->getAreaId(), 'timezone' => $event->getTimezone(), 'url' => substr($event->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'ticket_url' => substr($event->getTicketUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'is_physical' => $event->getIsPhysical() ? 1 : 0, 'is_virtual' => $event->getIsVirtual() ? 1 : 0, 'edit_comment' => $eventEditMetaDataModel->getEditComment(), 'custom_fields' => json_encode($event->getCustomFields())));
         // Group can be passed as model or as field on event!
         if (!$group && $event->getGroupId()) {
             $group = new GroupModel();
             $group->setId($event->getGroupId());
         }
         if ($group || $additionalGroups) {
             $stat = $DB->prepare("INSERT INTO event_in_group (group_id,event_id,added_by_user_account_id,added_at,is_main_group,addition_approved_at) " . "VALUES (:group_id,:event_id,:added_by_user_account_id,:added_at,:is_main_group,:addition_approved_at)");
             if ($group) {
                 $stat->execute(array('group_id' => $group->getId(), 'event_id' => $event->getId(), 'added_by_user_account_id' => $eventEditMetaDataModel->getUserAccount() ? $eventEditMetaDataModel->getUserAccount()->getId() : null, 'added_at' => \TimeSource::getFormattedForDataBase(), 'addition_approved_at' => \TimeSource::getFormattedForDataBase(), 'is_main_group' => 1));
             }
             if ($additionalGroups && is_array($additionalGroups)) {
                 foreach ($additionalGroups as $additionalGroup) {
                     if ($additionalGroup->getId() != $group->getId()) {
                         $stat->execute(array('group_id' => $additionalGroup->getId(), 'event_id' => $event->getId(), 'added_by_user_account_id' => $eventEditMetaDataModel->getUserAccount() ? $eventEditMetaDataModel->getUserAccount()->getId() : null, 'added_at' => \TimeSource::getFormattedForDataBase(), 'addition_approved_at' => \TimeSource::getFormattedForDataBase(), 'is_main_group' => 0));
                     }
                 }
             }
         }
         if ($eventEditMetaDataModel->getUserAccount()) {
             if ($event->getGroupId()) {
                 $ufgr = new UserWatchesGroupRepository();
                 $ufgr->startUserWatchingGroupIdIfNotWatchedBefore($eventEditMetaDataModel->getUserAccount(), $event->getGroupId());
             } else {
                 // TODO watch site?
             }
         }
         if ($importedEvent) {
             $stat = $DB->prepare("INSERT INTO imported_event_is_event (imported_event_id, event_id, created_at) " . "VALUES (:imported_event_id, :event_id, :created_at)");
             $stat->execute(array('imported_event_id' => $importedEvent->getId(), 'event_id' => $event->getId(), 'created_at' => \TimeSource::getFormattedForDataBase()));
         }
         if ($tags) {
             $stat = $DB->prepare("INSERT INTO event_has_tag (tag_id,event_id,added_by_user_account_id,added_at,addition_approved_at) " . "VALUES (:tag_id,:event_id,:added_by_user_account_id,:added_at,:addition_approved_at)");
             foreach ($tags as $tag) {
                 $stat->execute(array('tag_id' => $tag->getId(), 'event_id' => $event->getId(), 'added_by_user_account_id' => $eventEditMetaDataModel->getUserAccount() ? $eventEditMetaDataModel->getUserAccount()->getId() : null, 'added_at' => \TimeSource::getFormattedForDataBase(), 'addition_approved_at' => \TimeSource::getFormattedForDataBase()));
             }
         }
         if ($medias) {
             $stat = $DB->prepare("INSERT INTO media_in_event (event_id,media_id,added_by_user_account_id,added_at,addition_approved_at) " . "VALUES (:event_id,:media_id,:added_by_user_account_id,:added_at,:addition_approved_at)");
             foreach ($medias as $media) {
                 $stat->execute(array('event_id' => $event->getId(), 'media_id' => $media->getId(), 'added_by_user_account_id' => $eventEditMetaDataModel->getUserAccount() ? $eventEditMetaDataModel->getUserAccount()->getId() : null, 'added_at' => \TimeSource::getFormattedForDataBase(), 'addition_approved_at' => \TimeSource::getFormattedForDataBase()));
             }
         }
         if ($eventEditMetaDataModel->getCreatedFromNewEventDraftID()) {
             $stat = $DB->prepare("UPDATE new_event_draft_information SET event_id=:event_id WHERE id=:id");
             $stat->execute(array('event_id' => $event->getId(), 'id' => $eventEditMetaDataModel->getCreatedFromNewEventDraftID()));
             $stat = $DB->prepare("INSERT INTO new_event_draft_history (new_event_draft_id, event_id, user_account_id,created_at,details_changed,was_existing_event_id_changed,not_duplicate_events_changed) " . "VALUES (:new_event_draft_id, :event_id, :user_account_id,:created_at,-2,-2,-2 )");
             $stat->execute(array('new_event_draft_id' => $eventEditMetaDataModel->getCreatedFromNewEventDraftID(), 'event_id' => $event->getId(), 'user_account_id' => $eventEditMetaDataModel->getUserAccount() ? $eventEditMetaDataModel->getUserAccount()->getId() : null, 'created_at' => \TimeSource::getFormattedForDataBase()));
         }
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 public function update(EventModel $event, $fields, EventEditMetaDataModel $eventEditMetaDataModel)
 {
     $alreadyInTransaction = $this->db->inTransaction();
     // Make Information Data
     $fieldsSQL1 = array();
     $fieldsParams1 = array('id' => $event->getId());
     foreach ($fields as $field) {
         if ($field == 'custom') {
             $fieldsSQL1[] = " custom_fields = :custom_fields ";
             $fieldsParams1['custom_fields'] = json_encode($event->getCustomFields());
         } else {
             $fieldsSQL1[] = " " . $field . "=:" . $field . " ";
             if ($field == 'summary') {
                 $fieldsParams1['summary'] = substr($event->getSummary(), 0, VARCHAR_COLUMN_LENGTH_USED);
             } else {
                 if ($field == 'description') {
                     $fieldsParams1['description'] = $event->getDescription();
                 } else {
                     if ($field == 'start_at') {
                         $fieldsParams1['start_at'] = $event->getStartAt()->format("Y-m-d H:i:s");
                     } else {
                         if ($field == 'end_at') {
                             $fieldsParams1['end_at'] = $event->getEndAt()->format("Y-m-d H:i:s");
                         } else {
                             if ($field == 'venue_id') {
                                 $fieldsParams1['venue_id'] = $event->getVenueId();
                             } else {
                                 if ($field == 'area_id') {
                                     $fieldsParams1['area_id'] = $event->getAreaId();
                                 } else {
                                     if ($field == 'country_id') {
                                         $fieldsParams1['country_id'] = $event->getCountryId();
                                     } else {
                                         if ($field == 'timezone') {
                                             $fieldsParams1['timezone'] = substr($event->getTimezone(), 0, VARCHAR_COLUMN_LENGTH_USED);
                                         } else {
                                             if ($field == 'url') {
                                                 $fieldsParams1['url'] = substr($event->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED);
                                             } else {
                                                 if ($field == 'ticket_url') {
                                                     $fieldsParams1['ticket_url'] = substr($event->getTicketUrl(), 0, VARCHAR_COLUMN_LENGTH_USED);
                                                 } else {
                                                     if ($field == 'is_physical') {
                                                         $fieldsParams1['is_physical'] = $event->getIsPhysical() ? 1 : 0;
                                                     } else {
                                                         if ($field == 'is_virtual') {
                                                             $fieldsParams1['is_virtual'] = $event->getIsVirtual() ? 1 : 0;
                                                         } else {
                                                             if ($field == 'is_cancelled') {
                                                                 $fieldsParams1['is_cancelled'] = $event->getIsCancelled() ? 1 : 0;
                                                             } else {
                                                                 if ($field == 'is_deleted') {
                                                                     $fieldsParams1['is_deleted'] = $event->getIsDeleted() ? 1 : 0;
                                                                 } else {
                                                                     if ($field == 'is_duplicate_of_id') {
                                                                         $fieldsParams1['is_duplicate_of_id'] = $event->getIsDuplicateOfId();
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // Make History Data
     $fieldsSQL2 = array('event_id', 'user_account_id', 'created_at', 'approved_at', 'from_ip');
     $fieldsSQLParams2 = array(':event_id', ':user_account_id', ':created_at', ':approved_at', ':from_ip');
     $fieldsParams2 = array('event_id' => $event->getId(), 'user_account_id' => $eventEditMetaDataModel->getUserAccount() ? $eventEditMetaDataModel->getUserAccount()->getId() : null, 'created_at' => $this->timesource->getFormattedForDataBase(), 'approved_at' => $this->timesource->getFormattedForDataBase(), 'from_ip' => $eventEditMetaDataModel->getIp());
     if ($eventEditMetaDataModel->getRevertedFromHistoryCreatedAt()) {
         $fieldsSQL2[] = ' reverted_from_created_at ';
         $fieldsSQLParams2[] = ' :reverted_from_created_at ';
         $fieldsParams2['reverted_from_created_at'] = $eventEditMetaDataModel->getRevertedFromHistoryCreatedAt()->format("Y-m-d H:i:s");
     }
     if ($eventEditMetaDataModel->getEditComment()) {
         $fieldsSQL2[] = ' edit_comment ';
         $fieldsSQLParams2[] = ' :edit_comment ';
         $fieldsParams2['edit_comment'] = $eventEditMetaDataModel->getEditComment();
     }
     foreach ($this->possibleFields as $field) {
         if (in_array($field, $fields) || $field == 'summary') {
             $fieldsSQL2[] = " " . $field . " ";
             $fieldsSQLParams2[] = " :" . $field . " ";
             if ($field == 'summary') {
                 $fieldsParams2['summary'] = substr($event->getSummary(), 0, VARCHAR_COLUMN_LENGTH_USED);
             } else {
                 if ($field == 'description') {
                     $fieldsParams2['description'] = $event->getDescription();
                 } else {
                     if ($field == 'start_at') {
                         $fieldsParams2['start_at'] = $event->getStartAt()->format("Y-m-d H:i:s");
                     } else {
                         if ($field == 'end_at') {
                             $fieldsParams2['end_at'] = $event->getEndAt()->format("Y-m-d H:i:s");
                         } else {
                             if ($field == 'venue_id') {
                                 $fieldsParams2['venue_id'] = $event->getVenueId();
                             } else {
                                 if ($field == 'area_id') {
                                     $fieldsParams2['area_id'] = $event->getAreaId();
                                 } else {
                                     if ($field == 'country_id') {
                                         $fieldsParams2['country_id'] = $event->getCountryId();
                                     } else {
                                         if ($field == 'timezone') {
                                             $fieldsParams2['timezone'] = substr($event->getTimezone(), 0, VARCHAR_COLUMN_LENGTH_USED);
                                         } else {
                                             if ($field == 'url') {
                                                 $fieldsParams2['url'] = substr($event->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED);
                                             } else {
                                                 if ($field == 'ticket_url') {
                                                     $fieldsParams2['ticket_url'] = substr($event->getTicketUrl(), 0, VARCHAR_COLUMN_LENGTH_USED);
                                                 } else {
                                                     if ($field == 'is_physical') {
                                                         $fieldsParams2['is_physical'] = $event->getIsPhysical() ? 1 : 0;
                                                     } else {
                                                         if ($field == 'is_virtual') {
                                                             $fieldsParams2['is_virtual'] = $event->getIsVirtual() ? 1 : 0;
                                                         } else {
                                                             if ($field == 'is_cancelled') {
                                                                 $fieldsParams2['is_cancelled'] = $event->getIsCancelled() ? 1 : 0;
                                                             } else {
                                                                 if ($field == 'is_deleted') {
                                                                     $fieldsParams2['is_deleted'] = $event->getIsDeleted() ? 1 : 0;
                                                                 } else {
                                                                     if ($field == 'is_duplicate_of_id') {
                                                                         $fieldsParams2['is_duplicate_of_id'] = $event->getIsDuplicateOfId();
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " 0 ";
         } else {
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " -2 ";
         }
     }
     if (in_array("custom", $fields)) {
         $fieldsSQL2[] = " custom_fields ";
         $fieldsSQLParams2[] = " :custom_fields ";
         $fieldsParams2['custom_fields'] = json_encode($event->getCustomFields());
         $fieldsSQL2[] = " custom_fields_changed ";
         $fieldsSQLParams2[] = " 0 ";
     } else {
         $fieldsSQL2[] = " custom_fields_changed ";
         $fieldsSQLParams2[] = " -2 ";
     }
     try {
         if (!$alreadyInTransaction) {
             $this->db->beginTransaction();
         }
         // Information SQL
         $stat = $this->db->prepare("UPDATE event_information  SET " . implode(",", $fieldsSQL1) . " WHERE id=:id");
         $stat->execute($fieldsParams1);
         // History SQL
         $stat = $this->db->prepare("INSERT INTO event_history (" . implode(",", $fieldsSQL2) . ") VALUES (" . implode(",", $fieldsSQLParams2) . ")");
         $stat->execute($fieldsParams2);
         if (!$alreadyInTransaction) {
             $this->db->commit();
         }
     } catch (Exception $e) {
         if (!$alreadyInTransaction) {
             $this->db->rollBack();
         }
         throw $e;
     }
 }