PMA\libraries\DisplayResults::getTable PHP Method

getTable() public method

Prepare a table of results returned by a SQL query.
public getTable ( &$dt_result, &$displayParts, array $analyzed_sql_results, boolean $is_limited_display = false ) : string
$analyzed_sql_results array analyzed sql results
$is_limited_display boolean With limited operations or not
return string $table_html Generated HTML content for resulted table
    public function getTable(&$dt_result, &$displayParts, $analyzed_sql_results, $is_limited_display = false)
    {
        /**
         * The statement this table is built for.
         * @var \SqlParser\Statements\SelectStatement
         */
        $statement = $analyzed_sql_results['statement'];
        $table_html = '';
        // Following variable are needed for use in isset/empty or
        // use with array indexes/safe use in foreach
        $fields_meta = $this->__get('fields_meta');
        $showtable = $this->__get('showtable');
        $printview = $this->__get('printview');
        // why was this called here? (already called from sql.php)
        //$this->setConfigParamsForDisplayTable();
        /**
         * @todo move this to a central place
         * @todo for other future table types
         */
        $is_innodb = isset($showtable['Type']) && $showtable['Type'] == self::TABLE_TYPE_INNO_DB;
        if ($is_innodb && PMA_isJustBrowsing($analyzed_sql_results, true)) {
            // "j u s t   b r o w s i n g"
            $pre_count = '~';
            $after_count = Util::showHint(Sanitize::sanitize(__('May be approximate. See [doc@faq3-11]FAQ 3.11[/doc].')));
        } else {
            $pre_count = '';
            $after_count = '';
        }
        // 1. ----- Prepares the work -----
        // 1.1 Gets the information about which functionalities should be
        //     displayed
        list($displayParts, $total) = $this->_setDisplayPartsAndTotal($displayParts);
        // 1.2 Defines offsets for the next and previous pages
        if ($displayParts['nav_bar'] == '1') {
            list($pos_next, $pos_prev) = $this->_getOffsets();
        }
        // end if
        // 1.3 Extract sorting expressions.
        //     we need $sort_expression and $sort_expression_nodirection
        //     even if there are many table references
        $sort_expression = array();
        $sort_expression_nodirection = array();
        $sort_direction = array();
        if (!empty($statement->order)) {
            foreach ($statement->order as $o) {
                $sort_expression[] = $o->expr->expr . ' ' . $o->type;
                $sort_expression_nodirection[] = $o->expr->expr;
                $sort_direction[] = $o->type;
            }
        } else {
            $sort_expression[] = '';
            $sort_expression_nodirection[] = '';
            $sort_direction[] = '';
        }
        $number_of_columns = count($sort_expression_nodirection);
        // 1.4 Prepares display of first and last value of the sorted column
        $sorted_column_message = '';
        for ($i = 0; $i < $number_of_columns; $i++) {
            $sorted_column_message .= $this->_getSortedColumnMessage($dt_result, $sort_expression_nodirection[$i]);
        }
        // 2. ----- Prepare to display the top of the page -----
        // 2.1 Prepares a messages with position information
        if ($displayParts['nav_bar'] == '1' && isset($pos_next)) {
            $message = $this->_setMessageInformation($sorted_column_message, $analyzed_sql_results, $total, $pos_next, $pre_count, $after_count);
            $table_html .= Util::getMessage($message, $this->__get('sql_query'), 'success');
        } elseif ((!isset($printview) || $printview != '1') && !$is_limited_display) {
            $table_html .= Util::getMessage(__('Your SQL query has been executed successfully.'), $this->__get('sql_query'), 'success');
        }
        // 2.3 Prepare the navigation bars
        if (strlen($this->__get('table')) === 0) {
            if ($analyzed_sql_results['querytype'] == 'SELECT') {
                // table does not always contain a real table name,
                // for example in MySQL 5.0.x, the query SHOW STATUS
                // returns STATUS as a table name
                $this->__set('table', $fields_meta[0]->table);
            } else {
                $this->__set('table', '');
            }
        }
        // can the result be sorted?
        if ($displayParts['sort_lnk'] == '1') {
            // At this point, $sort_expression is an array but we only verify
            // the first element in case we could find that the table is
            // sorted by one of the choices listed in the
            // "Sort by key" drop-down
            list($unsorted_sql_query, $sort_by_key_html) = $this->_getUnsortedSqlAndSortByKeyDropDown($analyzed_sql_results, $sort_expression[0]);
        } else {
            $sort_by_key_html = $unsorted_sql_query = '';
        }
        if ($displayParts['nav_bar'] == '1' && empty($statement->limit)) {
            $table_html .= $this->_getPlacedTableNavigations($pos_next, $pos_prev, self::PLACE_TOP_DIRECTION_DROPDOWN, $is_innodb, $sort_by_key_html);
        }
        // 2b ----- Get field references from Database -----
        // (see the 'relation' configuration variable)
        // initialize map
        $map = array();
        $target = array();
        if (!empty($statement->from)) {
            foreach ($statement->from as $field) {
                if (!empty($field->table)) {
                    $target[] = $field->table;
                }
            }
        }
        if (strlen($this->__get('table')) > 0) {
            // This method set the values for $map array
            $this->_setParamForLinkForeignKeyRelatedTables($map);
            // Coming from 'Distinct values' action of structure page
            // We manipulate relations mechanism to show a link to related rows.
            if ($this->__get('is_browse_distinct')) {
                $map[$fields_meta[1]->name] = array($this->__get('table'), $fields_meta[1]->name, '', $this->__get('db'));
            }
        }
        // end if
        // end 2b
        // 3. ----- Prepare the results table -----
        if ($is_limited_display) {
            $table_html .= "<br>";
        }
        $table_html .= $this->_getTableHeaders($displayParts, $analyzed_sql_results, $unsorted_sql_query, $sort_expression, $sort_expression_nodirection, $sort_direction, $is_limited_display);
        $table_html .= '<tbody>' . "\n";
        $table_html .= $this->_getTableBody($dt_result, $displayParts, $map, $analyzed_sql_results, $is_limited_display);
        $this->__set('display_params', null);
        $table_html .= '</tbody>' . "\n" . '</table>';
        // 4. ----- Prepares the link for multi-fields edit and delete
        if ($displayParts['del_lnk'] == self::DELETE_ROW && $displayParts['del_lnk'] != self::KILL_PROCESS) {
            $table_html .= $this->_getMultiRowOperationLinks($dt_result, $analyzed_sql_results, $displayParts['del_lnk']);
        }
        // 5. ----- Get the navigation bar at the bottom if required -----
        if ($displayParts['nav_bar'] == '1' && empty($statement->limit)) {
            $table_html .= $this->_getPlacedTableNavigations($pos_next, $pos_prev, self::PLACE_BOTTOM_DIRECTION_DROPDOWN, $is_innodb, $sort_by_key_html);
        } elseif (!isset($printview) || $printview != '1') {
            $table_html .= "\n" . '<br /><br />' . "\n";
        }
        // 6. ----- Prepare "Query results operations"
        if ((!isset($printview) || $printview != '1') && !$is_limited_display) {
            $table_html .= $this->_getResultsOperations($displayParts, $analyzed_sql_results);
        }
        return $table_html;
    }

Usage Example

Esempio n. 1
0
/**
 * Function to get html for the sql query results table
 *
 * @param DisplayResults $displayResultsObject instance of DisplayResult
 * @param string         $pmaThemeImage        theme image uri
 * @param string         $url_query            url query
 * @param array          $displayParts         the parts to display
 * @param bool           $editable             whether the result table is
 *                                             editable or not
 * @param int            $unlim_num_rows       unlimited number of rows
 * @param int            $num_rows             number of rows
 * @param bool           $showtable            whether to show table or not
 * @param object         $result               result of the executed query
 * @param array          $analyzed_sql_results analyzed sql results
 * @param bool           $is_limited_display   Show only limited operations or not
 *
 * @return String
 */
function PMA_getHtmlForSqlQueryResultsTable($displayResultsObject, $pmaThemeImage, $url_query, $displayParts, $editable, $unlim_num_rows, $num_rows, $showtable, $result, $analyzed_sql_results, $is_limited_display = false)
{
    $printview = isset($_REQUEST['printview']) ? $_REQUEST['printview'] : null;
    $table_html = '';
    $browse_dist = !empty($_REQUEST['is_browse_distinct']);
    if ($analyzed_sql_results['is_procedure']) {
        do {
            if (!isset($result)) {
                $result = $GLOBALS['dbi']->storeResult();
            }
            $num_rows = $GLOBALS['dbi']->numRows($result);
            if ($result !== false && $num_rows > 0) {
                $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
                $fields_cnt = count($fields_meta);
                $displayResultsObject->setProperties($num_rows, $fields_meta, $analyzed_sql_results['is_count'], $analyzed_sql_results['is_export'], $analyzed_sql_results['is_func'], $analyzed_sql_results['is_analyse'], $num_rows, $fields_cnt, $GLOBALS['querytime'], $pmaThemeImage, $GLOBALS['text_dir'], $analyzed_sql_results['is_maint'], $analyzed_sql_results['is_explain'], $analyzed_sql_results['is_show'], $showtable, $printview, $url_query, $editable, $browse_dist);
                $displayParts = array('edit_lnk' => $displayResultsObject::NO_EDIT_OR_DELETE, 'del_lnk' => $displayResultsObject::NO_EDIT_OR_DELETE, 'sort_lnk' => '1', 'nav_bar' => '1', 'bkm_form' => '1', 'text_btn' => '1', 'pview_lnk' => '1');
                $table_html .= $displayResultsObject->getTable($result, $displayParts, $analyzed_sql_results, $is_limited_display);
            }
            $GLOBALS['dbi']->freeResult($result);
            unset($result);
        } while ($GLOBALS['dbi']->moreResults() && $GLOBALS['dbi']->nextResult());
    } else {
        if (isset($result) && $result) {
            $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
            $fields_cnt = count($fields_meta);
        }
        $_SESSION['is_multi_query'] = false;
        $displayResultsObject->setProperties($unlim_num_rows, $fields_meta, $analyzed_sql_results['is_count'], $analyzed_sql_results['is_export'], $analyzed_sql_results['is_func'], $analyzed_sql_results['is_analyse'], $num_rows, $fields_cnt, $GLOBALS['querytime'], $pmaThemeImage, $GLOBALS['text_dir'], $analyzed_sql_results['is_maint'], $analyzed_sql_results['is_explain'], $analyzed_sql_results['is_show'], $showtable, $printview, $url_query, $editable, $browse_dist);
        $table_html .= $displayResultsObject->getTable($result, $displayParts, $analyzed_sql_results, $is_limited_display);
        $GLOBALS['dbi']->freeResult($result);
    }
    return $table_html;
}
DisplayResults