public function create(GroupModel $group, SiteModel $site, UserAccountModel $creator)
 {
     global $DB;
     try {
         $DB->beginTransaction();
         $stat = $DB->prepare("SELECT max(slug) AS c FROM group_information WHERE site_id=:site_id");
         $stat->execute(array('site_id' => $site->getId()));
         $data = $stat->fetch();
         $group->setSlug($data['c'] + 1);
         $stat = $DB->prepare("INSERT INTO group_information (site_id, slug, title,url,description,created_at,twitter_username,approved_at) " . "VALUES (:site_id, :slug, :title, :url, :description, :created_at, :twitter_username,:approved_at) RETURNING id");
         $stat->execute(array('site_id' => $site->getId(), 'slug' => $group->getSlug(), 'title' => substr($group->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'url' => substr($group->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'twitter_username' => substr($group->getTwitterUsername(), 0, VARCHAR_COLUMN_LENGTH_USED), 'description' => $group->getDescription(), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase()));
         $data = $stat->fetch();
         $group->setId($data['id']);
         $stat = $DB->prepare("INSERT INTO group_history (group_id, title, url, description, user_account_id  , created_at, approved_at, twitter_username, is_new) VALUES " . "(:group_id, :title, :url, :description, :user_account_id  , :created_at, :approved_at, :twitter_username, '1')");
         $stat->execute(array('group_id' => $group->getId(), 'title' => substr($group->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'url' => substr($group->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'twitter_username' => substr($group->getTwitterUsername(), 0, VARCHAR_COLUMN_LENGTH_USED), 'description' => $group->getDescription(), 'user_account_id' => $creator->getId(), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase()));
         $data = $stat->fetch();
         $ufgr = new UserWatchesGroupRepository();
         $ufgr->startUserWatchingGroupIfNotWatchedBefore($creator, $group);
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 /**
  * This function takes a set of proposed new events in. It looks for any duplicate events already saved and filters them out.
  * @return Array New proposed events where duplicates don't exist.
  */
 public function filterEventsForExisting(EventModel $sourceEvent, $events)
 {
     $group = new GroupModel();
     $group->setId($sourceEvent->getGroupId());
     $out = array();
     foreach ($events as $event) {
         $erb = new EventRepositoryBuilder();
         $erb->setGroup($group);
         $erb->setStart($event->getStartAt());
         $erb->setEnd($event->getEndAt());
         $existingEvents = $erb->fetchAll();
         if (count($existingEvents) > 0) {
         } else {
             $out[] = $event;
         }
     }
     return $out;
 }
 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();
     }
 }