/**
  * @dataProvider dataForTestSet
  */
 function testSet4($slug, $text, $result)
 {
     $event = new EventModel();
     $event->setSlug($slug);
     $event->setSummary($text);
     $this->assertEquals($result, $event->getSlugForUrl());
 }
 function testChangCustomField1()
 {
     TimeSource::mock(2014, 1, 1, 9, 0, 0);
     $customFieldDefinition1 = new EventCustomFieldDefinitionModel();
     $customFieldDefinition1->setId(1);
     $customFieldDefinition1->setExtensionId('org.openacalendar');
     $customFieldDefinition1->setType('TextSingleLine');
     $customFieldDefinition1->setKey('cats');
     $customFieldDefinition1->setLabel('cats');
     $eventSet = new EventRecurSetModel();
     $eventSet->setTimeZoneName('Europe/London');
     $eventSet->setCustomFields(array($customFieldDefinition1));
     $initialEvent = new EventModel();
     $initialEvent->setSlug(1);
     $initialEvent->setCustomField($customFieldDefinition1, "Dogs go miaow");
     // this is the inital wrong value
     $initialEvent->setStartAt(getUTCDateTime(2014, 1, 7, 19, 0, 0));
     $initialEvent->setEndAt(getUTCDateTime(2014, 1, 7, 21, 0, 0));
     $eventSet->setInitialEventJustBeforeLastChange($initialEvent);
     $initialEventEdited = clone $initialEvent;
     $initialEventEdited->setCustomField($customFieldDefinition1, "Dogs go woof");
     // this is the later corrected value
     $eventSet->setInitalEvent($initialEventEdited);
     $futureEvent1 = new EventModel();
     $futureEvent1->setSlug(2);
     $futureEvent1->setCustomField($customFieldDefinition1, "Dogs go miaow");
     // this is the earlier wrong value that we will want to overwrite
     $futureEvent1->setStartAt(getUTCDateTime(2014, 1, 14, 19, 0, 0));
     $futureEvent1->setEndAt(getUTCDateTime(2014, 1, 14, 21, 0, 0));
     $futureEvent2 = new EventModel();
     $futureEvent2->setSlug(3);
     $futureEvent2->setCustomField($customFieldDefinition1, "Dogs go woof");
     // this value has already been corrected
     $futureEvent2->setStartAt(getUTCDateTime(2014, 1, 21, 19, 0, 0));
     $futureEvent2->setEndAt(getUTCDateTime(2014, 1, 21, 21, 0, 0));
     $futureEvent3 = new EventModel();
     $futureEvent3->setSlug(4);
     $futureEvent3->setCustomField($customFieldDefinition1, "Dogs go miaowwwwwwww");
     // this value has already been edited. Possible to overwrite but not recommended
     $futureEvent3->setStartAt(getUTCDateTime(2014, 1, 21, 19, 0, 0));
     $futureEvent3->setEndAt(getUTCDateTime(2014, 1, 21, 21, 0, 0));
     $eventSet->setFutureEvents(array($futureEvent1, $futureEvent2, $futureEvent3));
     $eventHistory = new \models\EventHistoryModel();
     $eventHistory->setFromDataBaseRow(array('event_id' => null, 'summary' => null, 'description' => null, 'start_at' => null, 'end_at' => null, 'created_at' => null, 'is_deleted' => null, 'is_cancelled' => null, 'country_id' => null, 'timezone' => null, 'venue_id' => null, 'url' => null, 'ticket_url' => null, 'is_virtual' => null, 'is_physical' => null, 'area_id' => null, 'user_account_id' => null, 'summary_changed' => '-1', 'description_changed' => '-1', 'start_at_changed' => '-1', 'end_at_changed' => '-1', 'is_deleted_changed' => '-1', 'country_id_changed' => '-1', 'timezone_changed' => '-1', 'venue_id_changed' => '-1', 'url_changed' => '-1', 'is_virtual_changed' => '-1', 'is_physical_changed' => '-1', 'area_id_changed' => '-1', 'is_new' => '-1', 'custom_fields' => json_encode(array('1' => 'Dogs go woof')), 'custom_fields_changed' => json_encode(array('1' => 1))));
     $eventSet->setInitalEventLastChange($eventHistory);
     ## Detect !!!
     $eventSet->applyChangeToFutureEvents();
     ## Test Changes Picked Up
     $proposedChanges1 = $eventSet->getFutureEventsProposedChangesForEventSlug($futureEvent1->getSlug());
     $this->assertEquals(false, $proposedChanges1->getSummaryChangePossible());
     $this->assertEquals(false, $proposedChanges1->getSummaryChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getDescriptionChangePossible());
     $this->assertEquals(false, $proposedChanges1->getDescriptionChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getTimezoneChangePossible());
     $this->assertEquals(false, $proposedChanges1->getTimezoneChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getCountryAreaVenueIdChangePossible());
     $this->assertEquals(false, $proposedChanges1->getCountryAreaVenueIdChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getUrlChangePossible());
     $this->assertEquals(false, $proposedChanges1->getUrlChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getIsVirtualChangePossible());
     $this->assertEquals(false, $proposedChanges1->getIsVirtualChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getIsPhysicalChangePossible());
     $this->assertEquals(false, $proposedChanges1->getIsPhysicalChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getStartEndAtChangePossible());
     $this->assertEquals(false, $proposedChanges1->getStartEndAtChangeSelected());
     $this->assertEquals(true, $proposedChanges1->getCustomFieldChangePossible($customFieldDefinition1));
     $this->assertEquals(true, $proposedChanges1->getCustomFieldChangeSelected($customFieldDefinition1));
     $proposedChanges2 = $eventSet->getFutureEventsProposedChangesForEventSlug($futureEvent2->getSlug());
     $this->assertEquals(false, $proposedChanges2->getSummaryChangePossible());
     $this->assertEquals(false, $proposedChanges2->getSummaryChangeSelected());
     $this->assertEquals(false, $proposedChanges2->getDescriptionChangePossible());
     $this->assertEquals(false, $proposedChanges2->getDescriptionChangeSelected());
     $this->assertEquals(false, $proposedChanges2->getTimezoneChangePossible());
     $this->assertEquals(false, $proposedChanges2->getTimezoneChangeSelected());
     $this->assertEquals(false, $proposedChanges2->getCountryAreaVenueIdChangePossible());
     $this->assertEquals(false, $proposedChanges2->getCountryAreaVenueIdChangeSelected());
     $this->assertEquals(false, $proposedChanges2->getUrlChangePossible());
     $this->assertEquals(false, $proposedChanges2->getUrlChangeSelected());
     $this->assertEquals(false, $proposedChanges2->getIsVirtualChangePossible());
     $this->assertEquals(false, $proposedChanges2->getIsVirtualChangeSelected());
     $this->assertEquals(false, $proposedChanges2->getIsPhysicalChangePossible());
     $this->assertEquals(false, $proposedChanges2->getIsPhysicalChangeSelected());
     $this->assertEquals(false, $proposedChanges2->getStartEndAtChangePossible());
     $this->assertEquals(false, $proposedChanges2->getStartEndAtChangeSelected());
     $this->assertEquals(false, $proposedChanges2->isAnyChangesPossible());
     $this->assertEquals(false, $proposedChanges2->getCustomFieldChangePossible($customFieldDefinition1));
     $this->assertEquals(false, $proposedChanges2->getCustomFieldChangeSelected($customFieldDefinition1));
     $proposedChanges3 = $eventSet->getFutureEventsProposedChangesForEventSlug($futureEvent3->getSlug());
     $this->assertEquals(false, $proposedChanges3->getSummaryChangePossible());
     $this->assertEquals(false, $proposedChanges3->getSummaryChangeSelected());
     $this->assertEquals(false, $proposedChanges3->getDescriptionChangePossible());
     $this->assertEquals(false, $proposedChanges3->getDescriptionChangeSelected());
     $this->assertEquals(false, $proposedChanges3->getTimezoneChangePossible());
     $this->assertEquals(false, $proposedChanges3->getTimezoneChangeSelected());
     $this->assertEquals(false, $proposedChanges3->getCountryAreaVenueIdChangePossible());
     $this->assertEquals(false, $proposedChanges3->getCountryAreaVenueIdChangeSelected());
     $this->assertEquals(false, $proposedChanges3->getUrlChangePossible());
     $this->assertEquals(false, $proposedChanges3->getUrlChangeSelected());
     $this->assertEquals(false, $proposedChanges3->getIsVirtualChangePossible());
     $this->assertEquals(false, $proposedChanges3->getIsVirtualChangeSelected());
     $this->assertEquals(false, $proposedChanges3->getIsPhysicalChangePossible());
     $this->assertEquals(false, $proposedChanges3->getIsPhysicalChangeSelected());
     $this->assertEquals(false, $proposedChanges3->getStartEndAtChangePossible());
     $this->assertEquals(false, $proposedChanges3->getStartEndAtChangeSelected());
     $this->assertEquals(true, $proposedChanges3->isAnyChangesPossible());
     $this->assertEquals(true, $proposedChanges3->getCustomFieldChangePossible($customFieldDefinition1));
     $this->assertEquals(false, $proposedChanges3->getCustomFieldChangeSelected($customFieldDefinition1));
     ## Now Set to don't update and run and test
     $proposedChanges1->setCustomFieldChangeSelected($customFieldDefinition1, false);
     $this->assertEquals(false, $proposedChanges1->applyToEvent($futureEvent1, $initialEventEdited));
     $this->assertEquals("Dogs go miaow", $futureEvent1->getCustomField($customFieldDefinition1));
     ## Now Set to do update and run and test
     $proposedChanges1->setCustomFieldChangeSelected($customFieldDefinition1, true);
     $this->assertEquals(true, $proposedChanges1->applyToEvent($futureEvent1, $initialEventEdited));
     $this->assertEquals("Dogs go woof", $futureEvent1->getCustomField($customFieldDefinition1));
 }
 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();
     }
 }
 function testChangeStartEnd1()
 {
     TimeSource::mock(2014, 1, 1, 9, 0, 0);
     $eventSet = new EventRecurSetModel();
     $eventSet->setTimeZoneName('Europe/London');
     $initialEvent = new EventModel();
     $initialEvent->setSlug(1);
     $initialEvent->setSummary("Party");
     $initialEvent->setStartAt(getUTCDateTime(2014, 1, 7, 19, 0, 0));
     $initialEvent->setEndAt(getUTCDateTime(2014, 1, 7, 21, 0, 0));
     $eventSet->setInitialEventJustBeforeLastChange($initialEvent);
     $initialEventEdited = clone $initialEvent;
     $initialEventEdited->setStartAt(getUTCDateTime(2014, 1, 7, 20, 0, 0));
     $initialEventEdited->setEndAt(getUTCDateTime(2014, 1, 7, 23, 0, 0));
     $eventSet->setInitalEvent($initialEventEdited);
     $futureEvent1 = new EventModel();
     $futureEvent1->setSlug(2);
     $futureEvent1->setSummary("Party");
     $futureEvent1->setStartAt(getUTCDateTime(2014, 1, 14, 19, 0, 0));
     $futureEvent1->setEndAt(getUTCDateTime(2014, 1, 14, 21, 0, 0));
     $eventSet->setFutureEvents(array($futureEvent1));
     $eventHistory = new \models\EventHistoryModel();
     $eventHistory->setFromDataBaseRow(array('event_id' => null, 'summary' => null, 'description' => null, 'start_at' => null, 'end_at' => null, 'created_at' => null, 'is_deleted' => null, 'is_cancelled' => null, 'country_id' => null, 'timezone' => null, 'venue_id' => null, 'url' => null, 'ticket_url' => null, 'is_virtual' => null, 'is_physical' => null, 'area_id' => null, 'user_account_id' => null, 'summary_changed' => '-1', 'description_changed' => '-1', 'start_at_changed' => '1', 'end_at_changed' => '1', 'is_deleted_changed' => '-1', 'country_id_changed' => '-1', 'timezone_changed' => '-1', 'venue_id_changed' => '-1', 'url_changed' => '-1', 'is_virtual_changed' => '-1', 'is_physical_changed' => '-1', 'area_id_changed' => '-1', 'is_new' => '-1', 'custom_fields' => null, 'custom_fields_changed' => null));
     $eventSet->setInitalEventLastChange($eventHistory);
     ## Detect !!!
     $eventSet->applyChangeToFutureEvents();
     ## Test Changes Picked Up
     $proposedChanges1 = $eventSet->getFutureEventsProposedChangesForEventSlug($futureEvent1->getSlug());
     $this->assertEquals(false, $proposedChanges1->getSummaryChangePossible());
     $this->assertEquals(false, $proposedChanges1->getSummaryChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getDescriptionChangePossible());
     $this->assertEquals(false, $proposedChanges1->getDescriptionChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getTimezoneChangePossible());
     $this->assertEquals(false, $proposedChanges1->getTimezoneChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getCountryAreaVenueIdChangePossible());
     $this->assertEquals(false, $proposedChanges1->getCountryAreaVenueIdChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getUrlChangePossible());
     $this->assertEquals(false, $proposedChanges1->getUrlChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getIsVirtualChangePossible());
     $this->assertEquals(false, $proposedChanges1->getIsVirtualChangeSelected());
     $this->assertEquals(false, $proposedChanges1->getIsPhysicalChangePossible());
     $this->assertEquals(false, $proposedChanges1->getIsPhysicalChangeSelected());
     $this->assertEquals(true, $proposedChanges1->getStartEndAtChangePossible());
     $this->assertEquals(true, $proposedChanges1->getStartEndAtChangeSelected());
     $this->assertEquals(true, $proposedChanges1->isAnyChangesPossible());
     ## Now Set to don't update and run and test
     $proposedChanges1->setStartEndAtChangeSelected(false);
     $this->assertEquals(false, $proposedChanges1->applyToEvent($futureEvent1, $initialEventEdited));
     $this->assertEquals(getUTCDateTime(2014, 1, 14, 19, 0, 0)->getTimestamp(), $futureEvent1->getStartAtInUTC()->getTimestamp());
     $this->assertEquals(getUTCDateTime(2014, 1, 14, 21, 0, 0)->getTimestamp(), $futureEvent1->getEndAtInUTC()->getTimestamp());
     ## Now Set to do update and run and test
     $proposedChanges1->setStartEndAtChangeSelected(true);
     $this->assertEquals(true, $proposedChanges1->applyToEvent($futureEvent1, $initialEventEdited));
     $this->assertEquals(getUTCDateTime(2014, 1, 14, 20, 0, 0)->getTimestamp(), $futureEvent1->getStartAtInUTC()->getTimestamp());
     $this->assertEquals(getUTCDateTime(2014, 1, 14, 23, 0, 0)->getTimestamp(), $futureEvent1->getEndAtInUTC()->getTimestamp());
 }