예제 #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Media::find();
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'album_id' => $this->album_id, 'album_position' => $this->album_position, 'user_id' => $this->user_id, 'size' => $this->size, 'created' => $this->created, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'type', $this->type]);
     return $dataProvider;
 }
예제 #2
0
 public function actionDelete($id)
 {
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     $items['files'] = [];
     if (($model = Media::findOne($id)) !== null) {
         try {
             $model->delete();
             $items['files'][][$model->name] = true;
             $items['files'][][$id] = true;
             return $items;
         } catch (\Exception $e) {
             $items['files'][][$model->name] = false;
             $items['files'][][$id] = false;
             $items['files'][]['error'] = $e->getMessage();
             return $items;
         }
     } else {
         $items['files'][][$id] = false;
         $items['files'][]['error'] = 'The requested image file does not exist.';
         return $items;
     }
 }
예제 #3
0
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['language'], 'required'], [['media_id'], 'integer'], [['language'], 'string', 'max' => 16], [['alt', 'title'], 'string', 'max' => 255], [['media_id'], 'exist', 'skipOnError' => true, 'targetClass' => Media::className(), 'targetAttribute' => ['media_id' => 'id']]];
 }
예제 #4
0
 /**
  * Delete a media album and all its media files
  */
 protected function deleteMediaAlbum()
 {
     $files = Media::find()->where(['album_id' => $this->owner->{$this->attribute}])->asArray()->all();
     foreach ($files as $file) {
         if (($model = Media::findOne($file['id'])) !== null) {
             $model->delete();
         }
     }
     if (($album = MediaAlbum::findOne($this->owner->{$this->attribute})) !== null) {
         $album->delete();
         $this->owner->{$this->attribute} = null;
         return true;
     } else {
         Yii::warning('Cannot delete Media Album.');
         return false;
     }
 }
예제 #5
0
    /**
     * The specific js for the model files
     */
    protected function mediaModelJs()
    {
        //Prepair the files array akin the blueimp fileupload.
        if (!empty($this->model->mediaFiles)) {
            $files = [];
            if (is_string($this->model->mediaFiles)) {
                // $files[] = \kmergen\media\models\Media::find()->where(['url' => $this->model->mediaFiles])->asArray()->one();
                $files[] = \kmergen\media\models\Media::find()->where(['url' => $this->model->mediaFiles])->one();
            } else {
                $files = $this->model->mediaFiles;
            }
            foreach ($files as $file) {
                //                $id = $file['id'];
                //                $fuFiles[$id]['id'] = $file['id'];
                //                $fuFiles[$id]['name'] = $file['name'];
                //                $fuFiles[$id]['size'] = (int)$file['size'];
                //                $fuFiles[$id]['url'] = $file['url'];
                //                $fuFiles[$id]['status'] = $file['status'];
                //                $fuFiles[$id]['type'] = $file['type'];
                //                if (strpos($file['type'], 'image/') !== false) {
                //                    $fuFiles[$id]['thumbnailUrl'] = Yii::$app->image->thumb($file['url'], array_key_exists('thumbStyle', $this->mediaOptions) ? $this->mediaOptions['thumbStyle'] : 'small');
                //
                //                    //We need the translation array indexed by language
                //                    $translations = $file['translations'];
                //                    if (isset($translations[0])) {
                //                        $fuFiles[$id]['translations'] = ArrayHelper::index($translations, 'language');
                //                    } else {
                //                        $fuFiles[$id]['translations'] = $file['translations'];
                //                    }
                //                }
                //                $fuFiles[$id]['deleteUrl'] = Url::to(['/media/upload-delete', 'id' => $file['id']]);
                //                $fuFiles[$id]['deleteType'] = 'POST';
                $fuFiles[$file->id]['id'] = $file->id;
                $fuFiles[$file->id]['name'] = $file->name;
                $fuFiles[$file->id]['size'] = (int) $file->size;
                $fuFiles[$file->id]['url'] = $file->url;
                $fuFiles[$file->id]['status'] = $file->status;
                $fuFiles[$file->id]['type'] = $file->type;
                if (strpos($file->type, 'image/') !== false) {
                    $fuFiles[$file->id]['thumbnailUrl'] = Yii::$app->image->thumb($file->url, array_key_exists('thumbStyle', $this->mediaOptions) ? $this->mediaOptions['thumbStyle'] : 'small');
                    //We need the translation array indexed by language
                    $translations = (array) $file->translations;
                    if (isset($translations[0])) {
                        $fuFiles[$file->id]['translations'] = ArrayHelper::index($translations, 'language');
                    } else {
                        $fuFiles[$file->id]['translations'] = $file->translations;
                    }
                }
                $fuFiles[$file->id]['deleteUrl'] = Url::to(['/media/upload-delete', 'id' => $file->id]);
                $fuFiles[$file->id]['deleteType'] = 'POST';
            }
        } else {
            $fuFiles = [];
        }
        $fuFiles = Json::encode($fuFiles);
        $languages = Json::encode($this->languages);
        return <<<JS
            addExistFiles();
        
            function addExistFiles()
            {
                var fuFiles = {$fuFiles};
                jQuery(document.createElement('div')).addClass('hidden-file-inputs').appendTo('form');

                var cOpt = \$('#{$this->options['id']}').fileupload('option');
                var data = {};
                data.languages = {$languages}
                  data.formatFileSize = function(bytes) {
                    if (typeof bytes !== 'number') {
                        return '';
                    }
                    if (bytes >= 1000000000) {
                        return (bytes / 1000000000).toFixed(2) + ' GB';
                    }
                    if (bytes >= 1000000) {
                        return (bytes / 1000000).toFixed(2) + ' MB';
                    }
                    return (bytes / 1000).toFixed(2) + ' KB';
                };

                jQuery.each( fuFiles, function( index, filedata ) {
                    //Convert the translations for better handling in download template
                    data.options = cOpt;
                    //Convert filedata to array
                    data.files = [filedata];
                    \$('.files').append(tmpl('template-download', data));
                    //Enable all translation input fields they are not empty and check the enable translation checkbox
                    var translationRows = \$('.template-translation').find('.row');
                    \$(translationRows).each(function(){
                        var inputs = \$(this).find('input[type=text]');
                        if (inputs.eq(0).val() || inputs.eq(1).val()) {
                            \$(inputs).prop('disabled', false);    
                            \$(this).find('input[type=checkbox]').prop('checked', true);   
                        }
                    });
                    createFileInputs(filedata);
                    jQuery('.template-download').addClass('in');

                });
            }
                        
            function createFileInputs(filedata)
            {
                jQuery(document.createElement('input')).attr({type: 'hidden', name: 'MediaFiles[' + filedata.id + '][id]', value: filedata.id}).appendTo('.hidden-file-inputs');
                jQuery(document.createElement('input')).attr({type: 'hidden', name: 'MediaFiles[' + filedata.id + '][name]', value: filedata.name}).appendTo('.hidden-file-inputs');
                jQuery(document.createElement('input')).attr({type: 'hidden', name: 'MediaFiles[' + filedata.id + '][size]', value: filedata.size}).appendTo('.hidden-file-inputs');
                jQuery(document.createElement('input')).attr({type: 'hidden', name: 'MediaFiles[' + filedata.id + '][url]', value: filedata.url}).appendTo('.hidden-file-inputs');
                jQuery(document.createElement('input')).attr({type: 'hidden', name: 'MediaFiles[' + filedata.id + '][type]', value: filedata.type}).appendTo('.hidden-file-inputs');
                jQuery(document.createElement('input')).attr({type: 'hidden', name: 'MediaFiles[' + filedata.id + '][status]', value: filedata.status}).appendTo('.hidden-file-inputs');
            }
                
            function regenerateFileInputs()
            {
                jQuery('.hidden-file-inputs').empty();
                jQuery('.template-download').each(function(index){
                    createFileInputs(\$(this).data());
                })  
            }
JS;
    }
예제 #6
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.');
     }
 }