Skip to content

Commit

Permalink
Export Job Pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
aothyab authored and saeed-corals committed Sep 16, 2024
1 parent 8103741 commit d3ba7b6
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 69 deletions.
26 changes: 24 additions & 2 deletions Corals/core/Foundation/DataTables/BaseDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ public function __construct()
$filters = array_merge($this->getFilters(), $this->getCustomRenderedFilters());

if ($this->usesQueryBuilderFilters) {
$this->addScope(new CoralsQueryBuilderDataTableScope($filters));
$this->addScope(new CoralsQueryBuilderDataTableScope($filters, request('q')));
} else {
$this->addScope(new CoralsScope($filters));
$this->addScope(new CoralsScope(
$this->getFilters(),
request()->filters,
$urlFilters ?? [], [
'class' => get_class($this),
'parameters' => $this->resolveDataTableParameters()
]));
}

if ($this->request->boolean('deleted')) {
Expand Down Expand Up @@ -74,6 +80,22 @@ public function builder(): Builder
return app(CoralsBuilder::class);
}

/**
* @return array
*/
protected function resolveDataTableParameters()
{
$objectParameters = [];

$objectReflection = new \ReflectionClass($this);

foreach ($objectReflection->getConstructor()->getParameters() as $parameter) {
$objectParameters[$parameter->getName()] = $this->{$parameter->getName()};
}

return $objectParameters;
}

/**
* Optional method if you want to use html builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@

class CoralsQueryBuilderDataTableScope implements DataTableScope
{

protected $filters;

/**
* CoralsQueryBuilderDataTableScope constructor.
* @param $filters
* @param $urlQuery
*/
public function __construct($filters)
public function __construct(protected $filters, protected $urlQuery)
{
$this->filters = $filters;
}

public function apply($query)
{
if ($queryBuilderJson = request('q')) {
if ($queryBuilderJson = $this->urlQuery) {
$queryBuilderParser = new QueryBuilderParser($this->filters);
$query = $queryBuilderParser->parse(json_encode($queryBuilderJson), $query);
}
Expand Down
48 changes: 33 additions & 15 deletions Corals/core/Foundation/DataTables/CoralsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,55 @@

class CoralsScope implements DataTableScope
{
public $filters;

public function __construct($filters)
/**
* CoralsScope constructor.
* @param $filters
* @param $requestFilter
* @param array $urlFilters
* @param array $dataTable
*/
public function __construct(
protected $filters,
protected $requestFilter,
protected $urlFilters = [],
protected array $dataTable = []
)
{
$this->filters = $filters;
}

public function apply($query)
{
$ajaxRequestFilters = urldecode($this->requestFilter);

$filters = $this->filters;

if (empty($filters)) {
return $query;
}

$requestFilters = request()->get('filters');
$filtersRequest = array_merge(
\Arr::dot($this->urlFilters),
getRequestFiltersArray($ajaxRequestFilters)
);

if (!is_array($requestFilters)) {
$requestFilters = urldecode($requestFilters);
$requestFilters = get_request_filters_array($requestFilters);
}
if ($this->dataTable) {
$dt = app(
data_get($this->dataTable, 'class'),
data_get($this->dataTable, 'parameters'),
);

$requestFilters = array_merge(request()->only(array_keys($filters)), $requestFilters);
if (method_exists($dt, 'applyDefaultFilters')) {
$dt->applyDefaultFilters($query, $filtersRequest, $this->urlFilters);
}
}

if (empty($requestFilters)) {
if (!$ajaxRequestFilters || empty($filters)) {
return $query;
}

request()->request->add($filtersRequest);

$baseTable = $query->getModel()->getTable();

foreach ($requestFilters as $column => $value) {

foreach ($filtersRequest as $column => $value) {
$filter = Arr::get($filters, $column, Arr::get($filters, $column . "[]"));

if (empty($filter) || !($value || $value === 0 || $value === 0.0 || $value === false)) {
Expand Down
20 changes: 16 additions & 4 deletions Corals/core/Foundation/FoundationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\Queue;
use Illuminate\Queue\Worker;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Lang;
Expand Down Expand Up @@ -89,6 +90,8 @@ public function boot()


$this->registerMissingHtmlMacro();

$this->registerCreateQueuePayloadCallback();
}

/**
Expand Down Expand Up @@ -210,10 +213,6 @@ public function register()
});






Actions::do_action('post_coral_registration');

// Bind 'hashids' shared component to the IoC container
Expand Down Expand Up @@ -251,6 +250,7 @@ protected function mobileResetPasswordConfiguration(): void
$this->resetPasswordEmailCallback();
$this->extendPasswordBroker();
}

/**
*
*/
Expand Down Expand Up @@ -322,4 +322,16 @@ protected function extendQueueWorker()
);
});
}

/**
*
*/
protected function registerCreateQueuePayloadCallback(): void
{
Queue::createPayloadUsing(function () {
return [
'user_id' => data_get(user(), 'id')
];
});
}
}
49 changes: 45 additions & 4 deletions Corals/core/Foundation/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ function format_time($time, $format = 'h:i A')
if (!function_exists('log_exception')) {
function log_exception(
\Exception $exception = null,
$object = null,
$action = null,
$message = null,
$echo_message = false
$object = null,
$action = null,
$message = null,
$echo_message = false
)
{
logger(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2), -1));
Expand Down Expand Up @@ -1120,3 +1120,44 @@ function getCurrentTimeForFileName()
return now()->format('Y_dM_H_i');
}
}

if (!function_exists('getRequestFiltersArray')) {
/**
* @param $requestFilters
* @return array|string|string[]
*/
function getRequestFiltersArray($requestFilters)
{
$array = explode("&", $requestFilters);

$array = str_replace('#amp#', '&', $array);
if (!(count($array) == 1 && $array[0] == "")) {
$index = 0;

foreach ($array as $key => $value) {
$filter = explode("=", $value);
preg_match_all('/(.*)\[(.*?)\]/', $filter[0], $matches);
if (is_array($matches[0]) && (count($matches[0]) > 0)) {
if ($filter[1]) {
if (strpos($filter[1], ',') !== false) {
foreach (explode(',', $filter[1]) as $f) {
$array[$matches[1][0]][] = $f;
}
} else {
$nodeKey = empty(trim($matches[2][0], "'")) ? $index++ : trim($matches[2][0], "'");
$array[$matches[1][0]][$nodeKey] = $filter[1];
}
}
} else {
if ($filter[1]) {
$array[$filter[0]] = $filter[1];
}
}
unset ($array[$key]);
}
return $array;
} else {
return [];
}
}
}
Loading

0 comments on commit d3ba7b6

Please sign in to comment.