Exemplo n.º 1
0
 /**
  * Creates a new Album model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Album();
     $model->tags = [];
     // default values
     $model->is_public = 1;
     $model->user_id = Yii::$app->user->id;
     $matchModel = new \common\models\MatchSearch();
     $relation = new Relation();
     $relation->relationable_type = Relation::RELATIONABLE_ALBUM;
     $matches = $matchModel::find()->orderBy(['date' => SORT_DESC])->limit(10)->all();
     $matchesList = [];
     foreach ($matches as $match) {
         $matchDate = date('d.m.Y', strtotime($match->date));
         $matchesList[$match->id] = $match->name . ' (' . $matchDate . ')';
     }
     if ($model->load(Yii::$app->request->post())) {
         // Set slug
         $model->slug = $model->genSlug($model->title);
         // Save the model to have a record number
         if (!$model->save()) {
             return $this->render('create', ['model' => $model]);
         }
         // Adding new tags
         if (is_array($model->tags)) {
             foreach ($model->tags as $id) {
                 $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);
         // Save cover image
         $uploadedFile = UploadedFile::getInstance($model, 'coverImage');
         if (!empty($uploadedFile)) {
             // Save origionals
             $originalAsset = new Asset();
             $originalAsset->assetable_type = Asset::ASSETABLE_ALBUM_COVER;
             $originalAsset->assetable_id = $model->id;
             $originalAsset->uploadedFile = $uploadedFile;
             $originalAsset->saveAsset();
             // Save thumbnails
             $imageID = $originalAsset->id;
             $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_ALBUM_COVER);
             foreach ($thumbnails as $thumbnail) {
                 $asset = new Asset();
                 $asset->assetable_type = Asset::ASSETABLE_ALBUM_COVER;
                 $asset->assetable_id = $model->id;
                 $asset->parent_id = $imageID;
                 $asset->thumbnail = $thumbnail;
                 $asset->uploadedFile = $uploadedFile;
                 $asset->saveAsset();
             }
         }
         // Save images
         $model->images = UploadedFile::getInstances($model, 'images');
         if ($model->images) {
             foreach ($model->images as $image) {
                 // Save origionals
                 $asset = new Asset();
                 $asset->assetable_type = Asset::ASSETABLE_ALBUM;
                 $asset->assetable_id = $model->id;
                 $asset->uploadedFile = $image;
                 $asset->saveAsset();
                 // Save thumbnails
                 $imageID = $asset->id;
                 $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_ALBUM);
                 foreach ($thumbnails as $thumbnail) {
                     $asset = new Asset();
                     $asset->parent_id = $imageID;
                     $asset->thumbnail = $thumbnail;
                     $asset->assetable_type = Asset::ASSETABLE_ALBUM;
                     $asset->assetable_id = $model->id;
                     $asset->uploadedFile = $image;
                     $asset->saveAsset();
                 }
             }
         }
         $relation->relationable_id = $model->id;
         $relation->relationable_type = Relation::RELATIONABLE_ALBUM;
         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();
             }
         }
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'relation' => $relation, 'matchModel' => $matchModel, 'matchesList' => $matchesList]);
     }
 }