Skip to content

Commit

Permalink
refactor: Use trait to reduce duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
zooley committed Apr 9, 2024
1 parent b65a358 commit 4c84517
Show file tree
Hide file tree
Showing 35 changed files with 261 additions and 681 deletions.
26 changes: 7 additions & 19 deletions app/Modules/Core/Http/Controllers/Admin/ModulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Artisan;
use App\Modules\Core\Models\Extension;
use App\Halcyon\Http\StatefulRequest;
use App\Halcyon\Http\Concerns\UsesFilters;
use App\Halcyon\Access\Rules;
use App\Halcyon\Access\Asset;

class ModulesController extends Controller
{
use UsesFilters;

/**
* Display a listing of the resource.
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
$filters = array(
$filters = $this->getStatefulFilters($request, 'core.modules', [
'search' => null,
'state' => 'enabled',
// Paging
Expand All @@ -32,21 +34,7 @@ public function index(StatefulRequest $request)
// Sorting
'order' => 'name',
'order_dir' => 'asc'
);

$reset = false;
$request = $request->mergeWithBase();
foreach ($filters as $key => $default)
{
if ($key != 'page'
&& $request->has($key) //&& session()->has('core.modules.filter_' . $key)
&& $request->input($key) != session()->get('core.modules.filter_' . $key))
{
$reset = true;
}
$filters[$key] = $request->state('core.modules.filter_' . $key, $key, $default);
}
$filters['page'] = $reset ? 1 : $filters['page'];
]);

if (!in_array($filters['order'], ['id', 'name', 'element', 'folder', 'ordering', 'enabled']))
{
Expand Down
26 changes: 7 additions & 19 deletions app/Modules/Finder/Http/Controllers/Admin/FacetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@
use Illuminate\Contracts\View\View;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use App\Halcyon\Http\StatefulRequest;
use App\Halcyon\Http\Concerns\UsesFilters;
use App\Modules\Finder\Models\Facet;
use App\Modules\Finder\Models\Service;
use App\Modules\Finder\Models\ServiceFacet;

class FacetsController extends Controller
{
use UsesFilters;

/**
* Display a listing of tags
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
// Get filters
$filters = array(
$filters = $this->getStatefulFilters($request, 'finder.facets', [
'search' => null,
'state' => 'published',
// Paging
Expand All @@ -32,21 +34,7 @@ public function index(StatefulRequest $request)
// Sorting
'order' => Facet::$orderBy,
'order_dir' => Facet::$orderDir,
);

$reset = false;
$request = $request->mergeWithBase();
foreach ($filters as $key => $default)
{
if ($key != 'page'
&& $request->has($key) //&& session()->has('finder.facets.filter_' . $key)
&& $request->input($key) != session()->get('finder.facets.filter_' . $key))
{
$reset = true;
}
$filters[$key] = $request->state('finder.facets.filter_' . $key, $key, $default);
}
$filters['page'] = $reset ? 1 : $filters['page'];
]);

if (!in_array($filters['order'], array('id', 'name', 'unixgroup', 'members_count')))
{
Expand Down
26 changes: 7 additions & 19 deletions app/Modules/Finder/Http/Controllers/Admin/FieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
use Illuminate\Contracts\View\View;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use App\Halcyon\Http\StatefulRequest;
use App\Halcyon\Http\Concerns\UsesFilters;
use App\Modules\Finder\Models\Field;

class FieldsController extends Controller
{
use UsesFilters;

/**
* Display a listing of tags
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
// Get filters
$filters = array(
$filters = $this->getStatefulFilters($request, 'finder.fields', [
'search' => null,
'state' => 'published',
// Paging
Expand All @@ -30,21 +32,7 @@ public function index(StatefulRequest $request)
// Sorting
'order' => Field::$orderBy,
'order_dir' => Field::$orderDir,
);

$reset = false;
$request = $request->mergeWithBase();
foreach ($filters as $key => $default)
{
if ($key != 'page'
&& $request->has($key) //&& session()->has('finder.fields.filter_' . $key)
&& $request->input($key) != session()->get('finder.fields.filter_' . $key))
{
$reset = true;
}
$filters[$key] = $request->state('finder.fields.filter_' . $key, $key, $default);
}
$filters['page'] = $reset ? 1 : $filters['page'];
]);

if (!in_array($filters['order'], array('id', 'name')))
{
Expand Down
26 changes: 7 additions & 19 deletions app/Modules/Finder/Http/Controllers/Admin/ServicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@
use Illuminate\Contracts\View\View;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use App\Halcyon\Http\StatefulRequest;
use App\Halcyon\Http\Concerns\UsesFilters;
use App\Modules\Finder\Models\Service;
use App\Modules\Finder\Models\Field;
use App\Modules\Finder\Models\ServiceField;

class ServicesController extends Controller
{
use UsesFilters;

/**
* Display a listing of tags
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
// Get filters
$filters = array(
$filters = $this->getStatefulFilters($request, 'finder.services', [
'search' => null,
'state' => 'published',
// Paging
Expand All @@ -32,21 +34,7 @@ public function index(StatefulRequest $request)
// Sorting
'order' => Service::$orderBy,
'order_dir' => Service::$orderDir,
);

$reset = false;
$request = $request->mergeWithBase();
foreach ($filters as $key => $default)
{
if ($key != 'page'
&& $request->has($key) //&& session()->has('finder.services.filter_' . $key)
&& $request->input($key) != session()->get('finder.services.filter_' . $key))
{
$reset = true;
}
$filters[$key] = $request->state('finder.services.filter_' . $key, $key, $default);
}
$filters['page'] = $reset ? 1 : $filters['page'];
]);

if (!in_array($filters['order'], array('id', 'name')))
{
Expand Down
5 changes: 2 additions & 3 deletions app/Modules/Groups/Http/Controllers/Site/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use OpenSpout\Reader\CSV\Reader as CsvReader;
use OpenSpout\Reader\XLSX\Reader as XlsxReader;
use App\Halcyon\Http\StatefulRequest;
use App\Modules\Groups\Models\FieldOfScience;
use App\Modules\Groups\Models\Group;
use App\Modules\Groups\Models\Department;
Expand All @@ -28,10 +27,10 @@ class GroupsController extends Controller
/**
* Display a listing of tags
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
$user = auth()->user();

Expand Down
19 changes: 8 additions & 11 deletions app/Modules/Impact/Http/Controllers/Admin/ImpactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\View\View;
use Illuminate\Routing\Controller;
use App\Halcyon\Http\StatefulRequest;
use App\Halcyon\Http\Concerns\UsesFilters;
use App\Modules\Impact\Models\Table;
use App\Modules\Impact\Models\Impact;
use App\Modules\Impact\Models\AwardReport;
Expand All @@ -15,26 +15,23 @@

class ImpactController extends Controller
{
use UsesFilters;

/**
* Display a listing of tags
* Display a listing of entries
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
// Get filters
$filters = array(
$filters = $this->getStatefulFilters($request, 'impact', [
'search' => null,
// Paging
'limit' => config('list_limit', 20),
'page' => 1,
);

foreach ($filters as $key => $default)
{
$filters[$key] = $request->state('impact.filter_' . $key, $key, $default);
}
]);
$filters['start'] = ($filters['limit'] * $filters['page']) - $filters['limit'];

$it = (new Table)->getTable();
Expand Down
17 changes: 7 additions & 10 deletions app/Modules/Issues/Http/Controllers/Site/TodosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Contracts\View\View;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use App\Halcyon\Http\StatefulRequest;
use App\Halcyon\Http\Concerns\UsesFilters;
use App\Modules\Issues\Models\Issue;
use App\Modules\Issues\Models\Issueresource;
use App\Modules\Issues\Models\Comment;
Expand All @@ -16,28 +16,25 @@

class TodosController extends Controller
{
use UsesFilters;

/**
* Display a listing of articles
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
// Get filters
$filters = array(
$filters = $this->getStatefulFilters($request, 'issues.todos', [
'search' => null,
'timeperiod' => null,
'limit' => config('list_limit', 20),
'page' => 1,
'order' => Issue::$orderBy,
'order_dir' => Issue::$orderDir,
);

foreach ($filters as $key => $default)
{
$filters[$key] = $request->state('issues.todos.filter_' . $key, $key, $default);
}
]);

if (!in_array($filters['order'], ['id', 'userid', 'name', 'description', 'datetimecreated', 'recurringtimeperiodid']))
{
Expand Down
45 changes: 16 additions & 29 deletions app/Modules/News/Http/Controllers/Admin/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
use App\Modules\News\Models\Stemmedtext;
use App\Modules\News\Notifications\ArticleCreated;
use App\Modules\News\Notifications\ArticleUpdated;
use App\Halcyon\Http\StatefulRequest;
use App\Halcyon\Http\Concerns\UsesFilters;
use App\Halcyon\Utility\PorterStemmer;
use Carbon\Carbon;

class ArticlesController extends Controller
{
use UsesFilters;

/**
* Display templates?
*
Expand All @@ -29,43 +31,28 @@ class ArticlesController extends Controller
/**
* Display a listing of articles
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function index(StatefulRequest $request)
public function index(Request $request)
{
$action = 'index';
if ($this->template)
{
$action = 'template';
}

// Get filters
$filters = array(
$filters = $this->getStatefulFilters($request, 'news.' . $action, [
'search' => null,
'state' => 'published',
'state' => ($this->template ? '*' : 'published'),
'access' => null,
'limit' => config('list_limit', 20),
'page' => 1,
'order' => 'id',
'order_dir' => 'desc',
'type' => null,
);

$action = 'index';
if ($this->template)
{
$action = 'template';
$filters['state'] = '*';
}

$reset = false;
$request = $request->mergeWithBase();
foreach ($filters as $key => $default)
{
if ($key != 'page'
&& $request->has($key) //&& session()->has('news.' . $action . '.filter_' . $key)
&& $request->input($key) != session()->get('news.' . $action . '.filter_' . $key))
{
$reset = true;
}
$filters[$key] = $request->state('news.' . $action . '.filter_' . $key, $key, $default);
}
$filters['page'] = $reset ? 1 : $filters['page'];
]);

if (!in_array($filters['order'], ['id', 'headline', 'datetimecreated', 'state', 'newstypeid']))
{
Expand Down Expand Up @@ -157,10 +144,10 @@ public function index(StatefulRequest $request)
/**
* Display a listing of templates
*
* @param StatefulRequest $request
* @param Request $request
* @return View
*/
public function templates(StatefulRequest $request)
public function templates(Request $request)
{
$this->template = 1;

Expand Down
Loading

0 comments on commit 4c84517

Please sign in to comment.