<commit_before><?php

namespace App\Http\Controllers\Job;

use App\Http\Controllers\Controller;
use App\Models\Job;
use App\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class JobController extends Controller
{
    public function index(Request $request, $page=1, $perPage=100) {
        //$jobs = ['firstjob', 'secondjob', 'thirdjob', 'etc.'];

        $user = $request->user();

        $jobs = DB::table('job')->where('is_published', 1)->paginate($perPage);
        if(!empty($user) && isset($user->id)) {
            $notes = $this->getPrivateNoteCounts($jobs, $user->id);
        } else {
            $notes = null;
        }
        $jobsInTotal = $jobs->total();
        $counterInitValue = (($jobs->currentPage() - 1) * $jobs->perPage());
        //$jobs = Job::all()->where('is_published', 1)->forPage($page, $perPage);

        $searchInput = null;
        $transformedJobInfo = $this->transformJobsInfo($jobs, $searchInput);

        return view('job.index', compact('jobs', 'jobsInTotal', 'counterInitValue', 'notes', 'searchInput', 'transformedJobInfo'));
    }

    protected function getPrivateNoteCounts($jobs, $userId) {

        $notesResult = array();
        foreach($jobs as $job) {
            $notesResult[$job->id] = 0;
        }
        $notes = DB::table('notes')
            ->select('job_id', DB::raw('count(*) as total'))
            ->whereIn('job_id', array_keys($notesResult))
            ->whereIn('user_id', [$userId])
            ->groupBy('job_id')
            ->get();

        foreach($notes as $note) {
            $notesResult[$note->job_id] = $note->total;
        }

        return $notesResult;

    }

    public function show(Request $request, $job) {

        $user = $request->user();

        if($user && $user->exists()) {
            $job = Job::with(['notes' => function($query) use ($user) {
                $query->where('user_id', '=', $user->id);
            }])->find($job);
        } else {
            $job = Job::with(['notes' => function($query) use ($user) {
                $query->where('user_id', '=', NULL);
            }])->find($job);
        }

        // @todo: Admin:
        // $job->load('notes.user');
        // ?

        return view('job.show', compact('job'));
    }

    public function find(Request $request, $page=1, $perPage=100) {
        //$jobs = ['firstjob', 'secondjob', 'thirdjob', 'etc.'];

        $user = $request->user();

        $searchInput = $request->input('searchInput', '');
        $jobs = DB::table('job')->where('is_published', 1)->where('content_static_without_tags', 'like', '%' . $searchInput . '%')->paginate($perPage);
        if(!empty($user) && isset($user->id)) {
            $notes = $this->getPrivateNoteCounts($jobs, $user->id);
        } else {
            $notes = null;
        }<commit_msg>Order by change for job lists (index and search):  updated_at DESC, datetime_imported DESC, file_datetime DESC, created_at DESC<commit_after><?php

namespace App\Http\Controllers\Job;

use App\Http\Controllers\Controller;
use App\Models\Job;
use App\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class JobController extends Controller
{
    public function index(Request $request, $page=1, $perPage=100) {
        //$jobs = ['firstjob', 'secondjob', 'thirdjob', 'etc.'];

        $user = $request->user();

        $jobs = DB::table('job')
            ->where('is_published', 1)
            ->orderBy('updated_at', 'desc')
            ->orderBy('datetime_imported', 'desc')
            ->orderBy('file_datetime', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate($perPage);
        if(!empty($user) && isset($user->id)) {
            $notes = $this->getPrivateNoteCounts($jobs, $user->id);
        } else {
            $notes = null;
        }
        $jobsInTotal = $jobs->total();
        $counterInitValue = (($jobs->currentPage() - 1) * $jobs->perPage());
        //$jobs = Job::all()->where('is_published', 1)->forPage($page, $perPage);

        $searchInput = null;
        $transformedJobInfo = $this->transformJobsInfo($jobs, $searchInput);

        return view('job.index', compact('jobs', 'jobsInTotal', 'counterInitValue', 'notes', 'searchInput', 'transformedJobInfo'));
    }

    protected function getPrivateNoteCounts($jobs, $userId) {

        $notesResult = array();
        foreach($jobs as $job) {
            $notesResult[$job->id] = 0;
        }
        $notes = DB::table('notes')
            ->select('job_id', DB::raw('count(*) as total'))
            ->whereIn('job_id', array_keys($notesResult))
            ->whereIn('user_id', [$userId])
            ->groupBy('job_id')
            ->get();

        foreach($notes as $note) {
            $notesResult[$note->job_id] = $note->total;
        }

        return $notesResult;

    }

    public function show(Request $request, $job) {

        $user = $request->user();

        if($user && $user->exists()) {
            $job = Job::with(['notes' => function($query) use ($user) {
                $query->where('user_id', '=', $user->id);
            }])->find($job);
        } else {
            $job = Job::with(['notes' => function($query) use ($user) {
                $query->where('user_id', '=', NULL);
            }])->find($job);
        }

        // @todo: Admin:
        // $job->load('notes.user');
        // ?

        return view('job.show', compact('job'));
    }

    public function find(Request $request, $page=1, $perPage=100) {
        //$jobs = ['firstjob', 'secondjob', 'thirdjob', 'etc.'];

        $user = $request->user();

        $searchInput = $request->input('searchInput', '');
        $jobs = DB::table('job')
            ->where('is_published', 1)
            ->where('content_static_without_tags', 'like', '%' . $searchInput . '%')
            ->orderBy('updated_at', 'desc')
            ->orderBy('datetime_imported', 'desc')
            ->orderBy('file_datetime', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate($perPage);
        if(!empty($user) && isset($user->id)) {
            $notes = $this->getPrivateNoteCounts($jobs, $user->id);
        } else {
            $notes = null;
        }
{'fraction_non_alphanumeric': 0.1940274924948649, 'fraction_numerical': 0.004108073945331016, 'mean_word_length': 2.408723747980614, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '406427', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1919, 'n_words': 350}