/** * 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; }
/** * Create title for media */ private function createTitle() { $title_parts = pathinfo($this->filename); $this->title = $title_parts['filename']; $this->title = \kato\helpers\KatoBase::sanitizeFile($this->title); $this->title = str_replace('_', ' ', str_replace('-', ' ', $this->title)); $this->title = ucwords($this->title); }
?> > <?php echo $media->render(['imgTag' => true, 'width' => 90, 'height' => 90, 'class' => 'img-responsive']); ?> </a> <div class="caption"> <h4> <?php echo Editable::widget(ArrayHelper::merge($titleOptions, ['options' => ['id' => 'ed' . $media->id], 'clientOptions' => ['pk' => $media->id]])); ?> </h4> <div class="row"> <div class="col-md-6"> <small><?php echo \kato\helpers\KatoBase::formatBytes($media->byteSize, 'MB', 3); ?> </small> </div> <div class="col-md-6 status"> <?php echo Editable::widget(ArrayHelper::merge($statusOptions, ['options' => ['id' => 'edw' . $media->id], 'clientOptions' => ArrayHelper::merge($statusClientOptions, ['pk' => $media->id . '2'])])); ?> </div> </div> </div> </div> </div> <?php if (isset($isNew)) {
use kato\helpers\KatoBase; use kato\DropZone; ?> <div class="block full"> <!-- Dropzone Title --> <div class="block-title"> <h2><i class="fa fa-cloud-upload"></i> File Upload <small>Drag and Drop files to upload them!</small></h2> </div> <!-- END Dropzone Title --> <!-- Dropzone Content --> <!-- Dropzone.js, You can check out https://github.com/enyo/dropzone/wiki for usage examples --> <?php echo DropZone::widget(['options' => ['url' => \Yii::$app->urlManagerFrontend->createUrl(['media/default/upload', 'content_id' => $model->id, 'content_type' => $model->className()]), 'addRemoveLinks' => true, 'maxFilesize' => KatoBase::formatBytes(Yii::$app->params['maxUploadSize'], 'MB', '0', true), 'acceptedFiles' => implode(",", Yii::$app->params['acceptedUploadTypes'])], 'clientEvents' => ['success' => "function(file, responseText){\n var response = JSON.parse(responseText);\n \$.get( '" . \Yii::$app->urlManagerFrontend->createUrl(['media/default/render-row']) . "?id=' + response.id, function(data) {\n \$('.media-container').prepend(data);\n });\n }"]]); ?> <!-- END Dropzone Content --> </div> <div class="row media-container"> <?php if ($model->media) { ?> <?php foreach ($model->media as $media) { ?> <?php echo $this->render('@media/views/default/mediaRow.php', ['media' => $media]); ?> <?php