function newEvent(Request $request, Application $app)
 {
     /////////////////////////////////////////////////////// Set up incoming vars
     $newEventDraft = new NewEventDraftModel();
     $newEventDraft->setSiteId($app['currentSite']->getId());
     $newEventDraft->setUserAccountId($app['currentUser'] ? $app['currentUser']->getId() : null);
     $incomingData = array();
     // check for incoming date
     if (isset($_GET['date']) && trim($_GET['date'])) {
         $bits = explode("-", $_GET['date']);
         if (count($bits) == 3 && intval($bits[0]) && intval($bits[1]) && intval($bits[2])) {
             $incomingData['event.start_at'] = \TimeSource::getDateTime();
             $incomingData['event.start_at']->setTimezone(new \DateTimeZone($app['currentTimeZone']));
             $incomingData['event.start_at']->setDate($bits[0], $bits[1], $bits[2]);
             $incomingData['event.start_at']->setTime(9, 0, 0);
             $incomingData['event.end_at'] = clone $incomingData['event.start_at'];
             $incomingData['event.end_at']->setTime(17, 0, 0);
         }
     }
     // check for incoming area
     if (isset($_GET['area']) && trim($_GET['area'])) {
         $ar = new AreaRepository();
         $area = $ar->loadBySlug($app['currentSite'], $request->query->get('area'));
         if ($area) {
             $incomingData['area.id'] = $area->getId();
             $incomingData['area.title'] = $area->getTitle();
         }
     }
     // check for incoming group
     if (isset($_GET['group']) && trim($_GET['group'])) {
         $gr = new GroupRepository();
         $group = $gr->loadBySlug($app['currentSite'], $request->query->get('group'));
         if ($group) {
             $newEventDraft->setDetailsValue('group.id', $group->getId());
             $newEventDraft->setDetailsValue('group.title', $group->getTitle());
         }
     }
     /////////////////////////////////////////////////////// Check Permissions and Prompt IF NEEDED
     if (!$app['currentUser'] && !$app['currentUserActions']->has("org.openacalendar", "eventNew") && $app['anyVerifiedUserActions']->has("org.openacalendar", "eventNew")) {
         return $app['twig']->render('site/eventnew/new.useraccountneeded.html.twig', array('incomingData' => $incomingData));
     }
     if (!$app['currentUser']) {
         $app->abort(403, "Not allowed");
     }
     /////////////////////////////////////////////////////// Set up draft and start!
     foreach ($incomingData as $k => $v) {
         $newEventDraft->setDetailsValue('incoming.' . $k, $v);
     }
     $repo = new NewEventDraftRepository();
     $repo->create($newEventDraft);
     $steps = $this->getSteps($request, $app, $newEventDraft);
     $firstStepIdx = 0;
     // The first step is WHO but if group is already set we already know, and we want to jump straight to the next step!
     $steps[0]->processIsAllInformationGathered();
     if ($steps[0]->getIsAllInformationGathered()) {
         $firstStepIdx = 1;
     }
     return $app->redirect('/event/new/' . $newEventDraft->getSlug() . "/" . $steps[$firstStepIdx]->getStepID());
 }
 public function fetchAll()
 {
     $this->buildStart();
     $this->build();
     $this->buildStat();
     $results = array();
     while ($data = $this->stat->fetch()) {
         $newEventDraft = new NewEventDraftModel();
         $newEventDraft->setFromDataBaseRow($data);
         $results[] = $newEventDraft;
     }
     return $results;
 }
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder->add('summary', 'text', array('label' => 'Summary', 'required' => true, 'max_length' => VARCHAR_COLUMN_LENGTH_USED, 'attr' => array('autofocus' => 'autofocus'), 'data' => $this->eventDraft->getDetailsValue('event.summary')));
     $builder->add('description', 'textarea', array('label' => 'Description', 'required' => false, 'data' => $this->eventDraft->getDetailsValue('event.description')));
     $builder->add('url', new \symfony\form\MagicUrlType(), array('label' => 'Information Web Page URL', 'required' => false, 'data' => $this->eventDraft->getDetailsValue('event.url')));
     $builder->add('ticket_url', new \symfony\form\MagicUrlType(), array('label' => 'Tickets Web Page URL', 'required' => false, 'data' => $this->eventDraft->getDetailsValue('event.ticket_url')));
     $this->customFields = array();
     foreach ($this->site->getCachedEventCustomFieldDefinitionsAsModels() as $customField) {
         if ($customField->getIsActive()) {
             $extension = $this->extensionManager->getExtensionById($customField->getExtensionId());
             if ($extension) {
                 $fieldType = $extension->getEventCustomFieldByType($customField->getType());
                 if ($fieldType) {
                     $this->customFields[] = $customField;
                     $options = $fieldType->getSymfonyFormOptions($customField);
                     $options['data'] = $this->eventDraft->getDetailsValue('event.custom.' . $customField->getKey());
                     $builder->add('custom_' . $customField->getKey(), $fieldType->getSymfonyFormType($customField), $options);
                 }
             }
         }
     }
     if ($this->site->getIsFeatureVirtualEvents()) {
         //  if both are an option, user must check which one.
         if ($this->site->getIsFeaturePhysicalEvents()) {
             $builder->add("is_virtual", "checkbox", array('required' => false, 'label' => 'Is event accessible online?', 'data' => $this->eventDraft->hasDetailsValue('event.is_virtual') ? $this->eventDraft->getDetailsValue('event.is_virtual') : $this->fieldIsVirtualDefault));
         } else {
             $builder->add('is_virtual', 'hidden', array('data' => true));
         }
     } else {
         $builder->add('is_virtual', 'hidden', array('data' => false));
     }
     if ($this->site->getIsFeaturePhysicalEvents()) {
         //  if both are an option, user must check which one.
         if ($this->site->getIsFeatureVirtualEvents()) {
             $builder->add("is_physical", "checkbox", array('required' => false, 'label' => 'Does the event happen at a place?', 'data' => $this->eventDraft->hasDetailsValue('event.is_physical') ? $this->eventDraft->getDetailsValue('event.is_physical') : $this->fieldIsPhysicalDefault));
         } else {
             $builder->add('is_physical', 'hidden', array('data' => true));
         }
     } else {
         $builder->add('is_physical', 'hidden', array('data' => false));
     }
     /** @var \closure $myExtraFieldValidator **/
     $myExtraFieldValidator = function (FormEvent $event) {
         global $CONFIG;
         $form = $event->getForm();
         // URL validation. We really can't do much except verify ppl haven't put a space in, which they might do if they just type in Google search terms (seen it done)
         if (strpos($form->get("url")->getData(), " ") !== false) {
             $form['url']->addError(new FormError("Please enter a URL"));
         }
         if (strpos($form->get("ticket_url")->getData(), " ") !== false) {
             $form['ticket_url']->addError(new FormError("Please enter a URL"));
         }
         // Title
         if (!trim($form->get('summary')->getData())) {
             $form->get('summary')->addError(new FormError("Please enter a summary"));
         }
         // TODO it has to be at least one or the other of physical or virtual
     };
     // adding the validator to the FormBuilderInterface
     $builder->addEventListener(FormEvents::POST_BIND, $myExtraFieldValidator);
 }
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $crb = new CountryRepositoryBuilder();
     $crb->setSiteIn($this->site);
     $this->defaultCountry = null;
     $defaultCountryID = null;
     $countries = $crb->fetchAll();
     if (count($countries) > 1) {
         $countriesForSelect = array();
         foreach ($countries as $country) {
             $countriesForSelect[$country->getId()] = $country->getTitle();
             if ($this->defaultCountry == null && in_array($this->timeZoneName, $country->getTimezonesAsList())) {
                 $this->defaultCountry = $country;
                 $defaultCountryID = $country->getId();
             }
         }
         $builder->add('country_id', 'choice', array('label' => 'Country', 'choices' => $countriesForSelect, 'required' => true, 'data' => $defaultCountryID));
     } else {
         if (count($countries) == 1) {
             $this->defaultCountry = $countries[0];
             $builder->add('country_id', 'hidden', array('data' => $this->defaultCountry->getId()));
         }
     }
     $timezones = array();
     // Must explicetly set name as key otherwise Symfony forms puts an ID in, and that's no good for processing outside form
     foreach ($this->site->getCachedTimezonesAsList() as $timezone) {
         $timezones[$timezone] = $timezone;
     }
     if (count($timezones) != 1) {
         $builder->add('timezone', 'choice', array('label' => 'Time Zone', 'choices' => $timezones, 'required' => true));
     } else {
         $timezone = array_pop($timezones);
         $builder->add('timezone', 'hidden', array('data' => $timezone));
     }
     $years = array(date('Y'), date('Y') + 1);
     $data = null;
     if ($this->eventDraft->hasDetailsValue('event.start_at')) {
         $data = $this->eventDraft->getDetailsValueAsDateTime('event.start_at');
     } else {
         if ($this->eventDraft->hasDetailsValue('event.start_end_freetext.start')) {
             $data = $this->eventDraft->getDetailsValueAsDateTime('event.start_end_freetext.start');
         } else {
             if ($this->eventDraft->hasDetailsValue('incoming.event.start_at')) {
                 $data = $this->eventDraft->getDetailsValueAsDateTime('incoming.event.start_at');
             }
         }
     }
     $startOptions = array('label' => 'Start', 'date_widget' => 'single_text', 'date_format' => 'd/M/y', 'model_timezone' => 'UTC', 'view_timezone' => $this->timeZoneName, 'years' => $years, 'attr' => array('class' => 'dateInput'), 'required' => true, 'data' => $data);
     if ($this->formWidgetTimeMinutesMultiples > 1) {
         $startOptions['minutes'] = array();
         for ($i = 0; $i <= 59; $i = $i + $this->formWidgetTimeMinutesMultiples) {
             $startOptions['minutes'][] = $i;
         }
     }
     $builder->add('start_at', 'datetime', $startOptions);
     $data = null;
     if ($this->eventDraft->hasDetailsValue('event.end_at')) {
         $data = $this->eventDraft->getDetailsValueAsDateTime('event.end_at');
     } else {
         if ($this->eventDraft->hasDetailsValue('event.start_end_freetext.end')) {
             $data = $this->eventDraft->getDetailsValueAsDateTime('event.start_end_freetext.end');
         } else {
             if ($this->eventDraft->hasDetailsValue('incoming.event.end_at')) {
                 $data = $this->eventDraft->getDetailsValueAsDateTime('incoming.event.end_at');
             }
         }
     }
     $endOptions = array('label' => 'End', 'date_widget' => 'single_text', 'date_format' => 'd/M/y', 'model_timezone' => 'UTC', 'view_timezone' => $this->timeZoneName, 'years' => $years, 'attr' => array('class' => 'dateInput'), 'required' => true, 'data' => $data);
     if ($this->formWidgetTimeMinutesMultiples > 1) {
         $endOptions['minutes'] = array();
         for ($i = 0; $i <= 59; $i = $i + $this->formWidgetTimeMinutesMultiples) {
             $endOptions['minutes'][] = $i;
         }
     }
     $builder->add('end_at', 'datetime', $endOptions);
     /** @var \closure $myExtraFieldValidator **/
     $myExtraFieldValidator = function (FormEvent $event) {
         global $CONFIG;
         $form = $event->getForm();
         $myExtraFieldStart = $form->get('start_at')->getData();
         $myExtraFieldEnd = $form->get('end_at')->getData();
         // Validate end is after start?
         if ($myExtraFieldStart > $myExtraFieldEnd) {
             $form['start_at']->addError(new FormError("The start can not be after the end!"));
         }
         // validate not to far in future
         $max = \TimeSource::getDateTime();
         $max->add(new \DateInterval("P" . $CONFIG->eventsCantBeMoreThanYearsInFuture . "Y"));
         if ($myExtraFieldStart > $max) {
             $form['start_at']->addError(new FormError("The event can not be more than " . ($CONFIG->eventsCantBeMoreThanYearsInFuture > 1 ? $CONFIG->eventsCantBeMoreThanYearsInFuture . " years" : "a year") . " in the future."));
         }
         if ($myExtraFieldEnd > $max) {
             $form['end_at']->addError(new FormError("The event can not be more than " . ($CONFIG->eventsCantBeMoreThanYearsInFuture > 1 ? $CONFIG->eventsCantBeMoreThanYearsInFuture . " years" : "a year") . " in the future."));
         }
         // validate not to far in past
         $min = \TimeSource::getDateTime();
         $min->sub(new \DateInterval("P" . $CONFIG->eventsCantBeMoreThanYearsInPast . "Y"));
         if ($myExtraFieldStart < $min) {
             $form['start_at']->addError(new FormError("The event can not be more than " . ($CONFIG->eventsCantBeMoreThanYearsInPast > 1 ? $CONFIG->eventsCantBeMoreThanYearsInPast . " years" : "a year") . " in the past."));
         }
         if ($myExtraFieldEnd < $min) {
             $form['end_at']->addError(new FormError("The event can not be more than " . ($CONFIG->eventsCantBeMoreThanYearsInPast > 1 ? $CONFIG->eventsCantBeMoreThanYearsInPast . " years" : "a year") . " in the past."));
         }
     };
     // adding the validator to the FormBuilderInterface
     $builder->addEventListener(FormEvents::POST_BIND, $myExtraFieldValidator);
 }
 public function markIsDuplicateOf(NewEventDraftModel $newEventDraftModel, EventModel $eventModel)
 {
     global $DB;
     try {
         $DB->beginTransaction();
         $stat = $DB->prepare("UPDATE new_event_draft_information SET was_existing_event_id=:was_existing_event_id, updated_at=:updated_at WHERE id=:id");
         $stat->execute(array('id' => $newEventDraftModel->getId(), 'was_existing_event_id' => $eventModel->getId(), 'updated_at' => \TimeSource::getFormattedForDataBase()));
         $stat = $DB->prepare("INSERT INTO new_event_draft_history (new_event_draft_id, was_existing_event_id, user_account_id,created_at,details_changed,event_id_changed,not_duplicate_events_changed) " . "VALUES (:new_event_draft_id, :was_existing_event_id, :user_account_id,:created_at,-2,-2,-2 )");
         $stat->execute(array('new_event_draft_id' => $newEventDraftModel->getId(), 'was_existing_event_id' => $eventModel->getId(), 'user_account_id' => $newEventDraftModel->getUserAccountId() ? $newEventDraftModel->getUserAccountId() : null, 'created_at' => \TimeSource::getFormattedForDataBase()));
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }