Skip to content

Instantly share code, notes, and snippets.

@devalexandre
Forked from FredAzevedo/ContaPagarList.class.php
Last active April 30, 2018 00:13
Show Gist options
  • Save devalexandre/d8586da369f48a0ccfd7a33918d8c081 to your computer and use it in GitHub Desktop.
Save devalexandre/d8586da369f48a0ccfd7a33918d8c081 to your computer and use it in GitHub Desktop.
<?php
/**
* ContaPagarList Listing
* @author Fred Az.
*/
class ContaPagarList extends TStandardList
{
protected $form; // registration form
protected $datagrid; // listing
protected $pageNavigation;
protected $transformCallback;
protected $formGrid;
/**
* Page constructor
*/
public function __construct()
{
parent::__construct();
parent::setDatabase('sample'); // defines the database
parent::setActiveRecord('ContaPagar'); // defines the active record
parent::setDefaultOrder('id', 'asc'); // defines the default order
// parent::setCriteria($criteria) // define a standard filter
parent::addFilterField('id', '=', 'id'); // filterField, operator, formField
parent::addFilterField('data_conta', '=', 'data_conta'); // filterField, operator, formField
parent::addFilterField('descricao', 'like', 'descricao'); // filterField, operator, formField
parent::addFilterField('documento', 'like', 'documento'); // filterField, operator, formField
parent::addFilterField('data_vencimento', '=', 'data_vencimento'); // filterField, operator, formField
parent::addFilterField('valor', '=', 'valor'); // filterField, operator, formField
// creates the form
$this->form = new TQuickForm('form_search_ContaPagar');
$this->form->class = 'tform'; // change CSS class
$this->form = new BootstrapFormWrapper($this->form);
$this->form->style = 'display: table;width:100%'; // change style
$this->form->setFormTitle('ContasPagar');
// create the form fields
$id = new TEntry('id');
$data_conta = new TDate('data_conta');
$descricao = new TEntry('descricao');
$documento = new TEntry('documento');
$data_vencimento = new TDate('data_vencimento');
$valor = new TEntry('valor');
// add the fields
$this->form->addQuickField('ID', $id, '100%' );
$this->form->addQuickField('Data Conta', $data_conta, '100%' );
$this->form->addQuickField('Descrição', $descricao, '100%' );
$this->form->addQuickField('Documento', $documento, '100%' );
$this->form->addQuickField('Vencimento', $data_vencimento, '100%' );
$this->form->addQuickField('Valor', $valor, '100%' );
// keep the form filled during navigation with session data
$this->form->setData( TSession::getValue('ContaPagar_filter_data') );
// add the search form actions
$btn = $this->form->addQuickAction(_t('Find'), new TAction(array($this, 'onSearch')), 'fa:search');
$btn->class = 'btn btn-sm btn-primary';
$btn = $this->form->addQuickAction(_t('New'), new TAction(array('ContaPagarForm', 'onEdit')), 'bs:plus-sign green');
$btnBaixar = $this->form->addQuickAction('Baixar', new TAction(array($this, 'onSave')), 'fa:arrow-circle-down');
// creates a DataGrid
$this->datagrid = new TDataGrid;
$this->datagrid->disableDefaultClick();
$this->datagrid = new BootstrapDatagridWrapper($this->datagrid);
$this->datagrid->style = 'width: 100%';
$this->datagrid->datatable = 'true';
// $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
// form grid
$this->formGrid = new TForm;
$this->form->add($this->datagrid);
$this->datagrid->disableDefaultClick(); // important!
// creates the datagrid columns
$column_check = new TDataGridColumn('check', 'Selecione', 'center'); // vau dar erro pq todos checkes teram o mesmo nome
$column_id = new TDataGridColumn('id', 'ID', 'right');
$column_data_conta = new TDataGridColumn('data_conta', 'Data Conta', 'center');
$column_descricao = new TDataGridColumn('descricao', 'Descrição', 'left');
$column_documento = new TDataGridColumn('documento', 'Documento', 'left');
$column_data_vencimento = new TDataGridColumn('data_vencimento', 'Vencimento', 'center');
$column_valor = new TDataGridColumn('valor', 'Valor', 'right');
// add the columns to the DataGrid
$this->datagrid->addColumn($column_check);
$this->datagrid->addColumn($column_id);
$this->datagrid->addColumn($column_data_conta);
$this->datagrid->addColumn($column_descricao);
$this->datagrid->addColumn($column_documento);
$this->datagrid->addColumn($column_data_vencimento);
$this->datagrid->addColumn($column_valor);
// creates the datagrid column actions
$order_id = new TAction(array($this, 'onReload'));
$order_id->setParameter('order', 'id');
$column_id->setAction($order_id);
$order_data_conta = new TAction(array($this, 'onReload'));
$order_data_conta->setParameter('order', 'data_conta');
$column_data_conta->setAction($order_data_conta);
$order_data_vencimento = new TAction(array($this, 'onReload'));
$order_data_vencimento->setParameter('order', 'data_vencimento');
$column_data_vencimento->setAction($order_data_vencimento);
$order_valor = new TAction(array($this, 'onReload'));
$order_valor->setParameter('order', 'valor');
$column_valor->setAction($order_valor);
// inline editing
/*$data_conta_edit = new TDataGridAction(array($this, 'onInlineEdit'));
$data_conta_edit->setField('id');
$column_data_conta->setEditAction($data_conta_edit);
$documento_edit = new TDataGridAction(array($this, 'onInlineEdit'));
$documento_edit->setField('id');
$column_documento->setEditAction($documento_edit);
$data_vencimento_edit = new TDataGridAction(array($this, 'onInlineEdit'));
$data_vencimento_edit->setField('id');
$column_data_vencimento->setEditAction($data_vencimento_edit);*/
// define the transformer method over image
$column_data_conta->setTransformer( function($value, $object, $row) {
$date = new DateTime($value);
return $date->format('d/m/Y');
});
// define the transformer method over image
$column_data_vencimento->setTransformer( function($value, $object, $row) {
$date = new DateTime($value);
return $date->format('d/m/Y');
});
// define the transformer method over image
$column_valor->setTransformer( function($value, $object, $row) {
return 'R$ ' . number_format($value, 2, ',', '.');
});
// create EDIT action
$action_edit = new TDataGridAction(array('ContaPagarForm', 'onEdit'));
//$action_edit->setUseButton(TRUE);
//$action_edit->setButtonClass('btn btn-default');
$action_edit->setLabel(_t('Edit'));
$action_edit->setImage('fa:pencil-square-o blue fa-lg');
$action_edit->setField('id');
$this->datagrid->addAction($action_edit);
// create DELETE action
$action_del = new TDataGridAction(array($this, 'onDelete'));
//$action_del->setUseButton(TRUE);
//$action_del->setButtonClass('btn btn-default');
$action_del->setLabel(_t('Delete'));
$action_del->setImage('fa:trash-o red fa-lg');
$action_del->setField('id');
$this->datagrid->addAction($action_del);
// create the datagrid model
$this->datagrid->createModel();
// create the page navigation
$this->pageNavigation = new TPageNavigation;
$this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
$this->pageNavigation->setWidth($this->datagrid->getWidth());
// vertical box container
$container = new TVBox;
$container->style = 'width: 100%';
$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
$container->add(TPanelGroup::pack('Contas a Pagar', $this->form));
$container->add(TPanelGroup::pack('', $this->datagrid, $this->pageNavigation));
parent::add($container);
}
function onReload($param = NULL)
{
try
{
// open a transaction with database 'samples'
TTransaction::open('sample');
// creates a repository for Category
$repository = new TRepository('ContaPagar');
// creates a criteria, ordered by id
$criteria = new TCriteria;
$order = isset($param['order']) ? $param['order'] : 'id';
$criteria->setProperty('order', $order);
// load the objects according to criteria
$categories = $repository->load($criteria);
$this->datagrid->clear();
if ($categories)
{
// iterate the collection of active records
foreach ($categories as $item)
{
$mycheck = new TCheckButton('check');
$item->check = $mycheck;
$item->check->setIndexValue($item->id);
// add the object inside the datagrid
$this->datagrid->addItem($item);
}
}
// close the transaction
TTransaction::close();
$this->loaded = true;
}
catch (Exception $e) // in case of exception
{
// shows the exception error message
new TMessage('error', $e->getMessage());
// undo all pending operations
TTransaction::rollback();
}
}
public function onSave($param)
{
$data = $this->form->getData(); // optional parameter: active record class
// put the data back to the form
$data = $this->form->setData($data);
// creates a string with the form element's values
$message = 'Escolhido : ' . $data->check . '<br>';
// show the message
new TMessage('info', $message);
}
/**
* shows the page
*/
function show()
{
$this->onReload();
parent::show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment