diff --git a/app/Http/Controllers/Dashboard/IncidentTemplateController.php b/app/Http/Controllers/Dashboard/IncidentTemplateController.php new file mode 100644 index 00000000..44a291d1 --- /dev/null +++ b/app/Http/Controllers/Dashboard/IncidentTemplateController.php @@ -0,0 +1,166 @@ + + */ +class IncidentTemplateController extends Controller +{ + /** + * Stores the sub-sidebar tree list. + * + * @var array + */ + protected $subMenu = []; + + /** + * The guard instance. + * + * @var \Illuminate\Contracts\Auth\Guard + */ + protected $auth; + + /** + * The system instance. + * + * @var \CachetHQ\Cachet\Integrations\Contracts\System + */ + protected $system; + + /** + * Creates a new incident controller instance. + * + * @param \Illuminate\Contracts\Auth\Guard $auth + * + * @return void + */ + public function __construct(Guard $auth, System $system) + { + $this->auth = $auth; + $this->system = $system; + + View::share('sub_title', trans('dashboard.incidents.title')); + } + + /** + * Shows the incident templates. + * + * @return \Illuminate\View\View + */ + public function showTemplates() + { + return View::make('dashboard.templates.index') + ->withPageTitle(trans('dashboard.incidents.templates.title').' - '.trans('dashboard.dashboard')) + ->withIncidentTemplates(IncidentTemplate::all()); + } + + /** + * Shows the add incident template view. + * + * @return \Illuminate\View\View + */ + public function showAddIncidentTemplate() + { + return View::make('dashboard.templates.add') + ->withPageTitle(trans('dashboard.incidents.templates.add.title').' - '.trans('dashboard.dashboard')); + } + + /** + * Shows the edit incident template view. + * + * @param \CachetHQ\Cachet\Models\IncidentTemplate $template + * + * @return \Illuminate\View\View + */ + public function showEditTemplateAction(IncidentTemplate $template) + { + return View::make('dashboard.templates.edit') + ->withPageTitle(trans('dashboard.incidents.templates.edit.title').' - '.trans('dashboard.dashboard')) + ->withTemplate($template); + } + + /** + * Deletes an incident template. + * + * @param \CachetHQ\Cachet\Models\IncidentTemplate $template + * + * @return \Illuminate\Http\RedirectResponse + */ + public function deleteTemplateAction(IncidentTemplate $template) + { + $template->delete(); + + return cachet_redirect('dashboard.templates') + ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.templates.delete.success'))); + } + + /** + * Creates a new incident template. + * + * @return \Illuminate\Http\RedirectResponse + */ + public function createIncidentTemplateAction() + { + try { + IncidentTemplate::create([ + 'name' => Binput::get('name'), + 'template' => Binput::get('template', null, false, false), + ]); + } catch (ValidationException $e) { + return cachet_redirect('dashboard.templates.create') + ->withInput(Binput::all()) + ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.incidents.templates.add.failure'))) + ->withErrors($e->getMessageBag()); + } + + return cachet_redirect('dashboard.templates') + ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.templates.add.success'))); + } + + /** + * Edit an incident template. + * + * @param \CachetHQ\Cachet\Models\IncidentTemplate $template + * + * @return \Illuminate\Http\RedirectResponse + */ + public function editTemplateAction(IncidentTemplate $template) + { + try { + $template->update(Binput::get('template')); + } catch (ValidationException $e) { + return cachet_redirect('dashboard.templates.edit', ['id' => $template->id]) + ->withUpdatedTemplate($template) + ->withTemplateErrors($e->getMessageBag()->getErrors()); + } + + return cachet_redirect('dashboard.templates.edit', ['id' => $template->id]) + ->withUpdatedTemplate($template); + } +} diff --git a/app/Http/Routes/Dashboard/TemplateRoutes.php b/app/Http/Routes/Dashboard/TemplateRoutes.php index 289f9752..ee4ed5df 100644 --- a/app/Http/Routes/Dashboard/TemplateRoutes.php +++ b/app/Http/Routes/Dashboard/TemplateRoutes.php @@ -44,29 +44,29 @@ class TemplateRoutes ], function (Registrar $router) { $router->get('/', [ 'as' => 'get:dashboard.templates', - 'uses' => 'IncidentController@showTemplates', + 'uses' => 'IncidentTemplateController@showTemplates', ]); $router->get('create', [ 'as' => 'get:dashboard.templates.create', - 'uses' => 'IncidentController@showAddIncidentTemplate', + 'uses' => 'IncidentTemplateController@showAddIncidentTemplate', ]); $router->post('create', [ 'as' => 'post:dashboard.templates.create', - 'uses' => 'IncidentController@createIncidentTemplateAction', + 'uses' => 'IncidentTemplateController@createIncidentTemplateAction', ]); $router->get('{incident_template}', [ 'as' => 'get:dashboard.templates.edit', - 'uses' => 'IncidentController@showEditTemplateAction', + 'uses' => 'IncidentTemplateController@showEditTemplateAction', ]); $router->post('{incident_template}', [ 'as' => 'post:dashboard.templates.edit', - 'uses' => 'IncidentController@editTemplateAction', + 'uses' => 'IncidentTemplateController@editTemplateAction', ]); $router->delete('{incident_template}', [ 'as' => 'delete:dashboard.templates.delete', - 'uses' => 'IncidentController@deleteTemplateAction', + 'uses' => 'IncidentTemplateController@deleteTemplateAction', ]); }); }