| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @link http://www.writesdown.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (c) 2015 WritesDown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license http://www.writesdown.com/license/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace frontend\controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use common\models\Media; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use common\models\MediaComment as Comment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use yii\web\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yii\web\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class MediaController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Agiel K. Saputra <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class MediaController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param integer|null $id Media ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param string|null $slug Media slug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @throws \yii\web\NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function actionView($id = null, $slug = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $render = 'view'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $comment = new Comment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if ($id) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } elseif ($slug) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $model = $this->findModelBySlug($slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 44 |  | View Code Duplication |         if ($comment->load(Yii::$app->request->post()) && $comment->save()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             if (!$comment->parent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $model->comment_count++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             if ($model->save()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $this->refresh(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 |  | View Code Duplication |         if ($model->password && $model->password !== Yii::$app->request->post('password')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return $this->render('protected', ['media' => $model]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (is_file($this->view->theme->basePath . '/media/view-' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             . substr($model->mime_type, 0, strpos($model->mime_type, '/', 1)) . '.php') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $render = 'view-' . substr($model->mime_type, 0, strpos($model->mime_type, '/', 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return $this->render($render, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'media' => $model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'metadata' => $model->getMeta('metadata'), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'comment' => $comment, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Finds the Media model based on its primary key value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * If the model is not found, a 404 HTTP exception will be thrown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param integer $id Media ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return Media the loaded model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @throws NotFoundHttpException if the model cannot be found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     protected function findModel($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if (($model = Media::findOne($id)) !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Finds the Media model based on its primary key value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * If the model is not found, a 404 HTTP exception will be thrown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $slug Media slug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return Media the loaded model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @throws NotFoundHttpException if the model cannot be found | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 95 |  | View Code Duplication |     protected function findModelBySlug($slug) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         if (($model = Media::findOne(['slug' => $slug])) !== null) { | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             return $model; | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                        
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: