Contao\DC_Table::limitMenu PHP Method

limitMenu() protected method

Return a select menu to limit results
protected limitMenu ( boolean $blnOptional = false ) : string
$blnOptional boolean
return string
    protected function limitMenu($blnOptional = false)
    {
        /** @var AttributeBagInterface $objSessionBag */
        $objSessionBag = \System::getContainer()->get('session')->getBag('contao_backend');
        $session = $objSessionBag->all();
        $filter = $GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['mode'] == 4 ? $this->strTable . '_' . CURRENT_ID : $this->strTable;
        $fields = '';
        // Set limit from user input
        if (\Input::post('FORM_SUBMIT') == 'tl_filters' || \Input::post('FORM_SUBMIT') == 'tl_filters_limit') {
            $strLimit = \Input::post('tl_limit');
            if ($strLimit == 'tl_limit') {
                unset($session['filter'][$filter]['limit']);
            } else {
                // Validate the user input (thanks to aulmn) (see #4971)
                if ($strLimit == 'all' || preg_match('/^[0-9]+,[0-9]+$/', $strLimit)) {
                    $session['filter'][$filter]['limit'] = $strLimit;
                }
            }
            $objSessionBag->replace($session);
            if (\Input::post('FORM_SUBMIT') == 'tl_filters_limit') {
                $this->reload();
            }
        } else {
            $this->limit = $session['filter'][$filter]['limit'] != '' ? $session['filter'][$filter]['limit'] == 'all' ? null : $session['filter'][$filter]['limit'] : '0,' . \Config::get('resultsPerPage');
            $arrProcedure = $this->procedure;
            $arrValues = $this->values;
            $query = "SELECT COUNT(*) AS count FROM " . $this->strTable;
            if (!empty($this->root) && is_array($this->root)) {
                $arrProcedure[] = 'id IN(' . implode(',', $this->root) . ')';
            }
            // Support empty ptable fields
            if ($GLOBALS['TL_DCA'][$this->strTable]['config']['dynamicPtable']) {
                $arrProcedure[] = $this->ptable == 'tl_article' ? "(ptable=? OR ptable='')" : "ptable=?";
                $arrValues[] = $this->ptable;
            }
            if (!empty($arrProcedure)) {
                $query .= " WHERE " . implode(' AND ', $arrProcedure);
            }
            $objTotal = $this->Database->prepare($query)->execute($arrValues);
            $this->total = $objTotal->count;
            $options_total = 0;
            $blnIsMaxResultsPerPage = false;
            // Overall limit
            if ($this->total > \Config::get('maxResultsPerPage') && ($this->limit === null || preg_replace('/^.*,/', '', $this->limit) == \Config::get('maxResultsPerPage'))) {
                if ($this->limit === null) {
                    $this->limit = '0,' . \Config::get('maxResultsPerPage');
                }
                $blnIsMaxResultsPerPage = true;
                \Config::set('resultsPerPage', \Config::get('maxResultsPerPage'));
                $session['filter'][$filter]['limit'] = \Config::get('maxResultsPerPage');
            }
            $options = '';
            // Build options
            if ($this->total > 0) {
                $options = '';
                $options_total = ceil($this->total / \Config::get('resultsPerPage'));
                // Reset limit if other parameters have decreased the number of results
                if ($this->limit !== null && ($this->limit == '' || preg_replace('/,.*$/', '', $this->limit) > $this->total)) {
                    $this->limit = '0,' . \Config::get('resultsPerPage');
                }
                // Build options
                for ($i = 0; $i < $options_total; $i++) {
                    $this_limit = $i * \Config::get('resultsPerPage') . ',' . \Config::get('resultsPerPage');
                    $upper_limit = $i * \Config::get('resultsPerPage') + \Config::get('resultsPerPage');
                    if ($upper_limit > $this->total) {
                        $upper_limit = $this->total;
                    }
                    $options .= '
  <option value="' . $this_limit . '"' . \Widget::optionSelected($this->limit, $this_limit) . '>' . ($i * \Config::get('resultsPerPage') + 1) . ' - ' . $upper_limit . '</option>';
                }
                if (!$blnIsMaxResultsPerPage) {
                    $options .= '
  <option value="all"' . \Widget::optionSelected($this->limit, null) . '>' . $GLOBALS['TL_LANG']['MSC']['filterAll'] . '</option>';
                }
            }
            // Return if there is only one page
            if ($blnOptional && ($this->total < 1 || $options_total < 2)) {
                return '';
            }
            $fields = '
<select name="tl_limit" class="tl_select' . ($session['filter'][$filter]['limit'] != 'all' && $this->total > \Config::get('resultsPerPage') ? ' active' : '') . '" onchange="this.form.submit()">
  <option value="tl_limit">' . $GLOBALS['TL_LANG']['MSC']['filterRecords'] . '</option>' . $options . '
</select> ';
        }
        return '

<div class="tl_limit tl_subpanel">
<strong>' . $GLOBALS['TL_LANG']['MSC']['showOnly'] . ':</strong> ' . $fields . '
</div>';
    }