<?php

class DashboardTable extends CWidget {

    public $rowHeaders;
    public $columnHeaders;
    public $visibleColumns;
    private $column_visible = array();
    public $data;
    public $colors;
    public $params_used_in_all_urls;
    public $params_used_in_all_urls_rows;
    public $params_used_in_all_urls_columns;

//    public function init($headers, $columnHeaders, $data, $colors) {
//	$this->rowHeaders = $headers;
//	$this->columnHeaders = $columnHeaders;
//	$this->data = $data;
//	$this->colors = $colors;
//    }

    public function run() {
	$this->renderItems();
    }

    /**
     * Renders the data items for the grid view.
     */
    public function renderItems() {
	echo "<table class='dashboard'>";
	$this->renderTableHeaders();
	$this->renderTableBody();
	echo "</table>";
    }

    /**
     * Renders the table header.
     */
    public function renderTableHeaders() {
	if ($this->columnHeaders != null) {
	    $multi_segment_string = '';
	    $none_segment_string = '';
	    echo "<thead>";
	    echo "<tr>";
	    echo "<th>";
	    echo "</th>";
	    foreach ($this->columnHeaders as $colidx => $column) {
		if (array_key_exists($colidx, $this->visibleColumns)) {//($colidx, $this->visibleColumns)) {
		    //$key = $this->visibleColumns[$colidx];
		    $this->column_visible[$column] = true;
		    if ($column == 'Multi Segment')
			$multi_segment_string = "<td style ='background-color:rgb(195,217,255)' class=" . str_replace(' ', '', strtolower($column)) . ">" . $column . "</td>";
		    else
		    if ($column == 'Non Segment')
			$none_segment_string = "<td style ='background-color:rgb(195,217,255)' class=" . str_replace(' ', '', strtolower($column)) . ">" . $column . "</td>";
		    else {
			echo "<td style ='background-color:rgb(195,217,255)' class=" . str_replace(' ', '', strtolower($column)) . ">" . $column;
			echo "</td>";
		    }
		}
		else
		    $this->column_visible[$column] = false;
	    }
	    echo $multi_segment_string;
	    echo $none_segment_string;
	    echo "</tr>";
	    echo "</thead>";
	}
    }

    /**
     * Renders the table body.
     */
    public function renderTableBody() {
	$htmlOptions = array();
	$n = count($this->data);
	echo "<tbody>";

	if ($n > 0) {
	    foreach ($this->data as $groupIds => $group) {
		foreach ($group as $groupInfoidx => $groupInfo) {
		    if (array_key_exists('visible', $group))
			if ($group['visible']) {
			    if ($groupInfoidx == 'data') {
				if (!array_key_exists('show_group_title', $group)) {
				    echo '<tr class="' . str_replace(' ', '', strtolower($groupIds)) . '">';
				    echo "<td class='group'>" . $groupIds;
				    echo "</td>";
				    echo "<td colspan=" . count($this->columnHeaders) . ">";
				    echo "</td>";
				    echo "</tr>";
				} else
				if ($group['show_group_title'] == true) {
				    echo '<tr class="' . str_replace(' ', '', strtolower($groupIds)) . '">';
				    echo "<td class='group'>" . $groupIds;
				    echo "</td>";
				    echo "<td colspan=" . count($this->columnHeaders) . ">";
				    echo "</td>";
				    echo "</tr>";
				}
				foreach ($groupInfo as $rowidx => $row) {
				    if (array_key_exists('url', $group)) {
					if (array_key_exists('skips_values', $group))
					    $this->renderTableRow($groupIds, $rowidx, $groupInfo, $group['url'], $group['skips_values']);
					else
					    $this->renderTableRow($groupIds, $rowidx, $groupInfo, $group['url']);
				    }
				    else
					$this->renderTableRow($groupIds, $rowidx, $groupInfo);
				}
			    }
			}
		}
	    }
	} else {
	    echo '<tr><td colspan="' . count($this->data) . '" class="empty">';
//            $this->renderEmptyText();
	    echo "</td></tr>";
	}
	echo "</tbody>";
    }

    /**
     * Renders a table body row.
     * @param integer $row the row number (zero-based).
     */
    public function renderTableRow($row_class, $rowidx, $row, $url = null, $skip_values = null) {

	$multi_segment_string = '';
	$none_segment_string = '';
	echo '<tr class=' . str_replace(' ', '', strtolower($row_class)) . ' style="background-color:' . $this->colors[$rowidx] . '">';
	echo "<th>" . $this->rowHeaders[$rowidx];
	echo "</th>";
	foreach ($row[$rowidx] as $colidx => $column) {
	    $row_url = $url;
	    if ($this->column_visible[$colidx]) {
		if ($row_url != null) {
		    //$row_url[] = array($this->columnHeaders[$colidx] => $column);
		    if ($this->params_used_in_all_urls_columns != null) {
			foreach ($this->params_used_in_all_urls_columns as $key => $value) {
			    $row_url[$key] = $value[$colidx];
			}
		    }
		    if ($this->params_used_in_all_urls_rows != null) {
			foreach ($this->params_used_in_all_urls_rows as $key => $value) {
			    $row_url[$key] = $value[$rowidx];
			}
		    }
		    if ($this->params_used_in_all_urls != null) {
			foreach ($this->params_used_in_all_urls as $key => $value) {
			    $row_url[$key] = $value;
			}
		    }
		    if ($skip_values != null && !in_array($column, $skip_values))
			$this->renderTableColumn($colidx, $column, true, $row_url, $multi_segment_string, $none_segment_string);
		    //echo "<td class=" . str_replace(' ', '', strtolower($this->columnHeaders[$colidx])) . ">" . CHtml::link($column, $row_url);
		    else
			$this->renderTableColumn($colidx, $column, false, null, $multi_segment_string, $none_segment_string);
		    //echo "<td class=" . str_replace(' ', '', strtolower($this->columnHeaders[$colidx])) . ">" . $column;
		}
		else
		    $this->renderTableColumn($colidx, $column, false, null, $multi_segment_string, $none_segment_string);
		//echo "<td class=" . str_replace(' ', '', strtolower($this->columnHeaders[$colidx])) . ">" . $column;
		if ($colidx == 'Multi Segment')
		    $multi_segment_string .= "</td>";
		elseif ($colidx == 'Non Segment')
		    $none_segment_string .= "</td>";
		else
		    echo "</td>";
	    }
	}
	echo $multi_segment_string;
	echo $none_segment_string;
	echo "</tr>";
    }

    /**
     * Renders a table body row.
     * @param integer $row the row number (zero-based).
     */
    public function renderTableColumn($colidx, $column, $is_link, $row_url, &$multi_segment_string, &$none_segment_string) {
	if ($is_link) {
	    if ($colidx == 'Multi Segment')
		$multi_segment_string = "<td class=" . str_replace(' ', '', strtolower($colidx)) . ">" . CHtml::link($column, $row_url);
	    elseif ($colidx == 'Non Segment')
		$none_segment_string = "<td class=" . str_replace(' ', '', strtolower($colidx)) . ">" . CHtml::link($column, $row_url);
	    else
		echo "<td class=" . str_replace(' ', '', strtolower($colidx)) . ">" . CHtml::link($column, $row_url);
	}
	else {
	    if ($colidx == 'Multi Segment')
		$multi_segment_string = "<td class=" . str_replace(' ', '', strtolower($colidx)) . ">" . $column;
	    elseif ($colidx == 'Non Segment')
		$none_segment_string = "<td class=" . str_replace(' ', '', strtolower($colidx)) . ">" . $column;
	    else
		echo "<td class=" . str_replace(' ', '', strtolower($colidx)) . ">" . $column;
	}
    }

}
?>
