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

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\EntityRequest;
use phpDocumentor\Reflection\Types\Mixed;
use Symfony\Component\HttpFoundation\File\UploadedFile;

use Illuminate\Http\Request;
use Carbon\Carbon;

use DB;
use Log;
use Auth;
use App\Entity;
use App\EntityFilters;
use App\EntityType;
use App\EntityStatus;
use App\Tag;
use App\Alias;
use App\Role;
use App\Photo;
use App\Follow;

class EntitiesController extends Controller {

    // define a list of variables
    protected $rpp;
    protected $page;
    protected $sort;
    protected $sortBy;
    protected $sortOrder;
    protected $defaultCriteria;
    protected $hasFilter;

	public function __construct(Entity $entity)
	{
		$this->middleware('auth', ['only' => array('create', 'edit', 'store', 'update')]);
		$this->entity = $entity;

        // prefix for session storage
        $this->prefix = 'app.entities.';

        // default list variables
        $this->rpp = 5;
        $this->page = 1;
        $this->sort = array('name', 'desc');
        $this->sortBy = 'name';
        $this->sortOrder = 'asc';
        $this->defaultCriteria = NULL;
        $this->hasFilter = 1;
		parent::__construct();
	}


    /**
     * Update the page list parameters from the request
     *
     */
    protected function updatePaging($request)
    {
        // set sort by column
        if ($request->input('sort_by')) {
            $this->sortBy = $request->input('sort_by');
        };

        // set sort direction
        if ($request->input('sort_order')) {
            $this->sortOrder = $request->input('sort_order');
        };

        // set results per page
        if ($request->input('rpp')) {
            $this->rpp = $request->input('rpp');
        };
    }


    /**
     * Gets the reporting options from the request and saves to session
     *
     * @param Request $request
     */
    public function getReportingOptions(Request $request)
    {
        foreach (array('page', 'rpp', 'sort', 'criteria') as $option)
        {
            if (!$request->has($option))
            {
                continue;
            }
            switch ($option)
            {
                case 'sort':
                    $value = array
                    (
                        $request->input($option),
                        $request->input('sort_order', 'asc'),
                    );
                    break;
                default:
                    $value = $request->input($option);
                    break;
            }
            call_user_func
            (
                array($this, sprintf('set%s', ucwords($option))),
                $value
            );
        }
    }

    /**
     * Get user session attribute
     *
     * @param String $attribute
     * @param Mixed $default
     * @param Request $request
     * @return Mixed
     */
    protected function getAttribute($attribute, $default = null, Request $request)
    {
        return $request->session()
            ->get($this->prefix.$attribute, $default);
    }

    /**
     * Get session filters
     *
     * @return Array
     */
    protected function getFilters(Request $request)
    {
        return $this->getAttribute('filters', $this->getDefaultFilters(), $request);
    }

    /**
     * Criteria provides a way to define criteria to be applied to a tab on the index page.
     *
     * @return array
     */
    protected function getCriteria()
    {
        return $this->criteria;
    }

    /**
     * Get the current page for this module
     *
     * @return integner
     */
    protected function getPage()
    {
        return $this->getAttribute('page', 1);
    }

    /**
     * Get the current results per page
     *
     * @param Request $request
     * @return integer
     */
    protected function getRpp(Request $request)
    {
        //$rpp = $request->session()->get('filters', $this->rpp);
        return $this->getAttribute('rpp', $this->rpp);
    }
    /**
     * Get the sort order and column
     *
     * @return array
     */
    protected function getSort(Request $request)
    {
        return $this->getAttribute('sort', $this->getDefaultSort());
    }



    /**
     * Get the default sort array
     *
     * @return Array
     */
    protected function getDefaultSort()
    {
        return array('id', 'desc');
    }


    /**
     * Get the default filters array
     *
     * @return Array
     */
    protected function getDefaultFilters()
    {
        return array();
    }

    /**
     * Set user session attribute
     *
     * @param String $attribute
     * @param Mixed $value
     * @param Request $request
     * @return Mixed
     */
    protected function setAttribute($attribute, $value, Request $request)
    {
        return $request->session()
            ->put($this->prefix.$attribute, $value);
    }

    /**
     * Set filters attribute
     *
     * @param array $input
     * @return array
     */
    protected function setFilters(Request $request, array $input)
    {
        // example: $input = array('filter_tag' => 'role', 'filter_name' => 'xano');
        return $this->setAttribute('filters', $input, $request);
    }
    /**
     * Set criteria.
     *
     * @param array $input
     * @return string
     */
    protected function setCriteria($input)
    {
        $this->criteria = $input;
        return $this->criteria;
    }
    /**
     * Set page attribute
     *
     * @param integer $input
     * @return integer
     */
    protected function setPage($input)
    {
        return $this->setAttribute('page', $input);
    }
    /**
     * Set results per page attribute
     *
     * @param integer $input
     * @return integer
     */
    protected function setRpp($input)
    {
        return $this->setAttribute('rpp', 5);
    }
    /**
     * Set sort order attribute
     *
     * @param array $input
     * @return array
     */
    protected function setSort(array $input)
    {
        return $this->setAttribute('sort', $input);
    }


    /**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index(Request $request, EntityFilters $filters)
    {
        $hasFilter = 1;

        // get all active entites plus those created by the logged in user, ordered by type and name

        // updates sort, rpp from request
        $this->updatePaging($request);

        // get filters from session
        $filters = $this->getFilters($request);

        // base criteria
        $query = $this->buildCriteria($request);

        // get the threads
        $entities = $query->paginate($this->rpp);

        // if the user is not authenticated, filter out any guarded entities
        if (!Auth::check()) {
            $entities->filter(function($e){
                return ($e->visibility->name != 'Guarded');
            });
        }

        return view('entities.index')
            ->with(['rpp' => $this->rpp, 'sortBy' => $this->sortBy, 'sortOrder' => $this->sortOrder, 'hasFilter' => $hasFilter, 'filters' => $filters])
            ->with(compact('entities'))
            ->render();
    }

    /**
     * Builds the criteria from the session
     *
     * @return $query
     */
    public function buildCriteria(Request $request)
    {
        $hasFilter = 1;

        // get all the filters from the session<commit_msg>Removed filter for entity visibility bug.<commit_after><?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\EntityRequest;
use phpDocumentor\Reflection\Types\Mixed;
use Symfony\Component\HttpFoundation\File\UploadedFile;

use Illuminate\Http\Request;
use Carbon\Carbon;

use DB;
use Log;
use Auth;
use App\Entity;
use App\EntityFilters;
use App\EntityType;
use App\EntityStatus;
use App\Tag;
use App\Alias;
use App\Role;
use App\Photo;
use App\Follow;

class EntitiesController extends Controller {

    // define a list of variables
    protected $rpp;
    protected $page;
    protected $sort;
    protected $sortBy;
    protected $sortOrder;
    protected $defaultCriteria;
    protected $hasFilter;

	public function __construct(Entity $entity)
	{
		$this->middleware('auth', ['only' => array('create', 'edit', 'store', 'update')]);
		$this->entity = $entity;

        // prefix for session storage
        $this->prefix = 'app.entities.';

        // default list variables
        $this->rpp = 5;
        $this->page = 1;
        $this->sort = array('name', 'desc');
        $this->sortBy = 'name';
        $this->sortOrder = 'asc';
        $this->defaultCriteria = NULL;
        $this->hasFilter = 1;
		parent::__construct();
	}


    /**
     * Update the page list parameters from the request
     *
     */
    protected function updatePaging($request)
    {
        // set sort by column
        if ($request->input('sort_by')) {
            $this->sortBy = $request->input('sort_by');
        };

        // set sort direction
        if ($request->input('sort_order')) {
            $this->sortOrder = $request->input('sort_order');
        };

        // set results per page
        if ($request->input('rpp')) {
            $this->rpp = $request->input('rpp');
        };
    }


    /**
     * Gets the reporting options from the request and saves to session
     *
     * @param Request $request
     */
    public function getReportingOptions(Request $request)
    {
        foreach (array('page', 'rpp', 'sort', 'criteria') as $option)
        {
            if (!$request->has($option))
            {
                continue;
            }
            switch ($option)
            {
                case 'sort':
                    $value = array
                    (
                        $request->input($option),
                        $request->input('sort_order', 'asc'),
                    );
                    break;
                default:
                    $value = $request->input($option);
                    break;
            }
            call_user_func
            (
                array($this, sprintf('set%s', ucwords($option))),
                $value
            );
        }
    }

    /**
     * Get user session attribute
     *
     * @param String $attribute
     * @param Mixed $default
     * @param Request $request
     * @return Mixed
     */
    protected function getAttribute($attribute, $default = null, Request $request)
    {
        return $request->session()
            ->get($this->prefix.$attribute, $default);
    }

    /**
     * Get session filters
     *
     * @return Array
     */
    protected function getFilters(Request $request)
    {
        return $this->getAttribute('filters', $this->getDefaultFilters(), $request);
    }

    /**
     * Criteria provides a way to define criteria to be applied to a tab on the index page.
     *
     * @return array
     */
    protected function getCriteria()
    {
        return $this->criteria;
    }

    /**
     * Get the current page for this module
     *
     * @return integner
     */
    protected function getPage()
    {
        return $this->getAttribute('page', 1);
    }

    /**
     * Get the current results per page
     *
     * @param Request $request
     * @return integer
     */
    protected function getRpp(Request $request)
    {
        //$rpp = $request->session()->get('filters', $this->rpp);
        return $this->getAttribute('rpp', $this->rpp);
    }
    /**
     * Get the sort order and column
     *
     * @return array
     */
    protected function getSort(Request $request)
    {
        return $this->getAttribute('sort', $this->getDefaultSort());
    }



    /**
     * Get the default sort array
     *
     * @return Array
     */
    protected function getDefaultSort()
    {
        return array('id', 'desc');
    }


    /**
     * Get the default filters array
     *
     * @return Array
     */
    protected function getDefaultFilters()
    {
        return array();
    }

    /**
     * Set user session attribute
     *
     * @param String $attribute
     * @param Mixed $value
     * @param Request $request
     * @return Mixed
     */
    protected function setAttribute($attribute, $value, Request $request)
    {
        return $request->session()
            ->put($this->prefix.$attribute, $value);
    }

    /**
     * Set filters attribute
     *
     * @param array $input
     * @return array
     */
    protected function setFilters(Request $request, array $input)
    {
        // example: $input = array('filter_tag' => 'role', 'filter_name' => 'xano');
        return $this->setAttribute('filters', $input, $request);
    }
    /**
     * Set criteria.
     *
     * @param array $input
     * @return string
     */
    protected function setCriteria($input)
    {
        $this->criteria = $input;
        return $this->criteria;
    }
    /**
     * Set page attribute
     *
     * @param integer $input
     * @return integer
     */
    protected function setPage($input)
    {
        return $this->setAttribute('page', $input);
    }
    /**
     * Set results per page attribute
     *
     * @param integer $input
     * @return integer
     */
    protected function setRpp($input)
    {
        return $this->setAttribute('rpp', 5);
    }
    /**
     * Set sort order attribute
     *
     * @param array $input
     * @return array
     */
    protected function setSort(array $input)
    {
        return $this->setAttribute('sort', $input);
    }


    /**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index(Request $request, EntityFilters $filters)
    {
        $hasFilter = 1;

        // get all active entites plus those created by the logged in user, ordered by type and name

        // updates sort, rpp from request
        $this->updatePaging($request);

        // get filters from session
        $filters = $this->getFilters($request);

        // base criteria
        $query = $this->buildCriteria($request);

        // get the threads
        $entities = $query->paginate($this->rpp);


        return view('entities.index')
            ->with(['rpp' => $this->rpp, 'sortBy' => $this->sortBy, 'sortOrder' => $this->sortOrder, 'hasFilter' => $hasFilter, 'filters' => $filters])
            ->with(compact('entities'))
            ->render();
    }

    /**
     * Builds the criteria from the session
     *
     * @return $query
     */
    public function buildCriteria(Request $request)
    {
        $hasFilter = 1;

        // get all the filters from the session
{'fraction_non_alphanumeric': 0.13218508646025562, 'fraction_numerical': 0.0009568723942314265, 'mean_word_length': 1.8645262333594361, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 136, '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': '2878854', 'n_tokens_mistral': 4123, 'n_tokens_neox': 4043, 'n_words': 1059}