Categories_parser::table PHP Method

table() public method

Categories Table
public table ( $data = '' ) : mixed
return mixed
    function table($data = '')
    {
        // Set the default options
        $defaults = array('cat_parent' => 0, 'sort_column' => 'cat_order', 'sort_order' => 'desc', 'show_total' => 'no', 'show_image' => 'yes', 'show_description' => 'no', 'heading' => '', 'cols' => 2, 'table_attr' => 'width="100%" class="cat_table"', 'row_start' => '', 'row_alt_start' => '', 'cell_start' => '', 'cell_alt_start' => '', 'trail_pad' => ' ');
        $options = $this->_ci->settings->get_params($data['attributes'], $defaults);
        $this->_ci->load->model('categories/categories_model');
        $this->_ci->load->helper('html');
        // setup table template
        $tmpl = array('table_open' => '<table ' . $options['table_attr'] . '>', 'row_start' => '<tr ' . $options['row_start'] . '>', 'row_end' => '</tr>', 'cell_start' => '<td ' . $options['cell_start'] . '>', 'cell_end' => '</td>', 'row_alt_start' => '<tr ' . $options['row_alt_start'] . '>', 'row_alt_end' => '</tr>', 'cell_alt_start' => '<td ' . $options['cell_alt_start'] . '>', 'cell_alt_end' => '</td>', 'table_close' => '</table>');
        // Load the table library
        $this->_ci->load->library('table');
        // Set the template as defined above.
        $this->_ci->table->set_template($tmpl);
        $this->_ci->table->set_empty($options['trail_pad']);
        // Make the columns
        $this->_ci->db->select('cat_id,cat_uri,cat_name,cat_image,cat_description')->from('categories')->where('cat_parent', (int) $options['cat_parent'])->where('cat_display', 'yes');
        // Allowed order by. This prevents some one from using something to produce an invalid query.
        $allowed_order_by = array('asc', 'desc');
        if (!in_array(strtolower($options['sort_order']), $allowed_order_by)) {
            $sort_order = 'desc';
        } else {
            $sort_order = $options['sort_order'];
        }
        // This sets the allowed order by clauses. Prevents invalid query.
        $allowed_sort = $this->_ci->db->list_fields('categories');
        if (in_array($options['sort_column'], $allowed_sort)) {
            $this->_ci->db->order_by($options['sort_column'], $sort_order);
        } else {
            $this->_ci->db->orderby('cat_order', 'DESC')->orderby('cat_parent', 'asc')->orderby('cat_name', 'asc');
        }
        $query = $this->_ci->db->get();
        $cats = array();
        if ($query->num_rows() == 0) {
            return FALSE;
        }
        foreach ($query->result_array() as $row) {
            $td = '';
            // Show the category image.
            if ($options['show_image'] == 'yes') {
                $this->_ci->benchmark->mark('cat_pi_show_images_start');
                if ($row['cat_image'] != '') {
                    $image_properties = array('src' => $this->_ci->config->item('cat_image_path') . $row['cat_image'], 'alt' => $row['cat_name'], 'class' => 'cat_image ' . $row['cat_uri'], 'title' => $row['cat_name']);
                } else {
                    $image_properties = array('src' => 'themes/' . $this->_ci->settings->get_setting('site_theme') . '/images/folder.gif', 'alt' => $row['cat_name'], 'class' => 'cat_image ' . $row['cat_uri'], 'title' => $row['cat_name']);
                }
                $td .= img($image_properties) . '&nbsp;';
                $this->_ci->benchmark->mark('cat_pi_show_images_end');
            }
            $td .= '<a class="cat_name" href="' . site_url('categories/' . $row['cat_uri']) . '">' . $row['cat_name'] . '</a>';
            // Show the total listings in this category and all children
            if ($options['show_total'] == 'yes') {
                $this->_ci->benchmark->mark('cat_pi_total_articles_start');
                $this->_ci->load->library('categories/categories_library');
                $total = $this->_ci->categories_model->total_articles($row['cat_id']);
                $td .= ' <span class="total">(' . $total . ')</span>';
                $this->_ci->benchmark->mark('cat_pi_total_articles_end');
            }
            // Show the description
            if ($options['show_description'] == 'yes') {
                $td .= '<br />' . $row['cat_description'];
            }
            // Make it into an array
            $td_arr = array($td);
            // Now merge this with others
            $cats = array_merge($cats, $td_arr);
        }
        $new_list = $this->_ci->table->make_columns($cats, $options['cols']);
        $output = '';
        if ($options['heading'] != '') {
            $output .= '<h2>' . $options['heading'] . '</h2>';
        }
        $output .= $this->_ci->table->generate($new_list);
        // finally clear the template incase it is used twice.
        $this->_ci->table->clear();
        return $output;
    }