From 1a4727bac2ff51f15b38bb574ccef1a875b59125 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 8 Jan 2015 08:58:09 +0000 Subject: [PATCH] Incident Templates are now supported. --- app/assets/js/start.js | 26 ++++++++ app/lang/en/dashboard.php | 9 ++- app/lang/fr/dashboard.php | 6 ++ app/routes/dashboard.php | 20 +++++- app/views/dashboard/incidents/add.blade.php | 9 +++ .../incidents/templates/add.blade.php | 2 +- .../incidents/templates/edit.blade.php | 43 +++++++++++++ .../incidents/templates/index.blade.php | 35 +++++++++++ .../partials/dashboard/sidebar.blade.php | 8 +-- src/Http/Controllers/DashAPIController.php | 15 +++++ .../Controllers/DashIncidentController.php | 61 ++++++++++++++++++- src/Models/IncidentTemplate.php | 2 +- 12 files changed, 224 insertions(+), 12 deletions(-) create mode 100644 app/views/dashboard/incidents/templates/edit.blade.php create mode 100644 app/views/dashboard/incidents/templates/index.blade.php diff --git a/app/assets/js/start.js b/app/assets/js/start.js index 11eb9aae..c8e4a249 100644 --- a/app/assets/js/start.js +++ b/app/assets/js/start.js @@ -156,6 +156,32 @@ $(function() { }); }); + // Incident management + $('select[name=template]').on('change', function() { + var $this = $(this).find('option:selected'), + slug = $this.val(); + + // Only fetch the template if we've picked one. + if (slug) { + $.ajax({ + async: true, + dataType: 'json', + data: { + slug: slug + }, + url: '/dashboard/api/incidents/templates', + success: function(tpl) { + var $form = $('form[name=IncidentForm]'); + $form.find('input[name=incident\\[name\\]]').val(tpl.name); + $form.find('textarea[name=incident\\[message\\]]').val(tpl.template); + }, + error: function() { + (new CachetHQ.Notifier()).notify('There was an error finding that template.'); + } + }); + } + }); + // Banner removal JS $('#remove-banner').click(function(){ $('#banner-view').remove(); diff --git a/app/lang/en/dashboard.php b/app/lang/en/dashboard.php index f1d9c616..8695ba3d 100644 --- a/app/lang/en/dashboard.php +++ b/app/lang/en/dashboard.php @@ -9,6 +9,7 @@ return [ 'incidents' => 'Incidents', 'logged' => '{0} There are no incidents, good work.|You have logged one incident.|You have reported :count incidents.', 'incident-create-template' => 'Create Template', + 'incident-templates' => 'Incident Templates', 'add' => [ 'title' => 'Add an incident', 'success' => 'Incident added.', @@ -22,11 +23,17 @@ return [ // Incident templates 'templates' => [ - 'add' => [ + 'title' => 'Incident Templates', + 'add' => [ 'title' => 'Create an incident template', 'success' => 'Template created.', 'failure' => 'Something went wrong with the incident template.', ], + 'edit' => [ + 'title' => 'Edit template', + 'success' => 'Template has been updated!', + 'failure' => 'Something went wrong updating the incident template', + ], ], ], diff --git a/app/lang/fr/dashboard.php b/app/lang/fr/dashboard.php index c9b13d67..fff5d6d3 100644 --- a/app/lang/fr/dashboard.php +++ b/app/lang/fr/dashboard.php @@ -9,6 +9,7 @@ return [ 'incidents' => 'Incidents', 'logged' => '{0} Il n\'y a aucun incident, bien joué !|Vous avez reporté un incident.|Vous avez reporté :count incidents.', 'incident-create-template' => 'Créer un modèle', + 'incident-templates' => 'Modèles d\'incident', 'add' => [ 'title' => 'Ajouter un incident', 'success' => 'Incident ajouté.', @@ -27,6 +28,11 @@ return [ 'success' => 'Modèle créé.', 'failure' => 'Il s\'est passé quelque chose avec ce modèle d\'incident.', ], + 'edit' => [ + 'title' => 'Modifier un modèle', + 'success' => 'Modèle mis à jour.', + 'failure' => 'Une erreur s\'est produite lors de la mise à jour du modèle.', + ], ], ], diff --git a/app/routes/dashboard.php b/app/routes/dashboard.php index 2ad831f5..6c935283 100644 --- a/app/routes/dashboard.php +++ b/app/routes/dashboard.php @@ -47,11 +47,24 @@ Route::group(['before' => 'auth', 'prefix' => 'dashboard', 'namespace' => 'Cache Route::delete('{incident}/delete', 'DashIncidentController@deleteIncidentAction'); Route::get('{incident}/edit', 'DashIncidentController@showEditIncidentAction'); Route::post('{incident}/edit', 'DashIncidentController@editIncidentAction'); - Route::get('template', [ - 'as' => 'dashboard.incidents.template', + }); + + // Incident Templates + Route::group(['prefix' => 'templates'], function () { + Route::get('/', [ + 'as' => 'dashboard.templates', + 'uses' => 'DashIncidentController@showTemplates', + ]); + + Route::get('add', [ + 'as' => 'dashboard.templates.add', 'uses' => 'DashIncidentController@showAddIncidentTemplate', ]); - Route::post('template', 'DashIncidentController@createIncidentTemplateAction'); + Route::post('add', 'DashIncidentController@createIncidentTemplateAction'); + + Route::get('{incident_template}/edit', 'DashIncidentController@showEditTemplateAction'); + Route::post('{incident_template}/edit', 'DashIncidentController@editTemplateAction'); + Route::delete('{incident_template}/delete', 'DashIncidentController@deleteTemplateAction'); }); // Metrics @@ -119,6 +132,7 @@ Route::group(['before' => 'auth', 'prefix' => 'dashboard', 'namespace' => 'Cache // Internal API. // This should only be used for making requests within the dashboard. Route::group(['prefix' => 'api'], function () { + Route::get('incidents/templates', 'DashAPIController@getIncidentTemplate'); Route::post('components/order', 'DashAPIController@postUpdateComponentOrder'); Route::post('components/{component}', 'DashAPIController@postUpdateComponent'); }); diff --git a/app/views/dashboard/incidents/add.blade.php b/app/views/dashboard/incidents/add.blade.php index eab50098..bb937808 100644 --- a/app/views/dashboard/incidents/add.blade.php +++ b/app/views/dashboard/incidents/add.blade.php @@ -16,6 +16,15 @@ @include('partials.dashboard.errors') {{ Form::open(['name' => 'IncidentForm', 'class' => 'form-vertical', 'role' => 'form']) }}
+
+ + +
diff --git a/app/views/dashboard/incidents/templates/add.blade.php b/app/views/dashboard/incidents/templates/add.blade.php index 45115a2b..a4640b3b 100644 --- a/app/views/dashboard/incidents/templates/add.blade.php +++ b/app/views/dashboard/incidents/templates/add.blade.php @@ -6,7 +6,7 @@
- {{ trans('dashboard.incidents.incidents') }} + {{ trans('dashboard.incidents.templates.title') }} > {{ trans('dashboard.incidents.templates.add.title') }} diff --git a/app/views/dashboard/incidents/templates/edit.blade.php b/app/views/dashboard/incidents/templates/edit.blade.php new file mode 100644 index 00000000..fbe83dc4 --- /dev/null +++ b/app/views/dashboard/incidents/templates/edit.blade.php @@ -0,0 +1,43 @@ +@extends('layout.dashboard') + +@section('content') +
+ + + {{ trans('dashboard.incidents.templates.title') }} + + > {{ trans('dashboard.incidents.templates.edit.title') }} +
+
+
+
+ @if($updatedTemplate = Session::get('updatedTemplate')) +
+ @if($updatedTemplate->isValid()) + {{ sprintf("%s %s", trans('dashboard.notifications.awesome'), trans('dashboard.incidents.templates.edit.success')) }} + @else + {{ sprintf("%s %s", trans('dashboard.notifications.whoops'), trans('dashboard.incidents.templates.edit.failure').' '.$updatedTemplate->getErrors()) }} + @endif +
+ @endif + + {{ Form::open(['name' => 'IncidentTemplateForm', 'class' => 'form-vertical', 'role' => 'form']) }} +
+
+ + +
+
+ + +
+
+ + + {{ Form::close() }} +
+
+
+@stop diff --git a/app/views/dashboard/incidents/templates/index.blade.php b/app/views/dashboard/incidents/templates/index.blade.php new file mode 100644 index 00000000..561e6b07 --- /dev/null +++ b/app/views/dashboard/incidents/templates/index.blade.php @@ -0,0 +1,35 @@ +@extends('layout.dashboard') + +@section('content') +
+ + + {{ trans('dashboard.incidents.templates.title') }} + + + {{ trans('dashboard.incidents.templates.add.title') }} + +
+
+
+
+
+ @foreach($incidentTemplates as $template) +
+
+ {{ $template->name }} +

{{ $template->template }}

+
+ +
+ @endforeach +
+
+
+
+@stop diff --git a/app/views/partials/dashboard/sidebar.blade.php b/app/views/partials/dashboard/sidebar.blade.php index 2ad2a0d3..d488ccbd 100644 --- a/app/views/partials/dashboard/sidebar.blade.php +++ b/app/views/partials/dashboard/sidebar.blade.php @@ -37,11 +37,11 @@ {{ trans('dashboard.incidents.incidents') }} - {{--
  • - - {{ trans('cachet.dashboard.incident-create-template') }} +
  • + + {{ trans('dashboard.incidents.incident-templates') }} -
  • --}} +
  • diff --git a/src/Http/Controllers/DashAPIController.php b/src/Http/Controllers/DashAPIController.php index d3ef6af6..af59ca12 100644 --- a/src/Http/Controllers/DashAPIController.php +++ b/src/Http/Controllers/DashAPIController.php @@ -3,8 +3,10 @@ namespace CachetHQ\Cachet\Http\Controllers; use CachetHQ\Cachet\Models\Component; +use CachetHQ\Cachet\Models\IncidentTemplate; use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Routing\Controller; class DashAPIController extends Controller @@ -44,4 +46,17 @@ class DashAPIController extends Controller return $componentData; } + + public function getIncidentTemplate() + { + $templateSlug = Binput::get('slug'); + + $template = IncidentTemplate::where('slug', $templateSlug)->first(); + + if ($template) { + return $template; + } + + throw new ModelNotFoundException('Incident template for '.$templateSlug.' could not be found.'); + } } diff --git a/src/Http/Controllers/DashIncidentController.php b/src/Http/Controllers/DashIncidentController.php index 21f932b5..aee21e03 100644 --- a/src/Http/Controllers/DashIncidentController.php +++ b/src/Http/Controllers/DashIncidentController.php @@ -35,8 +35,22 @@ class DashIncidentController extends Controller public function showAddIncident() { return View::make('dashboard.incidents.add')->with([ - 'pageTitle' => trans('dashboard.incidents.add.title').' - '.trans('dashboard.dashboard'), - 'components' => Component::all(), + 'pageTitle' => trans('dashboard.incidents.add.title').' - '.trans('dashboard.dashboard'), + 'components' => Component::all(), + 'incidentTemplates' => IncidentTemplate::all(), + ]); + } + + /** + * Shows the incident templates. + * + * @return \Illuminate\View\View + */ + public function showTemplates() + { + return View::make('dashboard.incidents.templates.index')->with([ + 'pageTitle' => trans('dashboard.incidents.templates.title').' - '.trans('dashboard.dashboard'), + 'incidentTemplates' => IncidentTemplate::all(), ]); } @@ -89,6 +103,35 @@ class DashIncidentController extends Controller ]); } + /** + * 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.incidents.templates.edit')->with([ + 'pageTitle' => trans('dashboard.incidents.templates.edit.title').' - '.trans('dashboard.dashboard'), + 'template' => $template, + ]); + } + + /** + * Deletes an incident template. + * + * @param \CachetHQ\Cachet\Models\IncidentTemplate $template + * + * @return \Illuminate\Http\RedirectResponse + */ + public function deleteTemplateAction(IncidentTemplate $template) + { + $template->delete(); + + return Redirect::back(); + } + /** * Creates a new incident template. * @@ -178,4 +221,18 @@ class DashIncidentController extends Controller return Redirect::to('dashboard/incidents')->with('success', $successMsg); } + + /** + * Edit an incident template. + * + * @param \CachetHQ\Cachet\Models\IncidentTemplate $template + * + * @return \Illuminate\Http\RedirectResponse + */ + public function editTemplateAction(IncidentTemplate $template) + { + $template->update(Binput::get('template')); + + return Redirect::back()->with('updatedTemplate', $template); + } } diff --git a/src/Models/IncidentTemplate.php b/src/Models/IncidentTemplate.php index 15d7a37d..1976b36f 100644 --- a/src/Models/IncidentTemplate.php +++ b/src/Models/IncidentTemplate.php @@ -24,7 +24,7 @@ class IncidentTemplate extends Model * @var string[] */ protected $rules = [ - 'name' => 'alpha|required', + 'name' => 'required', 'template' => 'required', ];