Advertisement
roniewill

Upload em diretório externo

Sep 19th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. <?php
  2. /**
  3. * OfertaForm Registration
  4. * @author <your name here>
  5. */
  6. class OfertaForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $frame;
  10.  
  11. use Adianti\Base\AdiantiStandardFormTrait; // Standard form methods
  12.  
  13. /**
  14. * Class constructor
  15. * Creates the page and the registration form
  16. */
  17. function __construct()
  18. {
  19. parent::__construct();
  20.  
  21. $this->setDatabase('identalsim'); // defines the database
  22. $this->setActiveRecord('Oferta'); // defines the active record
  23.  
  24. // creates the form
  25. $this->form = new TQuickForm('form_Oferta');
  26. $this->form->class = 'tform'; // change CSS class
  27. $this->form = new BootstrapFormWrapper($this->form);
  28. $this->form->style = 'display: table;width:100%'; // change style
  29.  
  30. // define the form title
  31. $this->form->setFormTitle('Oferta');
  32.  
  33.  
  34.  
  35. // create the form fields
  36. // field custom idpessoa
  37. $idoferta = new TEntry('idoferta');
  38. $nomeoferta = new TEntry('nomeoferta');
  39. $datainicio = new TDate('datainicio');
  40. $datatermino = new TDate('datatermino');
  41. //$ordem = new TEntry('ordem');
  42. $idpessoaestabelecimento = new TDBCombo('idpessoaestabelecimento', 'identalsim', 'EstabelecimentoPessoaJuridica', 'idpessoa', 'nomefantasia');
  43. $enderecodaimagem = new TFile('enderecodaimagem');
  44. $detalhamentooferta = new TText('detalhamentooferta');
  45. $idtipooferta = new TDBCombo('idtipooferta', 'identalsim', 'TipoOferta', 'idtipooferta', 'tipooferta');
  46. $idcategoriaoferta = new TDBCombo('idcategoriaoferta', 'identalsim', 'CategoriaOferta', 'idcategoriaoferta', 'categoriaoferta');
  47. //$acompanhavel = new TEntry('acompanhavel');
  48.  
  49. // complete upload action
  50. $enderecodaimagem->setCompleteAction(new TAction([$this, 'onComplete']));
  51.  
  52.  
  53. // add the fields
  54. $this->form->setFieldsByRow(2);
  55. $this->form->addQuickField('ID', $idoferta, 100 );
  56. $this->form->addQuickField('Nome', $nomeoferta, 300 , new TRequiredValidator);
  57. $this->form->addQuickField('Banner', $enderecodaimagem, 300 , new TRequiredValidator);
  58. $this->form->addQuickField('Data inicial', $datainicio, 100 , new TRequiredValidator);
  59. $this->form->addQuickField('Data final', $datatermino, 100 , new TRequiredValidator);
  60. //$this->form->addQuickField('Ordem', $ordem, 100 );
  61. $this->form->addQuickField('Categoria', $idcategoriaoferta, 300 , new TRequiredValidator);
  62. $this->form->addQuickField('Oferta tipo', $idtipooferta, 300 , new TRequiredValidator);
  63. $this->form->addQuickField('Estabelecimento', $idpessoaestabelecimento, 300 , new TRequiredValidator);
  64. $this->form->addQuickField('Descrição', $detalhamentooferta, 300 , new TRequiredValidator);
  65. //$this->form->addQuickField('Acompanhavel', $acompanhavel, 200 );
  66.  
  67.  
  68. $detalhamentooferta->setSize(300, 100);
  69.  
  70. $this->frame = new TElement('div');
  71. $this->frame->id = 'photo_frame';
  72. $this->frame->style = 'width:400px;height:auto;min-height:200px;border:1px solid gray;padding:4px;';
  73. $row = $this->form->addRow();
  74. $row->addCell('');
  75. $row->addCell($this->frame);
  76.  
  77. $enderecodaimagem->setSize(200, 40);
  78.  
  79. if (!empty($idoferta))
  80. {
  81. $idoferta->setEditable(FALSE);
  82. }
  83.  
  84. /** samples
  85. $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  86. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  87. $fieldX->setSize( 100, 40 ); // set size
  88. **/
  89.  
  90. // create the form actions
  91. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  92. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'bs:plus-sign green');
  93.  
  94. // vertical box container
  95. $container = new TVBox;
  96. $container->style = 'width: 90%';
  97. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  98. $container->add(TPanelGroup::pack('Nova Oferta', $this->form));
  99.  
  100. parent::add($container);
  101. }
  102.  
  103. /**
  104. * On complete upload
  105. */
  106. public static function onComplete($param)
  107. {
  108. new TMessage('info', 'Imagem carregada: '.$param['enderecodaimagem']);
  109.  
  110. // refresh photo_frame
  111. TScript::create("$('#photo_frame').html('')");
  112. TScript::create("$('#photo_frame').append(\"<img style='width:100%' src='tmp/{$param['enderecodaimagem']}'>\");");
  113. }
  114.  
  115. /**
  116. * Overloaded method onSave()
  117. * Executed whenever the user clicks at the save button
  118. */
  119. public function onSave()
  120. {
  121. // first, use the default onSave()
  122. $object = parent::onSave();
  123.  
  124. // if the object has been saved
  125. if ($object instanceof Oferta)
  126. {
  127. $source_file = 'tmp/'.$object->enderecodaimagem;
  128. $target_file = 'images/' . md5(time()) . $object->enderecodaimagem;
  129. $finfo = new finfo(FILEINFO_MIME_TYPE);
  130.  
  131. // if the user uploaded a source file
  132. if (file_exists($source_file) AND ($finfo->file($source_file) == 'image/png' OR $finfo->file($source_file) == 'image/jpeg'))
  133. {
  134. // move to the target directory
  135. rename($source_file, $target_file);
  136. try
  137. {
  138. TTransaction::open($this->database);
  139. // update the photo_path
  140. $object->enderecodaimagem = $target_file;
  141. $object->store();
  142.  
  143. TTransaction::close();
  144. }
  145. catch (Exception $e) // in case of exception
  146. {
  147. new TMessage('error', $e->getMessage());
  148. TTransaction::rollback();
  149. }
  150. }
  151. $image = new TImage($object->photo_path);
  152. $image->style = 'width: 100%';
  153. $this->frame->add( $image );
  154. }
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement