<?php
/*****************************************************************************
 * ManageErrors.php
 * Contains the controller for managing errors.
 *
 * Kabuki CMS (C) 2013-2015, Aaron van Geffen
 *****************************************************************************/

class ManageErrors extends HTMLController
{
	public function __construct()
	{
		// Ensure it's just admins at this point.
		if (!Registry::get('user')->isAdmin())
			throw new NotAllowedException();

		// Clearing, are we?
		if (isset($_POST['clear']) && Session::validateSession('get'))
		{
			ErrorLog::flush();
			header('Location: ' . BASEURL . '/manageerrors/');
			exit;
		}

		Session::resetSessionToken();

		$options = [
			'title' => 'Error log',
			'form' => [
				'action' => BASEURL . '/manageerrors/?' . Session::getSessionTokenKey() . '=' . Session::getSessionToken(),
				'method' => 'post',
				'class' => 'col-md-6 text-end',
				'buttons' => [
					'clear' => [
						'type' => 'submit',
						'caption' => 'Delete all',
						'class' => 'btn-danger',
					],
				],
			],
			'columns' => [
				'id_entry' => [
					'value' => 'id_entry',
					'header' => '#',
					'is_sortable' => true,
				],
				'message' => [
					'header' => 'Message / URL',
					'is_sortable' => false,
					'format' => function($row) {
						return $row['message'] . '<br>' .
							'<div><a onclick="this.parentNode.childNodes[1].style.display=\'block\';this.style.display=\'none\';">Show debug info</a>' .
							'<pre style="display: none">' . htmlspecialchars($row['debug_info']) .
							'</pre></div>' .
							'<small><a href="' . BASEURL .
								htmlspecialchars($row['request_uri']) . '">' .
								htmlspecialchars($row['request_uri']) . '</a></small>';
					},
				],
				'file' => [
					'value' => 'file',
					'header' => 'File',
					'is_sortable' => true,
				],
				'line' => [
					'value' => 'line',
					'header' => 'Line',
					'is_sortable' => true,
				],
				'time' => [
					'format' => [
						'type' => 'timestamp',
						'pattern' => 'long',
						'value' => 'time',
					],
					'header' => 'Time',
					'is_sortable' => true,
				],
				'ip' => [
					'value' => 'ip_address',
					'header' => 'IP',
					'is_sortable' => true,
				],
				'uid' => [
					'header' => 'UID',
					'is_sortable' => true,
					'link' => BASEURL . '/edituser/?id={ID_USER}',
					'value' => 'id_user',
				],
			],
			'default_sort_order' => 'id_entry',
			'default_sort_direction' => 'down',
			'start' => $_GET['start'] ?? 0,
			'sort_order' => $_GET['order'] ?? '',
			'sort_direction' => $_GET['dir'] ?? '',
			'no_items_label' => "No errors to display -- we're all good!",
			'items_per_page' => 20,
			'index_class' => 'col-md-6',
			'base_url' => BASEURL . '/manageerrors/',
			'get_count' => 'ErrorLog::getCount',
			'get_data' => 'ErrorLog::getOffset',
		];

		$error_log = new GenericTable($options);
		parent::__construct('Error log - Page ' . $error_log->getCurrentPage() .' - ' . SITE_TITLE);
		$this->page->adopt(new TabularData($error_log));
	}
}