Created
March 18, 2017 16:14
-
-
Save cyberlex404/3a7d53a0d702a58507c4a7f6fefc6c24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\holiday\Form; | |
| use Drupal\Core\Datetime\DateFormatter; | |
| use Drupal\Core\Datetime\DrupalDateTime; | |
| use Drupal\Core\Form\FormBase; | |
| use Drupal\Core\Form\FormStateInterface; | |
| use Drupal\holiday\Entity\Inquiry; | |
| use Drupal\holiday\Event\InquiryAdded; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| use Drupal\Core\Entity\EntityStorageBase; | |
| use Drupal\Core\Entity\EntityTypeManager; | |
| use Drupal\Core\Session\AccountProxy; | |
| use Drupal\Core\Config\Config; | |
| use Drupal\Component\Utility\Crypt; | |
| /** | |
| * Class Holiday. | |
| * | |
| * @package Drupal\holiday\Form | |
| */ | |
| class Holiday extends FormBase { | |
| /** | |
| * Drupal\Core\Entity\EntityTypeManager definition. | |
| * | |
| * @var \Drupal\Core\Entity\EntityTypeManager | |
| */ | |
| protected $entityTypeManager; | |
| /** | |
| * Drupal\Core\Session\AccountProxy definition. | |
| * | |
| * @var \Drupal\Core\Session\AccountProxy | |
| */ | |
| protected $currentUser; | |
| /** | |
| * Drupal\Core\Config\Config definition. | |
| * | |
| * @var \Drupal\Core\Config\Config | |
| */ | |
| protected $holidayConfig; | |
| /** | |
| * Holiday constructor. | |
| * @param $entityTypeManager | |
| * @param \Drupal\Core\Session\AccountProxy $current_user | |
| */ | |
| public function __construct( | |
| $entityTypeManager, | |
| AccountProxy $current_user, | |
| Config $holidayConfig, | |
| DateFormatter $dateFormatter | |
| ) { | |
| $this->entityTypeManager = $entityTypeManager; | |
| $this->currentUser = $current_user; | |
| $this->holidayConfig = $holidayConfig; | |
| $this->dateFormatter = $dateFormatter; | |
| } | |
| public static function create(ContainerInterface $container) { | |
| return new static( | |
| $container->get('entity_type.manager'), | |
| $container->get('current_user'), | |
| $container->get('config.factory')->get('holiday.holidayformconfig'), | |
| $container->get('date.formatter') | |
| ); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getFormId() { | |
| return 'holiday'; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function buildForm(array $form, FormStateInterface $form_state) { | |
| $form['holiday_welcome'] = [ | |
| '#type' => 'item', | |
| '#markup' => $this->holidayConfig->get('welcome_text')['value'], | |
| ]; | |
| $form['inquiry'] = [ | |
| '#type' => 'container', //details | |
| '#title' => $this->t('Inquiry'), | |
| //'#open' => TRUE, | |
| '#attributes' => [ | |
| 'class' => ['inquiry', 'row'], | |
| ], | |
| ]; | |
| $form['inquiry']['name'] = [ | |
| '#type' => 'textfield', | |
| '#required' => TRUE, | |
| '#title' => $this->t('Your name'), | |
| '#placeholder' => 'Иванов Иван', | |
| '#description' => $this->t('Please, introduce yourself'), | |
| '#maxlength' => 64, | |
| '#size' => 250, | |
| '#attributes' => [ | |
| 'class' => ['inquiry-name'], | |
| ], | |
| '#prefix' => '<div class="col-sm-6">', | |
| '#suffix' => '</div>', | |
| ]; | |
| $form['inquiry']['city'] = [ | |
| '#type' => 'textfield', | |
| '#title' => $this->t('City'), | |
| '#placeholder' => 'Москва', // | |
| '#description' => $this->t('Where do you live?'), | |
| '#maxlength' => 64, | |
| '#size' => 250, | |
| '#prefix' => '<div class="col-sm-6">', | |
| '#suffix' => '</div>', | |
| ]; | |
| $defaultDate = DrupalDateTime::createFromTimestamp(REQUEST_TIME); | |
| $form['inquiry']['holiday_start'] = [ | |
| '#type' => 'datetime', | |
| '#date_date_element' => 'date', | |
| '#date_time_element' => 'none', | |
| '#date_year_range' => '0:+1', | |
| '#default_value' => $defaultDate, | |
| '#required' => TRUE, | |
| '#title' => $this->t('Holiday start'), | |
| '#attributes' => [ | |
| 'class' => ['holiday-start'], | |
| ], | |
| '#prefix' => '<div class="col-sm-3">', | |
| '#suffix' => '</div>', | |
| ]; | |
| $form['inquiry']['holiday_end'] = [ | |
| '#type' => 'datetime', | |
| '#date_date_element' => 'date', | |
| '#date_time_element' => 'none', | |
| '#date_year_range' => '0:+1', | |
| '#default_value' => $defaultDate, | |
| '#required' => TRUE, | |
| '#title' => $this->t('Holiday end'), | |
| '#attributes' => [ | |
| 'class' => ['holiday-end'], | |
| ], | |
| '#prefix' => '<div class="col-sm-3">', | |
| '#suffix' => '</div>', | |
| ]; | |
| $form['inquiry']['count_people'] = [ | |
| '#type' => 'textfield', | |
| '#required' => TRUE, | |
| '#title' => $this->t('Count person'), | |
| '#placeholder' => '3-е взрослых, 1 реб. 5 лет', | |
| '#maxlength' => 64, | |
| '#size' => 250, | |
| '#attributes' => [ | |
| 'class' => ['count-people'], | |
| ], | |
| '#prefix' => '<div class="col-sm-6">', | |
| '#suffix' => '</div>', | |
| ]; | |
| $form['notes'] = [ | |
| '#type' => 'textarea', | |
| '#title' => $this->t('Notes and wishes'), | |
| '#description' => $this->t('Notes and wishes to the place of rest and services'), | |
| '#placeholder' => 'Баня, лодка для прогулок и рыбалки, экскурсии...', | |
| ]; | |
| $form['contacts'] = [ | |
| '#type' => 'container', //details | |
| '#title' => $this->t('Contacts'), | |
| //'#open' => TRUE, | |
| '#attributes' => [ | |
| 'class' => ['contacts', 'row'], | |
| ], | |
| ]; | |
| $form['contacts']['telephone'] = [ | |
| '#type' => 'tel', | |
| '#required' => TRUE, | |
| '#title' => $this->t('Telephone'), | |
| '#description' => $this->t('You telephone'), | |
| '#placeholder' => '+375 (29) 147-25-30', | |
| '#prefix' => '<div class="col-sm-6">', | |
| '#suffix' => '</div>', | |
| ]; | |
| $form['contacts']['email'] = [ | |
| '#type' => 'email', | |
| '#required' => TRUE, | |
| '#title' => $this->t('E-mail'), | |
| '#placeholder' => 'you-awesome-name@yandex.ru', | |
| '#prefix' => '<div class="col-sm-6">', | |
| '#suffix' => '</div>', | |
| ]; | |
| $form['accept'] = [ | |
| '#type' => 'checkbox', | |
| '#title' => $this->t('I accept the terms of service'), | |
| ]; | |
| $form['submit'] = [ | |
| '#type' => 'submit', | |
| '#value' => $this->t('Submit'), | |
| ]; | |
| return $form; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function validateForm(array &$form, FormStateInterface $form_state) { | |
| $holidayStart = $form_state->getValue('holiday_start'); | |
| $holidayEnd = $form_state->getValue('holiday_end'); | |
| if($holidayEnd < $holidayStart) { | |
| $form_state->setErrorByName('holiday_end', $this->t('Date must be greater @start.', ['@start' => $holidayStart])); | |
| } | |
| if($form_state->getValue('accept') == FALSE) { | |
| $form_state->setErrorByName('accept', $this->t('You must accept the @terms_of_service', ['@terms_of_service' => $this->t('terms of service')])); | |
| } | |
| parent::validateForm($form, $form_state); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function submitForm(array &$form, FormStateInterface $form_state) { | |
| // Display result. | |
| foreach ($form_state->getValues() as $key => $value) { | |
| drupal_set_message($key . ': ' . $value); | |
| } | |
| $inquiry = $this->inquiryCreate($form_state); | |
| $event = new InquiryAdded($inquiry, ['inquiry' => $inquiry]); | |
| $event_dispatcher = \Drupal::service('event_dispatcher'); | |
| $event_dispatcher->dispatch(InquiryAdded::EVENT_NAME, $event); | |
| $form_state->setRedirect('holiday.inquiry_actions', ['inquiry' => $inquiry->id(), 'access_code' => $inquiry->getAccessCode()]); | |
| } | |
| /** | |
| * @param \Drupal\Core\Form\FormStateInterface $form_state | |
| * @return \Drupal\holiday\Entity\Inquiry | |
| */ | |
| protected function inquiryCreate(FormStateInterface $form_state) { | |
| $inquiry = Inquiry::create([ | |
| 'user_id' => 1, | |
| 'name' => 'NEW', | |
| 'tourist' => $form_state->getValue('name'), | |
| 'email' => $form_state->getValue('email'), | |
| 'telephone' => $form_state->getValue('telephone'), | |
| 'notes' => $form_state->getValue('notes'), | |
| 'count_people' => $form_state->getValue('count_people'), | |
| 'city' => $form_state->getValue('city'), | |
| 'holiday_start' => $this->toTimestamp($form_state->getValue('holiday_start')), // toTimestamp | |
| 'holiday_end' => $this->toTimestamp($form_state->getValue('holiday_end')), | |
| 'access_code' => Crypt::randomBytesBase64(32), | |
| ]); | |
| $inquiry->save(); | |
| return $this->setInquiryName($inquiry); | |
| } | |
| protected function setInquiryName(Inquiry $inquiry) { | |
| $prefix = 'i17-'; | |
| $id = $inquiry->id(); | |
| $inquiry->setName($prefix . $id); | |
| $inquiry->save(); | |
| return $inquiry; | |
| } | |
| protected function toTimestamp($string) { | |
| // 2017-03-08 | |
| $date = DrupalDateTime::createFromFormat('Y-m-d H:i:s e', $string); | |
| $date->setTime(12, 0); | |
| return $date->format('U'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment