condition('status', 1); $query->condition('type', 'courts'); $courtsIds = $query->execute(); $courts = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($courtsIds); $courtsLst = array(); $allowedHours = array(); $i=0; foreach ($courts as $court) { $courtsLst[$court->nid->value] = $court->title->value; $allowedHours["'".$court->nid->value."'"] = $this->makeAllowedHours($court); } $query2 = \Drupal::entityQuery('node'); $query2->condition('status', 1); $query2->condition('type', 'reservations'); $query2->condition('field_reservation_time', time(), '>'); $reservationIds = $query2->execute(); $reservations = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($reservationIds); $reservationsLst = array(); // dpm($reservation->field_reservation_time->value); foreach ($reservations as $reservation) { $court = $reservation->field_court->target_id; // $reservationsLst[date("j.m.Y", $reservation->field_reservation_time->value)][] = date("H:i", $reservation->field_reservation_time->value); $reservationsLst[$court][date("d.m.Y", strtotime($reservation->field_reservation_time->value))][] = date("H:i", strtotime($reservation->field_reservation_time->value)); } $reservationArr = array(); foreach ($reservationsLst as $court => $reservations) { $court_reservations = array(); foreach ($reservations as $key => $value) { $court_reservations["'".$key."'"] = $value; } foreach ($court_reservations as $date => $arr) { // dpm($arr); $reservationArr["'".$court."'"][$date] = "['" . implode("','", $arr). "']"; } // echo'reservations["' . $key . '"]=["' . implode('","', $value) . '"];'; } //dpm($reservationArr); $form['message'] = array( '#markup' => $this->t('Make your reservation.'), ); $form['add'] = array( '#type' => 'fieldset', '#title' => t('Your information'), ); $form['add']['pname'] = array( '#type' => 'textfield', '#title' => t('Your name') . ":", '#size' => 30, '#required' => TRUE ); $form['add']['pemail'] = array( '#type' => 'email', '#title' => t('E-mail') . ":", '#size' => 15, '#required' => TRUE ); $form['add']['phone'] = array( '#type' => 'tel', '#title' => t('Your phone') . ":", '#size' => 15, '#required' => TRUE // '#description' => t("Values greater than 127 will cause an exception. Try it - it's a great example why exception handling is needed with DTBNG."), ); $form['court'] = array( '#type' => 'select', '#title' => t('Court') . ":", '#options' => $courtsLst, '#required' => TRUE, '#attributes' => array('id' => 'court'), // '#options' => [ // '1' => $this->t('One'), // '2' => $this->t('Two'), // //'2' => [ // //'2.1' => $this->t('Two point one'), // //'2.2' => $this->t('Two point two'), // //], // '3' => $this->t('Three'), // ] //'#size' => 15, //'#attributes' => array('id'=>'datetimepicker', 'class' => array('datetimepicker')), ); $form['reservation_time'] = array( '#type' => 'textfield', '#title' => t('Reserve on') . ":", '#size' => 15, '#attributes' => array('id' => 'datetimepicker', 'class' => array('datetimepicker')), '#required' => TRUE ); $form['note'] = array( '#type' => 'textarea', '#title' => t('Note') . ":", '#rows' => 5, '#cols' => 80, '#resizable' => 'none', '#description' => t("You can leave your note here."), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Add'), ); $form['#attached']['library'][] = 'reservations/reservation_add'; $form['#attached']['drupalSettings']['reservations']['reservation_add']['reservations'] = $reservationArr; $form['#attached']['drupalSettings']['reservations']['reservation_add']['allowedHours'] = $allowedHours; return $form; } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { // Confirm that age is numeric. // if (empty($form_state->getValue('pname'))) { // $form_state->setErrorByName('pname', $this->t('Please fill your name.')); // } if (empty($form_state->getValue('pemail'))) { $form_state->setErrorByName('pemail', $this->t('Please fill your e-mail.')); } if (empty($form_state->getValue('phone'))) { $form_state->setErrorByName('phone', $this->t('Please fill your phone.')); } if (empty($form_state->getValue('reservation_time'))) { $form_state->setErrorByName('reservation_time', $this->t('Please fill the time for your reservation.')); } if (!intval($form_state->getValue('court')) || $form_state->getValue('court') <= 0) { $form_state->setErrorByName('court', $this->t('Please select court.')); } } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Gather the current user so the new record has ownership. // $account = $this->currentUser(); // // Save the submitted entry. // $entry = array( // 'name' => $form_state->getValue('name'), // 'surname' => $form_state->getValue('surname'), // 'age' => $form_state->getValue('age'), // 'uid' => $account->id(), // ); // $return = DbtngExampleStorage::insert($entry); // if ($return) { // drupal_set_message(t('Created entry @entry', array('@entry' => print_r($entry, TRUE)))); // } //Get vlaues from the form $pname = $form_state->getValue('pname'); $pemail = $form_state->getValue('pemail'); $phone = $form_state->getValue('phone'); $court = $form_state->getValue('court'); $reservation_time = $form_state->getValue('reservation_time'); $note = $form_state->getValue('note'); // $user = $this->currentUser(); // drupal_set_message(print_r($courts)); //Create an Entity. $entry = entity_create('node', array('type' => 'reservations')); //Specify the author. $entry->uid = \Drupal::currentUser()->id(); // Perform some kind of data manipulation $entry->title->value = $pname; $entry->field_person_name->value = $pname; $entry->field_person_phone->value = $phone; $entry->field_person_e_mail->value = $pemail; $entry->field_court->target_id = $court; // $entry->field_reservation_time->value = \Drupal::service('date.formatter')->format(time(), 'html_datetime'); $entry->field_reservation_time->value = date(DATETIME_DATETIME_STORAGE_FORMAT, strtotime($reservation_time)); $entry->field_note->value = $note; $entry->save(); drupal_set_message(t('Your reservation has been created.')); drupal_set_message(t('We will see you on @date at @hour', array( '@date' => date('d.m.Y', strtotime($reservation_time)), '@hour' => date('H:i', strtotime($reservation_time)) ) ) ); } private function makeAllowedHours($court=null){ if(!$court){ return ""; } $allowedHours = array(); $workingHours[0] = array($court->field_sunday_from->value, $court->field_sunday_to->value); $workingHours[1] = array($court->field_monday_from->value, $court->field_monday_to->value); $workingHours[2] = array($court->field_tuesday_from->value, $court->field_tuesday_to->value); $workingHours[3] = array($court->field_wednesday_from->value, $court->field_wednesday_to->value); $workingHours[4] = array($court->field_thursday_from->value, $court->field_thursday_to->value); $workingHours[5] = array($court->field_friday_from->value, $court->field_friday_to->value); $workingHours[6] = array($court->field_saturday_from->value, $court->field_saturday_to->value); foreach ($workingHours as $key => $value) { $start= explode(':', $value[0]); $start= (int)$start[0]; $end= explode(':', $value[1]); $end= (int)$end[0]; $allowedHoursTmp = array(); for($i=$start;$i<=$end;$i++){ $allowedHoursTmp[]=str_pad($i, 2, '0', STR_PAD_LEFT).":00"; } $allowedHours["'".$key."'"] = implode(",", $allowedHoursTmp); } // dpm($allowedHours); return $allowedHours; } }