mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-02-23 19:24:03 +01:00
151 lines
4.3 KiB
PHP
151 lines
4.3 KiB
PHP
<?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\Http\Controllers\Api;
|
|
|
|
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
|
|
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
|
|
use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand;
|
|
use CachetHQ\Cachet\Models\Component;
|
|
use CachetHQ\Cachet\Models\Tag;
|
|
use Exception;
|
|
use GrahamCampbell\Binput\Facades\Binput;
|
|
use Illuminate\Contracts\Auth\Guard;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
class ComponentController extends AbstractApiController
|
|
{
|
|
use DispatchesJobs;
|
|
|
|
/**
|
|
* Get all components.
|
|
*
|
|
* @param \Symfony\Component\HttpFoundation\Request $request
|
|
* @param \Illuminate\Contracts\Auth\Guard $auth
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getComponents(Request $request, Guard $auth)
|
|
{
|
|
if ($auth->check()) {
|
|
$components = Component::whereRaw('1 = 1');
|
|
} else {
|
|
$components = Component::enabled();
|
|
}
|
|
|
|
return $this->paginator($components->paginate(Binput::get('per_page', 20)), $request);
|
|
}
|
|
|
|
/**
|
|
* Get a single component.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Component $component
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getComponent(Component $component)
|
|
{
|
|
return $this->item($component);
|
|
}
|
|
|
|
/**
|
|
* Create a new component.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function postComponents()
|
|
{
|
|
try {
|
|
$component = $this->dispatch(new AddComponentCommand(
|
|
Binput::get('name'),
|
|
Binput::get('description'),
|
|
Binput::get('status'),
|
|
Binput::get('link'),
|
|
Binput::get('order'),
|
|
Binput::get('group_id'),
|
|
(bool) Binput::get('enabled', true)
|
|
));
|
|
} catch (Exception $e) {
|
|
throw new BadRequestHttpException();
|
|
}
|
|
|
|
if (Binput::has('tags')) {
|
|
// The component was added successfully, so now let's deal with the tags.
|
|
$tags = preg_split('/ ?, ?/', Binput::get('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 $this->item($component);
|
|
}
|
|
|
|
/**
|
|
* Update an existing component.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Component $component
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function putComponent(Component $component)
|
|
{
|
|
try {
|
|
$this->dispatch(new UpdateComponentCommand(
|
|
$component,
|
|
Binput::get('name'),
|
|
Binput::get('description'),
|
|
Binput::get('status'),
|
|
Binput::get('link'),
|
|
Binput::get('order'),
|
|
Binput::get('group_id'),
|
|
(bool) Binput::get('enabled', true)
|
|
));
|
|
} catch (Exception $e) {
|
|
throw new BadRequestHttpException();
|
|
}
|
|
|
|
if (Binput::has('tags')) {
|
|
$tags = preg_split('/ ?, ?/', Binput::get('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 $this->item($component);
|
|
}
|
|
|
|
/**
|
|
* Delete an existing component.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Component $component
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function deleteComponent(Component $component)
|
|
{
|
|
$this->dispatch(new RemoveComponentCommand($component));
|
|
|
|
return $this->noContent();
|
|
}
|
|
}
|