mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
no message
This commit is contained in:
1 parent
9c7b176508
commit
a5f6fbd720
3 files changed
+144
-45
No files matched your search
@@ -84,21 +84,75 @@ class SubTask extends Model
|
||||
$this->touch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizácia timeblocks - aktualizuje existujúce, vytvorí nové, zmaže nepoužité
|
||||
*/
|
||||
public function syncTimeblocks(Array $timeblocks)
|
||||
{
|
||||
if (empty($timeblocks)) {
|
||||
return $this->timeblocks()->delete();
|
||||
}
|
||||
// pripravenie array na upsert
|
||||
$timeblocks = array_map(function($item) {
|
||||
|
||||
// Získame ID existujúcich timeblocks tohto subtasku
|
||||
$existingIds = $this->timeblocks()->pluck('id')->toArray();
|
||||
|
||||
// Rozdelíme na existujúce (s ID) a nové (bez ID alebo s neexistujúcim ID)
|
||||
$timeblocksToUpdate = [];
|
||||
$timeblocksToCreate = [];
|
||||
$receivedIds = [];
|
||||
|
||||
foreach ($timeblocks as $item) {
|
||||
$item['sub_task_id'] = $this->id;
|
||||
return $item;
|
||||
|
||||
// Ak má ID a toto ID existuje v našich timeblocks, aktualizujeme
|
||||
if (isset($item['id']) && in_array($item['id'], $existingIds)) {
|
||||
$timeblocksToUpdate[] = $item;
|
||||
$receivedIds[] = $item['id'];
|
||||
} else {
|
||||
// Nový timeblock - odstránime ID aby sa vytvoril nový záznam
|
||||
unset($item['id']);
|
||||
$timeblocksToCreate[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
// Zmažeme timeblocks, ktoré neboli poslané z frontendu
|
||||
$this->timeblocks()->whereNotIn('id', $receivedIds)->delete();
|
||||
|
||||
// Aktualizujeme existujúce
|
||||
foreach ($timeblocksToUpdate as $timeblock) {
|
||||
$this->timeblocks()->where('id', $timeblock['id'])->update([
|
||||
'start' => $timeblock['start'],
|
||||
'end' => $timeblock['end'],
|
||||
]);
|
||||
}
|
||||
|
||||
// Vytvoríme nové
|
||||
if (! empty($timeblocksToCreate)) {
|
||||
$this->timeblocks()->createMany($timeblocksToCreate);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Vytvorenie nových timeblocks - pre použitie pri publikovaní draftu
|
||||
*/
|
||||
public function createTimeblocks(Array $timeblocks)
|
||||
{
|
||||
if (empty($timeblocks)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timeblocksToCreate = array_map(function($item) {
|
||||
// Odstránime ID a sub_task_id pre istotu
|
||||
unset($item['id']);
|
||||
unset($item['sub_task_id']);
|
||||
return [
|
||||
'start' => $item['start'],
|
||||
'end' => $item['end'],
|
||||
'is_finished' => $item['is_finished'] ?? false,
|
||||
];
|
||||
}, $timeblocks);
|
||||
|
||||
// zmazanie vykazov, ktore neboli poslane z frontendu
|
||||
$this->timeblocks()->whereNotIn('id', collect($timeblocks)->pluck('id')->filter())->delete();
|
||||
|
||||
$this->timeblocks()->upsert($timeblocks, ['id']);
|
||||
$this->timeblocks()->createMany($timeblocksToCreate);
|
||||
}
|
||||
|
||||
public function deleteAttachments()
|
||||
|
||||
Reference in new issue
Block a user