public function actionUpload() { Yii::$app->response->format = Response::FORMAT_JSON; $result = []; $files = UploadedFile::getInstances(new File(), 'uploadedFiles'); foreach ($files as $index => $file) { $model = new File(); $model->uploadedFiles = [$file]; $model->user_id = Yii::$app->user->id; $model->storage = File::STORAGE_OS; $model->extension = $file->extension; $model->type = File::defineType($model->extension); if ($model->upload()) { $item = ['id' => $model->id, 'url' => $model->getUrl(), 'type' => $model->getMimeType()]; if ($releaseId = Yii::$app->request->get('rid')) { $release = Release::findOne($releaseId); if ($release && $release->artist->user_id == Yii::$app->user->id) { if ($model->type == $model::TYPE_IMAGE) { $cover = new Cover(['release_id' => $release->id, 'file_id' => $model->id, 'is_main' => false]); $cover->save(); $item['image_id'] = $cover->id; } elseif ($model->type == $model::TYPE_AUDIO) { $track = new Track(['release_id' => $release->id, 'original_name' => $file->baseName, 'number' => $release->getTracks()->count() + 1, 'file_id' => $model->id]); $track->save(); $item['track_id'] = $track->id; $item['comname'] = $track->getComname(); $item['number'] = $track->number; } } } if ($artistId = Yii::$app->request->get('aid')) { $photo = new Photo(['artist_id' => (string) $artistId, 'file_id' => $model->id, 'is_main' => false]); $artist = Artist::findOne($artistId); if ($artist && $artist->user_id == Yii::$app->user->id) { $photo->save(); $item['image_id'] = $photo->id; } } // if ($model->type == $model::TYPE_AUDIO) { // $getID3 = new getID3(); // $tags = $getID3->analyze($model->getPath(true))['tags']['id3v2']; // $item['meta'] = [ // 'title' => $tags['title'][0], // 'number' => explode('/', $tags['track_number'][0])[0], // 'disc' => explode('/', $tags['part_of_a_set'][0])[0], // 'lyric' => $tags['unsynchronised_lyric'][0], // 'info' => $tags['comment'][0], // ]; // } if ($model->type == $model::TYPE_AUDIO) { $item['meta'] = ['title' => $file->baseName, 'number' => $index + 1, 'disc' => '', 'lyric' => '', 'info' => '']; } $result[] = $item; } } return $result; }
/** * 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]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Cover::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'release_id' => $this->release_id, 'file_id' => $this->file_id, 'is_main' => $this->is_main]); return $dataProvider; }
Event::on(Cover::className(), Cover::EVENT_BEFORE_INSERT, function ($event) { // если это первый кавер, то делаем его основным, // иначе, если этот кавер должен стать основным, сбрасываем is_main у всех предыдущих каверов $cover = $event->sender; if (!$cover->release->getCovers()->count()) { $cover->is_main = true; } elseif ($cover->is_main) { Cover::resetIsMain($cover->release_id); } }); Event::on(Cover::className(), Cover::EVENT_BEFORE_UPDATE, function ($event) { // если этот кавер должен стать основным, сбрасываем is_main у всех предыдущих каверов $cover = $event->sender; if ($cover->is_main) { Cover::resetIsMain($cover->release_id); } }); Event::on(Cover::className(), Cover::EVENT_BEFORE_DELETE, function ($event) { // если удаляется основной кавер, вместо него основным становится следующий по очереди $cover = $event->sender; if ($cover->is_main) { $newMainCover = $cover->release->getCovers()->andWhere(['is_main' => 0])->orderBy('id')->limit(1)->one(); if ($newMainCover) { $newMainCover->is_main = 1; $newMainCover->save(); } } }); Event::on(Cover::className(), Cover::EVENT_BEFORE_DELETE, function ($event) { $event->sender->file->delete(); });
/** * Finds the Cover model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Cover the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Cover::findOne($id)) !== null) { if ($model->file->user_id != Yii::$app->user->id) { throw new \yii\web\ForbiddenHttpException(); } return $model; } else { throw new \yii\web\NotFoundHttpException('The requested page does not exist.'); } }
public function actionCover() { $model = Cover::findOne(Yii::$app->request->post('pk', '')); $ownerId = $model->file->user_id; return self::update($model, $ownerId); }
/** * @return \yii\db\ActiveQuery */ public function getCovers() { return $this->hasMany(Cover::className(), ['release_id' => 'id']); }