コード例 #1
0
 /**
  * Render remote images
  * Usage: echo Yii::$app->media->render('HTTP://URL_TO_IMAGE_HERE.JPG')
  * @param $imgSrc
  * @param array $data
  * @return bool|string
  */
 public function render($imgSrc, $data = [])
 {
     $media = new Media();
     $media->mimeType = 'image/jpeg';
     $media->filename = basename($imgSrc);
     if (strpos($imgSrc, 'files') !== false) {
         $media->source = ltrim($imgSrc);
     } else {
         $media->source = 'frontend/web' . $imgSrc;
     }
     return $media->render($data);
 }
コード例 #2
0
 /**
  * Relate Media
  * Usage: $model->media();
  * @param null $type
  * @return static
  */
 public function getMedia($type = null)
 {
     $media = $this->hasMany(Media::className(), ['id' => 'media_id']);
     if ($type !== null) {
         $media->where('media_type = :type', [':type' => $type]);
     }
     $media->via('contentMedia');
     return $media;
 }
コード例 #3
0
ファイル: Media.php プロジェクト: perminder-klair/kato-core
 /**
  * Upload to file and insert into media table
  * @param $file
  * @return array
  */
 private function insertMedia($file)
 {
     /**
      * @var \yii\web\UploadedFile $file
      */
     $result = ['success' => false, 'message' => 'File could not be saved.'];
     if ($file->size > Yii::$app->params['maxUploadSize']) {
         $result['message'] = 'Max upload size limit reached';
     }
     $uploadTime = date("Y-m-W");
     $media = new MediaModel();
     $media->filename = KatoBase::sanitizeFile($file->baseName) . '-' . KatoBase::genRandomString(4) . '.' . $file->extension;
     $media->mimeType = $file->type;
     $media->byteSize = $file->size;
     $media->extension = $file->extension;
     $media->source = basename(\Yii::$app->params['uploadPath']) . '/' . $uploadTime . '/' . $media->filename;
     if (!is_file($media->source)) {
         //If saved upload the file
         $uploadPath = \Yii::$app->params['uploadPath'] . $uploadTime;
         if (!is_dir($uploadPath)) {
             mkdir($uploadPath, 0777, true);
         }
         if ($file->saveAs($uploadPath . '/' . $media->filename)) {
             //Save to media table
             if ($media->save(false)) {
                 $result['success'] = true;
                 $result['message'] = 'Upload Success';
                 $result['data'] = $media;
             } else {
                 $result['message'] = "Database record could not be saved.";
             }
         } else {
             $result['message'] = "File could not be saved.";
         }
     } else {
         $result['message'] = "File already exists.";
     }
     return $result;
 }
コード例 #4
0
 public function search($params)
 {
     $query = Media::find();
     if (!isset($_GET['sort'])) {
         $query->orderBy('id DESC');
     }
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'create_time' => $this->create_time, 'byteSize' => $this->byteSize, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'filename', $this->filename])->andFilterWhere(['like', 'source', $this->source])->andFilterWhere(['like', 'source_location', $this->source_location])->andFilterWhere(['like', 'extension', $this->extension])->andFilterWhere(['like', 'mimeType', $this->mimeType])->andFilterWhere(['like', 'media_type', $this->media_type]);
     return $dataProvider;
 }
コード例 #5
0
 /**
  * Finds the Media model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Media the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Media::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }