<commit_before><?php
/**
 * BackendController - A backend Controller for the included example Modules.
 *
 * @author <NAME> - <EMAIL>
 * @version 3.0
 */

namespace App\Core;

use Nova\Http\Request;
use Nova\Routing\Route;
use Nova\Support\Facades\Auth;
use Nova\Support\Facades\Event;
use Nova\Support\Facades\Input;
use Nova\Support\Facades\Redirect;
use Nova\Support\Facades\Response;
use Nova\Support\Facades\View;

use App\Core\ThemedController;

use Modules\Messages\Models\Message;
use Modules\System\Models\Notification;


abstract class BackendController extends ThemedController
{
    /**
     * The currently used Template.
     *
     * @var string
     */
    protected $theme = 'AdminLite';

    /**
     * The currently used Layout.
     *
     * @var mixed
     */
    protected $layout = 'Backend';


    /**
     * Method executed before any action.
     */
    protected function before()
    {
        if (! Auth::check()) {
            // The User is not authenticated; nothing to do.
            return;
        }

        // The User is logged in; setup the Backend Menu.
        $user = Auth::user();

        //
        $items = $this->getMenuItems($user);

        View::share('menuItems', $items);

        //
        $notifications = Notification::where('user_id', $user->id)->unread()->count();

        View::share('notificationCount', $notifications);

        //
        $messages = Message::where('receiver_id', $user->id)->unread()->count();

        View::share('privateMessageCount', $messages);
    }

    private function getMenuItems($user)
    {
        $items = array();

        // Prepare the Event payload.
        $payload = array($user);

        // Fire the Event 'backend.menu' and store the results.
        $results = Event::fire('backend.menu', $payload);

        // Merge all results on a menu items array.
        foreach ($results as $result) {
            if (is_array($result) && ! empty($result)) {
                $items = array_merge($items, $result);
            }
        }

        // Sort the base menu items by their weight and title.
        $items = array_sort($items, function($value) {
            return sprintf('%06d - %s', $value['weight'], $value['title']);
        });

        // Sort the child menu items by their weight and title.
        foreach ($items as &$item) {
            $children = array_get($item, 'children', array());

            if (empty($children)) continue;

            $children = array_sort($children, function($value) {
                return sprintf('%06d - %s', $value['weight'], $value['title']);
            });

            $item['children'] = $children;
        }

        return $items;
    }

    /**
     * Server Side Processor for DataTables.
     *
     * @param Nova\Database\Query\Builder|Nova\Database\ORM\Builder $query
     * @param array $columns
     *
     * @return \Nova\Http\JsonResponse
     */
    protected function dataTable($query, array $columns)
    {
        $input = Input::only('columns', 'draw', 'start', 'length', 'search', 'order');

        // Retrieve the request variables.
        $requestColumns = array_get($input, 'columns', array());

        $draw   = array_get($input, 'draw', 0);
        $start  = array_get($input, 'start', 0);
        $length = array_get($input, 'length', 25);
        $search = array_get($input, 'search.value', '');
        $order  = array_get($input, 'order', array());

        //
        $totalCount = $query->count();

        // Handle the column ordering.
        if (! empty($order)) {
            foreach ($order as $options) {
                $columnIdx = intval($options['column']);

                $requestColumn = array_get($input, 'columns.' .$columnIdx, array());

                //
                $data = $requestColumn['data'];

                $column = array_first($columns, function ($key, $value) use ($data)
                {
                    return ($value['dt'] == $data);
                });

                if ($requestColumn['orderable'] == 'true') {
                    $dir = ($options['dir'] === 'asc') ? 'ASC' : 'DESC';

                    $query->orderBy($column['db'], $dir);
                }
            }
        }

        // Handle the global searching.
        $search = trim($search);

        if (! empty($search)) {
            $query->whereNested(function($query) use($requestColumns, $columns, $search)
            {
                foreach($requestColumns as $requestColumn) {
                    $data = $requestColumn['data'];

                    $column = array_first($columns, function ($key, $value) use ($data)
                    {
                        return ($value['dt'] == $data);
                    });

                    if ($requestColumn['searchable'] == 'true') {
                        $query->orWhere($column['db'], 'LIKE', '%' .$search .'%');
                    }
                }
            });
        }

        // Handle the column searching.
        foreach($requestColumns as $requestColumn) {
            $data = $requestColumn['data'];

            $column = array_first($columns, function ($key, $value) use ($data)
            {
                return ($value['dt'] == $data);
            });

            $search = trim($requestColumn['search']['value']);

            if (($requestColumn['searchable'] == 'true') && (strlen($search) > 0)) {
                $query->where($column['db'], 'LIKE', '%'.$searchValue.'%');
            }
        }

        $filteredCount = $query->count();

        // Handle the pagination and retrieve the data from database.
        $results = $query->skip($start)->take($length)->get();

        // Format the retrieved data to respect the DataTables specs.
        $data = array();<commit_msg>Improve App\Core\BackendController<commit_after><?php
/**
 * BackendController - A backend Controller for the included example Modules.
 *
 * @author <NAME> - <EMAIL>
 * @version 3.0
 */

namespace App\Core;

use Nova\Http\Request;
use Nova\Routing\Route;
use Nova\Support\Facades\Auth;
use Nova\Support\Facades\Event;
use Nova\Support\Facades\Input;
use Nova\Support\Facades\Redirect;
use Nova\Support\Facades\Response;
use Nova\Support\Facades\View;

use App\Core\ThemedController;

use Modules\Messages\Models\Message;
use Modules\System\Models\Notification;


abstract class BackendController extends ThemedController
{
    /**
     * The currently used Template.
     *
     * @var string
     */
    protected $theme = 'AdminLite';

    /**
     * The currently used Layout.
     *
     * @var mixed
     */
    protected $layout = 'Backend';


    /**
     * Method executed before any action.
     */
    protected function before()
    {
        if (! Auth::check()) {
            // The User is not authenticated; nothing to do.
            return;
        }

        // The User is logged in; setup the Backend Menu.
        $user = Auth::user();

        //
        $items = $this->getMenuItems($user);

        View::share('menuItems', $items);

        //
        $notifications = Notification::where('user_id', $user->id)->unread()->count();

        View::share('notificationCount', $notifications);

        //
        $messages = Message::where('receiver_id', $user->id)->unread()->count();

        View::share('privateMessageCount', $messages);
    }

    private function getMenuItems($user)
    {
        $items = array();

        // Prepare the Event payload.
        $payload = array($user);

        // Fire the Event 'backend.menu' and store the results.
        $results = Event::fire('backend.menu', $payload);

        // Merge all results on a menu items array.
        foreach ($results as $result) {
            if (is_array($result) && ! empty($result)) {
                $items = array_merge($items, $result);
            }
        }

        // Sort the base menu items by their weight and title.
        $items = array_sort($items, function($value) {
            return sprintf('%06d - %s', $value['weight'], $value['title']);
        });

        // Sort the child menu items by their weight and title.
        foreach ($items as &$item) {
            $children = array_get($item, 'children', array());

            if (empty($children)) continue;

            $children = array_sort($children, function($value) {
                return sprintf('%06d - %s', $value['weight'], $value['title']);
            });

            $item['children'] = $children;
        }

        return $items;
    }

    /**
     * Server Side Processor for DataTables.
     *
     * @param Nova\Database\Query\Builder|Nova\Database\ORM\Builder $query
     * @param array $columns
     *
     * @return \Nova\Http\JsonResponse
     */
    protected function dataTable($query, array $columns)
    {
        $totalCount = $query->count();

        // Retrieve the request variables.
        $input = Input::only('columns', 'draw', 'start', 'length', 'search', 'order');

        $requestColumns = array_get($input, 'columns', array());

        $draw   = array_get($input, 'draw',   0);
        $start  = array_get($input, 'start',  0);
        $length = array_get($input, 'length', 25);
        $order  = array_get($input, 'order',  array());

        // Handle the global searching.
        $search = trim(array_get($input, 'search.value', ''));

        if (! empty($search)) {
            $query->whereNested(function($query) use($requestColumns, $columns, $search)
            {
                foreach($requestColumns as $requestColumn) {
                    $data = $requestColumn['data'];

                    $column = array_first($columns, function ($key, $value) use ($data)
                    {
                        return ($value['dt'] == $data);
                    });

                    if ($requestColumn['searchable'] == 'true') {
                        $field = $column['db'];

                        $query->orWhere($field, 'LIKE', '%' .$search .'%');
                    }
                }
            });
        }

        // Handle the column searching.
        foreach($requestColumns as $requestColumn) {
            $data = $requestColumn['data'];

            $column = array_first($columns, function ($key, $value) use ($data)
            {
                return ($value['dt'] == $data);
            });

            $search = trim(array_get($requestColumn, 'search.value', ''));

            if (($requestColumn['searchable'] == 'true') && (strlen($search) > 0)) {
                $field = $column['db'];

                $query->where($field, 'LIKE', '%' .$search .'%');
            }
        }

        $filteredCount = $query->count();

        // Handle the column ordering.
        if (! empty($order)) {
            foreach ($order as $options) {
                $columnIdx = intval($options['column']);

                $requestColumn = array_get($input, 'columns.' .$columnIdx, array());

                //
                $data = $requestColumn['data'];

                $column = array_first($columns, function ($key, $value) use ($data)
                {
                    return ($value['dt'] == $data);
                });

                if ($requestColumn['orderable'] == 'true') {
                    $field = $column['db'];

                    $dir = ($options['dir'] === 'asc') ? 'ASC' : 'DESC';

                    $query->orderBy($field, $dir);
                }
            }
        }

        // Handle the pagination and retrieve the data from database.
        $results = $query->skip($start)->take($length)->get();

        // Format the retrieved data to respect the DataTables specs.
        $data = array();
{'fraction_non_alphanumeric': 0.15673686012669064, 'fraction_numerical': 0.0018832391713747645, 'mean_word_length': 1.864182397646482, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 41, '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': '7223560', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3093, 'n_words': 757}