Advertisement
lordjackson

ServidorFormFoto

May 21st, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.25 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * classe ServidorFormFoto
  5.  * Cadastro de Servidor: Contem o formularo da Foto
  6.  */
  7. ini_set('display_errors', 1);
  8. ini_set('display_startup_erros', 1);
  9. error_reporting(E_ALL);
  10.  
  11. class ServidorFormFoto extends TPage {
  12.  
  13.     private $form;     // formulario de cadastro
  14.  
  15.     /*
  16.      * metodo construtor
  17.      * Cria a pagina do formulario
  18.      */
  19.  
  20.     public function __construct() {
  21.         parent::__construct();
  22.  
  23.         // instancia um formulario
  24.         // creates the form
  25.         $this->form = new TForm('form_Servidor');
  26.         //$this->form = new TQuickForm;
  27.         //   $this->form->setEnctype("multipart/form-data");
  28.         $this->form->class = 'form_Servidor_foto';
  29.         $table = new TTable;
  30.         $table->width = '100%';
  31.         $table_buttons = new TTable;
  32.  
  33.         // add the table inside the form
  34.         $this->form->add($table);
  35.  
  36.         // creates a label with the title
  37.         $titulo = new TLabel('Formulario Servidor');
  38.         $titulo->setFontSize(18);
  39.         $titulo->setFontFace('Arial');
  40.         $titulo->setFontColor('red');
  41.  
  42.         // cria os campos do formulario
  43.         $id = new THidden('servidor_id');
  44.         $id->setValue(filter_input(INPUT_GET, 'fk'));
  45.         $foto = new TFile('foto');
  46.         $foto->setProperty("accept", "image/jpg");
  47.  
  48.         $usuarioalteracao = new THidden('usuarioalteracao');
  49.         $dataalteracao = new THidden('dataalteracao');
  50.  
  51.         //cria a colecao da tabela estrangeira do Cabecalho
  52.         // inicia transacao com o banco 'pg_ceres'
  53.         TTransaction::open('pg_ceres');
  54.         // instancia objeto da Classe Record
  55.         $cadastro = new ServidorRecord(filter_input(INPUT_GET, 'fk'));
  56.         // cria um criterio de selecao, ordenado pelo id do servidor
  57.         //adiciona os objetos na tela com as informacoes do servidor
  58.         if ($cadastro) {
  59.             $nome = new TLabel($cadastro->nome);
  60.             $matricula = new TLabel($cadastro->matricula);
  61.         }
  62.         // finaliza a transacao
  63.         TTransaction::close();
  64.  
  65.         // define os tamanhos dos campos
  66.         $foto->setSize(400);
  67.  
  68.         // add the form fields
  69.         ################ Pagina 01 - Dados Pessoais ################
  70.        $table->addRowSet('', $id);
  71.         $table->addRowSet('', $usuarioalteracao);
  72.         $table->addRowSet('', $dataalteracao);
  73.  
  74.         // adiciona campos
  75.         $table->addRowSet('', $titulo);
  76.         $table->addRowSet(new TLabel('Matricula:'), $matricula);
  77.         $table->addRowSet("<b>Nome <font color=red>*</font></b>", $nome);
  78.         $table->addRowSet(new TLabel('Foto'), $foto);
  79.  
  80.         //mostra a foto do servidor se o metodo eh onEdit
  81.         if (filter_input(INPUT_GET, 'method') == 'onEdit') {
  82.             $foto2 = new TImage('app/images/servidor/servidor_' . filter_input(INPUT_GET, 'fk') . '.jpg', 'foto', 150, 150);
  83.             $table->addRowSet('', $foto2);
  84.         }
  85.  
  86.         //Define o auto-sugerir
  87.         $nome->setProperty('placeholder', 'Informe o Nome');
  88.  
  89.         // create an action button (save)
  90.         $save_button = new TButton('save');
  91.  
  92.         // create an action servidor (save)
  93.         $action = new TAction(array($this, 'onSave'));
  94.         $action->setParameter('fk', '' . filter_input(INPUT_GET, 'fk') . '');
  95.         $save_button->setAction($action, 'Save');
  96.  
  97.         $save_button->setImage('ico_save.png');
  98.  
  99.         // create an action button (new)
  100.         $new_button = new TButton('list');
  101.         $new_button->setAction(new TAction(array("ServidorList", 'onReload')), 'Voltar');
  102.         $new_button->setImage('ico_datagrid.png');
  103.  
  104.         // add a row for the form action
  105.         $row = $table_buttons->addRow();
  106.         $row->addCell($save_button);
  107.         $row->addCell($new_button);
  108.  
  109.         // add a row for the form action
  110.         $row = $table->addRow();
  111.         $row->class = 'tformaction';
  112.         $cell = $row->addCell($table_buttons);
  113.         $cell->colspan = 2;
  114.         // define wich are the form fields
  115.         $this->form->setFields(array($id, $foto, $save_button, $new_button));
  116.  
  117.         // wrap the page content using vertical box
  118.         $vbox = new TVBox;
  119.         $vbox->add($this->form);
  120.  
  121.         parent::add($vbox);
  122.     }
  123.  
  124.     /*
  125.      * metodo onSave()
  126.      * Executada quando o usuario clicar no botao salvar do formulario
  127.      */
  128.  
  129.     function onSave() {
  130.  
  131.         //   $arqtmp = $_FILES["foto"]["tmp_name"];
  132.         $servidor_id = $_REQUEST['servidor_id'];
  133.         $foto = $_REQUEST['foto'];
  134.         $msg = "";
  135.  
  136.         ############ Exemplo 01 ############
  137.        // have attachments
  138.         if ($foto) {
  139.             $nome_foto = 'servidor_' . $servidor_id;
  140.             $target_folder = 'app/images/servidor/';
  141.             $target_file = $target_folder . '/' . $foto;
  142.             @mkdir($target_folder);
  143.             rename('app/images/servidor/' . $nome_foto, $target_file);
  144.             $msg = 'OK';
  145.         }
  146.  
  147.         ############ Exemplo 02 ############
  148.        /* Defina aqui o tamanho maximo do arquivo em bytes: */
  149.         if ($_FILES['foto']['size'] > 1024000) {
  150.             $msg = "O Arquvio deve no maximo 1Mb";
  151.         } else {
  152.             /* Defina aqui o diretorio destino do upload */
  153.             if (!empty($_FILES['foto']['tmp_name']) and is_file($_FILES['foto']['tmp_name'])) {
  154.                 $caminho = 'app/images/servidor/servidor_' . $servidor_id . '.jpg';
  155.  
  156.                 /* Defina aqui o tipo de arquivo suportado */
  157.                 if (eregi(".jpg$", $_FILES['foto']['name'])) {
  158.                     copy($_FILES['foto']['tmp_name'], $caminho);
  159.                     $msg = 'OK';
  160.                 } else {
  161.                     $msg = 'Apenas aquivo do tipo JPG';
  162.                 }
  163.             } else {
  164.                 $msg = 'Caminho ou nome de arquivo Invalido!';
  165.             }
  166.         }
  167.  
  168.         // exibe um dialogo ao usuario
  169.         //$msg = 'Dados armazenados com sucesso';
  170.         if ($msg == 'OK') {
  171.             new TMessage("info", "Registro salvo com sucesso!");
  172.             TApplication::gotoPage('ServidorList', 'onReload'); // reload
  173.         } else {
  174.             new TMessage("error", "Erro ao salvar a foto!");
  175.         }
  176.     }
  177.  
  178.     /*
  179.      * metodo onEdit()
  180.      * Edita os dados de um registro
  181.      */
  182.  
  183.     function onEdit($param) {
  184.         try {
  185.             if (isset($param['key'])) {
  186.  
  187.                 // get the parameter $key
  188.                 $key = $param['key'];
  189.  
  190.                 TTransaction::open('pg_ceres');   // open a transaction with database 'samples'
  191.  
  192.                 $object = new ServidorRecord($key);        // instantiates object Servidor
  193.                 //$fotoimg  = new TImage('app.img/servidor/servidor_'.$key.'.jpg','foto',50,50);
  194.                 //$this->form->add($fotoimg);
  195.  
  196.                 $this->form->setData($object);   // fill the form with the active record data
  197.  
  198.                 TTransaction::close();           // close the transaction
  199.             } else {
  200.                 $this->form->clear();
  201.             }
  202.         } catch (Exception $e) { // in case of exception
  203.             // shows the exception error message
  204.             new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  205.             // undo all pending operations
  206.             TTransaction::rollback();
  207.         }
  208.     }
  209.  
  210. }
  211.  
  212. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement