'Link worktime from one form to another

I have a model in which we can choose the opening hours of the institution for each day of the week, from such and such to such and such, for example Monday 12:00 AM - 11:30 PM

<?php

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;

class WorkHours extends _source_WorkHours
{
    public const WEEK_DAY_MON = 'Mon';
    public const WEEK_DAY_TUE = 'Tue';
    public const WEEK_DAY_WED = 'Wed';
    public const WEEK_DAY_THU = 'Thu';
    public const WEEK_DAY_FRI = 'Fri';
    public const WEEK_DAY_SAT = 'Sat';
    public const WEEK_DAY_SUN = 'Sun';


    /**
     * {@inheritdoc}
     */
    public function behaviors(): array
    {
        return [
            'timestamp' => [
                'class'      => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
                'value'      => new Expression('NOW()'),
            ],
        ];
    }

    public static function weekDays(): array
    {
        return [
            self::WEEK_DAY_MON => 'Monday',
            self::WEEK_DAY_TUE => 'Tuesday',
            self::WEEK_DAY_WED => 'Wednesday',
            self::WEEK_DAY_THU => 'Thursday',
            self::WEEK_DAY_FRI => 'Friday',
            self::WEEK_DAY_SAT => 'Saturday',
            self::WEEK_DAY_SUN => 'Sunday',
        ];
    }

    public static function getWeekDay(string $val): string
    {
        $ar = self::weekDays();

        return $ar[$val] ?? $val;
    }

    public static function hoursList(): array
    {
        $list = [];
        for ($i = 0; $i < 24; $i++) {
            $A = 'AM';
            $n = $i;
            if ($i >= 12) {
                $A = 'PM';
                $n = $i - 12;
            }

            $n = $n < 10 ? '0' . $n : $n;
            $_A = ($i === 12 ? 'AM' : $A);
            $list[$i . '.00'] = $n . '.00 ' . ($i === 0 ? 'PM' : $_A);
            $list[$i . '.30'] = $n . '.30 ' . $A;
        }
        return $list;
    }
}

Next, I use this model when creating an institution, and the ability to choose the opening hours there

<div class="form-group">
        <label class="control-label col-sm-3">Wokr Hours</label>
        <div class="col-sm-6">

            <div class="form-group">
                <?php foreach (WorkHours::weekDays() as $weekDay => $day) { ?>
                    <div class="col-sm-2"><?= $weekDay ?></div>
                    <div class="col-sm-5">
                        <?= $form->field(
                            $restaurantWorkHoursForm,
                            RestaurantWorkHoursForm::getAttributeName($weekDay, 'open'),
                            ['template' => '<div class="col-sm-12">{input}</div>']
                        )->dropDownList(WorkHours::hoursList(), ['prompt' => ''])->label(false) ?>
                    </div>
                    <div class="col-sm-5">
                        <?= $form->field(
                            $restaurantWorkHoursForm,
                            RestaurantWorkHoursForm::getAttributeName($weekDay, 'close'),
                            ['template' => '<div class="col-sm-12">{input}</div>']
                        )->dropDownList(WorkHours::hoursList(), ['prompt' => ''])->label(false) ?></div>
                <?php } ?>
            </div>
        </div>
    </div>

I also use this model when booking a place in an institution where you can choose the time, but here you can choose any time of the day

<a class="btn btn-fourth <?= $restaurantReservationForm->getErrors('time') ? 'btn-error' : '' ?>"
               id="reservation-time" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                <span class="icon br-clock"></span> <span class="js-value">
                    <?= $restaurantReservationForm->time
                        ? WorkHours::hoursList()[$restaurantReservationForm->time] : '-- : --' ?>
                </span>
            </a>
            <ul class="dropdown-menu dropdown-menu-height-fixed" aria-labelledby="reservation-time">
                <?php foreach (WorkHours::hoursList() as $k => $v) { ?>
                    <li><a class="dropdown-item <?= $restaurantReservationForm->time === $k ? 'active' : ''
                        ?>" href="#" data-value="<?= $k ?>"><?= $v ?></a></li>
                <?php } ?>
            </ul>

Now what is the question. I have two forms. In one, the time was indicated, in the other there should be a choice of time from the first form. How can you link the opening hours of the establishment that we indicated with the choice of the time of booking?

Let's say we have a working time 12:00 AM - 11:30 PM, any time is available in the booking, but it is necessary that there is a choice only within the opening hours of the institution that we specified earlier, that is, from 12:00 AM to 11:30 PM, and so on every day, if the time is not selected in the institution for some day , then the booking will also be empty

Is there a way to do this via json ?



Solution 1:[1]

This is solvable of course, but you will need to handle it both on the server-side and client-side. The need on the server-side is that you ensure that there is no shenanigans done by a technically advanced user and on the client-side your need is to help the user know from which period he or she can choose the value.

On the server-side you have several choices in terms of storage, but a feasible approach (assuming that the chosen interval will be reused in the future as well) is to store this value into the database. That would mean that you will need a CRUD for this value on server-side and an API function that serves client-side requests, telling the user what his/her choice was. On the server-side you can encode stuff into JSON via json_encode and send the result to the client.

You will need a validator both on the server-side and the client-side in order to determine whether a given date is inside the specified interval.

In short, you need:

  • Database schema support for this interval
  • Application server-side CRUD
  • Server-side API
  • Client-side functionality that handles the API request
  • Validator both on server and client-side
  • Handling the logic on the UI rendering and handling using the validator
  • Handling the logic when the chosen date or date interval is being stored

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lajos Arpad