mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-10 19:51:07 +03:30
fix media
This commit is contained in:
@@ -144,6 +144,7 @@ class ItemController extends Controller
|
||||
$Item = Item::create( [
|
||||
'name' => $request->name ,
|
||||
'size' => $request->size ,
|
||||
'unique_id' => $request->unique_id ,
|
||||
'specific' => $request->specific ,
|
||||
'note' => $request->note ,
|
||||
'udi' => $request->udi ,
|
||||
@@ -163,19 +164,16 @@ class ItemController extends Controller
|
||||
|
||||
public function update( StoreItemRequest $request , Item $Item )
|
||||
{
|
||||
try {
|
||||
$Item->update( [
|
||||
'name' => $request->name ,
|
||||
'size' => $request->size ,
|
||||
'specific' => $request->specific ,
|
||||
'note' => $request->note ,
|
||||
'udi' => $request->udi
|
||||
] );
|
||||
MediaHelper::moveMediaTo( $Item );
|
||||
return MessageResponse::success( trans( 'messages.success' ) );
|
||||
} catch ( \Exception $exception ) {
|
||||
dd( $exception );
|
||||
}
|
||||
$Item->update( [
|
||||
'name' => $request->name ,
|
||||
'size' => $request->size ,
|
||||
'unique_id' => $request->unique_id ,
|
||||
'specific' => $request->specific ,
|
||||
'note' => $request->note ,
|
||||
'udi' => $request->udi
|
||||
] );
|
||||
MediaHelper::moveMediaTo( $Item );
|
||||
return MessageResponse::success( trans( 'messages.success' ) );
|
||||
}
|
||||
|
||||
public function destroy( Item $Item )
|
||||
|
||||
@@ -11,27 +11,23 @@ class StoreMedia extends Controller
|
||||
{
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
try {
|
||||
$this->validate($request, [
|
||||
'batch_id' => 'required|max:40',
|
||||
'file' => 'required|mimes:jpeg,jpg,bmp,png,pdf'
|
||||
], [
|
||||
'required' => trans('validations.required'),
|
||||
'max' => trans('validations.max', ['number' => 255]),
|
||||
'mimes' => trans('validations.mimes', ['types' => 'jpeg,jpg,bmp,png,pdf']),
|
||||
]);
|
||||
$fakeModel = FakeModel::firstOrCreate(['batch_id' => $request->input('batch_id')]);
|
||||
$media = $fakeModel->addMedia($request->file('file'))->toMediaCollection('temp_medias');
|
||||
$this->validate($request, [
|
||||
'batch_id' => 'required|max:40',
|
||||
'file' => 'required|mimes:jpeg,jpg,bmp,png,pdf'
|
||||
], [
|
||||
'required' => trans('validations.required'),
|
||||
'max' => trans('validations.max', ['number' => 255]),
|
||||
'mimes' => trans('validations.mimes', ['types' => 'jpeg,jpg,bmp,png,pdf']),
|
||||
]);
|
||||
$fakeModel = FakeModel::firstOrCreate(['batch_id' => $request->input('batch_id')]);
|
||||
$media = $fakeModel->addMedia($request->file('file'))->toMediaCollection('temp_medias');
|
||||
|
||||
return response()->json(
|
||||
Responser::success(
|
||||
'فایل با موفقیت بارگزاری گردید',
|
||||
null,
|
||||
['id' => $media->id, 'url' => $media->getUrl()]
|
||||
)
|
||||
);
|
||||
}catch (\Exception $exception){
|
||||
dd($exception);
|
||||
}
|
||||
return response()->json(
|
||||
Responser::success(
|
||||
'فایل با موفقیت بارگزاری گردید',
|
||||
null,
|
||||
['id' => $media->id, 'url' => $media->getUrl()]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class StoreItemRequest extends FormRequest
|
||||
'size' => 'required',
|
||||
'specific' => 'required',
|
||||
'note' => 'nullable',
|
||||
// 'udi' => 'required|max:100|unique:items',
|
||||
'udi' => 'required|max:100|unique:items',
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -7,40 +7,37 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MediaHelper
|
||||
{
|
||||
public static function moveMediaTo( $model )
|
||||
{
|
||||
try {
|
||||
request()->validate( [
|
||||
'file_batch_id' => 'nullable|string' ,
|
||||
] );
|
||||
public static function moveMediaTo($model) {
|
||||
|
||||
if ( $fakeModel = FakeModel::where( 'batch_id' , request( 'file_batch_id' ) )->first() ) {
|
||||
foreach ( $fakeModel->getMedia( 'temp_medias' ) as $media ) {
|
||||
if ( substr( $media->mime_type , 0 , 5 ) == 'image' ) {
|
||||
$media->move( $model , 'images' );
|
||||
} else {
|
||||
$media->move( $model , 'files' );
|
||||
}
|
||||
request()->validate([
|
||||
'file_batch_id' => 'nullable|string',
|
||||
]);
|
||||
|
||||
if ($fakeModel = FakeModel::where('batch_id', request('file_batch_id'))->first()) {
|
||||
foreach($fakeModel->getMedia('temp_medias') as $media) {
|
||||
if(substr($media->mime_type, 0, 5) == 'image') {
|
||||
$media->move($model, 'images');
|
||||
} else {
|
||||
$media->move($model, 'files');
|
||||
}
|
||||
$fakeModel->delete();
|
||||
}
|
||||
} catch ( \Exception $exception ) {
|
||||
dd( $exception );
|
||||
$fakeModel->delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function storeMediaFor( Model $model , string $batchId = null )
|
||||
public static function storeMediaFor(Model $model, string $batchId = null)
|
||||
{
|
||||
if ( is_null( $batchId ) ) {
|
||||
$batchId = request()->input( 'file_batch_id' );
|
||||
if (is_null($batchId)) {
|
||||
$batchId = request()->input('file_batch_id');
|
||||
}
|
||||
|
||||
$modelName = self::getNamespaceMap( $model );
|
||||
$modelName = self::getNamespaceMap($model);
|
||||
|
||||
if ( $fakeModel = FakeModel::where( 'batch_id' , $batchId )->where( 'model_name' , $modelName )->first() ) {
|
||||
if ($fakeModel = FakeModel::where('batch_id', $batchId)->where('model_name', $modelName)->first()) {
|
||||
|
||||
foreach ( $fakeModel->media()->get() as $media ) {
|
||||
$media->move( $model , $media->collection_name );
|
||||
foreach($fakeModel->media()->get() as $media) {
|
||||
$media->move($model, $media->collection_name);
|
||||
}
|
||||
|
||||
$fakeModel->delete();
|
||||
@@ -49,24 +46,23 @@ class MediaHelper
|
||||
|
||||
}
|
||||
|
||||
private static function getNamespaceMap( Model $model )
|
||||
private static function getNamespaceMap(Model $model)
|
||||
{
|
||||
return $model->getMorphClass();
|
||||
}
|
||||
|
||||
public static function moveTinyMCEMediaTo( $model )
|
||||
{
|
||||
public static function moveTinyMCEMediaTo($model) {
|
||||
|
||||
request()->validate( [
|
||||
'tinymce_batch_id' => 'nullable|string' ,
|
||||
] );
|
||||
request()->validate([
|
||||
'tinymce_batch_id' => 'nullable|string',
|
||||
]);
|
||||
|
||||
if ( $fakeModel = FakeModel::where( 'batch_id' , request( 'tinymce_batch_id' ) )->first() ) {
|
||||
foreach ( $fakeModel->getMedia( 'temp_medias' ) as $media ) {
|
||||
if ( substr( $media->mime_type , 0 , 5 ) == 'image' ) {
|
||||
$media->assign( $model , 'tinymce_images' );
|
||||
if ($fakeModel = FakeModel::where('batch_id', request('tinymce_batch_id'))->first()) {
|
||||
foreach($fakeModel->getMedia('temp_medias') as $media) {
|
||||
if(substr($media->mime_type, 0, 5) == 'image') {
|
||||
$media->assign($model, 'tinymce_images');
|
||||
} else {
|
||||
$media->assign( $model , 'temp_medias' );
|
||||
$media->assign($model, 'temp_medias');
|
||||
}
|
||||
}
|
||||
$fakeModel->delete();
|
||||
@@ -74,16 +70,15 @@ class MediaHelper
|
||||
|
||||
}
|
||||
|
||||
public static function loadMedia( $model , ...$collections )
|
||||
{
|
||||
$model->loadMissing( [ 'media' => function ( $query ) use ( $collections ) {
|
||||
$query->select( '*' );
|
||||
if ( ! empty( $collections ) ) {
|
||||
$query->whereIn( 'collection_name' , $collections );
|
||||
public static function loadMedia($model, ...$collections) {
|
||||
$model->loadMissing(['media' => function($query) use ($collections) {
|
||||
$query->select('*');
|
||||
if (! empty($collections)) {
|
||||
$query->whereIn('collection_name', $collections);
|
||||
}
|
||||
} ] );
|
||||
if ( $model->media ) {
|
||||
foreach ( $model->media as $media ) {
|
||||
}]);
|
||||
if ($model->media) {
|
||||
foreach ($model->media as $media) {
|
||||
$media->address = $media->getUrl();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user