ok

Mini Shell

Direktori : /home2/selectio/public_html/fms-worksuite/app/DataTables/
Upload File :
Current File : /home2/selectio/public_html/fms-worksuite/app/DataTables/AppreciationsDataTable.php

<?php

namespace App\DataTables;

use App\Models\Notice;
use App\Models\Appreciation;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;

class AppreciationsDataTable extends BaseDataTable
{

    private $editAppreciationPermission;
    private $deleteAppreciationPermission;
    private $viewAppreciationPermission;

    public function __construct()
    {
        parent::__construct();
        $this->editAppreciationPermission = user()->permission('edit_appreciation');
        $this->deleteAppreciationPermission = user()->permission('delete_appreciation');
        $this->viewAppreciationPermission = user()->permission('view_appreciation');
    }

    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('check', function ($row) {
                return '<input type="checkbox" class="select-table-row" id="datatable-row-' . $row->id . '"  name="datatable_ids[]" value="' . $row->id . '" onclick="dataTableRowCheck(' . $row->id . ')">';
            })
            ->addColumn('action', function ($row) {

                $action = '<div class="task_view">

                    <div class="dropdown">
                        <a class="task_view_more d-flex align-items-center justify-content-center dropdown-toggle" type="link"
                            id="dropdownMenuLink-' . $row->id . '" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <i class="icon-options-vertical icons"></i>
                        </a>
                        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuLink-' . $row->id . '" tabindex="0">';

                $action .= '<a href="' . route('appreciations.show', $row->id) . '" class="dropdown-item openRightModal" ><i class="fa fa-eye mr-2"></i>' . __('app.view') . '</a>';

                if ($this->editAppreciationPermission == 'all' || ($this->editAppreciationPermission == 'added' && user()->id == $row->added_by) || ($this->editAppreciationPermission == 'owned' && user()->id == $row->award_to) || ($this->editAppreciationPermission == 'both' && ($row->added_by == user()->id || user()->id == $row->award_to))) {
                    $action .= '<a class="dropdown-item openRightModal" href="' . route('appreciations.edit', [$row->id]) . '">
                                <i class="fa fa-edit mr-2"></i>
                                ' . trans('app.edit') . '
                            </a>';
                }

                if ($this->deleteAppreciationPermission == 'all' || ($this->deleteAppreciationPermission == 'added' && user()->id == $row->added_by) || ($this->deleteAppreciationPermission == 'owned' && user()->id == $row->award_to) || ($this->deleteAppreciationPermission == 'both' && ($row->added_by == user()->id || user()->id == $row->award_to))) {
                    $action .= '<a class="dropdown-item delete-table-row" href="javascript:;" data-user-id="' . $row->id . '">
                                <i class="fa fa-trash mr-2"></i>
                                ' . trans('app.delete') . '
                            </a>';
                }

                $action .= '</div>
                    </div>
                </div>';

                return $action;
            })
            ->editColumn(
                'award_id',
                function ($row) {
                    if (isset($row->award->awardIcon)) {
                        return view('components.award-icon', [
                            'award' => $row->award
                        ]).' <span class="align-self-center ml-2">' . mb_ucwords($row->award->title) . '</span>';
                    }

                    return '-';

                }
            )
            ->addColumn(
                'appreciation_type',
                function ($row) {
                    if ($row->award) {
                        return mb_ucwords($row->award->title);
                    }

                    return '-';
                }
            )
            ->editColumn(
                'award_date',
                function ($row) {
                    return $row->award_date->translatedFormat($this->company->date_format);
                }
            )
            ->editColumn(
                'award_to',
                function ($row) {
                    return view('components.employee', [
                        'user' => $row->awardTo
                    ]);
                }
            )
            ->addColumn(
                'award_employee',
                function ($row) {
                    return $row->awardTo->name;
                }
            )
            ->addIndexColumn()
            ->smart(false)
            ->setRowId(function ($row) {
                return 'row-' . $row->id;
            })
            ->rawColumns(['check', 'action', 'award_id', 'award_to']);
    }

    /***
     * @param Notice $model
     * @return \Illuminate\Database\Query\Builder
     */
    public function query(Appreciation $model)
    {
        $request = $this->request();
        $model = $model->with(['award', 'award.awardIcon', 'awardTo'])->select('id', 'award_id', 'award_to', 'award_date', 'image', 'summary', 'created_at');

        $model->join('awards', 'awards.id', '=', 'appreciations.award_id')
            ->join('users', 'users.id', '=', 'appreciations.award_to');

        if ($request->startDate !== null && $request->startDate != 'null' && $request->startDate != '') {
            $startDate = Carbon::createFromFormat(company()->date_format, $request->startDate)->toDateString();
            $model = $model->where(DB::raw('DATE(appreciations.`award_date`)'), '>=', $startDate);
        }

        if ($request->endDate !== null && $request->endDate != 'null' && $request->endDate != '') {
            $endDate = Carbon::createFromFormat(company()->date_format, $request->endDate)->toDateString();
            $model = $model->where(DB::raw('DATE(appreciations.`award_date`)'), '<=', $endDate);
        }

        if ($this->viewAppreciationPermission == 'added') {
            $model->where('appreciations.added_by', user()->id);
        }

        if ($this->viewAppreciationPermission == 'owned') {
            $model->where('appreciations.award_to', user()->id);
        }

        if ($this->viewAppreciationPermission == 'both') {
            $model->where(function ($q) {
                $q->where('appreciations.added_by', '=', user()->id);

                $q->orWhere('appreciations.award_to', '=', user()->id);
            });
        }

        if ($request->searchText != '') {
            $model->where(function ($query) {
                $query->where('awards.title', 'like', '%' . request('searchText') . '%');
            });
        }

        if ($request->appreciationType != 'all') {
            $model->where(function ($query) use ($request) {
                $query->where('appreciations.award_id', $request->appreciationType);
            });
        }

        if ($request->awardTo != 'all') {
            $model->where(function ($query) use ($request) {
                $query->where('appreciations.award_to', $request->awardTo);
            });
        }

        $model->select('appreciations.*');

        return $model;
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->setBuilder('user-appreciation-table', 3)
            ->buttons(Button::make(['extend' => 'excel', 'text' => '<i class="fa fa-file-export"></i> ' . trans('app.exportExcel')]))
            ->parameters([
                'initComplete' => 'function () {
                   window.LaravelDataTables["user-appreciation-table"].buttons().container()
                    .appendTo("#table-actions")
                }',
                'fnDrawCallback' => 'function( oSettings ) {
                    $("body").tooltip({
                        selector: \'[data-toggle="tooltip"]\'
                    })
                }',
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'check' => [
                'title' => '<input type="checkbox" name="select_all_table" id="select-all-table" onclick="selectAllTable(this)">',
                'exportable' => false,
                'orderable' => false,
                'searchable' => false
            ],
            '#' => ['data' => 'DT_RowIndex', 'orderable' => false, 'searchable' => false, 'visible' => false, 'title' => '#'],
            __('modules.appreciations.awardToEmployee') => ['data' => 'award_to', 'exportable' => false, 'name' => 'award_to', 'title' => __('modules.appreciations.awardToEmployee')],
            __('modules.appreciations.awardTo') => ['data' => 'award_employee', 'name' => 'award_employee', 'visible' => false, 'title' => __('modules.appreciations.awardTo')],
            __('modules.appreciations.appreciationTypeName') => ['data' => 'award_id', 'exportable' => false, 'name' => 'award_id', 'title' => __('modules.appreciations.appreciationTypeName')],
            __('modules.appreciations.appreciationType') => ['data' => 'appreciation_type', 'name' => 'award_id', 'visible' => false, 'title' => __('modules.appreciations.appreciationType')],
            __('app.date') => ['data' => 'award_date', 'name' => 'award_date', 'title' => __('modules.appreciations.awardDate')],
            Column::computed('action', __('app.action'))
                ->exportable(false)
                ->printable(false)
                ->orderable(false)
                ->searchable(false)
                ->addClass('text-right pr-20')
        ];
    }

}

Zerion Mini Shell 1.0