Skip to content

Instantly share code, notes, and snippets.

@FredAzevedo
Last active May 12, 2018 20:07
Show Gist options
  • Save FredAzevedo/90da4930ef2212c282082636a41be6c8 to your computer and use it in GitHub Desktop.
Save FredAzevedo/90da4930ef2212c282082636a41be6c8 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
// 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');
// 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->formGrid->add($this->form);
// creates the datagrid columns
$column_check = new TDataGridColumn('checkbox', 'Selecione', 'center');
$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);
// 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)
{
$item->checkbox = new TCheckButton('checkbox'.$item->id);
$item->checkbox->setIndexValue($item->id);
$this->datagrid->addItem($item);
$this->form->addField($item->checkbox);
}
}
// 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();
$this->form->setData($data);
$selecionados = [];
foreach ($this->form->getFields() as $value => $field)
{
if ($field instanceof TCheckButton)
{
$parts = explode('checkbox', $value);
$id = $parts[1];
if ($field->getValue() == $id)
{
echo $selecionados[] = $id;
}
}
}
}
/**
* 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