Skip to content

Commit

Permalink
Merge pull request #2931 from nstapelbroek/feature/2895-custom-meta-d…
Browse files Browse the repository at this point in the history
…escriptions-per-incident

Custom meta descriptions per incident
  • Loading branch information
jbrooksuk committed Jan 27, 2019
2 parents 61cddfb + 33e5c34 commit 60a4c15
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/Bus/Commands/Incident/CreateIncidentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ final class CreateIncidentCommand
*
* @return void
*/
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [], $meta = [])
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [], array $meta = [])
{
$this->name = $name;
$this->status = $status;
Expand Down
12 changes: 11 additions & 1 deletion app/Bus/Commands/Incident/UpdateIncidentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ final class UpdateIncidentCommand
*/
public $template_vars;

/**
* Meta key/value pairs.
*
* @var array
*/
public $meta = [];

/**
* The validation rules.
*
Expand All @@ -122,6 +129,7 @@ final class UpdateIncidentCommand
'stickied' => 'nullable|bool',
'occurred_at' => 'nullable|string',
'template' => 'nullable|string',
'meta' => 'nullable|array',
];

/**
Expand All @@ -139,10 +147,11 @@ final class UpdateIncidentCommand
* @param string|null $occurred_at
* @param string|null $template
* @param array $template_vars
* @param array $meta
*
* @return void
*/
public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [])
public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [], array $meta = [])
{
$this->incident = $incident;
$this->name = $name;
Expand All @@ -156,5 +165,6 @@ public function __construct(Incident $incident, $name, $status, $message, $visib
$this->occurred_at = $occurred_at;
$this->template = $template;
$this->template_vars = $template_vars;
$this->meta = $meta;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CachetHQ\Cachet\Bus\Commands\Incident\CreateIncidentCommand;
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasCreatedEvent;
use CachetHQ\Cachet\Bus\Exceptions\Incident\InvalidIncidentTimestampException;
use CachetHQ\Cachet\Bus\Handlers\Traits\StoresMeta;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\IncidentTemplate;
Expand All @@ -32,6 +33,8 @@
*/
class CreateIncidentCommandHandler
{
use StoresMeta;

/**
* The authentication guard instance.
*
Expand Down Expand Up @@ -104,14 +107,7 @@ public function handle(CreateIncidentCommand $command)

// Store any meta?
if ($meta = $command->meta) {
foreach ($meta as $key => $value) {
Meta::create([
'key' => $key,
'value' => $value,
'meta_type' => 'incidents',
'meta_id' => $incident->id,
]);
}
$this->storeMeta($command->meta, 'incidents', $incident->id);
}

// Update the component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CachetHQ\Cachet\Bus\Commands\Incident\UpdateIncidentCommand;
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasUpdatedEvent;
use CachetHQ\Cachet\Bus\Exceptions\Incident\InvalidIncidentTimestampException;
use CachetHQ\Cachet\Bus\Handlers\Traits\StoresMeta;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\IncidentTemplate;
Expand All @@ -30,6 +31,8 @@
*/
class UpdateIncidentCommandHandler
{
use StoresMeta;

/**
* The authentication guard instance.
*
Expand Down Expand Up @@ -86,6 +89,11 @@ public function handle(UpdateIncidentCommand $command)
// Rather than making lots of updates, just fill and save.
$incident->save();

// Store any meta?
if ($meta = $command->meta) {
$this->storeMeta($command->meta, 'incidents', $incident->id);
}

// Update the component.
if ($component = Component::find($command->component_id)) {
execute(new UpdateComponentCommand(
Expand Down
81 changes: 81 additions & 0 deletions app/Bus/Handlers/Traits/StoresMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CachetHQ\Cachet\Bus\Handlers\Traits;

use CachetHQ\Cachet\Models\Meta;

trait StoresMeta
{
/**
* Stores all Meta values of a model.
*
* @param array $metaData
* @param string $metaType
* @param string|int $metaId
* @param string $metaModel
*
* @return void
*/
public function storeMeta($metaData, $metaType, $metaId, $metaModel = Meta::class)
{
// Validation required instead of type hinting because it could be passed as false or NULL
if (!is_array($metaData)) {
return;
}

foreach ($metaData as $key => $value) {
$modelInstance = call_user_func(
[$metaModel, 'firstOrNew'],
[
'key' => $key,
'meta_type' => $metaType,
'meta_id' => $metaId,
]
);

$value = $this->removeEmptyValues($value);
if (!empty($value)) {
$modelInstance->setAttribute('value', $value);
$modelInstance->save();
continue;
}

// The value is empty, remove the row
if ($modelInstance->exists) {
$modelInstance->delete();
}
}
}

/**
* Determine if a Value is empty.
*
* @param mixed $values
*
* @return array|mixed
*/
protected function removeEmptyValues($values)
{
if (!is_array($values)) {
return empty($values) ? null : $values;
}

foreach ($values as $key => $value) {
if (!empty($value)) {
continue;
}
unset($values[$key]);
}

return $values;
}
}
6 changes: 4 additions & 2 deletions app/Http/Controllers/Dashboard/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public function createIncidentAction()
Binput::get('stickied', false),
Binput::get('occurred_at'),
null,
[]
[],
['seo' => Binput::get('seo', [])]
));
} catch (ValidationException $e) {
return cachet_redirect('dashboard.incidents.create')
Expand Down Expand Up @@ -258,7 +259,8 @@ public function editIncidentAction(Incident $incident)
Binput::get('stickied', false),
Binput::get('occurred_at'),
null,
[]
[],
['seo' => Binput::get('seo', [])]
));
} catch (ValidationException $e) {
return cachet_redirect('dashboard.incidents.edit', ['id' => $incident->id])
Expand Down
5 changes: 5 additions & 0 deletions resources/lang/en/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@
'timezone' => 'Select Timezone',
],

'seo' => [
'title' => 'SEO Title',
'description' => 'SEO Description',
],

// Buttons
'add' => 'Add',
'save' => 'Save',
Expand Down
8 changes: 8 additions & 0 deletions resources/views/dashboard/incidents/add.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@
</label>
</div>
@endif
<div class="form-group">
<label>{{ trans('forms.seo.title') }}</label> <small class="text-muted">{{ trans('forms.optional') }}</small>
<input type="text" name="seo[title]" class="form-control" placeholder="{{ trans('forms.optional') }}">
</div>
<div class="form-group">
<label>{{ trans('forms.seo.description') }}</label> <small class="text-muted">{{ trans('forms.optional') }}</small>
<input type="text" name="seo[description]" class="form-control" placeholder="{{ trans('forms.optional') }}">
</div>
</fieldset>

<div class="form-group">
Expand Down
8 changes: 8 additions & 0 deletions resources/views/dashboard/incidents/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
<label>{{ trans('forms.incidents.occurred_at') }}</label> <small class="text-muted">{{ trans('forms.optional') }}</small>
<input type="text" name="occurred_at" class="form-control flatpickr-time" data-date-format="Y-m-d H:i" value="{{ $incident->occurred_at_datetimepicker }}" placeholder="{{ trans('forms.optional') }}">
</div>
<div class="form-group">
<label>{{ trans('forms.seo.title') }}</label> <small class="text-muted">{{ trans('forms.optional') }}</small>
<input type="text" name="seo[title]" class="form-control" value="{{ array_get($incident->meta, 'seo.title', '') }}">
</div>
<div class="form-group">
<label>{{ trans('forms.seo.description') }}</label> <small class="text-muted">{{ trans('forms.optional') }}</small>
<input type="text" name="seo[description]" class="form-control" value="{{ array_get($incident->meta, 'seo.description', '') }}">
</div>
</fieldset>

<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layout/master.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<meta name="description" content="@yield('description', trans('cachet.meta.description.overview', ['app' => $appName]))">

<meta property="og:type" content="website">
<meta property="og:title" content="{{ $siteTitle }}">
<meta property="og:title" content="@yield('title', $siteTitle)">
<meta property="og:image" content="/img/favicon.png">
<meta property="og:description" content="@yield('description', trans('cachet.meta.description.overview', ['app' => $appName]))">

Expand Down
4 changes: 2 additions & 2 deletions resources/views/single-incident.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@extends('layout.master')

@section('title', $incident->name.' | '.$siteTitle)
@section('title', array_get($incident->meta, 'seo.title', $incident->name).' | '.$siteTitle)

@section('description', trans('cachet.meta.description.incident', ['name' => $incident->name, 'date' => $incident->occurred_at_formatted]))
@section('description', array_get($incident->meta, 'seo.description', trans('cachet.meta.description.incident', ['name' => $incident->name, 'date' => $incident->occurred_at_formatted])))

@section('bodyClass', 'no-padding')

Expand Down
Loading

0 comments on commit 60a4c15

Please sign in to comment.