Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * OfertaForm Registration
- * @author <your name here>
- */
- class OfertaForm extends TPage
- {
- protected $form; // form
- protected $frame;
- use Adianti\Base\AdiantiStandardFormTrait; // Standard form methods
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- $this->setDatabase('identalsim'); // defines the database
- $this->setActiveRecord('Oferta'); // defines the active record
- // creates the form
- $this->form = new TQuickForm('form_Oferta');
- $this->form->class = 'tform'; // change CSS class
- $this->form = new BootstrapFormWrapper($this->form);
- $this->form->style = 'display: table;width:100%'; // change style
- // define the form title
- $this->form->setFormTitle('Oferta');
- // create the form fields
- // field custom idpessoa
- $idoferta = new TEntry('idoferta');
- $nomeoferta = new TEntry('nomeoferta');
- $datainicio = new TDate('datainicio');
- $datatermino = new TDate('datatermino');
- //$ordem = new TEntry('ordem');
- $idpessoaestabelecimento = new TDBCombo('idpessoaestabelecimento', 'identalsim', 'EstabelecimentoPessoaJuridica', 'idpessoa', 'nomefantasia');
- $enderecodaimagem = new TFile('enderecodaimagem');
- $detalhamentooferta = new TText('detalhamentooferta');
- $idtipooferta = new TDBCombo('idtipooferta', 'identalsim', 'TipoOferta', 'idtipooferta', 'tipooferta');
- $idcategoriaoferta = new TDBCombo('idcategoriaoferta', 'identalsim', 'CategoriaOferta', 'idcategoriaoferta', 'categoriaoferta');
- //$acompanhavel = new TEntry('acompanhavel');
- // complete upload action
- $enderecodaimagem->setCompleteAction(new TAction([$this, 'onComplete']));
- // add the fields
- $this->form->setFieldsByRow(2);
- $this->form->addQuickField('ID', $idoferta, 100 );
- $this->form->addQuickField('Nome', $nomeoferta, 300 , new TRequiredValidator);
- $this->form->addQuickField('Banner', $enderecodaimagem, 300 , new TRequiredValidator);
- $this->form->addQuickField('Data inicial', $datainicio, 100 , new TRequiredValidator);
- $this->form->addQuickField('Data final', $datatermino, 100 , new TRequiredValidator);
- //$this->form->addQuickField('Ordem', $ordem, 100 );
- $this->form->addQuickField('Categoria', $idcategoriaoferta, 300 , new TRequiredValidator);
- $this->form->addQuickField('Oferta tipo', $idtipooferta, 300 , new TRequiredValidator);
- $this->form->addQuickField('Estabelecimento', $idpessoaestabelecimento, 300 , new TRequiredValidator);
- $this->form->addQuickField('Descrição', $detalhamentooferta, 300 , new TRequiredValidator);
- //$this->form->addQuickField('Acompanhavel', $acompanhavel, 200 );
- $detalhamentooferta->setSize(300, 100);
- $this->frame = new TElement('div');
- $this->frame->id = 'photo_frame';
- $this->frame->style = 'width:400px;height:auto;min-height:200px;border:1px solid gray;padding:4px;';
- $row = $this->form->addRow();
- $row->addCell('');
- $row->addCell($this->frame);
- $enderecodaimagem->setSize(200, 40);
- if (!empty($idoferta))
- {
- $idoferta->setEditable(FALSE);
- }
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'bs:plus-sign green');
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add(TPanelGroup::pack('Nova Oferta', $this->form));
- parent::add($container);
- }
- /**
- * On complete upload
- */
- public static function onComplete($param)
- {
- new TMessage('info', 'Imagem carregada: '.$param['enderecodaimagem']);
- // refresh photo_frame
- TScript::create("$('#photo_frame').html('')");
- TScript::create("$('#photo_frame').append(\"<img style='width:100%' src='tmp/{$param['enderecodaimagem']}'>\");");
- }
- /**
- * Overloaded method onSave()
- * Executed whenever the user clicks at the save button
- */
- public function onSave()
- {
- // first, use the default onSave()
- $object = parent::onSave();
- // if the object has been saved
- if ($object instanceof Oferta)
- {
- $source_file = 'tmp/'.$object->enderecodaimagem;
- $target_file = 'images/' . md5(time()) . $object->enderecodaimagem;
- $finfo = new finfo(FILEINFO_MIME_TYPE);
- // if the user uploaded a source file
- if (file_exists($source_file) AND ($finfo->file($source_file) == 'image/png' OR $finfo->file($source_file) == 'image/jpeg'))
- {
- // move to the target directory
- rename($source_file, $target_file);
- try
- {
- TTransaction::open($this->database);
- // update the photo_path
- $object->enderecodaimagem = $target_file;
- $object->store();
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- $image = new TImage($object->photo_path);
- $image->style = 'width: 100%';
- $this->frame->add( $image );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement