<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides Admin Account management
 *
 * @package    lnApp
 * @subpackage Page/Account
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class Controller_Admin_Account extends Controller_TemplateDefault_Admin {
	protected $secure_actions = array(
		'ajaxlist'=>FALSE, // @todo To Change
		'list'=>TRUE,
		'listlog'=>TRUE,
	);

	public function action_ajaxlist() {
		$return = array();

		if (isset($_REQUEST['term']) AND trim($_REQUEST['term']))
			$return += ORM::factory('account')->list_autocomplete($_REQUEST['term']);

		$this->auto_render = FALSE;
		$this->response->headers('Content-Type','application/json');
		$this->response->body(json_encode(array_values($return)));
	}

	/**
	 * Show a list of account logins
	 */
	public function action_listlog() {
		Block::add(array(
			'title'=>_('Account Login Log'),
			'body'=>Table::display(
				ORM::factory('account_log')->order_by('id','DESC')->find_all(),
				25,
				array(
					'id'=>array('label'=>'ID'),
					'date_orig'=>array('label'=>'Date'),
					'account->name()'=>array('label'=>'Account'),
					'ip'=>array('label'=>'IP Address'),
					'details'=>array('label'=>'Details'),
				),
				array(
					'page'=>TRUE,
				)),
			));
	}

	/**
	 * Show a list of accounts
	 */
	public function action_list() {
		Block::add(array(
			'title'=>_('Customer List'),
			'body'=>Table::display(
				ORM::factory('account')->list_active(),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>'user/account/view/'),
					'accnum()'=>array('label'=>'Num'),
					'name(TRUE)'=>array('label'=>'Account'),
					'email'=>array('label'=>'Email'),
					'invoices_due_total(NULL,TRUE)'=>array('label'=>'Invoices','class'=>'right'),
					'count_services(TRUE,NULL)'=>array('label'=>'Services','class'=>'right'),
				),
				array(
					'page'=>TRUE,
					'type'=>'select',
					'form'=>'user/account/view',
				)),
		));
	}
}
?>
