<?php

class SelfService_HistoryController extends Am_Mvc_Controller_Grid
{
    /** User */
    protected $user = null;
    protected $layout = 'layout.phtml';

    function  preDispatch()
    {
        $this->getDi()->auth->requireLogin();
        $this->user = $this->getDi()->auth->getUser();
        parent::preDispatch();
    }

    function createGrid()
    {
        $ds = new Am_Query($this->getDi()->selfserviceHistoryTable, 'i');
        $ds->addWhere('user_id=?', $this->user->pk());
        $ds->addOrder('dattm', true);

        $grid = new Am_Grid_ReadOnly('_tr', ___('Transactions History'), $ds, $this->getRequest(), $this->view, $this->getDi());
        $grid->addField(new Am_Grid_Field_Date('dattm', ___('Date/Time')))
            ->setFormatDatetime();
        $grid->addField('value', ___('Value'), null, 'right');
        $grid->addField('description', ___('Description'));
        $grid->addField('expire', ___('Expire Date'))->setGetFunction(
            function($record){
                return $record->expire ? ($record->expire==Am_Period::MAX_SQL_DATE ? '-' : amDate($record->expire)) : '-';
            });
        return $grid;
    }
}