/**
  * Returns the rendered HTML template.
  *
  * @return  string  the item rendered in HTML
  */
 public function render()
 {
     // The formatted result has not been generated yet
     if (empty($this->result)) {
         // Generate the result using the default format for the chosen template
         $formats = ThumbsUp::config('default_formats');
         $this->format($formats[$this->template]);
     }
     // Prepare item data for template
     $item = get_object_vars($this);
     unset($item['template'], $item['options']);
     // Load the chosen template
     $template = new ThumbsUp_Template(THUMBSUP_DOCROOT . 'templates/' . $this->template . '.php');
     // Pass on all item data to the template
     $template->set('item', (object) $item)->set('template', $this->template)->set('options', (object) $this->options);
     // Render the template output
     return $template->render();
 }
    /**
     * Shows the admin dashboard: an overview of all ThumbsUp items.
     *
     * @return  void
     */
    public function action_dashboard()
    {
        $template = new ThumbsUp_Template(THUMBSUP_DOCROOT . 'admin/html/dashboard.php');
        // Filter on closed
        $filter_closed = (isset($_GET['filter_closed']) and preg_match('/^[01]$/D', (string) $_GET['filter_closed'])) ? (int) $_GET['filter_closed'] : '';
        if ($filter_closed !== '') {
            $sql_filter[] = 'closed = ' . $filter_closed;
        }
        // Filter on name
        $filter_name = isset($_GET['filter_name']) ? (string) $_GET['filter_name'] : '';
        if ($filter_name !== '') {
            $sql_filter[] = 'name LIKE "%' . substr(ThumbsUp::db()->quote($filter_name), 1, -1) . '%"';
        }
        // Build a WHERE clause if needed
        $sql_where = empty($sql_filter) ? '' : ' WHERE ' . implode(' AND ', $sql_filter);
        // Count the total items
        $sth = ThumbsUp::db()->prepare('SELECT COUNT(1) FROM ' . ThumbsUp::config('database_table_prefix') . 'items ' . $sql_where);
        $sth->execute();
        $total_items = (int) $sth->fetchColumn();
        // Build the dropdown options for items_per_page
        foreach (array(10, 20, 50, 100, 200, 500, 1000, 2000, 5000) as $i) {
            $items_per_page_select[] = $i;
            // Don't bother showing values higher than needed
            if ($total_items < $i) {
                break;
            }
        }
        // Grab the current items_per_page setting
        if (isset($_GET['items_per_page'])) {
            // A value of "0" equals "view all"
            $items_per_page = $_GET['items_per_page'] === '0' ? 0 : max(1, (int) $_GET['items_per_page']);
        } else {
            // Default value
            $items_per_page = min(50, end($items_per_page_select));
        }
        // Support custom entered items_per_page GET values
        if ($items_per_page !== 0 and !in_array($items_per_page, $items_per_page_select)) {
            $items_per_page_select[] = $items_per_page;
            sort($items_per_page_select);
        }
        // Add an option for "view all" at the end
        $items_per_page_select[] = 0;
        // More pagination variables
        $total_pages = $items_per_page === 0 ? 1 : max(1, (int) ceil($total_items / $items_per_page));
        $page = isset($_GET['page']) ? min($total_pages, max(1, (int) $_GET['page'])) : 1;
        // Limit the results if "view all" has not been selected
        $sql_limit = $items_per_page === 0 ? '' : ' LIMIT ' . $items_per_page . ' OFFSET ' . ($page - 1) * $items_per_page;
        // Load the items
        $sth = ThumbsUp::db()->prepare('SELECT
				id, name, date, closed, votes_up, votes_down,
				votes_up - votes_down AS votes_balance,
				votes_up + votes_down AS votes_total,
				votes_up / (votes_up + votes_down) * 100 AS votes_pct_up,
				votes_down / (votes_up + votes_down) * 100 AS votes_pct_down
			FROM ' . ThumbsUp::config('database_table_prefix') . 'items ' . $sql_where . ' ORDER BY name ' . $sql_limit);
        $sth->execute();
        $items = array();
        while ($row = $sth->fetch(PDO::FETCH_OBJ)) {
            $items[(int) $row->id] = $row;
        }
        // Pass on all data we need to the template
        $template->filter_closed = $filter_closed;
        $template->filter_name = $filter_name;
        $template->page = $page;
        $template->total_items = $total_items;
        $template->total_pages = $total_pages;
        $template->items_per_page = $items_per_page;
        $template->items_per_page_select = $items_per_page_select;
        $template->items = $items;
        echo $template->render();
    }
 /**
  * Renders the HTML template, and outputs it (by default).
  *
  * @param   boolean  TRUE to return the HTML as a string instead of echoing it
  * @return  mixed    void by default; string if $return has been set to TRUE
  */
 public function render($return = FALSE)
 {
     // Setup the desired template
     $template = new ThumbsUp_Template(THUMBSUP_DOCROOT . 'templates/' . $this->config['template'] . '/html.php');
     // Pass on selected data to the template
     $template->config = $this->config;
     $template->item = $this->item;
     // Output or return the HTML
     return $template->render($return);
 }