Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add visibility to component groups #2027

Merged
merged 15 commits into from
Oct 2, 2016
Merged
12 changes: 11 additions & 1 deletion app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ final class AddComponentGroupCommand
*/
public $collapsed;

/**
* Is the component visible to public?
*
* @var int
*/
public $visible;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is better named as visibility or something. To me, visible implies a boolean.

Copy link
Contributor Author

@yoyosan yoyosan Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense. I'll do the modification.

Copy link
Contributor Author

@yoyosan yoyosan Aug 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we no longer have a third option, I think it should stay as it is now. This way it's identical to the Incident model. Don't you agree?


/**
* The validation rules.
*
Expand All @@ -48,6 +55,7 @@ final class AddComponentGroupCommand
'name' => 'required|string',
'order' => 'int',
'collapsed' => 'int|between:0,3',
'visible' => 'bool',
];

/**
Expand All @@ -56,13 +64,15 @@ final class AddComponentGroupCommand
* @param string $name
* @param int $order
* @param int $collapsed
* @param int $visible
*
* @return void
*/
public function __construct($name, $order, $collapsed)
public function __construct($name, $order, $collapsed, $visible)
{
$this->name = $name;
$this->order = (int) $order;
$this->collapsed = $collapsed;
$this->visible = (int) $visible;
}
}
12 changes: 11 additions & 1 deletion app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ final class UpdateComponentGroupCommand
*/
public $collapsed;

/**
* Is the component visible to public?
*
* @var int
*/
public $visible;

/**
* The validation rules.
*
Expand All @@ -57,6 +64,7 @@ final class UpdateComponentGroupCommand
'name' => 'string',
'order' => 'int',
'collapsed' => 'int|between:0,3',
'visible' => 'bool',
];

/**
Expand All @@ -66,14 +74,16 @@ final class UpdateComponentGroupCommand
* @param string $name
* @param int $order
* @param int $collapsed
* @param int $visible
*
* @return void
*/
public function __construct(ComponentGroup $group, $name, $order, $collapsed)
public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible)
{
$this->group = $group;
$this->name = $name;
$this->order = (int) $order;
$this->collapsed = $collapsed;
$this->visible = (int) $visible;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function handle(AddComponentGroupCommand $command)
'name' => $command->name,
'order' => $command->order,
'collapsed' => $command->collapsed,
'visible' => $command->visible,
]);

event(new ComponentGroupWasAddedEvent($group));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function filter(UpdateComponentGroupCommand $command)
'name' => $command->name,
'order' => $command->order,
'collapsed' => $command->collapsed,
'visible' => $command->visible,
];

return array_filter($params, function ($val) {
Expand Down
44 changes: 40 additions & 4 deletions app/Composers/Modules/ComponentsComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\View\View;

/**
Expand All @@ -23,6 +24,25 @@
*/
class ComponentsComposer
{
/**
* The user session object.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;

/**
* Creates a new components composer instance.
*
* @param \Illuminate\Contracts\Auth\Guard $guard
*
* @return void
*/
public function __construct(Guard $guard)
{
$this->guard = $guard;
}

/**
* Index page view composer.
*
Expand All @@ -32,12 +52,28 @@ class ComponentsComposer
*/
public function compose(View $view)
{
// Component & Component Group lists.
$usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id');
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
$ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();
$componentGroups = $this->getVisibleGroupedComponents();
$ungroupedComponents = Component::ungrouped()->get();

$view->withComponentGroups($componentGroups)
->withUngroupedComponents($ungroupedComponents);
}

/**
* Get visible grouped components.
*
* @return \Illuminate\Support\Collection
*/
protected function getVisibleGroupedComponents()
{
$componentGroupsBuilder = ComponentGroup::query();
if (!$this->guard->check()) {
$componentGroupsBuilder->visible();
}

$usedComponentGroups = Component::grouped()->pluck('group_id');

return $componentGroupsBuilder->used($usedComponentGroups)
->get();
}
}
2 changes: 2 additions & 0 deletions app/Console/Commands/DemoSeederCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ protected function seedComponentGroups()
'name' => 'Websites',
'order' => 1,
'collapsed' => 0,
'visible' => ComponentGroup::VISIBLE_AUTHENTICATED,
], [
'name' => 'Alt Three',
'order' => 2,
'collapsed' => 1,
'visible' => ComponentGroup::VISIBLE_GUEST,
],
];

Expand Down
27 changes: 25 additions & 2 deletions app/Http/Controllers/Api/ComponentGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand;
use CachetHQ\Cachet\Models\ComponentGroup;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
Expand All @@ -29,6 +30,23 @@
*/
class ComponentGroupController extends AbstractApiController
{
/**
* The user session object.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblock this please.


/**
* Creates a new component group controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $guard
*/
public function __construct(Guard $guard)
{
$this->guard = $guard;
}

/**
* Get all groups.
*
Expand All @@ -37,6 +55,9 @@ class ComponentGroupController extends AbstractApiController
public function getGroups()
{
$groups = ComponentGroup::query();
if (!$this->guard->check()) {
$groups = ComponentGroup::visible();
}

$groups->search(Binput::except(['sort', 'order', 'per_page']));

Expand Down Expand Up @@ -74,7 +95,8 @@ public function postGroups()
$group = dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0),
Binput::get('collapsed', 0)
Binput::get('collapsed', 0),
Binput::get('visible', ComponentGroup::VISIBLE_AUTHENTICATED)
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
Expand All @@ -97,7 +119,8 @@ public function putGroup(ComponentGroup $group)
$group,
Binput::get('name'),
Binput::get('order'),
Binput::get('collapsed')
Binput::get('collapsed'),
Binput::get('visible')
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/Dashboard/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ public function postAddComponentGroup()
$group = dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0),
Binput::get('collapsed')
Binput::get('collapsed'),
Binput::get('visible')
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.groups.add')
Expand All @@ -304,7 +305,8 @@ public function updateComponentGroupAction(ComponentGroup $group)
$group,
Binput::get('name'),
$group->order,
Binput::get('collapsed')
Binput::get('collapsed'),
Binput::get('visible')
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
Expand Down
36 changes: 32 additions & 4 deletions app/Http/Controllers/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
Expand Down Expand Up @@ -52,16 +53,25 @@ class DashboardController extends Controller
*/
protected $feed;

/**
* The user session object.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;

/**
* Creates a new dashboard controller instance.
*
* @param \CachetHQ\Cachet\Integrations\Feed $feed
* @param \Illuminate\Contracts\Auth\Guard $guard
*
* @return void
*/
public function __construct(Feed $feed)
public function __construct(Feed $feed, Guard $guard)
{
$this->feed = $feed;
$this->guard = $guard;
$this->startDate = new Date();
$this->dateTimeZone = Config::get('cachet.timezone');
}
Expand All @@ -86,9 +96,9 @@ public function showDashboard()
$components = Component::orderBy('order')->get();
$incidents = $this->getIncidents();
$subscribers = $this->getSubscribers();
$usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id');
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
$ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();

$componentGroups = $this->getVisibleGroupedComponents();
$ungroupedComponents = Component::ungrouped()->get();

$welcomeUser = !Auth::user()->welcomed;
if ($welcomeUser) {
Expand Down Expand Up @@ -174,4 +184,22 @@ protected function getSubscribers()

return $allSubscribers;
}

/**
* Get visible grouped components.
*
* @return \Illuminate\Support\Collection
*/
protected function getVisibleGroupedComponents()
{
$componentGroupsBuilder = ComponentGroup::query();
if (!$this->guard->check()) {
$componentGroupsBuilder = ComponentGroup::visible();
}

$usedComponentGroups = Component::grouped()->pluck('group_id');

return $componentGroupsBuilder->used($usedComponentGroups)
->get();
}
}
29 changes: 29 additions & 0 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,35 @@ public function scopeDisabled(Builder $query)
return $query->where('enabled', false);
}

/**
* Finds all ungrouped components.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUngrouped(Builder $query)
{
return $query->enabled()
->where('group_id', 0)
->orderBy('order')
->orderBy('created_at');
}

/**
* Finds all grouped components.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeGrouped(Builder $query)
{
return $query->enabled()
->where('group_id', '>', 0)
->groupBy('group_id');
}

/**
* Returns all of the tags on this component.
*
Expand Down
Loading