fix(smartblock): fixed controller after frontend refactor

This commit is contained in:
Marco Cavalli 2025-04-03 14:59:19 +02:00
parent fa96a43ba4
commit a25fc1fc33

View file

@ -51,8 +51,8 @@ class SmartBlockController extends Controller
* @param $id
* @return mixed
*/
public function update(Request $request, $id) {
return $this->save($request, $id);
public function update(Request $request) {
return $this->save($request);
}
/**
@ -70,31 +70,38 @@ class SmartBlockController extends Controller
* @param $id
* @return mixed string
*/
public function save(Request $request, $id = null) {
public function save(Request $request) {
$user = Auth::user();
//dd($user);
$request->validate([
'name' => 'required|string',
'type' => 'required|string',
'length' => 'required|string',
'length_type' => 'required|string',
'criteria' => 'required|array'
]);
$criteria = $request->criteria;
$length = ($request->length_type == 'time') ? new LengthFormatter($request->length) : null;
$criteria = $this->createCriteria($request);
$length = 0;
$dbSmartBlock = SmartBlock::firstOrNew(['id' => $request->id]);
$dbSmartBlock = SmartBlock::firstOrNew(['id' => $id]);
$dbSmartBlock->fill([
'name' => $request->name,
'creator_id' => $user->id,
'description' => $request->description,
'length' => $length,
]);
'length' => $request->length,
])->save();
$this->saveCriteria($dbSmartBlock, $criteria);
//ToDo: save content
return $dbSmartBlock->with(['criteria', 'content'])->get()->toJson();
if (is_array($request->tracks) && count($request->tracks) > 0) {
SmartBlockContent::where('block_id','=',$dbSmartBlock->id)->delete();
foreach ($request->tracks as $key => $track) {
$this->saveContent($track['file'], $dbSmartBlock->id, $key);
}
}
return $dbSmartBlock->toJson();
}
/**
@ -103,28 +110,36 @@ class SmartBlockController extends Controller
* @param array $criteria
* @return void
*/
private function saveCriteria(SmartBlock $smartBlock, array $criteria): void {
private function saveCriteria(SmartBlock $smartBlock, Collection $criteria): void {
$ids = [];
foreach($criteria as $criterion) {
foreach($criteria as $key => $criterion) {
$criteriaGroup = null;
if (!in_array($criterion['criteria'], ['sort', 'limit', 'repeat_tracks', 'overflow_tracks'])) {
$criteriaGroup = $key;
}
$ids[] = SmartBlockCriteria::updateOrCreate([
'criteria' => $criterion['name'],
'criteria' => $criterion['criteria'],
'block_id' => $smartBlock->id,
],
[
'modifier' => $criterion['modifier'],
'value' => $criterion['value'],
'extra' => $criterion['extra'], //ToDo: understand this field
'criteriagroup' => $criterion['criteriagroup'],
]);
[
'modifier' => $criterion['modifier'],
'value' => $criterion['value'],
'extra' => $criterion['extra'], //ToDo: understand this field
'criteriagroup' => $criteriaGroup,
]);
}
// dd($ids);
if (!empty($ids)) {
SmartBlockCriteria::whereNotIn('id', $ids)->delete();
SmartBlockCriteria::whereId($smartBlock->id)->whereNotIn('id', array_column($ids, 'id'))->delete();
}
}
/**
* Get a tracks list from a set of smart block criteria (getting them from the db
* of directly passed in the request from the frontend)
* @param int $block_id
* @param int|null $instance_id
* @param Request $request
* @return mixed json string
* @throws \DateMalformedIntervalStringException
*/
@ -133,34 +148,11 @@ class SmartBlockController extends Controller
throw new BadMethodCallException('If block_id is 0 criteria array must be passed as parameter');
}
if ($block_id > 0) {
if ($block_id > 0 && count($request->criteria) === 0) {
$smartBlock = SmartBlock::whereId($block_id)->first();
$criteria = $smartBlock->criteria;
} else {
$criteria = new \Illuminate\Database\Eloquent\Collection();
foreach($request->criteria as $key => $criterion) {
$criteria->push(new SmartBlockCriteria($criterion));
}
$criteria->push(new SmartBlockCriteria([
'criteria' => 'limit',
'modifier' => $request->limit['type'],
'value' => $request->limit['quantity'],
]));
$criteria->push(new SmartBlockCriteria([
'criteria' => 'repeat_tracks',
'modifier' => 'N/A',
'value' => intval($request->repeat_tracks),
]));
$criteria->push(new SmartBlockCriteria([
'criteria' => 'overflow_tracks',
'modifier' => 'N/A',
'value' => intval($request->overflow_tracks),
]));
$criteria->push(new SmartBlockCriteria([
'criteria' => 'sort',
'modifier' => 'N/A',
'value' => $request->sort
]));
$criteria = $this->createCriteria($request);
}
//dd($criteria);
$files = File::smartBlockFilter($criteria)->get();
@ -168,6 +160,18 @@ class SmartBlockController extends Controller
return $this->criteriaLimit($criteria, $files);
}
private function createCriteria(Request $request) {
$criteria = new \Illuminate\Database\Eloquent\Collection();
foreach($request->criteria as $key => $criterion) {
if (is_null($criterion['modifier']) || $criterion['modifier'] === '') {
$criterion['modifier'] = 'N/A';
}
$criteria->push(new SmartBlockCriteria($criterion));
}
return $criteria;
}
/**
* Loop through files if limit criteria is based on total length and stop
* to loop when total length is reached
@ -179,33 +183,85 @@ class SmartBlockController extends Controller
private function criteriaLimit(Collection $criteria, Collection $files) {
$limit = $criteria->where('criteria', 'limit')->first();
$overflow = $criteria->where('criteria', 'overflow_tracks')->first()->value;
$repeat = $criteria->where('criteria', 'repeat_tracks')->first()->value;
//If limit is not based on sum tracks length is already done in query by filters
if (is_null($limit) || !in_array($limit->modifier, ['hours', 'minutes'])) {
if (is_null($limit)) {
return $files->toJson();
}
//If limit is based on sum tracks length, use them as DateInterval
$interval = DateInterval::createFromDateString($limit->value.' '.$limit->modifier);
$totLengthFromToday = new DateTime();
$intervalFromToday = clone $totLengthFromToday;
$intervalFromToday = $intervalFromToday->add($interval);
$list = collect();
foreach ($files as $file) {
$fileLength = new LengthFormatter($file->length);
$totLengthFromToday->add(DateInterval::createFromDateString(round($fileLength->toSeconds()).' seconds'));
if ($totLengthFromToday < $intervalFromToday) {
$list->push($file);
} else {
if ($overflow) {
[$list, $totLengthInSeconds] = $this->recursiveAddTracks($list, $files, $limit, $repeat, $overflow);
return json_encode([
'list' => $list,
'length' => $totLengthInSeconds
]);
//ToDo: add limit from instance length
}
private function recursiveAddTracks($list, $files, $limit, $repeat, $overflow) {
$totLengthInSeconds = 0;
//If limit is by time
if (in_array($limit->modifier, ['hours', 'minutes'])) {
//If limit is based on sum tracks length, use them as DateInterval
$interval = DateInterval::createFromDateString($limit->value.' '.$limit->modifier);
$totLengthFromToday = new DateTime();
$intervalFromToday = clone $totLengthFromToday;
$intervalFromToday = $intervalFromToday->add($interval);
foreach ($files as $file) {
$fileLength = new LengthFormatter($file->length);
$totLengthFromToday->add(DateInterval::createFromDateString(round($fileLength->toSeconds()).' seconds'));
if ($totLengthFromToday < $intervalFromToday) {
$list->push($file);
$totLengthInSeconds += $fileLength->toSeconds();
} else {
if ($overflow) {
$list->push($file);
$totLengthInSeconds += $fileLength->toSeconds();
break;
}
break;
}
break;
}
if ($repeat) {
while ($totLengthFromToday < $intervalFromToday) {
$newFiles = $files->shuffle();
$fileLength = new LengthFormatter($newFiles[0]->length);
$totLengthFromToday->add(DateInterval::createFromDateString(round($fileLength->toSeconds()).' seconds'));
if ($totLengthFromToday < $intervalFromToday) {
$list->push($newFiles[0]);
$totLengthInSeconds += $fileLength->toSeconds();
} else {
if ($overflow) {
$list->push($newFiles[0]);
$totLengthInSeconds += $fileLength->toSeconds();
break;
}
break;
}
}
}
//If limit is by items
} else {
$list = collect($files);
foreach ($files as $file) {
$fileLength = new LengthFormatter($file->length);
$totLengthInSeconds += $fileLength->toSeconds();
}
if ($repeat) {
while ($list->count() < $limit->value) {
$newFiles = $files->shuffle();
$list->push($newFiles[0]);
$fileLength = new LengthFormatter($newFiles[0]->length);
$totLengthInSeconds += $fileLength->toSeconds();
}
}
}
return $list->toJson();
//ToDo: add limit from instance length
return array($list, $totLengthInSeconds);
}
/**
@ -216,17 +272,28 @@ class SmartBlockController extends Controller
* @param int $position
* @return void
*/
private function saveContent(File $file, int $block_id, int $position = 0) {
private function saveContent(array $file, int $block_id, int $position = 0) {
SmartBlockContent::create([
'block_id' => $block_id,
'file_id' => $file->id,
'file_id' => $file['id'],
'position' => $position,
'trackoffset' => 0, //ToDo: understand this field
'cliplength' => $file->length,
'cuein' => $file->cuein,
'cueout' => $file->cueout,
'cliplength' => $file['length'],
'cuein' => $file['cuein'],
'cueout' => $file['cueout'],
'fadein' => '00:00:00', //ToDo: add fadein/fadeout
'fadeout' => '00:00:00',
]);
}
/**
* Delete a smart block, his criteria and its tracks list
* @param $id
* @return true
*/
public function destroy($id) {
SmartBlockContent::where('block_id', $id)->delete();
SmartBlockCriteria::where('block_id', $id)->delete();
SmartBlock::destroy($id);
return true;
}
}