OphTrOperationbooking_Operation_Sequence::beforeValidate PHP Method

beforeValidate() protected method

protected beforeValidate ( )
    protected function beforeValidate()
    {
        if ($this->start_date && !preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $this->start_date)) {
            $this->start_date = date('Y-m-d', strtotime($this->start_date));
        }
        if ($this->end_date && !preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $this->end_date)) {
            $this->end_date = date('Y-m-d', strtotime($this->end_date));
        }
        // Verify that this session doesn't conflict with any other sequences or sessions
        $criteria = new CDbCriteria();
        if ($this->id) {
            $criteria->addCondition('id <> :id');
            $criteria->params[':id'] = $this->id;
        }
        $criteria->addCondition('theatre_id = :theatre_id');
        $criteria->params[':theatre_id'] = $this->theatre_id;
        $criteria->addCondition('weekday = :weekday');
        $criteria->params[':weekday'] = $this->weekday;
        $criteria->addCondition('end_date is null or end_date >= :start_date');
        $criteria->params[':start_date'] = $this->start_date;
        $dateList = $this->getDateListForMonths(12);
        $conflicts = array();
        foreach (self::model()->findAll($criteria) as $sequence) {
            $s_dateList = $sequence->getDateListForMonths(12);
            foreach ($s_dateList as $date) {
                if (in_array($date, $dateList)) {
                    $start = strtotime("{$date} {$this->start_time}");
                    $end = strtotime("{$date} {$this->end_time}");
                    $s_start = strtotime("{$date} {$sequence->start_time}");
                    $s_end = strtotime("{$date} {$sequence->end_time}");
                    if ($start < $s_end && $start >= $s_start) {
                        if (!isset($conflicts[$sequence->id]['start_time'])) {
                            $this->addError('start_time', "This start time conflicts with sequence {$sequence->id}");
                            $conflicts[$sequence->id]['start_time'] = 1;
                        }
                    }
                    if ($end > $s_start && $end <= $s_end) {
                        if (!isset($conflicts[$sequence->id]['end_time'])) {
                            $this->addError('end_time', "This end time conflicts with sequence {$sequence->id}");
                            $conflicts[$sequence->id]['end_time'] = 1;
                        }
                    }
                    if ($start < $s_start && $end > $s_end) {
                        if (!isset($conflicts[$sequence->id]['end_time']) || !isset($conflicts[$sequence->id]['start_time'])) {
                            $this->addError('start_time', "This start time conflicts with sequence {$sequence->id}");
                            $conflicts[$sequence->id]['start_time'] = 1;
                            $this->addError('end_time', "This end time conflicts with sequence {$sequence->id}");
                            $conflicts[$sequence->id]['end_time'] = 1;
                        }
                    }
                }
            }
        }
        $criteria = new CDbCriteria();
        $criteria->addCondition('sequence_id <> :sequence_id or sequence_id is null');
        $criteria->params[':sequence_id'] = $this->id;
        $criteria->addCondition('theatre_id = :theatre_id');
        $criteria->params[':theatre_id'] = $this->theatre_id;
        $criteria->addInCondition('date', $dateList);
        $conflicts = array();
        foreach (OphTrOperationbooking_Operation_Session::model()->findAll($criteria) as $session) {
            $start = strtotime("{$session->date} {$this->start_time}");
            $end = strtotime("{$session->date} {$this->end_time}");
            $s_start = strtotime("{$session->date} {$session->start_time}");
            $s_end = strtotime("{$session->date} {$session->end_time}");
            if ($start < $s_end && $start >= $s_start) {
                if (!isset($conflicts[$session->id]['start_time'])) {
                    $this->addError('start_time', "This start time conflicts with session {$session->id}");
                    $conflicts[$session->id]['start_time'] = 1;
                }
            }
            if ($end > $s_start && $end <= $s_end) {
                if (!isset($conflicts[$session->id]['end_time'])) {
                    $this->addError('end_time', "This end time conflicts with session {$session->id}");
                    $conflicts[$session->id]['end_time'] = 1;
                }
            }
            if ($start < $s_start && $end > $s_end) {
                if (!isset($conflicts[$session->id]['end_time']) || !isset($conflicts[$session->id]['start_time'])) {
                    $this->addError('start_time', "This start time conflicts with session {$session->id}");
                    $conflicts[$session->id]['start_time'] = 1;
                    $this->addError('end_time', "This end time conflicts with session {$session->id}");
                    $conflicts[$session->id]['end_time'] = 1;
                }
            }
        }
        return parent::beforeValidate();
    }