/** * Upload file to server. * * @param bool $printResponse * * @return array * @throws \yii\web\NotFoundHttpException */ public function post($printResponse = true) { if (Yii::$app->request->get('delete') && ($id = Yii::$app->request->get('id'))) { return $this->delete($id, $printResponse); } $response = []; $this->_media = new Media(); $this->_media->file = UploadedFile::getInstance($this->_media, 'file'); if ($this->_media->file !== null && $this->_media->validate(['file'])) { if ($post_id = Yii::$app->request->get('post_id')) { $post = $this->findPost($post_id); $this->_media->media_post_id = $post->id; } $this->_media->media_title = $this->_media->file->baseName; $this->_media->media_mime_type = $this->_media->file->type; $this->handleFileUpload($this->_media->file); if ($this->_media->save()) { if ($this->_media->setMeta('metadata', $this->_meta)) { $response = $this->generateResponse($this->_media); } } } else { $response[] = ['media_error' => $this->_media->getErrors('file'), 'media_filename' => isset($this->_media->file->name) ? $this->_media->file->name : null, 'media_file_size' => isset($this->_media->file->size) ? $this->_media->file->size : null]; } $this->setResponse([$this->getOption('param_name') => [$response]]); return $this->getResponse($printResponse); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = MediaModel::find()->from(['media' => $this->tableName()]); $query->innerJoinWith(['mediaAuthor' => function ($query) { /* @var $query \yii\db\ActiveQuery */ return $query->from(['author' => User::tableName()]); }]); $query->leftJoin(['post' => Post::tableName()], 'media.media_post_id = post.id'); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->setSort(['attributes' => ArrayHelper::merge($dataProvider->sort->attributes, ['username' => ['asc' => ['username' => SORT_ASC], 'desc' => ['username' => SORT_DESC], 'label' => 'Author', 'value' => 'username']]), 'defaultOrder' => ['id' => SORT_DESC]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'media_author' => $this->media_author, 'media_post_id' => $this->media_post_id, 'media_comment_count' => $this->media_comment_count]); $query->andFilterWhere(['like', 'media_title', $this->media_title])->andFilterWhere(['like', 'media_excerpt', $this->media_excerpt])->andFilterWhere(['like', 'media_content', $this->media_content])->andFilterWhere(['like', 'media_password', $this->media_password])->andFilterWhere(['like', 'media_slug', $this->media_slug])->andFilterWhere(['like', 'media_mime_type', $this->media_mime_type])->andFilterWhere(['like', 'media_comment_status', $this->media_comment_status])->andFilterWhere(['like', 'media_date', $this->media_date])->andFilterWhere(['like', 'media_modified', $this->media_modified])->andFilterWhere(['like', 'post.post_title', $this->post_title])->andFilterWhere(['like', 'author.username', $this->username]); return $dataProvider; }
/** * Finds the Media model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param string $media_slug * * @return Media the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModelBySlug($media_slug) { if (($model = Media::findOne(['media_slug' => $media_slug])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<h5><?php echo Yii::t('content', 'Thumbnail'); ?> </h5> <div class="ibox-tools"> <a class="collapse-link"><i class="fa fa-chevron-up"></i></a> <a class="close-link"><i class="fa fa-times"></i></a> </div> </div> <div class="ibox-content"> <div id="post-thumbnail"> <?php if ($model->post_thumbnail_id !== null) { ?> <?php $thumbnail = Media::find()->andWhere(['id' => $model->post_thumbnail_id])->one(); ?> <?php echo Html::img(Yii::$app->urlManagerFront->baseUrl . '/uploads/' . $thumbnail->getMeta('metadata')['media_versions']['medium']['url']); ?> <?php } ?> </div> <?php echo $form->field($model, 'post_thumbnail_id')->hiddenInput()->label(false); ?> <?php echo Html::button($model->post_thumbnail_id === null ? Yii::t('content', 'Add thumbnail') : Yii::t('content', 'Change thumbnail'), ['data-url' => Url::to(['/content/media/popup', 'post_id' => $model->id, 'editor' => true]), 'class' => 'open-thumbnail-media btn btn-primary btn-flat btn-xs', 'id' => 'add-thumbnail-button']); ?> </div>
/** * 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.'); } }
/** * @return \yii\db\ActiveQuery */ public function getMedia() { return $this->hasOne(Media::className(), ['id' => 'media_id']); }
/** * Returns the thumbnail url. * * @param null $size * @return string */ public function getThumbnailUrl($size = null) { if ($size === null) { $size = 'full'; } $thumbnail = Media::find()->andWhere(['id' => $this->post_thumbnail_id])->one(); if ($thumbnail) { return Yii::$app->urlManagerFront->baseUrl . '/uploads/' . $thumbnail->getMeta('metadata')['media_versions'][$size]['url']; } return false; }