|
|
<?php
namespace App\Http\Controllers;
use App\Models\Activity; use App\Rules\MaxBound; use Illuminate\Http\Request; use Spatie\QueryBuilder\AllowedFilter; use Spatie\QueryBuilder\QueryBuilder;
class ActivityController extends Controller { public function index($business, Request $request) { permit('businessAccess'); $this->indexValidation($request); $per_page = $request->limit > 100 ? 10 : $request->limit; return $this->indexFiltering($business)->paginate($per_page); }
public function indexValidation($request) { $bound = 10; $this->validate($request, [ 'filter.project_id' => [new MaxBound($bound)] , 'filter.system_id' => [new MaxBound($bound)] , 'filter.workflow_id' => [new MaxBound($bound)] , 'filter.status_id' => [new MaxBound($bound)] , 'filter.sprint_id' => [new MaxBound($bound)] , 'filter.actor_id' => [new MaxBound($bound)] , 'filter.user_id' => [new MaxBound($bound)] , 'filter.subject_id' => [new MaxBound($bound)] , //todo: validation for crud_id and table_id
'filter.creates_before' => 'bail|nullable|date|date_format:Y-m-d' , 'filter.creates_after' => 'bail|nullable|date|date_format:Y-m-d' , 'filter.creates_in' => 'bail|nullable|numeric|max:90' , ]); } public function indexFiltering($business) { $query = Activity::where('business_id', $business); $activityQ = QueryBuilder::for($query) ->allowedFilters([ AllowedFilter::exact('project_id'), AllowedFilter::exact('system_id'), AllowedFilter::exact('workflow_id'), AllowedFilter::exact('status_id'), AllowedFilter::exact('sprint_id'), AllowedFilter::exact('task_id'), AllowedFilter::exact('actor_id'), AllowedFilter::exact('user_id'), AllowedFilter::exact('crud_id'), AllowedFilter::exact('table_id'), AllowedFilter::exact('subject_id'), AllowedFilter::scope('creates_before'), AllowedFilter::scope('creates_after'), AllowedFilter::scope('creates_in'), ]) ->defaultSort('-id') ->allowedSorts('id', 'created_at'); if (\request('_business_info')['info']['users'][\auth()->id()]['level'] != enum('levels.owner.id')) { $requested_projects = isset(\request('filter')['project_id']) ? array_unique(explode(',',\request('filter')['project_id'] ?? null )) : null; $requested_projects = collect($requested_projects)->keyBy(null)->toArray(); $project_ids = $this->myStateProjects($requested_projects); $activityQ->where(function ($q) use ($project_ids) { $q->whereIn('project_id', $project_ids['non_guest_ids']) ->orWhere(function ($q) use ($project_ids) { $q->whereIn('project_id', $project_ids['guest_ids']) ->where('user_id', auth()->id()); }); }); } return $activityQ; }
public function myStateProjects($requested_projects) { $non_guest_ids = []; $guest_ids = []; $is_empty = empty($requested_projects);
foreach (\request('_business_info')['info']['projects'] as $p_id => $p) {
$level = \request('_business_info')['info']['projects'][$p_id]['members'][\auth()->id()]['level'];
if (( $is_empty || isset($requested_projects[$p_id])) && $level > enum('levels.guest.id')) { array_push($non_guest_ids, $p_id); } if (( $is_empty || isset($requested_projects[$p_id])) && $level == enum('levels.guest.id')) { array_push($guest_ids, $p_id); } }
return ['non_guest_ids' => $non_guest_ids, 'guest_ids' => $guest_ids]; }
public function store($business, Request $request) { return Activity::create($request->merge(['business_id' => $business])->all()); }
public function delete() {
} }
|