Your IP : 216.73.216.79


Current Path : /var/www/html/reddsis/app/Http/Controllers/Backend/
Upload File :
Current File : /var/www/html/reddsis/app/Http/Controllers/Backend/DataDashboardController.php

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\Backend\DataDashboard;
use Illuminate\Http\Request;

class DataDashboardController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->get('search');

        $data = DataDashboard::query()
            ->when($search, fn ($q) => $q->where('title', 'like', "%{$search}%"))
            ->orderBy('sorting')
            ->paginate(10)
            ->withQueryString();

        return view('backend.module.dataDashboard.index', ['data' => $data, 'search' => $search]);
    }

    public function create()
    {
        return view('backend.module.dataDashboard.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'nullable|string|max:255|unique:data_dashboard,title',
            'content' => 'nullable|string',
            'sorting' => 'nullable|integer',
            'col_span' => 'nullable|integer|min:1|max:4',
            'activation_status' => 'nullable|integer',
        ]);

        $validated['created_by'] = auth()->id();
        $validated['updated_by'] = auth()->id();

        DataDashboard::create($validated);

        flash()->success('Data created successfully!');

        return redirect()->route('data-dashboard.index');
    }

    public function edit($id)
    {
        $data = DataDashboard::findOrFail($id);

        return view('backend.module.dataDashboard.edit', ['data' => $data]);
    }

    public function update(Request $request, $id)
    {
        $validated = $request->validate([
            'title' => 'nullable|string|max:255|unique:data_dashboard,title,' . $id,
            'content' => 'nullable|string',
            'sorting' => 'nullable|integer',
            'col_span' => 'nullable|integer|min:1|max:4',
            'activation_status' => 'nullable|integer',
        ]);

        $validated['updated_by'] = auth()->id();

        DataDashboard::findOrFail($id)->update($validated);

        flash()->success('Data updated successfully!');

        return redirect()->route('data-dashboard.index');
    }

    public function destroy($id)
    {
        DataDashboard::findOrFail($id)->delete();

        flash()->success('Data deleted successfully!');

        return redirect()->route('data-dashboard.index');
    }

    public function format(Request $request)
    {
        $code = $request->input('content', '');
        return response()->json(['formatted' => $this->formatMixedCode($code)]);
    }

    public function lint(Request $request)
    {
        $code = $request->input('content', '');
        $errors = [];

        if (preg_match('/<\?php/', $code)) {
            $tmp = tempnam(sys_get_temp_dir(), 'php_lint_');
            file_put_contents($tmp, $code);
            exec('php -l ' . escapeshellarg($tmp) . ' 2>&1', $output, $exitCode);
            unlink($tmp);
            $msg = implode("\n", $output);
            if ($exitCode !== 0 && preg_match('/on line (\d+)/', $msg, $m)) {
                $errors[] = [
                    'line' => (int)$m[1] - 1,
                    'message' => trim(str_replace($tmp, 'PHP', $msg)),
                ];
            }
        }

        return response()->json(['errors' => $errors]);
    }

    private function formatMixedCode(string $code): string
    {
        $lines = preg_split('/\R/', $code);
        $result = [];
        $indent = 0;

        foreach ($lines as $line) {
            $trimmed = trim($line);
            if ($trimmed === '') {
                $result[] = '';
                continue;
            }

            if (preg_match('/^\}/', $trimmed) || preg_match('/^\];/', $trimmed) || preg_match('/^\)/', $trimmed)) {
                $indent = max(0, $indent - 1);
            }

            if (preg_match('/^<\?php/', $trimmed) || preg_match('/^<\?=/', $trimmed)) {
                $result[] = $trimmed;
                continue;
            }
            if ($trimmed === '?>') {
                $result[] = $trimmed;
                continue;
            }

            $result[] = str_repeat('    ', $indent) . $trimmed;

            $open = substr_count($trimmed, '{');
            $close = substr_count($trimmed, '}');
            $indent += ($open - $close);
            if ($indent < 0) $indent = 0;

            if (preg_match('/^<\?php/', $trimmed) && substr_count($trimmed, '{') === 0) {
                $indent += 1;
            }
        }

        return implode("\n", $result);
    }

    public function reorder(Request $request)
    {
        $ids = $request->input("ids", []);
        $page = (int) $request->input('page', 1);
        $perPage = 10;
        $offset = ($page - 1) * $perPage;
        foreach ($ids as $index => $id) {
            \App\Models\Backend\DataDashboard::where("id", $id)->update(["sorting" => $offset + $index + 1]);
        }
        return response()->json(["success" => true]);
    }
}