Esempio n. 1
0
 public function actionUpmusic()
 {
     $songModel = new Song();
     if (isset($_POST['Song'])) {
         $songModel->attributes = $_POST['Song'];
         $songModel->userid = Yii::app()->session['uid'];
         if ($songModel->validate()) {
             if (!empty($_FILES['songname']['tmp_name'])) {
                 $file = $_FILES['songname'];
                 if (!MusicTypeCheck($file['name'], $file['size'])) {
                     Yii::app()->user->setFlash('upstatus', 'Sorry, 音乐文件大小或格式错误 :(');
                     $this->redirect(array("Upmusic"));
                     die;
                 }
                 $type = "." . GetFileExtension($file['name']);
                 Yii::import('application.vendors.*');
                 require_once 'Qiniu/rs.php';
                 require_once 'Qiniu/io.php';
                 $bucket = Yii::app()->params['bucket'];
                 $accessKey = Yii::app()->params['accessKey'];
                 $secretKey = Yii::app()->params['secretKey'];
                 $newname = time() . rand(10000, 99999) . $type;
                 //先保存记录
                 $songModel->song = $newname;
                 if ($songModel->save()) {
                     /**
                      * 
                      */
                 } else {
                     Yii::app()->user->setFlash('upstatus', 'Sorry,系统错误,上传音乐失败 :(');
                 }
                 Qiniu_SetKeys($accessKey, $secretKey);
                 $putPolicy = new Qiniu_RS_PutPolicy($bucket);
                 $upToken = $putPolicy->Token(null);
                 list($ret, $err) = Qiniu_Put($upToken, $newname, file_get_contents($file['tmp_name']), null);
                 if ($err === null) {
                     //成功
                     /***
                      * 
                      */
                     $this->redirect(array('admin/Imusic'));
                 } else {
                     //失败
                     Yii::app()->user->setFlash('upstatus', 'Sorry,系统错误,上传音乐失败 :(');
                 }
             }
         }
     }
     $data = array('songModel' => $songModel);
     $this->render("upmusic", $data);
 }
Esempio n. 2
0
 public function actionUpdate($id)
 {
     /* @var Artist $model */
     $model = $this->loadModel($id);
     // check access
     if (!Yii::app()->user->checkAccess('edit artist')) {
         throw new CHttpException(401);
     }
     // does this artists exists in our DB?
     if ($model === null) {
         Yii::log("Artist update requested with id {$id} but no such artist found!", CLogger::LEVEL_INFO, __METHOD__);
         throw new CHttpException(404, Yii::t("MusicModule.general", 'The requested page does not exist.'));
     }
     // enable adding songs by default (will be changed below if needed to)
     $enable_add_more_songs = true;
     if (isset($_POST['Artist'])) {
         // start optimistically
         $all_songs_valid = true;
         $songs_to_save = array();
         $success_saving_all = true;
         $model->attributes = $_POST['Artist'];
         if (isset($_POST['Song'])) {
             if (count($_POST['Song']) > Song::MAX_SONGS_PER_ARTIST) {
                 /*
                  * server side protection against attempt to submit more than MAX_SONGS_PER_ARTIST
                  * this should be accompanied with a client side (JS) protection.
                  * If its accompanied with client side protection then going into this code block means our system
                  * is being abused/"tested". No need to give a polite error message.
                  */
                 throw new CHttpException(500, Yii::t("MusicModule.forms", "The max amount of allowed songs is {max_songs_num}", array('{max_songs_num}' => Song::MAX_SONGS_PER_ARTIST)));
             }
             foreach ($_POST['Song'] as $index => $submitted_song) {
                 // We could have empty songs which means empty submitted forms in POST. Ignore those:
                 if ($submitted_song['title'] == '') {
                     // empty one - skip it, if you please.
                     continue;
                 }
                 // next, validate each submitted song instance
                 if ((int) $submitted_song['id'] > 0) {
                     /* Validate that the submitted song belong to this artist */
                     $song = Song::model()->findByPk($submitted_song['id']);
                     if ($song->artist->id != $model->id) {
                         Yii::log("Attempts to update Song with an id of {$song->id} but it belongs to an Artist with an id of {$song->model->id}" . " and not 'this' artist with id = {$model->id}", CLogger::LEVEL_ERROR, __METHOD__);
                         throw new CHttpException(500, "Error occurred");
                     }
                 } else {
                     // this submitted song object is a new model. instantiate one:
                     $song = new Song();
                 }
                 $song->attributes = $submitted_song;
                 if (!$song->validate()) {
                     // at least one invalid song:
                     $all_songs_valid = false;
                     // we do not 'break' here since we want validation on all song at the same shot
                 } else {
                     // put aside the valid song to be saved
                     $songs_to_save[] = $song;
                 }
             }
             // while we know that songs were submitted, determine if to show 'adding songs' or no.
             // a check whether the max songs per artist was exceeded was performed already above.
             if (count($_POST['Song']) == Song::MAX_SONGS_PER_ARTIST) {
                 $enable_add_more_songs = false;
             }
         }
         if ($all_songs_valid && $model->validate()) {
             /* all songs (if any) were valid and artist object is valid too. Save it all in one transaction. Save first the
              * artist as we need its id for its songs records
              */
             $trans = Yii::app()->db->beginTransaction();
             try {
                 // use aux variable for manipulating the image file.
                 $image = CUploadedFile::getInstance($model, 'icon_filename');
                 // check if a new image was submitted or not:
                 if ($image) {
                     /* the only thing that might have changed in the update is the extension name of the image (if you support more than 'only jpeg').
                      * therefore, if something was submitted, and since we already know the ID of the artist (this is an update scenario), we can
                      * determine the full updated icon_filename attribute of the model prior to its save() (unlike in create action - see there...).
                      */
                     $model->icon_filename = $model->getImageFsFilename($image);
                 }
                 $model->save(false);
                 // save the updated image, if any
                 if ($image) {
                     $image->saveAs($model->getImageFsFilename($image));
                 }
                 // save songs
                 foreach ($songs_to_save as $song) {
                     $song->save(false);
                 }
                 $trans->commit();
             } catch (Exception $e) {
                 // oops, saving artist or its songs failed. rollback, report us, and show the user an error.
                 $trans->rollback();
                 Yii::log("Error occurred while saving (update scenario) artist or its 'songs'. Rolling back... . Failure reason as reported in exception: " . $e->getMessage(), CLogger::LEVEL_ERROR, __METHOD__);
                 Yii::app()->user->setFlash('error', Yii::t("MusicModule.forms", "Error occurred"));
                 $success_saving_all = false;
             }
             if ($success_saving_all) {
                 $success_msg = count($songs_to_save) > 0 ? "Artist and song records have been updated" : "Artist record have been updated";
                 Yii::app()->user->setFlash('success', Yii::t("MusicModule.forms", $success_msg));
                 $this->redirect(array("view", "id" => $model->id));
             }
         }
     } else {
         // initial rendering of update form. prepare songs for printing.
         // we put it in the same variable as used for saving (that's the reason for the awkward variable naming).
         $songs_to_save = $model->songs;
     }
     $this->render('update', array('artist' => $model, 'songs' => isset($songs_to_save) ? $songs_to_save : array(new Song('insert')), 'enable_add_more_songs' => $enable_add_more_songs));
 }