<commit_before><?php namespace VotingApp\Http\Controllers;

use VotingApp\Models\Candidate;
use VotingApp\Models\Category;
use Illuminate\Http\Request;
use Auth;
use DB;

class CandidatesController extends Controller
{

    /**
     * Validation rules
     * @var array
     */
    protected $rules = [
        'name' => 'required',
        'category_id' => 'required',
        'photo_source' => 'url',
        'image' => 'image',
    ];

    public function __construct()
    {
        $this->middleware('admin', ['except' => ['index', 'show']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $showAsGuest = Auth::check() && Auth::user()->admin && $request->get('guest');

        // Show admin interface instead for administrators. Admin users can
        // use the `?guest=✓` query parameter to bypass the admin view.
        if(Auth::check() && Auth::user()->admin && !$showAsGuest) {
            return $this->adminIndex($request);
        }

        // Hide candidates if `show_candidates` setting is disabled, unless logged
        // in as an administrator & using "show as guest" override
        if(!setting('show_candidates') && !$showAsGuest) {
            return view('candidates.index', ['categories' => []]);
        }

        $categories = Category::with('candidates')->get();

        return view('candidates.index', compact('categories'));
    }

    /**
     * Display administrative view for candidates.
     *
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function adminIndex(Request $request){
        // Get optional request params.
        $sort_by = $request->get('sort_by');
        $direction = $request->get('direction');

        $query = DB::table('candidates')
            ->join('categories', 'categories.id', '=', 'candidates.category_id')
            ->leftJoin('votes', 'candidates.id', '=', 'votes.candidate_id')
            ->select('candidates.name as name', 'candidates.slug', 'candidates.id', 'candidates.gender', 'categories.name as category', DB::raw('COUNT(votes.id) as votes'))
            ->groupBy('candidates.name');

        // If a sorting method & direction are provided, order by them.
        if ($sort_by && $direction) {
            $query->orderBy($sort_by, $direction);
        }

        // Within given sorting method, always list in descending vote order.
        $query->orderBy('votes', 'DESC');

        $candidates = $query->get();

        return view('candidates.adminIndex', compact('candidates'));

    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()<commit_msg>Fix default sorting in admin view.<commit_after><?php namespace VotingApp\Http\Controllers;

use VotingApp\Models\Candidate;
use VotingApp\Models\Category;
use Illuminate\Http\Request;
use Auth;
use DB;

class CandidatesController extends Controller
{

    /**
     * Validation rules
     * @var array
     */
    protected $rules = [
        'name' => 'required',
        'category_id' => 'required',
        'photo_source' => 'url',
        'image' => 'image',
    ];

    public function __construct()
    {
        $this->middleware('admin', ['except' => ['index', 'show']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $showAsGuest = Auth::check() && Auth::user()->admin && $request->get('guest');

        // Show admin interface instead for administrators. Admin users can
        // use the `?guest=✓` query parameter to bypass the admin view.
        if(Auth::check() && Auth::user()->admin && !$showAsGuest) {
            return $this->adminIndex($request);
        }

        // Hide candidates if `show_candidates` setting is disabled, unless logged
        // in as an administrator & using "show as guest" override
        if(!setting('show_candidates') && !$showAsGuest) {
            return view('candidates.index', ['categories' => []]);
        }

        $categories = Category::with('candidates')->get();

        return view('candidates.index', compact('categories'));
    }

    /**
     * Display administrative view for candidates.
     *
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function adminIndex(Request $request){
        // Get optional request params.
        $sort_by = $request->get('sort_by', 'name');
        $direction = $request->get('direction', 'ASC');

        $query = DB::table('candidates')
            ->join('categories', 'categories.id', '=', 'candidates.category_id')
            ->leftJoin('votes', 'candidates.id', '=', 'votes.candidate_id')
            ->select('candidates.name as name', 'candidates.slug', 'candidates.id', 'candidates.gender', 'categories.name as category', DB::raw('COUNT(votes.id) as votes'))
            ->groupBy('candidates.name');

        // If a sorting method & direction are provided, order by them.
        if ($sort_by && $direction) {
            $query->orderBy($sort_by, $direction);
        }

        // Within given sorting method, always list in descending vote order.
        $query->orderBy('votes', 'DESC');

        $candidates = $query->get();

        return view('candidates.adminIndex', compact('candidates'));

    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
{'fraction_non_alphanumeric': 0.15329109468497007, 'fraction_numerical': 0.0, 'mean_word_length': 2.527622594661701, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '5098402', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1569, 'n_words': 400}