/**
  * Creates a new Artist model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Artist();
     $photos = [];
     if (Yii::$app->request->isPost) {
         for ($i = 0; $i < count(Yii::$app->request->post('Photo', [])); $i++) {
             $photos[] = new Photo();
         }
         $data = Yii::$app->request->post();
         $data['Artist']['user_id'] = Yii::$app->user->id;
     } else {
         $data = null;
     }
     if ($model->load($data) && $model->save()) {
         Photo::loadMultiple($photos, Yii::$app->request->post());
         foreach ($photos as $photo) {
             $photo->artist_id = $model->id;
         }
         if (Photo::validateMultiple($photos)) {
             foreach ($photos as $photo) {
                 $model->link('photos', $photo);
             }
         }
         $tags = Yii::$app->request->post('Artist', '');
         $tags = Tag::find()->where(['in', 'id', explode(',', $tags['tags'])])->all();
         foreach ($tags as $tag) {
             $model->link('tags', $tag);
         }
         return $this->redirect(['release/index', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'photos' => $photos]);
     }
 }
 public function store(Request $request)
 {
     $validation = Validator::make($request->all(), ['artist' => 'required|unique:artists,artist_name']);
     if ($validation->fails()) {
         return redirect('artists/new')->withInput()->withErrors($validation);
     }
     //        DB::table('artists')->insert([
     //            'artist_name' => $request->input('artist')
     //        ]);
     $artist = new Artist(['artist_name' => $request->input('artist')]);
     $artist->save();
     return redirect('artists/new')->with('success', true);
 }
 /**
  * Creates a new Release model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($artistId = null)
 {
     $release = new Release();
     if ($artistId) {
         $release->artist_id = $artistId;
     }
     $covers = [];
     $tracks = [];
     $artist_name = $artistId ? $release->artist->original_name : '';
     if (Yii::$app->request->isPost) {
         for ($i = 0; $i < count(Yii::$app->request->post('Cover', [])); $i++) {
             $covers[] = new Cover();
         }
         for ($i = 0; $i < count(Yii::$app->request->post('Track', [])); $i++) {
             $tracks[] = new Track();
         }
         $release->load(Yii::$app->request->post());
         $is_release_valid = $release->validate();
         if (!$release->artist_id) {
             $artist_name = Yii::$app->request->post()['artist_name'];
             $artist = new Artist(['original_name' => $artist_name, 'user_id' => Yii::$app->user->id]);
             $is_artist_valid = $artist->validate();
         } else {
             $is_artist_valid = true;
         }
         Cover::loadMultiple($covers, Yii::$app->request->post());
         $is_covers_valid = Cover::validateMultiple($covers);
         Track::loadMultiple($tracks, Yii::$app->request->post());
         $is_tracks_valid = Track::validateMultiple($tracks);
         if ($is_release_valid && $is_artist_valid && $is_covers_valid && $is_tracks_valid) {
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 if (isset($artist)) {
                     $artist->save(false);
                     $release->artist_id = $artist->id;
                 }
                 $release->save(false);
                 foreach ($covers as $cover) {
                     $release->link('covers', $cover);
                 }
                 foreach ($tracks as $track) {
                     $release->link('tracks', $track);
                 }
                 $tags = Yii::$app->request->post('Release', '');
                 $tags = Tag::find()->where(['in', 'id', explode(',', $tags['tags'])])->all();
                 foreach ($tags as $tag) {
                     $release->link('tags', $tag);
                 }
                 $transaction->commit();
                 return $this->redirect(['view', 'id' => $release->id]);
             } catch (\Exception $e) {
                 $transaction->rollBack();
                 throw $e;
             }
         }
     }
     return $this->render('create', ['release' => $release, 'covers' => $covers, 'tracks' => $tracks, 'trackProto' => new Track(), 'artist_name' => $artist_name]);
 }