Пример #1
0
 public function actionView()
 {
     $this->layout = 'hopam_detail';
     $hopam = Song::model()->findByPk($_GET['id']);
     $hopam->view += 1;
     $hopam->update(array('view'));
     //$comment = $this->newComment($hopam);
     $cs = Yii::app()->getClientScript();
     $js = $this->generateJs();
     $cs->registerScript('sharebox', $js, CClientScript::POS_READY);
     $this->render('view', array('hopam' => $hopam));
     //,'comment'=>$comment
 }
Пример #2
0
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = Song::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
Пример #3
0
 public function actionEditmusic($id)
 {
     $songModel = Song::model();
     $songModel = $songModel->findByPk($id);
     if (isset($_POST['Song'])) {
         $songModel->attributes = $_POST['Song'];
         if ($songModel->save()) {
             Yii::app()->user->setFlash('editmusicstatus', '修改音乐成功 :)');
         } else {
             Yii::app()->user->setFlash('editmusicstatus', '系统错误,修改失败 :(');
         }
     }
     $data = array('songModel' => $songModel);
     $this->render('editmusic', $data);
 }
Пример #4
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));
 }