/**
  * Finds the Groups model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Groups the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Groups::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Creates a new Lessons model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Lessons();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $group_info = Groups::findOne(["group_id" => $model->id_group]);
         function lesson_insert($id_group, $is_numerator)
         {
             $m = new Lessons();
             $m->is_holiday = $model->is_holiday;
             $m->all_group = $model->all_group;
             $m->id_discipline = $model->id_discipline;
             $m->id_teacher = $model->id_teacher;
             $m->id_classroom = $model->id_classroom;
             $m->id_group = $id_group;
             $m->id_faculty = $model->id_faculty;
             $m->id_speciality = $model->id_speciality;
             $m->course = $model->course;
             $m->semester = $model->semester;
             $m->id_okr = $model->id_okr;
             $m->is_numerator = $is_numerator;
             $m->day = $model->day;
             $m->comment = $model->comment;
             $m->lesson_number = $model->lesson_number;
             $m->insert();
         }
         //Код для всего потока
         if ($model->all_speciality) {
             $sp = Speciality::findAll(['id_faculty' => $model->id_faculty]);
             //Узнаем факультет этой группы
             $gr = Groups::find()->where(['inflow_year' => date("Y") - $model->course])->all();
             //Выбираем все группы с таким же годом поступления
             foreach ($gr as $g) {
                 foreach ($sp as $s) {
                     if ($s['speciality_id'] == $g['id_speciality']) {
                         //Если у группы и специальности совпадают ИД, это то, что нужно
                         $stream_groups[] = $g['group_id'];
                     }
                 }
             }
             //Сам код вставки
             /* 
                 Если выбрана галка "Числитель/знаменатель", единожды записываем данные для этой группы
             */
             if ($model->num_dem == 1) {
                 //Если выбрана галка "Числитель/знаменатель"
                 if ($model->is_numerator == 0) {
                     //Если ИД подгруппы совпадает - психуем и забиваем
                     $m = new Lessons();
                     $m->is_holiday = $model->is_holiday;
                     $m->all_group = $model->all_group;
                     $m->id_discipline = $model->id_discipline;
                     $m->id_teacher = $model->id_teacher;
                     $m->id_classroom = $model->id_classroom;
                     $m->id_group = $model->id_group;
                     $m->id_faculty = $model->id_faculty;
                     $m->id_speciality = $model->id_speciality;
                     $m->course = $model->course;
                     $m->semester = $model->semester;
                     $m->id_okr = $model->id_okr;
                     $m->is_numerator = 1;
                     $m->comment = $model->comment;
                     $m->day = $model->day;
                     $m->lesson_number = $model->lesson_number;
                     $m->insert();
                 } else {
                     //Иначе все по новой
                     $m = new Lessons();
                     $m->is_holiday = $model->is_holiday;
                     $m->all_group = $model->all_group;
                     $m->id_discipline = $model->id_discipline;
                     $m->id_teacher = $model->id_teacher;
                     $m->id_classroom = $model->id_classroom;
                     $m->id_group = $model->id_group;
                     $m->id_faculty = $model->id_faculty;
                     $m->id_speciality = $model->id_speciality;
                     $m->course = $model->course;
                     $m->semester = $model->semester;
                     $m->id_okr = $model->id_okr;
                     $m->is_numerator = 0;
                     $m->comment = $model->comment;
                     $m->day = $model->day;
                     $m->lesson_number = $model->lesson_number;
                     $m->insert();
                 }
                 foreach ($stream_groups as $sg) {
                     $is_group_for_subgroup = Groups::find()->where(['parent_group' => $sg])->all();
                     $group_speciality = Groups::find()->where(['group_id' => $sg])->select('id_speciality')->all();
                     if (!empty($is_group_for_subgroup) || $sg == $model->id_group) {
                         //Если у группы существуют подгруппы или это первоначальная группа - пропускаем
                         continue;
                     } else {
                         $m = new Lessons();
                         $m->is_holiday = $model->is_holiday;
                         $m->all_group = $model->all_group;
                         $m->id_discipline = $model->id_discipline;
                         $m->id_teacher = $model->id_teacher;
                         $m->id_classroom = $model->id_classroom;
                         $m->id_group = $sg;
                         $m->id_faculty = $model->id_faculty;
                         $m->id_speciality = $group_speciality[0]['id_speciality'];
                         $m->course = $model->course;
                         $m->semester = $model->semester;
                         $m->id_okr = $model->id_okr;
                         $m->is_numerator = 0;
                         $m->comment = $model->comment;
                         $m->day = $model->day;
                         $m->lesson_number = $model->lesson_number;
                         $m->insert();
                         $m = new Lessons();
                         $m->is_holiday = $model->is_holiday;
                         $m->all_group = $model->all_group;
                         $m->id_discipline = $model->id_discipline;
                         $m->id_teacher = $model->id_teacher;
                         $m->id_classroom = $model->id_classroom;
                         $m->id_group = $sg;
                         $m->id_faculty = $model->id_faculty;
                         $m->id_speciality = $group_speciality[0]['id_speciality'];
                         $m->course = $model->course;
                         $m->semester = $model->semester;
                         $m->id_okr = $model->id_okr;
                         $m->is_numerator = 1;
                         $m->comment = $model->comment;
                         $m->day = $model->day;
                         $m->lesson_number = $model->lesson_number;
                         $m->insert();
                     }
                 }
             } else {
                 //Если галка Числитель/знаменатель не выбрана
                 foreach ($stream_groups as $sg) {
                     $is_group_for_subgroup = Groups::find()->where(['parent_group' => $sg])->all();
                     $group_speciality = Groups::find()->where(['group_id' => $sg])->select('id_speciality')->all();
                     if (!empty($is_group_for_subgroup) || $sg == $model->id_group) {
                         //Если у группы существуют подгруппы или это первоначальная группа - пропускаем
                         continue;
                     } else {
                         $m = new Lessons();
                         $m->is_holiday = $model->is_holiday;
                         $m->all_group = $model->all_group;
                         $m->id_discipline = $model->id_discipline;
                         $m->id_teacher = $model->id_teacher;
                         $m->id_classroom = $model->id_classroom;
                         $m->id_group = $sg;
                         $m->id_faculty = $model->id_faculty;
                         $m->id_speciality = $group_speciality[0]['id_speciality'];
                         $m->course = $model->course;
                         $m->semester = $model->semester;
                         $m->id_okr = $model->id_okr;
                         $m->is_numerator = $model->is_numerator;
                         $m->comment = $model->comment;
                         $m->day = $model->day;
                         $m->lesson_number = $model->lesson_number;
                         $m->insert();
                     }
                 }
             }
             //Конец кода вставки
             //Конец кода для всего потока
         } else {
             //Если не выбрана галка "Для всего потока"
             if ($group_info['parent_group'] == 0) {
                 //Выясняем группа это или подгруппа
                 $is_group = true;
                 //Если это группа
                 if ($model->num_dem == 1) {
                     //Записываем если выбрана галочка Числитель/Знаменатель для 1 группы
                     $m = new Lessons();
                     if ($model->is_numerator == 1) {
                         $m->is_holiday = $model->is_holiday;
                         $m->all_group = $model->all_group;
                         $m->id_discipline = $model->id_discipline;
                         $m->id_teacher = $model->id_teacher;
                         $m->id_classroom = $model->id_classroom;
                         $m->id_group = $model->id_group;
                         $m->id_faculty = $model->id_faculty;
                         $m->id_speciality = $model->id_speciality;
                         $m->course = $model->course;
                         $m->semester = $model->semester;
                         $m->id_okr = $model->id_okr;
                         $m->is_numerator = 0;
                         $m->day = $model->day;
                         $m->comment = $model->comment;
                         $m->lesson_number = $model->lesson_number;
                         $m->insert();
                     } else {
                         $m->is_holiday = $model->is_holiday;
                         $m->all_group = $model->all_group;
                         $m->id_discipline = $model->id_discipline;
                         $m->id_teacher = $model->id_teacher;
                         $m->id_classroom = $model->id_classroom;
                         $m->id_group = $model->id_group;
                         $m->id_faculty = $model->id_faculty;
                         $m->id_speciality = $model->id_speciality;
                         $m->course = $model->course;
                         $m->semester = $model->semester;
                         $m->id_okr = $model->id_okr;
                         $m->is_numerator = 1;
                         $m->day = $model->day;
                         $m->comment = $model->comment;
                         $m->lesson_number = $model->lesson_number;
                         $m->insert();
                     }
                 }
             } else {
                 $is_group = false;
                 //Если это подгруппа
                 if ($model->all_group == 1) {
                     //Если выбрана галка для всей группы
                     $groups_arr = Groups::findAll(['parent_group' => $model->parent]);
                     if ($model->num_dem == 1) {
                         //Если выбрана галка числитель/знаменатель, сохраним это для этой подгруппы
                         $m = new Lessons();
                         if ($model->is_numerator == 1) {
                             $m->is_holiday = $model->is_holiday;
                             $m->all_group = $model->all_group;
                             $m->id_discipline = $model->id_discipline;
                             $m->id_teacher = $model->id_teacher;
                             $m->id_classroom = $model->id_classroom;
                             $m->id_group = $model->id_group;
                             $m->id_faculty = $model->id_faculty;
                             $m->id_speciality = $model->id_speciality;
                             $m->course = $model->course;
                             $m->semester = $model->semester;
                             $m->id_okr = $model->id_okr;
                             $m->is_numerator = 0;
                             $m->comment = $model->comment;
                             $m->day = $model->day;
                             $m->lesson_number = $model->lesson_number;
                             $m->insert();
                         } else {
                             $m->is_holiday = $model->is_holiday;
                             $m->all_group = $model->all_group;
                             $m->id_discipline = $model->id_discipline;
                             $m->id_teacher = $model->id_teacher;
                             $m->id_classroom = $model->id_classroom;
                             $m->id_group = $model->id_group;
                             $m->id_faculty = $model->id_faculty;
                             $m->id_speciality = $model->id_speciality;
                             $m->course = $model->course;
                             $m->semester = $model->semester;
                             $m->id_okr = $model->id_okr;
                             $m->is_numerator = 1;
                             $m->comment = $model->comment;
                             $m->day = $model->day;
                             $m->lesson_number = $model->lesson_number;
                             $m->insert();
                         }
                     }
                     foreach ($groups_arr as $ga) {
                         //Перебираем все группы
                         if ($ga['group_id'] == $model->id_group) {
                             //Если ИД подгруппы совпадает - психуем и забиваем
                             continue;
                         } else {
                             //Иначе все по новой
                             if ($model->num_dem == 1) {
                                 //Выбрана галочка Числитель/Знаменатель для всей группы
                                 $m = new Lessons();
                                 $m->is_holiday = $model->is_holiday;
                                 $m->all_group = $model->all_group;
                                 $m->id_discipline = $model->id_discipline;
                                 $m->id_teacher = $model->id_teacher;
                                 $m->id_classroom = $model->id_classroom;
                                 $m->id_group = $ga['group_id'];
                                 $m->id_faculty = $model->id_faculty;
                                 $m->id_speciality = $model->id_speciality;
                                 $m->course = $model->course;
                                 $m->semester = $model->semester;
                                 $m->id_okr = $model->id_okr;
                                 $m->is_numerator = 0;
                                 $m->comment = $model->comment;
                                 $m->day = $model->day;
                                 $m->lesson_number = $model->lesson_number;
                                 $m->insert();
                                 $m = new Lessons();
                                 $m->is_holiday = $model->is_holiday;
                                 $m->all_group = $model->all_group;
                                 $m->id_discipline = $model->id_discipline;
                                 $m->id_teacher = $model->id_teacher;
                                 $m->id_classroom = $model->id_classroom;
                                 $m->id_group = $ga['group_id'];
                                 $m->id_faculty = $model->id_faculty;
                                 $m->id_speciality = $model->id_speciality;
                                 $m->course = $model->course;
                                 $m->semester = $model->semester;
                                 $m->id_okr = $model->id_okr;
                                 $m->is_numerator = 1;
                                 $m->comment = $model->comment;
                                 $m->day = $model->day;
                                 $m->lesson_number = $model->lesson_number;
                                 $m->insert();
                             } else {
                                 $m = new Lessons();
                                 $m->is_holiday = $model->is_holiday;
                                 $m->all_group = $model->all_group;
                                 $m->id_discipline = $model->id_discipline;
                                 $m->id_teacher = $model->id_teacher;
                                 $m->id_classroom = $model->id_classroom;
                                 $m->id_group = $ga['group_id'];
                                 $m->id_faculty = $model->id_faculty;
                                 $m->id_speciality = $model->id_speciality;
                                 $m->course = $model->course;
                                 $m->semester = $model->semester;
                                 $m->id_okr = $model->id_okr;
                                 $m->comment = $model->comment;
                                 $m->is_numerator = $model->is_numerator;
                                 $m->day = $model->day;
                                 $m->lesson_number = $model->lesson_number;
                                 $m->insert();
                             }
                         }
                     }
                 } else {
                     if ($model->num_dem == 1) {
                         //Если выбрана галка числитель/знаменатель, сохраним это для этой подгруппы
                         $m = new Lessons();
                         if ($model->is_numerator == 1) {
                             $m->is_holiday = $model->is_holiday;
                             $m->all_group = $model->all_group;
                             $m->id_discipline = $model->id_discipline;
                             $m->id_teacher = $model->id_teacher;
                             $m->id_classroom = $model->id_classroom;
                             $m->id_group = $model->id_group;
                             $m->id_faculty = $model->id_faculty;
                             $m->id_speciality = $model->id_speciality;
                             $m->course = $model->course;
                             $m->semester = $model->semester;
                             $m->id_okr = $model->id_okr;
                             $m->is_numerator = 0;
                             $m->comment = $model->comment;
                             $m->day = $model->day;
                             $m->lesson_number = $model->lesson_number;
                             $m->insert();
                         } else {
                             $m->is_holiday = $model->is_holiday;
                             $m->all_group = $model->all_group;
                             $m->id_discipline = $model->id_discipline;
                             $m->id_teacher = $model->id_teacher;
                             $m->id_classroom = $model->id_classroom;
                             $m->id_group = $model->id_group;
                             $m->id_faculty = $model->id_faculty;
                             $m->id_speciality = $model->id_speciality;
                             $m->course = $model->course;
                             $m->semester = $model->semester;
                             $m->id_okr = $model->id_okr;
                             $m->is_numerator = 1;
                             $m->comment = $model->comment;
                             $m->day = $model->day;
                             $m->lesson_number = $model->lesson_number;
                             $m->insert();
                         }
                     }
                 }
             }
         }
         //Конец кода добавления
         if ($model->subgroup == 1) {
             $url = Url::to('index.php?r=timetable/lessons/editor&id' . $model->lesson_id . '&semester_for_editor=' . $model->semester . '&course_get=' . $model->course . '&faculty_id=' . $model->id_faculty . '&speciality_id=' . $model->id_speciality . '&group_id=' . $model->parent . '#lesson_id' . $model->lesson_id);
         } else {
             $url = Url::to('index.php?r=timetable/lessons/editor&id' . $model->lesson_id . '&semester_for_editor=' . $model->semester . '&course_get=' . $model->course . '&faculty_id=' . $model->id_faculty . '&speciality_id=' . $model->id_speciality . '&group_id=' . $model->id_group . '#lesson_id' . $model->lesson_id);
         }
         return $this->redirect($url);
     } else {
         return $this->renderAjax('create', ['model' => $model]);
     }
 }