2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Abstracts\Http;
|
|
|
|
|
2020-11-06 00:43:46 +03:00
|
|
|
use App\Abstracts\Http\Response;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Traits\Jobs;
|
|
|
|
use App\Traits\Relationships;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
|
use Illuminate\Pagination\Paginator;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
|
|
use Illuminate\Routing\Route;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
abstract class Controller extends BaseController
|
|
|
|
{
|
|
|
|
use AuthorizesRequests, Jobs, Relationships, ValidatesRequests;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new controller instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->setPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign permissions to methods.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-03-09 10:26:02 +03:00
|
|
|
public function setPermissions()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
// No need to check for permission in console
|
|
|
|
if (app()->runningInConsole()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$route = app(Route::class);
|
|
|
|
|
|
|
|
// Get the controller array
|
|
|
|
$arr = array_reverse(explode('\\', explode('@', $route->getAction()['uses'])[0]));
|
|
|
|
|
|
|
|
$controller = '';
|
|
|
|
|
2020-07-15 14:14:20 +03:00
|
|
|
// Add module
|
|
|
|
if (isset($arr[3]) && isset($arr[4])) {
|
|
|
|
if (strtolower($arr[4]) == 'modules') {
|
|
|
|
$controller .= Str::kebab($arr[3]) . '-';
|
|
|
|
} elseif (isset($arr[5]) && (strtolower($arr[5]) == 'modules')) {
|
|
|
|
$controller .= Str::kebab($arr[4]) . '-';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// Add folder
|
|
|
|
if (strtolower($arr[1]) != 'controllers') {
|
|
|
|
$controller .= Str::kebab($arr[1]) . '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add file
|
|
|
|
$controller .= Str::kebab($arr[0]);
|
|
|
|
|
|
|
|
// Skip ACL
|
2020-01-07 17:15:00 +03:00
|
|
|
$skip = ['portal-dashboard'];
|
2019-11-16 10:21:14 +03:00
|
|
|
if (in_array($controller, $skip)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 14:14:20 +03:00
|
|
|
// App\Http\Controllers\FooBar -->> foo-bar
|
|
|
|
// App\Http\Controllers\FooBar\Main -->> foo-bar-main
|
|
|
|
// Modules\Blog\Http\Controllers\Posts -->> blog-posts
|
|
|
|
// Modules\Blog\Http\Controllers\Portal\Posts -->> blog-portal-posts
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// Add CRUD permission check
|
2020-06-21 18:00:31 +03:00
|
|
|
$this->middleware('permission:create-' . $controller)->only('create', 'store', 'duplicate', 'import');
|
|
|
|
$this->middleware('permission:read-' . $controller)->only('index', 'show', 'edit', 'export');
|
|
|
|
$this->middleware('permission:update-' . $controller)->only('update', 'enable', 'disable');
|
2019-11-16 10:21:14 +03:00
|
|
|
$this->middleware('permission:delete-' . $controller)->only('destroy');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a pagination collection.
|
|
|
|
*
|
|
|
|
* @param array|Collection $items
|
|
|
|
* @param int $perPage
|
|
|
|
* @param int $page
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return LengthAwarePaginator
|
|
|
|
*/
|
|
|
|
public function paginate($items, $perPage = 15, $page = null, $options = [])
|
|
|
|
{
|
|
|
|
$perPage = $perPage ?: request('limit', setting('default.list_limit', '25'));
|
|
|
|
|
|
|
|
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
|
|
|
|
|
|
|
|
$items = $items instanceof Collection ? $items : Collection::make($items);
|
|
|
|
|
|
|
|
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
|
|
|
|
}
|
2020-11-06 00:43:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a response based on request type like HTML, JSON, or anything else.
|
|
|
|
*
|
|
|
|
* @param string $view
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function response($view, $data = [])
|
|
|
|
{
|
|
|
|
$class_name = str_replace('Controllers', 'Responses', (new \ReflectionClass($this))->getName());
|
|
|
|
|
|
|
|
if (class_exists($class_name)) {
|
|
|
|
$response = new $class_name($view, $data);
|
|
|
|
} else {
|
|
|
|
$response = new class($view, $data) extends Response {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|