Jump to content

Editar Formulário no controller do modulo


Recommended Posts

Fala Pessoal, estou com um problema que não consigo resolver.
 
Estou criando um modulo para cadastrar parceiros em uma loja. O modulo cria um botão no menu da esquerda no admin do prestashop 1.6, QUASE tudo está funcionando, atualmente ele instala e desisnstala o menu a esquerda, cadastra e exclui um novo parceiro, já apresenta uma tela com os parceiros cadastrados no front end, até aqui tudo perfeito.
 
O problema maior é que eu NÃO CONSIGO EDITAR O PARCEIRO EXISTENTE DE JEITO NENHUM (rsrs), quando eu clico em editar parceiro na lista de parceiros existente ele simplesmente abre novamente o formulário em branco e quando eu clico em salvar ao invés de editar o parceiro, ele simplesmente cria outro.
 
Por favor, se alguém puder me ajudar eu ficarei muito agradecido, preciso realmente dessa força.

 

Segue meu codigo do /modules/parceiros/controllers/admin/adminparceiroscontroller.php

 

 

<?php
class AdminParceirosController extends ModuleAdminController
{
    public function __construct()
    {
        $this->tpl_folder = 'parceiros';
        $this->table = 'parceiros';
        $this->lang = false;
        //$this->className = 'AdminParceiros';
        //$this->lang = false;
        //$this->image_dir = '../modules/parceiros/img/parceiros';
        $this->context = Context::getContext();
        $this->_defaultOrderBy = 'id_parceiros';
        $this->_defaultorderWay = 'DESC';
        $this->bootstrap = true;


        


        parent::__construct();
    }

    
public function renderList() {
        $this->addRowAction('edit');
        $this->addRowAction('delete');

        $this->fields_list = array(

            'id_parceiros' => array('title' => $this->l('Id'),'width' => 50,'type' => 'text','orderby' => true,'filter' => true,'search' => true),
                
            'nome' => array('title' => $this->l('Nome'),'width' => 440,'type' => 'text','lang' => false,'orderby' => true,'filter' => false,'search' => false),
                             
            'site' => array('title' => $this->l('Site'),'width' => 60,'type' => 'text','lang' => false,'orderby' => true,'filter' => true,'search' => true),
                
        );
        return parent::renderList();
    }

    
    
public function renderForm()
     {

      $img_desc = '';
        $img_desc .= $this->l('Upload a Feature Image from your computer.<br/>N.B : Only jpg image is allowed');
        if(Tools::getvalue('id_parceiros') != '' && Tools::getvalue('id_parceiros') != NULL){
             $img_desc .= '<br/><img style="height:auto;width:300px;clear:both;border:1px solid black;" alt="" src="'.__PS_BASE_URI__.'modules/parceiros/parceiros/'.Tools::getvalue('id_parceiros').'.jpg" /><br />';
        }
        $this->fields_form = array(
          'legend' => array(
          'title' => $this->l('Parceiros'),
            ),
            'input' => array(
                array(
                    'type' => 'hidden',
                    'name' => 'post_type',
                    'default_value' => 0,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Nome'),
                    'name' => 'nome',
                    'size' => 60,
                    'required' => true,
                    'desc' => $this->l('Enter partner name here'),
                    'lang' => false,
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Description'),
                    'name' => 'descricao',
                    'lang' => false,
                    'rows' => 5,
                    'cols' => 62,
                    'class' => 'rte',
                    'autoload_rte' => true,
                    'required' => false,
                    'desc' => $this->l('Enter Partner Description')
                ),
                array(
                    'type' => 'file',
                    'label' => $this->l('Feature Image:'),
                    'name' => 'image',
                    'display_image' => false,
                    'desc' => $img_desc
                ),
                
                array(
                    'type' => 'text',
                    'label' => $this->l('address'),
                    'name' => 'endereco',
                    'lang' => false,
                    'size' => 120,
                    'required' => false,
                    'desc' => $this->l('Enter Partner address here. ')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('neighborhood'),
                    'name' => 'bairro',
                    'lang' => false,
                    'size' => 40,
                    'required' => false,
                    'desc' => $this->l('Enter Partner address here. ')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('postcode'),
                    'name' => 'cep',
                    'lang' => false,
                    'size' => 8,
                    'required' => false,
                    'desc' => $this->l('Enter Partner address here. ')
                ),
                
                array(
                    'type' => 'text',
                    'label' => $this->l('Phone'),
                    'name' => 'telefone',
                    'size' => 60,
                    'lang' => false,
                    'required' => false,
                    'desc' => $this->l('Enter phone here')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('celphone'),
                    'name' => 'celular',
                    'size' => 60,
                    'lang' => false,
                    'required' => false,
                    'desc' => $this->l('Enter celphone here')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('site'),
                    'name' => 'site',
                    'size' => 100,
                    'lang' => false,
                    'required' => false,
                    'desc' => $this->l('Enter site here')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('email'),
                    'name' => 'email',
                    'size' => 100,
                    'lang' => false,
                    'required' => false,
                    'desc' => $this->l('Enter email here')
                )
                
                
            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'class' => 'button'
            )
        );


      
      
        return parent::renderForm();
    }

    

    public function postProcess()
    {
        if (Tools::isSubmit('deleteparceiros') && Tools::getValue('id_parceiros') != '')
        {
            $id_parceiros = Tools::getValue('id_parceiros');
           // $deletar = 'DELETE * FROM `' . _DB_PREFIX_ . 'parceiros` WHERE id_parceiros = '.$id_parceiros;
            $deletar = Db::getInstance()->delete($this->table, "id_parceiros = {$id_parceiros}");
            
            if (!$deletar){
                $this->errors[] = Tools::displayError('An error occurred while deleting the object.')
                        . ' <b>' . $this->table . ' (' . Db::getInstance()->getMsgError() . ')</b>';
            }else{
                Tools::redirectAdmin($this->context->link->getAdminLink('AdminParceiros'));
            }
        }elseif (Tools::isSubmit('submitAddparceiros')){

            //parent::validateRules();
            if (count($this->errors))
                return false;

            if (!$id_parceiros = Tools::getValue('id_parceiros')) { // PARA CRIAR


                $dados = array(
                    'nome' => Tools::getValue('nome'),
                    'endereco'      => Tools::getValue('endereco'),
                    'descricao'      => Tools::getValue('descricao'),
                    'telefone'      => Tools::getValue('telefone'),
                    'celular'      => Tools::getValue('celular'),
                    'site'      => Tools::getValue('site'),
                    'email'      => Tools::getValue('email'),
                    'position'      => 0,
                    'active'      => Tools::getValue('active')

                    );

                
                $inserir = Db::getInstance()->insert($this->table, $dados);

                if (!$inserir){
                    $this->errors[] = Tools::displayError('An error occurred while deleting the object.')
                            . ' <b>' . $this->table . ' (' . Db::getInstance()->getMsgError() . ')</b>';
                }else{
                    $this->processImageDestaque($_FILES,$this->getLastID());
                   Tools::redirectAdmin($this->context->link->getAdminLink('AdminParceiros'));
                }

            }elseif($id_parceiros = Tools::getValue('id_parceiros')) {
$id_parceiros = Tools::getValue('id_parceiros');
                $dados = array(
                    'nome' => Tools::getValue('nome'),
                    'endereco'      => Tools::getValue('endereco'),
                    'descricao'      => Tools::getValue('descricao'),
                    'telefone'      => Tools::getValue('telefone'),
                    'celular'      => Tools::getValue('celular'),
                    'site'      => Tools::getValue('site'),
                    'email'      => Tools::getValue('email'),
                    'position'      => 0,
                    'active'      => Tools::getValue('active')

                    );

                
                $inserir = Db::getInstance()->update($this->table, $dados, "id_parceiros = {$id_parceiros}");

                if (!$inserir){
                    $this->errors[] = Tools::displayError('An error occurred while deleting the object.')
                            . ' <b>' . $this->table . ' (' . Db::getInstance()->getMsgError() . ')</b>';
                }else{
                    $this->processImageDestaque($_FILES,$this->getLastID());
                   Tools::redirectAdmin($this->context->link->getAdminLink('AdminParceiros'));
                }
                        
            }
            

                        
        }
    }





    public static function GetImageAllType($type)
    {
        $img_type = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
            SELECT * FROM `'._DB_PREFIX_.'parceiros_imagetype` where `active` = 1 and `type` = "'.$type.'"');
        return $img_type;
    }

    public function processImageDestaque($FILES,$id){
 
            if (isset($FILES['image']) && isset($FILES['image']['tmp_name']) && !empty($FILES['image']['tmp_name'])) {
                if ($error = ImageManager::validateUpload($FILES['image'], 4000000))
                    return $this->displayError($this->l('Invalid image'));
                else {
                    $ext = substr($FILES['image']['name'], strrpos($FILES['image']['name'], '.') + 1);
                    $file_name = $id . '.' . $ext;
                    $path = _PS_MODULE_DIR_ .'parceiros/img/parceiros/' . $file_name;
                    if (!move_uploaded_file($FILES['image']['tmp_name'], $path))
                        return $this->displayError($this->l('An error occurred while attempting to upload the file.'));
                    else {
                        if (Configuration::hasContext('image', null, Shop::getContext()) && Configuration::get('BLOCKBANNER_IMG') != $file_name)
                            @unlink(dirname(__FILE__) . '/' . Configuration::get('BLOCKBANNER_IMG'));
                        
                        $images_types = $this->GetImageAllType('destaque');
                        foreach ($images_types as  $image_type)
            {
                            $dir = _PS_MODULE_DIR_ .'parceiros/img/parceiros/'.$id.'-'.stripslashes($image_type['type_name']).'.jpg';
                                        if (file_exists($dir))  
                        unlink($dir);
            }
            foreach ($images_types as $image_type)
            {
                                ImageManager::resize($path,_PS_MODULE_DIR_ .'parceiros/img/parceiros/'.$id.'-'.stripslashes($image_type['type_name']).'.jpg',
                                        (int)$image_type['width'], (int)$image_type['height']
                                        );
            }
                    }
                }
            }
    }

    public function getLastID()
    {
        $sql = 'SELECT id_parceiros FROM '._DB_PREFIX_.'parceiros';
                
               if (!$result = Db::getInstance()->executeS($sql))
                               return false;

               return Db::getInstance()->Insert_ID();
    }

    

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...