FEATURE #2354 @2h30m: add pipeline for edit and create new content
This commit is contained in:
@@ -1,85 +0,0 @@
|
||||
<?php
|
||||
namespace App\Models\Content\ModelTraits;
|
||||
|
||||
use App\Models\Content\ContentHasFile;
|
||||
use App\Models\FileManager\Directory;
|
||||
use App\Models\FileManager\File;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
trait Content
|
||||
{
|
||||
/**
|
||||
* Upload and assign files to newly created content page
|
||||
*
|
||||
* @param $files
|
||||
* @param $assignedFiles
|
||||
* @param null $revisionId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function uploadRelatedFiles($files, $assignedFiles = [], $revisionId = null)
|
||||
{
|
||||
$changed = 0;
|
||||
|
||||
// check if any file was removed
|
||||
$changed += $this->removeRelatedFiles($revisionId, $assignedFiles);
|
||||
|
||||
// allow uploading only on created pages
|
||||
if (!$this->id || !$files[0]) {
|
||||
return $changed;
|
||||
}
|
||||
|
||||
// find or create new directory for files assigned to content
|
||||
$directory = Directory::firstOrCreate([
|
||||
'name' => '.content_files',
|
||||
'hidden' => true
|
||||
]);
|
||||
$data = new \stdClass();
|
||||
$data->directoryId = $directory->id;
|
||||
|
||||
$fileModel = App::make(File::class);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (!$file) {
|
||||
continue;
|
||||
}
|
||||
$uploaded = $fileModel->upload($file, $data);
|
||||
|
||||
// add assignement
|
||||
ContentHasFile::create([
|
||||
'file_id' => $uploaded['id'],
|
||||
'content_id' => $this->id
|
||||
]);
|
||||
|
||||
if ($revisionId) {
|
||||
$this->saveHistory($revisionId, 'file', null, ['file' => $uploaded['file']->getClientOriginalName()]);
|
||||
}
|
||||
|
||||
$changed++;
|
||||
}
|
||||
|
||||
return $changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete files from association
|
||||
*
|
||||
* @param $revisionId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function removeRelatedFiles($revisionId, $assignedFiles)
|
||||
{
|
||||
$allFiles = $this->files()
|
||||
->whereNotIn('id', array_keys($assignedFiles))
|
||||
->get();
|
||||
|
||||
foreach ($allFiles as $file) {
|
||||
ContentHasFile::where('content_id', $this->id)
|
||||
->where('file_id', $file->id)
|
||||
->delete();
|
||||
}
|
||||
|
||||
return count($allFiles);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ trait ContentHistory
|
||||
/**
|
||||
* Returns history grouped by revisions (edit_batches)
|
||||
*
|
||||
* @param Content $content
|
||||
* @param ContentModel $content
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user