This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from kontenta/event
Visit events and visit widgets
- Loading branch information
Showing
9 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Kontenta\KontourSupport; | ||
|
||
use Illuminate\Contracts\Auth\Access\Authorizable; | ||
use Illuminate\Support\Facades\Auth; | ||
use Kontenta\Kontour\Contracts\PersonalRecentVisitsWidget as PersonalRecentVisitsWidgetContract; | ||
|
||
class PersonalRecentVisitsWidget implements PersonalRecentVisitsWidgetContract | ||
{ | ||
protected $repository; | ||
|
||
public function __construct(RecentVisitsRepository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
|
||
public function toHtml() | ||
{ | ||
return '<ul>' . $this->getVisits()->map(function ($visit) { | ||
return '<li data-kontour-visit-type="' . $visit->getType() . '">' . $visit->getLink()->toHtml() . '</li>'; | ||
})->implode("\n") . '</ul>'; | ||
} | ||
|
||
public function isAuthorized(Authorizable $user = null): bool | ||
{ | ||
return true; | ||
} | ||
|
||
private function getVisits() | ||
{ | ||
return $this->repository->getShowVisits()->merge($this->repository->getEditVisits())->filter(function ($visit) { | ||
return $visit->getUser()->is(Auth::guard(config('kontour.guard'))->user()); | ||
})->unique(function ($visit) { | ||
return $visit->getLink()->getUrl(); | ||
})->sortByDesc(function ($visit) { | ||
return $visit->getDateTime(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Kontenta\KontourSupport; | ||
|
||
use Illuminate\Contracts\Auth\Access\Authorizable; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Cache; | ||
use Kontenta\Kontour\Events\AdminToolVisited; | ||
use Kontenta\Kontour\Contracts\RecentVisitsRepository as RecentVisitsRepositoryContract; | ||
|
||
class RecentVisitsRepository implements RecentVisitsRepositoryContract | ||
{ | ||
private $keyPrefix = 'kontour-recent'; | ||
|
||
public function getShowVisits(): Collection | ||
{ | ||
return Cache::get($this->generateCacheKey('show'), new Collection()); | ||
} | ||
|
||
public function getEditVisits(): Collection | ||
{ | ||
return Cache::get($this->generateCacheKey('edit'), new Collection()); | ||
} | ||
|
||
public function subscribe($events) | ||
{ | ||
$events->listen(AdminToolVisited::class, [$this, 'handle']); | ||
} | ||
|
||
public function handle(AdminToolVisited $event) | ||
{ | ||
$key = $this->generateCacheKey($event->visit->getType()); | ||
$visits = Cache::get($key, new Collection()); | ||
$visits->push($event->visit); | ||
Cache::forever($key, $visits); | ||
} | ||
|
||
protected function generateCacheKey($type) { | ||
return 'kontour-recent-visits-'.$type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Kontenta\KontourSupport; | ||
|
||
use Illuminate\Contracts\Auth\Access\Authorizable; | ||
use Illuminate\Support\Facades\Auth; | ||
use Kontenta\Kontour\Contracts\TeamRecentVisitsWidget as TeamRecentVisitsWidgetContract; | ||
|
||
class TeamRecentVisitsWidget implements TeamRecentVisitsWidgetContract | ||
{ | ||
protected $repository; | ||
|
||
public function __construct(RecentVisitsRepository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
|
||
public function toHtml() | ||
{ | ||
return '<ul>' . $this->getVisits()->map(function ($visit) { | ||
return '<li data-kontour-visit-type="' . $visit->getType() . '" data-kontour-username="' . $visit->getUser()->getDisplayName() . '">' . $visit->getLink()->toHtml() . '</li>'; | ||
})->implode("\n") . '</ul>'; | ||
} | ||
|
||
public function isAuthorized(Authorizable $user = null): bool | ||
{ | ||
return true; | ||
} | ||
|
||
private function getVisits() | ||
{ | ||
return $this->repository->getEditVisits()->filter(function ($visit) { | ||
return !$visit->getUser()->is(Auth::guard(config('kontour.guard'))->user()); | ||
})->unique(function ($visit) { | ||
return $visit->getLink()->getUrl(); | ||
})->sortByDesc(function ($visit) { | ||
return $visit->getDateTime(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters