/**
  * Updates an existing VideoPost model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $image = $model->getAsset();
     $tags = $model->getTags();
     $assets = $model->getAssets();
     $model->tags = [];
     foreach ($tags as $tag) {
         $model->tags[] = $tag->id;
     }
     $videoAsset = $model->getVideoAsset();
     $relation = Relation::find()->where(['relationable_id' => $model->id, 'relationable_type' => Relation::RELATIONABLE_VIDEO])->one();
     $matchModel = new \common\models\MatchSearch();
     $matchesList = [];
     if (!isset($relation)) {
         $relation = new Relation();
         $relation->relationable_type = Relation::RELATIONABLE_VIDEO;
     }
     if (!isset($relation->match)) {
         $matches = $matchModel::find()->orderBy(['date' => SORT_DESC])->limit(10)->all();
         foreach ($matches as $match) {
             $matchDate = date('d.m.Y', strtotime($match->date));
             $matchesList[$match->id] = $match->name . ' (' . $matchDate . ')';
         }
     } else {
         $matchModel->championship_id = $relation->match->championship_id;
         $matchModel->league_id = $relation->match->league_id;
         $matchModel->season_id = $relation->match->season_id;
         $matchModel->command_home_id = $relation->match->command_home_id;
         $matchModel->command_guest_id = $relation->match->command_guest_id;
         $matchDate = date('d.m.Y', strtotime($relation->match->date));
         $matchesList[$relation->match->id] = $relation->match->name . ' (' . $matchDate . ')';
     }
     $model->title = html_entity_decode($model->title);
     $model->content = html_entity_decode($model->content);
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         // Set slug
         $model->slug = $model->genSlug($model->title);
         // Set image
         $uploadedFile = UploadedFile::getInstance($model, 'image');
         if ($uploadedFile) {
             // Remove old assets
             foreach ($assets as $asset) {
                 $asset->delete();
             }
             // Save origionals
             $asset = new Asset();
             $asset->assetable_type = Asset::ASSETABLE_VIDEO;
             $asset->assetable_id = $model->id;
             $asset->uploadedFile = $uploadedFile;
             $asset->saveAsset();
             // Save thumbnails
             $imageID = $asset->id;
             $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_VIDEO);
             foreach ($thumbnails as $thumbnail) {
                 $asset = new Asset();
                 $asset->parent_id = $imageID;
                 $asset->thumbnail = $thumbnail;
                 $asset->assetable_type = Asset::ASSETABLE_VIDEO;
                 $asset->assetable_id = $model->id;
                 $asset->uploadedFile = $uploadedFile;
                 $asset->saveAsset();
             }
         }
         // Save videofile
         $videoFile = UploadedFile::getInstance($model, 'video');
         if ($videoFile) {
             // Remove old assets
             $videoAsset->delete();
             // Save origionals
             $asset = new Asset();
             $asset->assetable_type = Asset::ASSETABLE_VIDEOFILE;
             $asset->assetable_id = $model->id;
             $asset->uploadedFile = $videoFile;
             $asset->saveVideoAsset();
         }
         $existingTags = [];
         // Remove tags
         foreach ($tags as $tag) {
             if (!is_array($model->tags) || !in_array($tag->id, $model->tags)) {
                 $model->removeTag($tag->id);
             } else {
                 $existingTags[] = $tag->id;
             }
         }
         // Adding new tags
         if (is_array($model->tags)) {
             foreach ($model->tags as $id) {
                 if (!in_array($id, $existingTags)) {
                     $model->addTag($id);
                 }
             }
         }
         $cached_tag_list = [];
         $newTags = $model->getTags();
         foreach ($newTags as $newTag) {
             $cached_tag_list[] = $newTag->name;
         }
         $model->cached_tag_list = implode(', ', $cached_tag_list);
         if (!isset($relation->relationable_id)) {
             $relation->relationable_id = $model->id;
             $relation->relationable_type = Relation::RELATIONABLE_VIDEO;
         }
         if ($relation->load(Yii::$app->request->post()) && $model->validate()) {
             if ($relation->parent_id != '' && is_array($relation->parent_id)) {
                 $relation->parent_id = $relation->parent_id[0];
             }
             if ($relation->parent_id && is_numeric($relation->parent_id)) {
                 $relation->save();
             } elseif (isset($relation->id)) {
                 $relation->delete();
             }
         }
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'image' => $image, 'videoAsset' => $videoAsset, 'tags' => $tags, 'relation' => $relation, 'matchModel' => $matchModel, 'matchesList' => $matchesList]);
     }
 }