<?php
/*****************************************************************************
 * ManageUsers.php
 * Contains the controller with the list of users.
 *
 * Kabuki CMS (C) 2013-2015, Aaron van Geffen
 *****************************************************************************/

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

		$options = [
			'form' => [
				'action' => BASEURL . '/edituser/',
				'method' => 'get',
				'class' => 'floatright',
				'buttons' => [
					'add' => [
						'type' => 'submit',
						'caption' => 'Add new user',
					],
				],
			],
			'columns' => [
				'id_user' => [
					'value' => 'id_user',
					'header' => 'ID',
					'is_sortable' => true,
				],
				'surname' => [
					'header' => 'Last name',
					'is_sortable' => true,
					'parse' => [
						'link' => BASEURL . '/edituser/?id={ID_USER}',
						'data' => 'surname',
					],
				],
				'first_name' => [
					'header' => 'First name',
					'is_sortable' => true,
					'parse' => [
						'link' => BASEURL . '/edituser/?id={ID_USER}',
						'data' => 'first_name',
					],
				],
				'slug' => [
					'header' => 'Slug',
					'is_sortable' => true,
					'parse' => [
						'link' => BASEURL . '/edituser/?id={ID_USER}',
						'data' => 'slug',
					],
				],
				'emailaddress' => [
					'value' => 'emailaddress',
					'header' => 'Email address',
					'is_sortable' => true,
				],
				'last_action_time' => [
					'parse' => [
						'type' => 'timestamp',
						'data' => [
							'timestamp' => 'last_action_time',
							'pattern' => 'long',
						],
					],
					'header' => 'Last activity',
					'is_sortable' => true,
				],
				'ip_address' => [
					'is_sortable' => true,
					'value' => 'ip_address',
					'header' => 'IP address',
				],
				'is_admin' => [
					'is_sortable' => true,
					'header' => 'Admin?',
					'parse' => [
						'type' => 'function',
						'data' => function($row) {
							return $row['is_admin'] ? 'yes' : 'no';
						}
					],
				],
			],
			'start' => !empty($_GET['start']) ? (int) $_GET['start'] : 0,
			'sort_order' => !empty($_GET['order']) ? $_GET['order'] : '',
			'sort_direction' => !empty($_GET['dir']) ? $_GET['dir'] : '',
			'title' => 'Manage users',
			'no_items_label' => 'No users meet the requirements of the current filter.',
			'items_per_page' => 30,
			'index_class' => 'floatleft',
			'base_url' => BASEURL . '/manageusers/',
			'get_data' => function($offset = 0, $limit = 30, $order = '', $direction = 'down') {
				if (!in_array($order, ['id_user', 'surname', 'first_name', 'slug', 'emailaddress', 'last_action_time', 'ip_address', 'is_admin']))
					$order = 'id_user';

				$data = Registry::get('db')->queryAssocs('
					SELECT *
					FROM users
					ORDER BY {raw:order}
					LIMIT {int:offset}, {int:limit}',
					[
						'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'),
						'offset' => $offset,
						'limit' => $limit,
					]);

				return [
					'rows' => $data,
					'order' => $order,
					'direction' => $direction,
				];
			},
			'get_count' => function() {
				return Registry::get('db')->queryValue('
					SELECT COUNT(*)
					FROM users');
			}
		];

		$table = new GenericTable($options);
		parent::__construct('User management - Page ' . $table->getCurrentPage() .' - ' . SITE_TITLE);
		$this->page->adopt(new TabularData($table));
	}
}