Cachet/app/Http/Controllers/Dashboard/ComponentController.php

303 lines
10 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (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\Http\Controllers\Dashboard;
2015-01-01 15:45:04 +00:00
2015-08-24 14:46:09 -05:00
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand;
2015-08-30 21:37:29 +01:00
use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
2015-09-18 15:26:34 +01:00
use CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand;
use CachetHQ\Cachet\Models\Component;
2015-01-04 20:56:10 +00:00
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Tag;
2015-01-02 12:05:50 +00:00
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
2015-01-01 15:45:04 +00:00
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class ComponentController extends Controller
2014-12-20 21:20:17 +00:00
{
use DispatchesJobs;
2015-11-04 15:02:56 +00:00
/**
* Array of sub-menu items.
*
* @var array
*/
2015-01-04 20:56:10 +00:00
protected $subMenu = [];
2015-11-04 15:02:56 +00:00
/**
* Creates a new component controller instance.
*
* @return void
*/
2015-01-04 20:56:10 +00:00
public function __construct()
{
$this->subMenu = [
'components' => [
2015-01-27 17:25:51 +00:00
'title' => trans('dashboard.components.components'),
'url' => route('dashboard.components.index'),
2015-01-27 17:25:37 +00:00
'icon' => 'ion-outlet',
2015-01-04 20:56:10 +00:00
'active' => false,
],
'groups' => [
'title' => trans_choice('dashboard.components.groups.groups', 2),
2015-01-05 12:01:51 +00:00
'url' => route('dashboard.components.groups'),
2015-01-04 20:56:10 +00:00
'icon' => 'ion-folder',
'active' => false,
],
];
2015-03-20 18:30:45 -06:00
View::share([
2015-07-02 16:37:38 +01:00
'sub_menu' => $this->subMenu,
'sub_title' => trans_choice('dashboard.components.components', 2),
2015-03-20 18:30:45 -06:00
]);
2015-01-04 20:56:10 +00:00
}
2014-12-20 21:20:17 +00:00
/**
* Shows the components view.
2014-12-29 23:07:46 +00:00
*
2014-12-20 21:20:17 +00:00
* @return \Illuminate\View\View
*/
public function showComponents()
{
$components = Component::orderBy('order')->orderBy('created_at')->get();
2015-01-04 20:56:10 +00:00
$this->subMenu['components']['active'] = true;
return View::make('dashboard.components.index')
->withPageTitle(trans_choice('dashboard.components.components', 2).' - '.trans('dashboard.dashboard'))
->withComponents($components)
->withSubMenu($this->subMenu);
2015-01-04 20:56:10 +00:00
}
/**
* Shows the component groups view.
*
* @return \Illuminate\View\View
*/
public function showComponentGroups()
{
$this->subMenu['groups']['active'] = true;
return View::make('dashboard.components.groups.index')
->withPageTitle(trans_choice('dashboard.components.groups.groups', 2).' - '.trans('dashboard.dashboard'))
->withGroups(ComponentGroup::orderBy('order')->get())
->withSubMenu($this->subMenu);
2014-12-20 21:20:17 +00:00
}
2014-12-20 21:20:17 +00:00
/**
* Shows the edit component view.
2014-12-29 23:07:46 +00:00
*
* @param \CachetHQ\Cachet\Models\Component $component
2014-12-29 23:07:46 +00:00
*
2014-12-20 21:20:17 +00:00
* @return \Illuminate\View\View
*/
public function showEditComponent(Component $component)
{
2015-01-04 20:56:10 +00:00
$groups = ComponentGroup::all();
$pageTitle = sprintf('"%s" - %s - %s', $component->name, trans('dashboard.components.edit.title'), trans('dashboard.dashboard'));
return View::make('dashboard.components.edit')
->withPageTitle($pageTitle)
->withComponent($component)
->withGroups($groups);
2014-12-20 21:20:17 +00:00
}
2014-12-20 20:40:48 +00:00
2014-12-20 21:20:17 +00:00
/**
* Updates a component.
2014-12-29 23:07:46 +00:00
*
* @param \CachetHQ\Cachet\Models\Component $component
2014-12-29 23:07:46 +00:00
*
2014-12-20 21:20:17 +00:00
* @return \Illuminate\Http\RedirectResponse
*/
public function updateComponentAction(Component $component)
{
2015-09-18 15:12:31 +01:00
$componentData = Binput::get('component');
$tags = array_pull($componentData, 'tags');
try {
2015-09-18 15:12:31 +01:00
$componentData['component'] = $component;
$component = $this->dispatchFromArray(UpdateComponentCommand::class, $componentData);
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.edit', ['id' => $component->id])
->withInput(Binput::all())
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.edit.failure')))
->withErrors($e->getMessageBag());
}
// The component was added successfully, so now let's deal with the tags.
2015-01-16 12:51:46 -06:00
$tags = preg_split('/ ?, ?/', $tags);
// For every tag, do we need to create it?
$componentTags = array_map(function ($taggable) use ($component) {
return Tag::firstOrCreate(['name' => $taggable])->id;
}, $tags);
$component->tags()->sync($componentTags);
return Redirect::route('dashboard.components.edit', ['id' => $component->id])
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.edit.success')));
2014-12-20 21:20:17 +00:00
}
2014-12-20 20:40:48 +00:00
2014-12-20 21:20:17 +00:00
/**
* Shows the add component view.
2014-12-29 23:07:46 +00:00
*
2014-12-20 21:20:17 +00:00
* @return \Illuminate\View\View
*/
public function showAddComponent()
{
return View::make('dashboard.components.add')
->withPageTitle(trans('dashboard.components.add.title').' - '.trans('dashboard.dashboard'))
->withGroups(ComponentGroup::all());
2014-12-20 21:20:17 +00:00
}
2014-12-20 20:40:48 +00:00
2014-12-20 21:20:17 +00:00
/**
* Creates a new component.
2014-12-29 23:07:46 +00:00
*
2014-12-20 21:20:17 +00:00
* @return \Illuminate\Http\RedirectResponse
*/
public function createComponentAction()
{
2015-09-18 15:12:31 +01:00
$componentData = Binput::get('component');
$tags = array_pull($componentData, 'tags');
try {
2015-09-18 15:12:31 +01:00
$component = $this->dispatchFromArray(AddComponentCommand::class, $componentData);
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.add')
->withInput(Binput::all())
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.add.failure')))
->withErrors($e->getMessageBag());
}
// The component was added successfully, so now let's deal with the tags.
2015-01-16 12:51:46 -06:00
$tags = preg_split('/ ?, ?/', $tags);
// For every tag, do we need to create it?
$componentTags = array_map(function ($taggable) use ($component) {
return Tag::firstOrCreate(['name' => $taggable])->id;
}, $tags);
$component->tags()->sync($componentTags);
return Redirect::route('dashboard.components.add')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));
2014-12-20 21:20:17 +00:00
}
2014-12-20 21:20:17 +00:00
/**
* Deletes a given component.
2014-12-29 23:07:46 +00:00
*
* @param \CachetHQ\Cachet\Models\Component $component
2014-12-29 23:07:46 +00:00
*
2014-12-20 21:20:17 +00:00
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteComponentAction(Component $component)
{
$this->dispatch(new RemoveComponentCommand($component));
return Redirect::route('dashboard.components.index');
2014-12-20 21:20:17 +00:00
}
2015-01-04 20:56:10 +00:00
/**
* Deletes a given component group.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup $group
*
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteComponentGroupAction(ComponentGroup $group)
{
$this->dispatch(new RemoveComponentGroupCommand($group));
return Redirect::route('dashboard.components.index');
}
2015-01-04 20:56:10 +00:00
/**
* Shows the add component group view.
*
* @return \Illuminate\View\View
*/
public function showAddComponentGroup()
{
return View::make('dashboard.components.groups.add')
->withPageTitle(trans('dashboard.components.groups.add.title').' - '.trans('dashboard.dashboard'));
2015-01-04 20:56:10 +00:00
}
2015-02-21 16:16:19 +00:00
/**
* Shows the edit component group view.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup $group
*
* @return \Illuminate\View\View
*/
public function showEditComponentGroup(ComponentGroup $group)
{
return View::make('dashboard.components.groups.edit')
->withPageTitle(trans('dashboard.components.groups.edit.title').' - '.trans('dashboard.dashboard'))
->withGroup($group);
2015-02-21 16:16:19 +00:00
}
2015-01-05 14:26:42 +00:00
/**
* Creates a new component.
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-01-04 20:56:10 +00:00
public function postAddComponentGroup()
{
try {
2015-08-30 21:37:29 +01:00
$group = $this->dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0)
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.groups.add')
->withInput(Binput::all())
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.groups.add.failure')))
->withErrors($e->getMessageBag());
}
return Redirect::route('dashboard.components.groups.add')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.groups.add.success')));
2015-01-04 20:56:10 +00:00
}
2015-02-21 16:16:19 +00:00
/**
* Updates a component group.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup $group
*
* @return \Illuminate\Http\RedirectResponse
*/
public function updateComponentGroupAction(ComponentGroup $group)
{
try {
2015-09-18 15:26:34 +01:00
$group = $this->dispatch(new UpdateComponentGroupCommand(
$group,
Binput::get('name'),
Binput::get('order', 0)
));
} catch (ValidationException $e) {
2015-09-18 15:26:34 +01:00
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
->withInput(Binput::all())
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.groups.edit.failure')))
->withErrors($e->getMessageBag());
}
2015-02-21 16:16:19 +00:00
2015-09-18 15:26:34 +01:00
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.groups.edit.success')));
2015-02-21 16:16:19 +00:00
}
}