Example #1
0
 /**
  * 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;
 }
        <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>
Example #3
0
 /**
  * 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;
 }
 /**
  * Get media files.
  *
  * @param bool $printResponse
  * @param int  $id
  *
  * @return array
  */
 public function get($id = null, $printResponse = true)
 {
     $response = [];
     $content = [];
     if ($id && ($media = $this->findMedia($id))) {
         $response = [$this->getSingularParamName() => $this->generateResponse($media)];
     } else {
         $query = Media::find();
         if ($post_id = Yii::$app->request->get('post_id')) {
             $query->andWhere(['media_post_id' => $post_id]);
         }
         if ($title = Yii::$app->request->get('title')) {
             $query->andWhere(['LIKE', 'media_title', $title]);
         }
         $query->orderBy(['id' => SORT_DESC]);
         $countQuery = clone $query;
         $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => $this->getOption('files_per_page')]);
         if ($models = $query->offset($pages->offset)->limit($pages->limit)->all()) {
             foreach ($models as $index => $media) {
                 /* @var $media Media */
                 $content[] = $this->generateResponse($media, $index);
             }
         }
         $response = [$this->getOption('param_name') => $content];
     }
     $this->setResponse($response);
     return $this->getResponse($printResponse);
 }