<?php

Yii::import('application.modules.auditTrail.widgets.portlets.ShowAuditTrail');

class HistorialWidget extends ShowAuditTrail {

    /**
        * generates content of widget the widget.
        * This renders the widget, if it is visible.
        */
    public function renderContent()
    {
            if($this->visible) {
                    $auditTrail = AuditTrail::model()->recently();
                    $auditTrail->model = get_class($this->model);
                    $auditTrail->model_id = $this->model->primaryKey;
                    $columnFormat = $this->getColumnFormat();

                    $dataProvider = $auditTrail->search();
                    $pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
                    $dataProvider->getPagination()->setPageSize($pageSize);
                    
                    $this->widget('application.extensions.SelGridView', array(
                        'id'=>'audit-trail-grid',
                        'dataProvider'=>$dataProvider,
                        'columns'=>$this->getColumnFormat(),
                        'filter'=>$this->model,
                        'filterPosition' => 'footer',
                        'cssFile' => Yii::app()->baseUrl . '/css/gridview2.css',
                        'itemsCssClass' => 'display',            
                        'pagerCssClass' => 'dataTables_paginate',
                        'template' => '{items}{summary}{pager}',            
                        'emptyText' => Yii::t('intranet', 'Este candidato no tiene cambios'),
                        'summaryCssClass' => 'dataTables_info',
                        'summaryText' => 'Mostrando registros del {start} al {end} de {count} en total.',
                        'selectableRows' => 1,
                        ));        
                    
            }
    }   
    
	/**
	 * Gets final column format. Starts with default column format (specified in this method
	 * and checks $this->dataGridColumnsOverride array to see if any columns need to use a
	 * user specified format.
	 * @return array The final format array, with any user specified formats taking precedent over defaults
	 */
	protected function getColumnFormat() {
		$evalUserLabel = $this->getEvalUserLabelCode();
		$columnFormat = array();
		$defaultColumnFormat = array(
                        'stamp' => array(
                            'type' => 'raw',
                            'name' => 'stamp',
                            'value' => 'Time::timeAgoInWords($data->stamp);',
                            'header' => Yii::t('intranet','Stamp'),
                            'filter' => '',                            
                            'headerHtmlOptions'=>array(
                                'class' => 'head0 sorting', 
                            ),
                            'cssClassExpression' => '"con0"',
                        ),
			'new_value' => array(
                            'name' => 'new_value',
                            'header' => Yii::t('intranet','Nuevo valor'),
                            'filter' => '',
                            'headerHtmlOptions'=>array(
                                'class' => 'head1 sorting', 
                            ),
                            'cssClassExpression' => '"con1"',
			),
			'old_value' => array(
                            'name' => 'old_value',
                            'header' => Yii::t('intranet','Valor anterior'),
                            'filter' => '',
                            'headerHtmlOptions'=>array(
                                'class' => 'head0 sorting', 
                            ),
                            'cssClassExpression' => '"con0"',                            
			),                    
			'action' => array(
                            'name' => 'action',
                            'header' => Yii::t('intranet','Acción'),                            
                            'filter'=> '',
                            'headerHtmlOptions'=>array(
                                'class' => 'head1 sorting', 
                            ),
                            'cssClassExpression' => '"con1"',                            
			),
			'field' => array(
                            'name' => 'field',
                            'header' => Yii::t('intranet','Campo'),
                            'filter' => '',
                            'headerHtmlOptions'=>array(
                                'class' => 'head0 sorting', 
                            ),
                            'cssClassExpression' => '"con0"',                            
			),
			'user_id' => array(
                            'name' => 'user_id',
                            'header' => Yii::t('intranet','ID usuario'),                            
                            'value'=>$evalUserLabel,
                            'filter'=> '',
                            'headerHtmlOptions'=>array(
                                'class' => 'head1 sorting', 
                            ),
                            'cssClassExpression' => '"con1"',                            
			),
		);
		
		foreach($defaultColumnFormat as $key => $format) {
			$columnFormat[] = isset($this->dataGridColumnsOverride[$key]) ? $this->dataGridColumnsOverride[$key] : $defaultColumnFormat[$key];
		}
		
		return $columnFormat;
	}
        
	protected function renderDecoration()
	{
            $this->title = null;
            parent::renderDecoration();
	}        
}
?>

