TMultiFile Exibindo na tela Olá, Estou com problemas de exibir imagem na tela com TMultiFile, está sendo gravado perfeitamente no banco, as images vão para pasta correta, na hora da gravação exibido perfeitamente em tela, mas na hora da edição não aparece. A unica coisa que fiz foi alterar o nome da pasta de gravação, está sendo gravada na pasta "imgpessoas" ao invés de "images" Obs: $photo_path = new...
AR
TMultiFile Exibindo na tela  
Olá,
Estou com problemas de exibir imagem na tela com TMultiFile, está sendo gravado perfeitamente no banco, as images vão para pasta correta, na hora da gravação exibido perfeitamente em tela, mas na hora da edição não aparece.

A unica coisa que fiz foi alterar o nome da pasta de gravação, está sendo gravada na pasta "imgpessoas" ao invés de "images"

Obs: $photo_path = new TFile('photo_path'); está funcionando perfeitamente.

 
  1. <?php
  2. /**
  3. * PessoaForm
  4. *
  5. * @version 1.0
  6. * @package dbaerp
  7. * @subpackage control
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class PessoaForm extends TWindow
  13. {
  14. protected $form; // form
  15. use Adianti\Base\AdiantiFileSaveTrait;
  16. /**
  17. * Form constructor
  18. * @param $param Request
  19. */
  20. public function __construct( $param )
  21. {
  22. parent::__construct();
  23. parent::setSize(0.8, null);
  24. parent::removePadding();
  25. parent::removeTitleBar();
  26. //parent::disableEscape();
  27. // creates the form
  28. $this->form = new BootstrapFormBuilder('form_Pessoa');
  29. $this->form->setFormTitle('Pessoa');
  30. $this->form->setProperty('style', 'margin:0;border:0');
  31. $this->form->setClientValidation(true);
  32. $this->form->appendPage('Informações Basicas');
  33. // create the form fields
  34. $id = new TEntry('id');
  35. $nome = new TEntry('nome');
  36. $nome_fantasia = new TEntry('nome_fantasia');
  37. $tipo = new TCombo('tipo');
  38. $codigo_nacional = new TEntry('codigo_nacional');
  39. $codigo_estadual = new TEntry('codigo_estadual');
  40. $codigo_municipal = new TEntry('codigo_municipal');
  41. $ibge = new TEntry('ibge');
  42. $fone = new TEntry('fone');
  43. $email = new TEntry('email');
  44. $observacao = new TText('observacao');
  45. $cep = new TEntry('cep');
  46. $logradouro = new TEntry('logradouro');
  47. $numero = new TEntry('numero');
  48. $complemento = new TEntry('complemento');
  49. $bairro = new TEntry('bairro');
  50. //------------------------------------------------
  51. $photo_path = new TFile('photo_path');
  52. $images = new TMultiFile('images');
  53. // allow just these extensions
  54. $photo_path->setAllowedExtensions( ['gif', 'png', 'jpg', 'jpeg'] );
  55. $images->setAllowedExtensions( ['gif', 'png', 'jpg', 'jpeg'] );
  56. // enable progress bar, preview
  57. $photo_path->enableFileHandling();
  58. $photo_path->enablePopover();
  59. // enable progress bar, preview, and gallery mode
  60. $images->enableFileHandling();
  61. $images->enableImageGallery();
  62. $images->enablePopover('Preview', '<img style="max-width:300px" src="download.php?file={file_name}">');
  63. //--------------------------------------------------
  64. $filter = new TCriteria;
  65. $filter->add(new TFilter('id', '<', '0'));
  66. $cidade_id = new TDBCombo('cidade_id', 'dbaerp', 'Cidade', 'id', 'nome', 'nome', $filter);
  67. $grupo_id = new TDBUniqueSearch('grupo_id', 'dbaerp', 'Grupo', 'id', 'nome');
  68. $papeis_id = new TDBMultiSearch('papeis_id', 'dbaerp', 'Papel', 'id', 'nome');
  69. $estado_id = new TDBCombo('estado_id', 'dbaerp', 'Estado', 'id', '{nome} ({uf})');
  70. $estado_id->setChangeAction( new TAction( [$this, 'onChangeEstado'] ) );
  71. $cep->setExitAction( new TAction([ $this, 'onExitCEP']) );
  72. $codigo_nacional->setExitAction( new TAction( [$this, 'onExitCNPJ'] ) );
  73. $cidade_id->enableSearch();
  74. $estado_id->enableSearch();
  75. $grupo_id->setMinLength(0);
  76. $papeis_id->setMinLength(0);
  77. $papeis_id->setSize('100%', 60);
  78. $observacao->setSize('100%', 60);
  79. $tipo->addItems( ['F' => 'Física', 'J' => 'Jurídica' ] );
  80. // add the fields
  81. $this->form->addFields( [ new TLabel('Id') ], [ $id ] );
  82. $this->form->addFields( [ new TLabel('Tipo') ], [ $tipo ], [ new TLabel('CPF/CNPJ') ], [ $codigo_nacional ] );
  83. $this->form->addFields( [ new TLabel('Nome') ], [ $nome ] );
  84. $this->form->addFields( [ new TLabel('Nome Fantasia') ], [ $nome_fantasia ] );
  85. $this->form->addFields( [ new TLabel('Classe')], [ $papeis_id ], [ new TLabel('Grupo') ], [ $grupo_id ] );
  86. $this->form->addFields( [ new TLabel('I.E.') ], [ $codigo_estadual ], [ new TLabel('I.M.') ], [ $codigo_municipal ] );
  87. $this->form->addFields( [ new TLabel('Fone') ], [ $fone ], [ new TLabel('Email') ], [ $email ] );
  88. $this->form->addFields( [ new TLabel('Observacao') ], [ $observacao ] );
  89. $this->form->addContent( [new TFormSeparator('Endereço')]);
  90. $this->form->addFields( [ new TLabel('Cep') ], [ $cep ] )->layout = ['col-sm-2 control-label', 'col-sm-4'];
  91. $this->form->addFields( [ new TLabel('Logradouro') ], [ $logradouro ], [ new TLabel('Numero') ], [ $numero ] );
  92. $this->form->addFields( [ new TLabel('Complemento') ], [ $complemento ],[ new TLabel('IBGE') ], [ $ibge ] ,[ new TLabel('Bairro') ], [ $bairro ] );
  93. $this->form->addFields( [ new TLabel('Estado') ], [$estado_id], [ new TLabel('Cidade') ], [ $cidade_id ] );
  94. // set sizes
  95. $id->setSize('100%');
  96. $nome->setSize('100%');
  97. $nome_fantasia->setSize('100%');
  98. $tipo->setSize('100%');
  99. $codigo_nacional->setSize('100%');
  100. $codigo_estadual->setSize('100%');
  101. $codigo_municipal->setSize('100%');
  102. $fone->setSize('100%');
  103. $email->setSize('100%');
  104. $observacao->setSize('100%');
  105. $cep->setSize('100%');
  106. $logradouro->setSize('100%');
  107. $numero->setSize('100%');
  108. $complemento->setSize('100%');
  109. $bairro->setSize('100%');
  110. $cidade_id->setSize('100%');
  111. $grupo_id->setSize('100%');
  112. $cep->setMask('99.999-999');
  113. $id->setEditable(FALSE);
  114. $nome->addValidation('Nome', new TRequiredValidator);
  115. $nome_fantasia->addValidation('Nome Fantasia', new TRequiredValidator);
  116. $tipo->addValidation('Tipo', new TRequiredValidator);
  117. $codigo_nacional->addValidation('CPF/CNPJ', new TRequiredValidator);
  118. $grupo_id->addValidation('Grupo', new TRequiredValidator);
  119. $fone->addValidation('Fone', new TRequiredValidator);
  120. $email->addValidation('Email', new TRequiredValidator);
  121. $email->addValidation('Email', new TEmailValidator);
  122. $cidade_id->addValidation('Cidade', new TRequiredValidator);
  123. $cep->addValidation('CEP', new TRequiredValidator);
  124. $logradouro->addValidation('Logradouro', new TRequiredValidator);
  125. $numero->addValidation('Número', new TRequiredValidator);
  126. // create the form actions
  127. // $this->form->addHeaderActionLink(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red');
  128. $this->form->addActionLink(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red');
  129. // $btn = $this->form->addHeaderActionLink(_t('Save'), new TAction([$this, 'onSave']), 'fa:save');
  130. $btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:save');
  131. $btn->class = 'btn btn-sm btn-primary';
  132. $this->form->addHeaderActionLink( _t('Close'), new TAction([__CLASS__, 'onClose'], ['static'=>'1']), 'fa:times red');
  133. $this->form->appendPage('Outras Informações');
  134. $this->form->addFields( [new TLabel('Foto')], [$photo_path] );
  135. $this->form->addFields( [new TLabel('Images')],[$images] );
  136. // vertical box container
  137. $container = new TVBox;
  138. $container->style = 'width: 100%';
  139. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  140. $container->add($this->form);
  141. parent::add($container);
  142. }
  143. /**
  144. * Save form data
  145. * @param $param Request
  146. */
  147. public function onSave( $param )
  148. {
  149. try
  150. {
  151. $data = $this->form->getData(); // get form data as array
  152. TTransaction::open('dbaerp'); // open a transaction
  153. // $this->form->setData($data); // fill form data
  154. $this->form->validate(); // validate form data
  155. // $data = $this->form->getData(); // get form data as array
  156. $object = new Pessoa; // create an empty object
  157. $object->fromArray( (array) $data); // load the object with data
  158. $object->store(); // save the object
  159. // copy file to target folder
  160. $this->saveFile($object, $data, 'photo_path', 'files/imgpessoas');
  161. $this->saveFiles($object, $data, 'images', 'files/imgpessoas', 'PessoaImage', 'image', 'pessoa_id');
  162. PessoaPapel::where('pessoa_id', '=', $object->id)->delete();
  163. if ($data->papeis_id)
  164. {
  165. foreach ($data->papeis_id as $papel_id)
  166. {
  167. $pp = new PessoaPapel;
  168. $pp->pessoa_id = $object->id;
  169. $pp->papel_id = $papel_id;
  170. $pp->store();
  171. }
  172. }
  173. // get the generated id
  174. $data->id = $object->id;
  175. $this->form->setData($data); // fill form data
  176. TTransaction::close(); // close the transaction
  177. new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
  178. }
  179. catch (Exception $e) // in case of exception
  180. {
  181. new TMessage('error', $e->getMessage()); // shows the exception error message
  182. $this->form->setData( $this->form->getData() ); // keep form data
  183. TTransaction::rollback(); // undo all pending operations
  184. }
  185. }
  186. /**
  187. * Clear form data
  188. * @param $param Request
  189. */
  190. public function onClear( $param )
  191. {
  192. $this->form->clear(TRUE);
  193. }
  194. /**
  195. * Load object to form data
  196. * @param $param Request
  197. */
  198. public function onEdit( $param )
  199. {
  200. try
  201. {
  202. if (isset($param['key']))
  203. {
  204. $key = $param['key'];
  205. TTransaction::open('dbaerp');
  206. $object = new Pessoa($key);
  207. $object->papeis_id = PessoaPapel::where('pessoa_id', '=', $object->id)->getIndexedArray('papel_id');
  208. //--------------------------------------------------
  209. //$object->images = PessoaImage::where('pessoa_id', '=', $param['key'])->getIndexedArray('image');
  210. $object->images = PessoaImage::where('pessoa_id', '=', $object->id)->getIndexedArray('pessoa_id');
  211. //--------------------------------------------------
  212. // $this->form->setData($object);
  213. // force fire events
  214. $data = new stdClass;
  215. $data->estado_id = $object->cidade->estado->id;
  216. $data->cidade_id = $object->cidade_id;
  217. TForm::sendData('form_Pessoa', $data);
  218. $this->form->setData($object);
  219. TTransaction::close();
  220. }
  221. else
  222. {
  223. $this->form->clear(TRUE);
  224. }
  225. }
  226. catch (Exception $e) // in case of exception
  227. {
  228. new TMessage('error', $e->getMessage()); // shows the exception error message
  229. TTransaction::rollback(); // undo all pending operations
  230. }
  231. }
  232. }
  233. ?>



 
  1. <?php
  2. /**
  3. * Product Active Record
  4. * @author Pablo Dall'Oglio
  5. */
  6. class PessoaImage extends TRecord
  7. {
  8. const TABLENAME = 'pessoa_image';
  9. const PRIMARYKEY= 'id';
  10. const IDPOLICY = 'max'; // {max, serial}
  11. /**
  12. * Constructor method
  13. */
  14. public function __construct($id = NULL)
  15. {
  16. parent::__construct($id);
  17. parent::addAttribute('pessoa_id');
  18. parent::addAttribute('image');
  19. }
  20. }
  21. ?>





Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (4)


RB

Adriano, era para funcionar

Tenta deixar alinha 277
 
  1. <?php
  2. $object->images = PessoaImage::where('pessoa_id', '=', $param['key'])->getIndexedArray('image');
  3. ?>
AR

Obrigado por me responder, Rubens!

Assim a imagem aparece perfeitamente, só que na gravação recebo esse erro:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "files/imgpessoas/1/ft3.jpg"

Saberia me dizer como resolvo ?
RB

Adriano,

Prece que o tipo de dado que esta gravando esta diferente do definido na tabela ( Parece que o campo image esta definido como integer).

Verifica se o campo image da tabela pessoa_image esta como integer e mude para text ou varchar.
AR

Já tinha visto, está como text. Eu uso PostgreSQL, será que tem alguma a ver ?