RF
Apresentar um vídeo do Youtube no iframe com url gravada em DB
Boa tarde! Estou fazendo um sistema simples de EAD, a ideia basicamente e cadastrar o vídeo do youtube no formulário, e depois o outro formulário apresentar aquele vídeo, eu coloquei um botão assistir vídeo no formulário (a imagem está em anexo), e esse botão deve pegar o item gravado no campo e reproduzir no iframe. Eu não consegui fazer o botão executar essa função. Mas coloquei um valor fixo na url do iframe.
segue o código do formulário:
segue o código do formulário:
- <?php
- class sala_aula extends TPage
- {
- protected $form;
- private $formFields = [];
- private static $database = 'ead';
- private static $activeRecord = 'Aula';
- private static $primaryKey = 'id';
- private static $formName = 'form_sala_aula';
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- if(!empty($param['target_container']))
- {
- $this->adianti_target_container = $param['target_container'];
- }
- // creates the form
- $this->form = new BootstrapFormBuilder(self::$formName);
- // define the form title
- $this->form->setFormTitle("Aula");
- $curso = new TDBCombo('curso', 'ead', 'Curso', 'id', '{nome}','id asc' );
- $secao = new TDBCombo('secao', 'ead', 'Secao', 'id', '{nome}','id asc' );
- $nome = new TEntry('nome');
- $id = new THidden('id');
- $descricao = new TEntry('descricao');
- $button_assistir_ao_video = new TButton('button_assistir_ao_video');
- $url_video = new TEntry('url_video');
- $video = new TElement('iframe');
- $aulaconcluida = new TDBCheckGroup('aulaconcluida', 'ead', 'ItConcluida', 'id', '{descricao}','id asc' );
- $avaliacao_aula = new TDBCheckGroup('avaliacao_aula', 'ead', 'ItAvaliacao', 'id', '{descricao}','id asc' );
- $url_video->setExitAction(new TAction([$this,'onExibirVideo']));
- $curso->addValidation("Curso", new TRequiredValidator());
- $secao->addValidation("Seção", new TRequiredValidator());
- $button_assistir_ao_video->setAction(new TAction([$this, 'onVideo']), "Assistir ao Vídeo");
- $button_assistir_ao_video->addStyleClass('btn-warning');
- $button_assistir_ao_video->setImage('fas:video #478FCA');
- $nome->setMaxLength(100);
- $descricao->setMaxLength(100);
- $aulaconcluida->setLayout('horizontal');
- $avaliacao_aula->setLayout('horizontal');
- $nome->setEditable(false);
- $curso->setEditable(false);
- $secao->setEditable(false);
- $url_video->setEditable(false);
- $video->width = '100%';
- $video->height = '350px';
- $video->src = "https://www.youtube.com/embed/T2CvkoVirkQ";
- $id->setSize(200);
- $nome->setSize('100%');
- $curso->setSize('100%');
- $secao->setSize('100%');
- $descricao->setSize('100%');
- $url_video->setSize('100%');
- $aulaconcluida->setSize(80);
- $avaliacao_aula->setSize(80);
- $this->video = $video;
- $row1 = $this->form->addFields([$curso],[$secao],[$nome]);
- $row1->layout = [' col-sm-4',' col-sm-4',' col-sm-4'];
- $row2 = $this->form->addContent([new TFormSeparator("", '#333', '18', '#eee')]);
- $row3 = $this->form->addFields([new TLabel("Descrição:", null, '14px', null)],[$id,$descricao]);
- $row4 = $this->form->addContent([new TFormSeparator("", '#333', '18', '#eee')]);
- $row5 = $this->form->addFields([$button_assistir_ao_video],[$url_video]);
- $row5->layout = [' col-sm-2',' col-sm-10'];
- $row6 = $this->form->addContent([new TFormSeparator("", '#333', '18', '#eee')]);
- $row7 = $this->form->addFields([$video]);
- $row7->layout = [' col-sm-12'];
- $row8 = $this->form->addContent([new TFormSeparator("", '#333', '18', '#eee')]);
- $row9 = $this->form->addFields([new TLabel("Aula concluída?", null, '14px', null)],[$aulaconcluida],[new TLabel("Avalie:", null, '14px', null)],[$avaliacao_aula]);
- // create the form actions
- $btn_onsave = $this->form->addAction("Salvar", new TAction([$this, 'onSave']), 'fas:save #ffffff');
- $this->btn_onsave = $btn_onsave;
- $btn_onsave->addStyleClass('btn-primary');
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 100%';
- $container->class = 'form-container';
- if(empty($param['target_container']))
- {
- $container->add(TBreadCrumb::create(["Cadastro","Sala de Aula"]));
- }
- $container->add($this->form);
- parent::add($container);
- }
- public static function onExibirVideo($param = null)
- {
- try
- {
- $data = $this->form->getData();
- // Pega a informação do campo Status
- $url = $data->url_video;
- TScript::create("
- $(video).attr('src','{$url}');
- ");
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- public static function onVideo($param = null)
- {
- try
- {
- $url = 'https://www.youtube.com/embed/k_Kv7hq77hc';
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- public function onSave($param = null)
- {
- try
- {
- TTransaction::open(self::$database); // open a transaction
- $messageAction = null;
- $this->form->validate(); // validate form data
- $object = new Aula(); // create an empty object
- $data = $this->form->getData(); // get form data as array
- $object->fromArray( (array) $data); // load the object with data
- $object->store(); // save the object
- $repository = AulaConcluida::where('aula_id', '=', $object->id);
- $repository->delete();
- if ($data->aulaconcluida)
- {
- foreach ($data->aulaconcluida as $aulaconcluida_value)
- {
- $aula_concluida = new AulaConcluida;
- $aula_concluida->it_concluida_id = $aulaconcluida_value;
- $aula_concluida->aula_id = $object->id;
- $aula_concluida->store();
- }
- }
- $repository = AvaliacaoAula::where('aula_id', '=', $object->id);
- $repository->delete();
- if ($data->avaliacao_aula)
- {
- foreach ($data->avaliacao_aula as $avaliacao_aula_value)
- {
- $avaliacao_aula = new AvaliacaoAula;
- $avaliacao_aula->it_avaliacao_id = $avaliacao_aula_value;
- $avaliacao_aula->aula_id = $object->id;
- $avaliacao_aula->store();
- }
- }
- // get the generated {PRIMARY_KEY}
- $data->id = $object->id;
- $this->form->setData($data); // fill form data
- TTransaction::close(); // close the transaction
- new TMessage('info', "Registro salvo", $messageAction);
- }
- catch (Exception $e) // in case of exception
- {
- //</catchAutoCode>
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key']; // get the parameter $key
- TTransaction::open(self::$database); // open a transaction
- $object = new Aula($key); // instantiates the Active Record
- $object->aulaconcluida = AulaConcluida::where('aula_id', '=', $object->id)->getIndexedArray('it_concluida_id', 'it_concluida_id');
- $object->avaliacao_aula = AvaliacaoAula::where('aula_id', '=', $object->id)->getIndexedArray('it_avaliacao_id', 'it_avaliacao_id');
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear(true);
- }
- public function onShow($param = null)
- {
- }
- }
Você está quase lá, acho que faltou definir um id para o iframe e referenciar esse id:
Boa tarde!
Nataniel, você é bom mesmo cara! Quando crescer quero ser igual você, rssss
Funcionou depois coloco aqui o código inteiro, pra servir de referência depois para os demais, deixa eu arredondar o formulário um pouco mais.
Obrigado!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!