2014-12-20 18:53:52 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-08-31 18:59:17 +01:00
|
|
|
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;
|
2015-08-24 14:38:10 -05:00
|
|
|
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
|
|
|
|
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
|
2015-10-14 17:19:24 +01:00
|
|
|
use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand;
|
2015-08-30 21:37:29 +01:00
|
|
|
use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
|
2015-08-26 13:36:10 +01:00
|
|
|
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
|
2015-09-18 15:26:34 +01:00
|
|
|
use CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand;
|
2015-01-02 00:18:19 +00:00
|
|
|
use CachetHQ\Cachet\Models\Component;
|
2015-01-04 20:56:10 +00:00
|
|
|
use CachetHQ\Cachet\Models\ComponentGroup;
|
2015-01-16 09:51:22 +00:00
|
|
|
use CachetHQ\Cachet\Models\Tag;
|
2015-01-02 12:05:50 +00:00
|
|
|
use GrahamCampbell\Binput\Facades\Binput;
|
2015-08-24 14:38:10 -05:00
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2015-08-05 17:18:51 -05:00
|
|
|
use Illuminate\Routing\Controller;
|
2015-01-01 15:45:04 +00:00
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
|
2015-08-05 17:18:51 -05:00
|
|
|
class ComponentController extends Controller
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2015-08-24 14:38:10 -05: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'),
|
2015-08-07 14:52:44 +01:00
|
|
|
'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' => [
|
2015-01-05 11:12:34 +00:00
|
|
|
'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()
|
|
|
|
{
|
2015-01-01 13:37:47 +00:00
|
|
|
$components = Component::orderBy('order')->orderBy('created_at')->get();
|
2014-12-20 18:53:52 +00:00
|
|
|
|
2015-01-04 20:56:10 +00:00
|
|
|
$this->subMenu['components']['active'] = true;
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
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;
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
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 18:53:52 +00:00
|
|
|
|
2014-12-20 21:20:17 +00:00
|
|
|
/**
|
|
|
|
* Shows the edit component view.
|
2014-12-29 23:07:46 +00:00
|
|
|
*
|
2015-01-02 00:18:19 +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();
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
$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
|
|
|
*
|
2015-01-02 00:18:19 +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');
|
2015-03-06 08:34:47 +00:00
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
try {
|
2015-09-18 15:12:31 +01:00
|
|
|
$componentData['component'] = $component;
|
2015-10-14 17:19:24 +01:00
|
|
|
$component = $this->dispatchFromArray(UpdateComponentCommand::class, $componentData);
|
2015-08-03 22:32:36 +01:00
|
|
|
} catch (ValidationException $e) {
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.edit', ['id' => $component->id])
|
2015-08-03 22:32:36 +01:00
|
|
|
->withInput(Binput::all())
|
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.edit.failure')))
|
|
|
|
->withErrors($e->getMessageBag());
|
2015-01-09 20:21:29 -06:00
|
|
|
}
|
|
|
|
|
2015-01-16 09:51:22 +00:00
|
|
|
// 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);
|
2015-01-16 09:51:22 +00:00
|
|
|
|
|
|
|
// For every tag, do we need to create it?
|
|
|
|
$componentTags = array_map(function ($taggable) use ($component) {
|
2015-08-03 22:32:36 +01:00
|
|
|
return Tag::firstOrCreate(['name' => $taggable])->id;
|
2015-01-16 09:51:22 +00:00
|
|
|
}, $tags);
|
|
|
|
|
|
|
|
$component->tags()->sync($componentTags);
|
|
|
|
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.edit', ['id' => $component->id])
|
2015-08-03 22:32:36 +01:00
|
|
|
->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()
|
|
|
|
{
|
2015-08-03 22:32:36 +01:00
|
|
|
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');
|
2015-01-16 09:51:22 +00:00
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
try {
|
2015-09-18 15:12:31 +01:00
|
|
|
$component = $this->dispatchFromArray(AddComponentCommand::class, $componentData);
|
2015-08-03 22:32:36 +01:00
|
|
|
} catch (ValidationException $e) {
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.add')
|
2015-08-03 22:32:36 +01:00
|
|
|
->withInput(Binput::all())
|
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.add.failure')))
|
|
|
|
->withErrors($e->getMessageBag());
|
2015-01-09 20:21:29 -06:00
|
|
|
}
|
|
|
|
|
2015-01-16 09:51:22 +00:00
|
|
|
// 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);
|
2015-01-16 09:51:22 +00:00
|
|
|
|
|
|
|
// For every tag, do we need to create it?
|
|
|
|
$componentTags = array_map(function ($taggable) use ($component) {
|
2015-08-03 22:32:36 +01:00
|
|
|
return Tag::firstOrCreate(['name' => $taggable])->id;
|
2015-01-16 09:51:22 +00:00
|
|
|
}, $tags);
|
|
|
|
|
|
|
|
$component->tags()->sync($componentTags);
|
|
|
|
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.add')
|
2015-08-03 22:32:36 +01:00
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));
|
2014-12-20 21:20:17 +00:00
|
|
|
}
|
2014-12-20 18:53:52 +00:00
|
|
|
|
2014-12-20 21:20:17 +00:00
|
|
|
/**
|
|
|
|
* Deletes a given component.
|
2014-12-29 23:07:46 +00:00
|
|
|
*
|
2015-01-02 00:18:19 +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)
|
|
|
|
{
|
2015-08-24 14:38:10 -05:00
|
|
|
$this->dispatch(new RemoveComponentCommand($component));
|
2014-12-20 18:53:52 +00:00
|
|
|
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.index');
|
2014-12-20 21:20:17 +00:00
|
|
|
}
|
2015-01-04 20:56:10 +00:00
|
|
|
|
2015-01-05 15:21:09 +00:00
|
|
|
/**
|
|
|
|
* Deletes a given component group.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\ComponentGroup $group
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function deleteComponentGroupAction(ComponentGroup $group)
|
|
|
|
{
|
2015-08-26 13:36:10 +01:00
|
|
|
$this->dispatch(new RemoveComponentGroupCommand($group));
|
2015-01-05 15:21:09 +00:00
|
|
|
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.index');
|
2015-01-05 15:21:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-04 20:56:10 +00:00
|
|
|
/**
|
|
|
|
* Shows the add component group view.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function showAddComponentGroup()
|
|
|
|
{
|
2015-08-03 22:32:36 +01:00
|
|
|
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)
|
|
|
|
{
|
2015-08-03 22:32:36 +01:00
|
|
|
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()
|
|
|
|
{
|
2015-08-03 22:32:36 +01:00
|
|
|
try {
|
2015-08-30 21:37:29 +01:00
|
|
|
$group = $this->dispatch(new AddComponentGroupCommand(
|
|
|
|
Binput::get('name'),
|
|
|
|
Binput::get('order', 0)
|
|
|
|
));
|
2015-08-03 22:32:36 +01:00
|
|
|
} catch (ValidationException $e) {
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.groups.add')
|
2015-08-03 22:32:36 +01:00
|
|
|
->withInput(Binput::all())
|
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.groups.add.failure')))
|
|
|
|
->withErrors($e->getMessageBag());
|
2015-01-09 20:21:29 -06:00
|
|
|
}
|
|
|
|
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.components.groups.add')
|
2015-08-03 22:32:36 +01:00
|
|
|
->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)
|
|
|
|
{
|
2015-08-03 22:32:36 +01:00
|
|
|
try {
|
2015-09-18 15:26:34 +01:00
|
|
|
$group = $this->dispatch(new UpdateComponentGroupCommand(
|
|
|
|
$group,
|
|
|
|
Binput::get('name'),
|
|
|
|
Binput::get('order', 0)
|
|
|
|
));
|
2015-08-03 22:32:36 +01:00
|
|
|
} catch (ValidationException $e) {
|
2015-09-18 15:26:34 +01:00
|
|
|
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
|
2015-08-03 22:32:36 +01:00
|
|
|
->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])
|
2015-08-03 22:32:36 +01:00
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.groups.edit.success')));
|
2015-02-21 16:16:19 +00:00
|
|
|
}
|
2014-12-20 18:53:52 +00:00
|
|
|
}
|