fix(smartblock): fixed controller after frontend refactor
This commit is contained in:
parent
fa96a43ba4
commit
a25fc1fc33
1 changed files with 136 additions and 69 deletions
|
@ -51,8 +51,8 @@ class SmartBlockController extends Controller
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, $id) {
|
public function update(Request $request) {
|
||||||
return $this->save($request, $id);
|
return $this->save($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,31 +70,38 @@ class SmartBlockController extends Controller
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return mixed string
|
* @return mixed string
|
||||||
*/
|
*/
|
||||||
public function save(Request $request, $id = null) {
|
public function save(Request $request) {
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
//dd($user);
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'name' => 'required|string',
|
'name' => 'required|string',
|
||||||
'type' => 'required|string',
|
'type' => 'required|string',
|
||||||
'length' => 'required|string',
|
|
||||||
'length_type' => 'required|string',
|
|
||||||
'criteria' => 'required|array'
|
'criteria' => 'required|array'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$criteria = $request->criteria;
|
$criteria = $this->createCriteria($request);
|
||||||
$length = ($request->length_type == 'time') ? new LengthFormatter($request->length) : null;
|
$length = 0;
|
||||||
|
|
||||||
|
$dbSmartBlock = SmartBlock::firstOrNew(['id' => $request->id]);
|
||||||
|
|
||||||
$dbSmartBlock = SmartBlock::firstOrNew(['id' => $id]);
|
|
||||||
$dbSmartBlock->fill([
|
$dbSmartBlock->fill([
|
||||||
'name' => $request->name,
|
'name' => $request->name,
|
||||||
'creator_id' => $user->id,
|
'creator_id' => $user->id,
|
||||||
'description' => $request->description,
|
'description' => $request->description,
|
||||||
'length' => $length,
|
'length' => $request->length,
|
||||||
]);
|
])->save();
|
||||||
|
|
||||||
$this->saveCriteria($dbSmartBlock, $criteria);
|
$this->saveCriteria($dbSmartBlock, $criteria);
|
||||||
//ToDo: save content
|
//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
|
* @param array $criteria
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function saveCriteria(SmartBlock $smartBlock, array $criteria): void {
|
private function saveCriteria(SmartBlock $smartBlock, Collection $criteria): void {
|
||||||
$ids = [];
|
$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([
|
$ids[] = SmartBlockCriteria::updateOrCreate([
|
||||||
'criteria' => $criterion['name'],
|
'criteria' => $criterion['criteria'],
|
||||||
'block_id' => $smartBlock->id,
|
'block_id' => $smartBlock->id,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'modifier' => $criterion['modifier'],
|
'modifier' => $criterion['modifier'],
|
||||||
'value' => $criterion['value'],
|
'value' => $criterion['value'],
|
||||||
'extra' => $criterion['extra'], //ToDo: understand this field
|
'extra' => $criterion['extra'], //ToDo: understand this field
|
||||||
'criteriagroup' => $criterion['criteriagroup'],
|
'criteriagroup' => $criteriaGroup,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
// dd($ids);
|
||||||
if (!empty($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 $block_id
|
||||||
* @param int|null $instance_id
|
* @param Request $request
|
||||||
* @return mixed json string
|
* @return mixed json string
|
||||||
* @throws \DateMalformedIntervalStringException
|
* @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');
|
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();
|
$smartBlock = SmartBlock::whereId($block_id)->first();
|
||||||
$criteria = $smartBlock->criteria;
|
$criteria = $smartBlock->criteria;
|
||||||
} else {
|
} else {
|
||||||
$criteria = new \Illuminate\Database\Eloquent\Collection();
|
$criteria = $this->createCriteria($request);
|
||||||
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
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
//dd($criteria);
|
//dd($criteria);
|
||||||
$files = File::smartBlockFilter($criteria)->get();
|
$files = File::smartBlockFilter($criteria)->get();
|
||||||
|
@ -168,6 +160,18 @@ class SmartBlockController extends Controller
|
||||||
return $this->criteriaLimit($criteria, $files);
|
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
|
* Loop through files if limit criteria is based on total length and stop
|
||||||
* to loop when total length is reached
|
* to loop when total length is reached
|
||||||
|
@ -179,33 +183,85 @@ class SmartBlockController extends Controller
|
||||||
private function criteriaLimit(Collection $criteria, Collection $files) {
|
private function criteriaLimit(Collection $criteria, Collection $files) {
|
||||||
$limit = $criteria->where('criteria', 'limit')->first();
|
$limit = $criteria->where('criteria', 'limit')->first();
|
||||||
$overflow = $criteria->where('criteria', 'overflow_tracks')->first()->value;
|
$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)) {
|
||||||
if (is_null($limit) || !in_array($limit->modifier, ['hours', 'minutes'])) {
|
|
||||||
return $files->toJson();
|
return $files->toJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$list = collect();
|
||||||
|
[$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
|
//If limit is based on sum tracks length, use them as DateInterval
|
||||||
$interval = DateInterval::createFromDateString($limit->value.' '.$limit->modifier);
|
$interval = DateInterval::createFromDateString($limit->value.' '.$limit->modifier);
|
||||||
$totLengthFromToday = new DateTime();
|
$totLengthFromToday = new DateTime();
|
||||||
$intervalFromToday = clone $totLengthFromToday;
|
$intervalFromToday = clone $totLengthFromToday;
|
||||||
$intervalFromToday = $intervalFromToday->add($interval);
|
$intervalFromToday = $intervalFromToday->add($interval);
|
||||||
$list = collect();
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
$fileLength = new LengthFormatter($file->length);
|
$fileLength = new LengthFormatter($file->length);
|
||||||
$totLengthFromToday->add(DateInterval::createFromDateString(round($fileLength->toSeconds()).' seconds'));
|
$totLengthFromToday->add(DateInterval::createFromDateString(round($fileLength->toSeconds()).' seconds'));
|
||||||
if ($totLengthFromToday < $intervalFromToday) {
|
if ($totLengthFromToday < $intervalFromToday) {
|
||||||
$list->push($file);
|
$list->push($file);
|
||||||
|
$totLengthInSeconds += $fileLength->toSeconds();
|
||||||
} else {
|
} else {
|
||||||
if ($overflow) {
|
if ($overflow) {
|
||||||
$list->push($file);
|
$list->push($file);
|
||||||
|
$totLengthInSeconds += $fileLength->toSeconds();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $list->toJson();
|
|
||||||
//ToDo: add limit from instance length
|
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 array($list, $totLengthInSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -216,17 +272,28 @@ class SmartBlockController extends Controller
|
||||||
* @param int $position
|
* @param int $position
|
||||||
* @return void
|
* @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([
|
SmartBlockContent::create([
|
||||||
'block_id' => $block_id,
|
'block_id' => $block_id,
|
||||||
'file_id' => $file->id,
|
'file_id' => $file['id'],
|
||||||
'position' => $position,
|
'position' => $position,
|
||||||
'trackoffset' => 0, //ToDo: understand this field
|
'trackoffset' => 0, //ToDo: understand this field
|
||||||
'cliplength' => $file->length,
|
'cliplength' => $file['length'],
|
||||||
'cuein' => $file->cuein,
|
'cuein' => $file['cuein'],
|
||||||
'cueout' => $file->cueout,
|
'cueout' => $file['cueout'],
|
||||||
'fadein' => '00:00:00', //ToDo: add fadein/fadeout
|
'fadein' => '00:00:00', //ToDo: add fadein/fadeout
|
||||||
'fadeout' => '00:00:00',
|
'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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue