/** * 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; }
/** * 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; } }
/** * 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; }