Jump to content

How to Edit module admin form?


Recommended Posts

Hi everyone,

I need help with editing form in ModuleAdminController.

When i try edit an existing value, the fields of form are blank and when i submit the save button a new row is created. =/

Somebody can help me please?

 

Follow my /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();
    }

    

}

 

 

Edited by mobster (see edit history)
Link to comment
Share on other sites

You need to put in this code in which ever action you want to display the form

 

public function newAction(){

$this->loadLayout();

$this->_addContent($this->getLayout()->createBlock('form/adminhtml_form_edit'))

                ->_addLeft($this->getLayout()->createBlock('form/adminhtml_form_edit_tabs'));

$this->renderLayout();

}

 


Link to comment
Share on other sites

×
×
  • Create New...