mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-10-10 18:13:31 -04:00
44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\LaraBuilder\Providers;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Modules\LaraBuilder\Entities\Revision as EntitiesRevision;
|
|
|
|
class Revision
|
|
{
|
|
public $sectionId;
|
|
|
|
public $directory;
|
|
|
|
public function storeRevision($request)
|
|
{
|
|
$data = $request->all();
|
|
$validator = Validator::make($data, [
|
|
'page_id' => 'required|int|exists:'.config('larabuilder.db_prefix').'pages,id',
|
|
'settings' => 'nullable',
|
|
]);
|
|
|
|
if (! $validator->passes()) {
|
|
return response()->json(['success' => false, 'error' => $validator->errors()->all()]);
|
|
}
|
|
|
|
if ($data['settings']) {
|
|
$data['settings'] = sanitizeArray($data['settings']);
|
|
}
|
|
$data['title'] = ucfirst($data['title']);
|
|
|
|
// delete revisions more than 30 by created_at
|
|
$max = 30;
|
|
$count = EntitiesRevision::where('page_id', $data['page_id'])->count();
|
|
if ($count >= $max) {
|
|
$oldestRevision = EntitiesRevision::where('page_id', $data['page_id'])->latest()->limit($max - 1)->get()->last();
|
|
EntitiesRevision::where('page_id', $data['page_id'])->where('id', '<', $oldestRevision->id)->delete();
|
|
}
|
|
|
|
$revision = EntitiesRevision::create($data);
|
|
|
|
return response()->json($revision);
|
|
}
|
|
}
|