<?php
/*****************************************************************************
 * ErrorLog.php
 * Contains key class ErrorLog.
 *
 * Kabuki CMS (C) 2013-2015, Aaron van Geffen
 *****************************************************************************/

class ErrorLog
{
	public static function log(array $data)
	{
		if (!Registry::has('db'))
			return false;

		return Registry::get('db')->query('
			INSERT INTO log_errors
			(id_user, message, debug_info, file, line, request_uri, time, ip_address)
			VALUES
			(:id_user, :message, :debug_info, :file, :line,
			 :request_uri, CURRENT_TIMESTAMP, :ip_address)',
			$data);
	}

	public static function flush()
	{
		return Registry::get('db')->query('DELETE FROM log_errors');
	}

	public static function getCount()
	{
		return Registry::get('db')->queryValue('
			SELECT COUNT(*)
			FROM log_errors');
	}

	public static function getOffset($offset, $limit, $order, $direction)
	{
		assert(in_array($order, ['id_entry', 'file', 'line', 'time', 'ipaddress', 'id_user']));
		$order = $order . ($direction === 'up' ? ' ASC' : ' DESC');

		return Registry::get('db')->queryAssocs('
			SELECT *
			FROM log_errors
			ORDER BY ' . $order . '
			LIMIT :offset, :limit',
			[
				'offset' => $offset,
				'limit' => $limit,
			]);
	}
}