Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function handle(CreateScheduleCommand $command)
{
try {
$schedule = Schedule::create($this->filter($command));
$schedule->attachComponents($command->components);
event(new ScheduleWasCreatedEvent($this->auth->user(), $schedule, (bool) $command->notify));
} catch (InvalidArgumentException $e) {
throw new ValidationException(new MessageBag([$e->getMessage()]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public function handle(DeleteScheduleCommand $command)
$schedule = $command->schedule;

event(new ScheduleWasRemovedEvent($this->auth->user(), $schedule));

$schedule->delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CachetHQ\Cachet\Models\Schedule;
use CachetHQ\Cachet\Services\Dates\DateFactory;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\DB;

/**
* This is the update schedule command handler.
Expand Down Expand Up @@ -65,6 +66,8 @@ public function handle(UpdateScheduleCommand $command)

$schedule->update($this->filter($command));

$schedule->attachComponents($command->components);

event(new ScheduleWasUpdatedEvent($this->auth->user(), $schedule));

return $schedule;
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/Dashboard/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use CachetHQ\Cachet\Bus\Commands\Schedule\DeleteScheduleCommand;
use CachetHQ\Cachet\Bus\Commands\Schedule\UpdateScheduleCommand;
use CachetHQ\Cachet\Integrations\Contracts\System;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\IncidentTemplate;
use CachetHQ\Cachet\Models\Schedule;
use GrahamCampbell\Binput\Facades\Binput;
Expand Down Expand Up @@ -79,6 +81,7 @@ public function showAddSchedule()

return View::make('dashboard.maintenance.add')
->withPageTitle(trans('dashboard.schedule.add.title').' - '.trans('dashboard.dashboard'))
->withComponents(Component::all())
->withIncidentTemplates($incidentTemplates)
->withNotificationsEnabled($this->system->canNotifySubscribers());
}
Expand Down Expand Up @@ -121,9 +124,9 @@ public function addScheduleAction()
public function showEditSchedule(Schedule $schedule)
{
$incidentTemplates = IncidentTemplate::all();

return View::make('dashboard.maintenance.edit')
->withPageTitle(trans('dashboard.schedule.edit.title').' - '.trans('dashboard.dashboard'))
->withComponents(Component::all())
->withIncidentTemplates($incidentTemplates)
->withSchedule($schedule);
}
Expand Down
47 changes: 40 additions & 7 deletions app/Models/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use McCool\LaravelAutoPresenter\HasPresenter;

class Schedule extends Model implements HasPresenter
Expand Down Expand Up @@ -57,7 +58,7 @@ class Schedule extends Model implements HasPresenter
* @var string[]
*/
protected $attributes = [
'status' => self::UPCOMING,
'status' => self::UPCOMING,
'completed_at' => null,
];

Expand All @@ -67,9 +68,9 @@ class Schedule extends Model implements HasPresenter
* @var string[]
*/
protected $casts = [
'name' => 'string',
'message' => 'string',
'status' => 'int',
'name' => 'string',
'message' => 'string',
'status' => 'int',
'scheduled_at' => 'datetime',
'completed_at' => 'datetime',
];
Expand All @@ -95,9 +96,9 @@ class Schedule extends Model implements HasPresenter
* @var string[]
*/
public $rules = [
'name' => 'required|string',
'message' => 'nullable|string',
'status' => 'required|int|between:0,2',
'name' => 'required|string',
'message' => 'nullable|string',
'status' => 'required|int|between:0,2',
];

/**
Expand Down Expand Up @@ -143,6 +144,26 @@ public function components()
return $this->hasMany(ScheduleComponent::class);
}

/**
* Added function to connect components trough the DB directly. Terrible solution. Works for me.
*/
public function attachComponents(array $components)
{
$this->detachAllComponents();
foreach ($components as $componentId => $component){
if(isset($component['affected']) && $component['affected']){
$data = [
'schedule_id' => $this->id,
'component_id' => (int) $componentId,
'component_status' => (int) $component['status'],
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
];
DB::table('schedule_components')->insert($data);
}
}
}

/**
* Scope schedules that are uncompleted.
*
Expand Down Expand Up @@ -216,4 +237,16 @@ public function getPresenterClass()
{
return SchedulePresenter::class;
}

private function detachAllComponents(): void
{
DB::table('schedule_components')->where('schedule_id', $this->id)->delete();
}

public function delete()
{
$this->detachAllComponents();
// Do some stuff before delete
return parent::delete();
}
}
Loading