<?php
/*****************************************************************************
 * TabularData.php
 * Contains the template that displays tabular data.
 *
 * Kabuki CMS (C) 2013-2015, Aaron van Geffen
 *****************************************************************************/

class TabularData extends SubTemplate
{
	public function __construct(GenericTable $table)
	{
		$this->_t = $table;

		$pageIndex = $table->getPageIndex();
		if ($pageIndex)
			$this->pager = new Pagination($pageIndex);
	}

	protected function html_content()
	{
		echo '
			<div class="admin_box">';

		$title = $this->_t->getTitle();
		if (!empty($title))
			echo '
				<h2>', $title, '</h2>';

		// Showing a page index?
		if (isset($this->pager))
			$this->pager->html_content();

		// Maybe even a small form?
		if (isset($this->_t->form_above))
			$this->showForm($this->_t->form_above);

		// Build the table!
		echo '
				<table class="table table-striped">
					<thead>
						<tr>';

		// Show the table's headers.
		foreach ($this->_t->getHeader() as $th)
		{
			echo '
							<th', (!empty($th['width']) ? ' width="' . $th['width'] . '"' : ''), (!empty($th['class']) ? ' class="' . $th['class'] . '"' : ''), ($th['colspan'] > 1 ? ' colspan="' . $th['colspan'] . '"' : ''), ' scope="', $th['scope'], '">',
							$th['href'] ? '<a href="' . $th['href'] . '">' . $th['label'] . '</a>' : $th['label'];

			if ($th['sort_mode'] )
				echo ' ', $th['sort_mode'] == 'up' ? '&uarr;' : '&darr;';

			echo '</th>';
		}

		echo '
						</tr>
					</thead>
					<tbody>';

		// Show the table's body.
		$body = $this->_t->getBody();
		if (is_array($body))
		{
			foreach ($body as $tr)
			{
				echo '
						<tr', (!empty($tr['class']) ? ' class="' . $tr['class'] . '"' : ''), '>';

				foreach ($tr['cells'] as $td)
					echo '
							<td', (!empty($td['width']) ? ' width="' . $td['width'] . '"' : ''), '>', $td['value'], '</td>';

				echo '
						</tr>';
			}
		}
		else
			echo '
						<tr>
							<td colspan="', count($this->_t->getHeader()), '">', $body, '</td>
						</tr>';

		echo '
					</tbody>
				</table>';

		// Maybe another small form?
		if (isset($this->_t->form_below))
			$this->showForm($this->_t->form_below);

		// Showing a page index?
		if (isset($this->pager))
			$this->pager->html_content();

		echo '
			</div>';
	}

	protected function showForm($form)
	{
		echo '
				<form action="', $form['action'], '" method="', $form['method'], '" class="table_form ', $form['class'], '">';

		if (!empty($form['fields']))
			foreach ($form['fields'] as $name => $field)
				echo '
					<input name="', $name, '" type="', $field['type'], '" placeholder="', $field['placeholder'], '"', isset($field['class']) ? ' class="' . $field['class'] . '"' : '', isset($field['value']) ? ' value="' . $field['value'] . '"' : '', '>';

		if (!empty($form['buttons']))
			foreach ($form['buttons'] as $name => $button)
				echo '
					<input name="', $name, '" type="', $button['type'], '" value="', $button['caption'], '" class="btn', isset($button['class']) ? ' ' . $button['class'] . '' : '', '">';

		echo '
				</form>';
	}
}