2014-12-20 18:53:52 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
|
|
|
* (c) James Brooks <james@cachethq.io>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-03-20 18:30:45 -06:00
|
|
|
namespace CachetHQ\Cachet\Http\Controllers\Admin;
|
2015-01-01 15:45:04 +00:00
|
|
|
|
2015-03-21 02:21:20 -06:00
|
|
|
use CachetHQ\Cachet\Http\Controllers\AbstractController;
|
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-03-06 08:34:47 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2015-01-01 15:45:04 +00:00
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
|
2015-03-21 02:21:20 -06:00
|
|
|
class ComponentController extends AbstractController
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2015-01-04 20:56:10 +00:00
|
|
|
protected $subMenu = [];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->subMenu = [
|
|
|
|
'components' => [
|
2015-01-27 17:25:51 +00:00
|
|
|
'title' => trans('dashboard.components.components'),
|
2015-01-05 12:01:51 +00:00
|
|
|
'url' => route('dashboard.components'),
|
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([
|
|
|
|
'subMenu' => $this->subMenu,
|
|
|
|
'subTitle' => trans_choice('dashboard.components.components', 2),
|
|
|
|
]);
|
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-01-01 10:39:22 +00:00
|
|
|
return View::make('dashboard.components.index')->with([
|
2015-01-05 11:12:34 +00:00
|
|
|
'pageTitle' => trans_choice('dashboard.components.components', 2).' - '.trans('dashboard.dashboard'),
|
2014-12-20 21:20:17 +00:00
|
|
|
'components' => $components,
|
2015-01-04 20:56:10 +00:00
|
|
|
'subMenu' => $this->subMenu,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the component groups view.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function showComponentGroups()
|
|
|
|
{
|
|
|
|
$this->subMenu['groups']['active'] = true;
|
|
|
|
|
2015-01-05 11:12:34 +00:00
|
|
|
return View::make('dashboard.components.groups.index')->with([
|
|
|
|
'pageTitle' => trans_choice('dashboard.components.groups.groups', 2).' - '.trans('dashboard.dashboard'),
|
2015-01-04 20:56:10 +00:00
|
|
|
'groups' => ComponentGroup::all(),
|
|
|
|
'subMenu' => $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-01-14 16:04:15 +00:00
|
|
|
$pageTitle = sprintf(
|
|
|
|
'"%s" - %s - %s',
|
|
|
|
$component->name,
|
|
|
|
trans('dashboard.components.edit.title'),
|
|
|
|
trans('dashboard.dashboard')
|
|
|
|
);
|
|
|
|
|
2015-01-01 10:39:22 +00:00
|
|
|
return View::make('dashboard.components.edit')->with([
|
2015-01-14 16:04:15 +00:00
|
|
|
'pageTitle' => $pageTitle,
|
2014-12-20 21:20:17 +00:00
|
|
|
'component' => $component,
|
2015-01-04 20:56:10 +00:00
|
|
|
'groups' => $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-01-02 12:05:50 +00:00
|
|
|
$_component = Binput::get('component');
|
2015-03-06 08:34:47 +00:00
|
|
|
$_component['user_id'] = Auth::user()->id;
|
2015-01-16 09:51:22 +00:00
|
|
|
$tags = array_pull($_component, 'tags');
|
2015-03-06 08:34:47 +00:00
|
|
|
|
2014-12-20 21:20:17 +00:00
|
|
|
$component->update($_component);
|
2014-12-20 20:40:48 +00:00
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
if (!$component->isValid()) {
|
2015-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Edit Component',
|
|
|
|
'success' => false,
|
|
|
|
]);
|
|
|
|
|
2015-01-09 20:21:29 -06:00
|
|
|
return Redirect::back()->withInput(Binput::all())
|
2015-01-14 16:04:15 +00:00
|
|
|
->with('title', sprintf(
|
2015-01-14 17:07:36 +00:00
|
|
|
'<strong>%s</strong> %s',
|
2015-01-14 16:04:15 +00:00
|
|
|
trans('dashboard.notifications.whoops'),
|
|
|
|
trans('dashboard.components.edit.failure')
|
|
|
|
))
|
2015-01-09 20:21:29 -06:00
|
|
|
->with('errors', $component->getErrors());
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Edit Component',
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
|
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) {
|
|
|
|
return Tag::firstOrCreate([
|
|
|
|
'name' => $taggable,
|
|
|
|
])->id;
|
|
|
|
}, $tags);
|
|
|
|
|
|
|
|
$component->tags()->sync($componentTags);
|
|
|
|
|
2015-01-14 16:04:15 +00:00
|
|
|
$successMsg = sprintf(
|
2015-01-14 17:07:36 +00:00
|
|
|
'<strong>%s</strong> %s',
|
2015-01-14 16:04:15 +00:00
|
|
|
trans('dashboard.notifications.awesome'),
|
|
|
|
trans('dashboard.components.edit.success')
|
|
|
|
);
|
|
|
|
|
|
|
|
return Redirect::back()->with('success', $successMsg);
|
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-01-04 20:56:10 +00:00
|
|
|
$groups = ComponentGroup::all();
|
|
|
|
|
2015-01-01 10:39:22 +00:00
|
|
|
return View::make('dashboard.components.add')->with([
|
2015-01-05 11:12:34 +00:00
|
|
|
'pageTitle' => trans('dashboard.components.add.title').' - '.trans('dashboard.dashboard'),
|
2015-01-04 20:56:10 +00:00
|
|
|
'groups' => $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
|
|
|
/**
|
|
|
|
* 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-01-02 12:05:50 +00:00
|
|
|
$_component = Binput::get('component');
|
2015-03-06 08:34:47 +00:00
|
|
|
$_component['user_id'] = Auth::user()->id;
|
2015-01-16 09:51:22 +00:00
|
|
|
// We deal with tags separately.
|
|
|
|
$tags = array_pull($_component, 'tags');
|
|
|
|
|
2014-12-20 21:20:17 +00:00
|
|
|
$component = Component::create($_component);
|
2014-12-20 18:53:52 +00:00
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
if (!$component->isValid()) {
|
2015-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Created Component',
|
|
|
|
'success' => false,
|
|
|
|
]);
|
|
|
|
|
2015-01-09 20:21:29 -06:00
|
|
|
return Redirect::back()->withInput(Binput::all())
|
2015-01-14 16:04:15 +00:00
|
|
|
->with('title', sprintf(
|
2015-01-14 17:07:36 +00:00
|
|
|
'<strong>%s</strong> %s',
|
2015-01-14 16:04:15 +00:00
|
|
|
trans('dashboard.notifications.whoops'),
|
|
|
|
trans('dashboard.components.add.failure')
|
|
|
|
))
|
2015-01-09 20:21:29 -06:00
|
|
|
->with('errors', $component->getErrors());
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Created Component',
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
|
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) {
|
|
|
|
return Tag::firstOrCreate([
|
|
|
|
'name' => $taggable,
|
|
|
|
])->id;
|
|
|
|
}, $tags);
|
|
|
|
|
|
|
|
$component->tags()->sync($componentTags);
|
|
|
|
|
2015-01-14 16:04:15 +00:00
|
|
|
$successMsg = sprintf(
|
2015-01-14 17:07:36 +00:00
|
|
|
'<strong>%s</strong> %s',
|
2015-01-14 16:04:15 +00:00
|
|
|
trans('dashboard.notifications.awesome'),
|
|
|
|
trans('dashboard.components.add.success')
|
|
|
|
);
|
|
|
|
|
|
|
|
return Redirect::back()->with('success', $successMsg);
|
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-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Deleted Component',
|
|
|
|
]);
|
|
|
|
|
2014-12-20 21:20:17 +00:00
|
|
|
$component->delete();
|
2014-12-20 18:53:52 +00:00
|
|
|
|
2014-12-20 21:20:17 +00:00
|
|
|
return Redirect::back();
|
|
|
|
}
|
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-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Deleted Component Group',
|
|
|
|
]);
|
|
|
|
|
2015-03-20 18:30:45 -06:00
|
|
|
$group->components->map(function ($component) {
|
|
|
|
$component->update([
|
|
|
|
'group_id' => 0,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2015-01-05 15:21:09 +00:00
|
|
|
$group->delete();
|
|
|
|
|
|
|
|
return Redirect::back();
|
|
|
|
}
|
|
|
|
|
2015-01-04 20:56:10 +00:00
|
|
|
/**
|
|
|
|
* Shows the add component group view.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function showAddComponentGroup()
|
|
|
|
{
|
2015-01-05 13:50:41 +00:00
|
|
|
return View::make('dashboard.components.groups.add')->with([
|
2015-01-05 11:12:34 +00:00
|
|
|
'pageTitle' => 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')->with([
|
|
|
|
'pageTitle' => trans('dashboard.components.groups.edit.title').' - '.trans('dashboard.dashboard'),
|
|
|
|
'group' => $group,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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-01-05 22:18:22 +00:00
|
|
|
$group = ComponentGroup::create(Binput::get('group'));
|
2015-01-04 20:56:10 +00:00
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
if (!$group->isValid()) {
|
2015-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Created Component Group',
|
|
|
|
'success' => false,
|
|
|
|
]);
|
|
|
|
|
2015-01-09 20:21:29 -06:00
|
|
|
return Redirect::back()->withInput(Binput::all())
|
2015-01-14 16:04:15 +00:00
|
|
|
->with('title', sprintf(
|
2015-01-14 17:07:36 +00:00
|
|
|
'<strong>%s</strong> %s',
|
2015-01-14 16:04:15 +00:00
|
|
|
trans('dashboard.notifications.whoops'),
|
|
|
|
trans('dashboard.components.groups.add.failure')
|
|
|
|
))
|
2015-01-09 20:21:29 -06:00
|
|
|
->with('errors', $group->getErrors());
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:24:34 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Created Component Group',
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
|
2015-01-14 16:04:15 +00:00
|
|
|
$successMsg = sprintf(
|
2015-01-14 17:07:36 +00:00
|
|
|
'<strong>%s</strong> %s',
|
2015-01-14 16:04:15 +00:00
|
|
|
trans('dashboard.notifications.awesome'),
|
|
|
|
trans('dashboard.components.groups.add.success')
|
|
|
|
);
|
|
|
|
|
|
|
|
return Redirect::back()->with('success', $successMsg);
|
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)
|
|
|
|
{
|
|
|
|
$groupData = Binput::get('group');
|
|
|
|
$group->update($groupData);
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
if (!$group->isValid()) {
|
2015-02-21 16:16:19 +00:00
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Edit Component Group',
|
|
|
|
'success' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return Redirect::back()->withInput(Binput::all())
|
|
|
|
->with('title', sprintf(
|
|
|
|
'<strong>%s</strong> %s',
|
|
|
|
trans('dashboard.notifications.whoops'),
|
|
|
|
trans('dashboard.components.groups.edit.failure')
|
|
|
|
))
|
|
|
|
->with('errors', $group->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
segment_track('Dashboard', [
|
|
|
|
'event' => 'Edit Component Group',
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$successMsg = sprintf(
|
|
|
|
'<strong>%s</strong> %s',
|
|
|
|
trans('dashboard.notifications.awesome'),
|
|
|
|
trans('dashboard.components.groups.edit.success')
|
|
|
|
);
|
|
|
|
|
|
|
|
return Redirect::back()->with('success', $successMsg);
|
|
|
|
}
|
2014-12-20 18:53:52 +00:00
|
|
|
}
|