219 lines
6.5 KiB
PHP
219 lines
6.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Repositories\Backend\Dharma;
|
||
|
|
|
||
|
|
use App\Constant\Constant;
|
||
|
|
use App\Exceptions\GeneralException;
|
||
|
|
use App\Models\Dharma;
|
||
|
|
use App\Models\Sampraday;
|
||
|
|
use App\Services\BaseService;
|
||
|
|
use Exception;
|
||
|
|
use Illuminate\Database\Eloquent\Builder;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Yajra\DataTables\Facades\DataTables;
|
||
|
|
|
||
|
|
class DharmaService extends BaseService
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Associated Service Model.
|
||
|
|
*/
|
||
|
|
protected const MODEL = Dharma::class;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is use for search query in Datatable.
|
||
|
|
* Also for sorting function we are using join query for appropriate sorting result
|
||
|
|
* @param $request
|
||
|
|
* @return $this|array|Builder
|
||
|
|
*/
|
||
|
|
public function prepareSearchQuery($request)
|
||
|
|
{
|
||
|
|
$searchQuery = [];
|
||
|
|
try {
|
||
|
|
$searchQuery = $this->query()->select(
|
||
|
|
'id',
|
||
|
|
'name',
|
||
|
|
'created_at'
|
||
|
|
);
|
||
|
|
|
||
|
|
$columns = ['','id', 'name', 'created_at'];
|
||
|
|
|
||
|
|
if (isset($request['name']) && $request['name'] != '') {
|
||
|
|
$searchQuery = $searchQuery->where('name', 'like', '%' . $request['name'] . '%');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isset($request['order']) && $request['order'] != "") {
|
||
|
|
$searchQuery = $searchQuery->orderBy(
|
||
|
|
$columns[$request['order'][0]['column']],
|
||
|
|
$request['order'][0]['dir']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
}
|
||
|
|
return $searchQuery;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the format the Datatable columns
|
||
|
|
*
|
||
|
|
* @param $request
|
||
|
|
* @return object
|
||
|
|
*/
|
||
|
|
public function getForDataTable($request): object
|
||
|
|
{
|
||
|
|
$response = (object)[];
|
||
|
|
try {
|
||
|
|
$dataTableQuery = $this->prepareSearchQuery($request);
|
||
|
|
// ->orderBy('id', 'DESC');
|
||
|
|
|
||
|
|
$response = Datatables::of($dataTableQuery)
|
||
|
|
->addColumn(
|
||
|
|
'checkbox_action',
|
||
|
|
function ($dharma) {
|
||
|
|
return $dharma->dharma_checkbox_action;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addIndexColumn()
|
||
|
|
->addColumn(
|
||
|
|
'created_at',
|
||
|
|
function ($user) {
|
||
|
|
return (($user->created_at) ? defaultDateTimeFormat($user->created_at) : '-');
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addColumn(
|
||
|
|
'action',
|
||
|
|
function ($dharma) {
|
||
|
|
if (
|
||
|
|
!empty($dharma->roles[0]['name']) != config('access.users.admin_role')
|
||
|
|
|| !empty($dharma->roles[0]['name']) != config('access.users.super_admin_role')
|
||
|
|
|| loggedInUser()->isSuperAdmin()
|
||
|
|
|| loggedInUser()->isAdmin()
|
||
|
|
) {
|
||
|
|
return $dharma->dharma_action_buttons;
|
||
|
|
}
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->setRowId(
|
||
|
|
function ($dharma) {
|
||
|
|
return 'recordRow-' . $dharma->id;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->rawColumns(['action', 'checkbox_action'])
|
||
|
|
->make(true);
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the create dharma
|
||
|
|
*
|
||
|
|
* @param array $request
|
||
|
|
* @return Dharma
|
||
|
|
* @throws GeneralException
|
||
|
|
*/
|
||
|
|
public function create(array $request): Dharma
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
return DB::transaction(
|
||
|
|
function () use ($request) {
|
||
|
|
|
||
|
|
$dharma = $this->query()->create(
|
||
|
|
[
|
||
|
|
'name' => $request['name'],
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
return $dharma;
|
||
|
|
}
|
||
|
|
);
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
throw new GeneralException(__('message.create_dharma_error'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the update dharma
|
||
|
|
*
|
||
|
|
* @param object $dharma
|
||
|
|
* @param array $request
|
||
|
|
* @return Dharma
|
||
|
|
* @throws GeneralException
|
||
|
|
*/
|
||
|
|
public function update(object $dharma, array $request): Dharma
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
return DB::transaction(
|
||
|
|
function () use ($dharma, $request) {
|
||
|
|
|
||
|
|
$this->query()->where('id', $dharma->id)->update(
|
||
|
|
[
|
||
|
|
'name' => !empty($request['name']) ? $request['name'] : $dharma->name,
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
return $dharma;
|
||
|
|
}
|
||
|
|
);
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
throw new GeneralException(__('message.update_dharma_error'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the multiple records action from the grid
|
||
|
|
*
|
||
|
|
* @param string $actionType
|
||
|
|
* @param array $data
|
||
|
|
* @return bool
|
||
|
|
* @throws GeneralException
|
||
|
|
*/
|
||
|
|
public function gridActions(string $actionType, array $data): bool
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
switch ($actionType) {
|
||
|
|
case Constant::STATUS_ACTIVE:
|
||
|
|
$this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ONE]);
|
||
|
|
return Constant::STATUS_TRUE;
|
||
|
|
case Constant::STATUS_INACTIVE:
|
||
|
|
$this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ZERO]);
|
||
|
|
return Constant::STATUS_TRUE;
|
||
|
|
case Constant::STATUS_DELETE:
|
||
|
|
$this->query()->whereIn('id', $data)->delete();
|
||
|
|
return Constant::STATUS_TRUE;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
throw new GeneralException(__('message.delete_dharma_error'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is use for get sampraday by dharma.
|
||
|
|
*
|
||
|
|
* @param $request
|
||
|
|
* @return $this|array|Builder
|
||
|
|
*/
|
||
|
|
public function getSampradayByDharma($data)
|
||
|
|
{
|
||
|
|
$gachadhiPaties = [];
|
||
|
|
try {
|
||
|
|
$sampraday = Sampraday::query();
|
||
|
|
$sampradaies = $sampraday->where('dharma_id', $data['dharma_id'])->get();
|
||
|
|
return $sampradaies;
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|