Jump to content

Recommended Posts

Bonjour,

 

j'ai développé mon propre module mais je bloque:

J'ai un formulaire qui m'affiche plusieurs champs de type input pour enregistrer des images. 

Malheureusement je n'arrive à uploader que la première image à chaque fois. Pas moyen de toute les uploads.

Ci joint mon controller... Quelqu'un aurait-il une idée 

<?php
if(!class_exists('ModelTendance')){
    require(_PS_MODULE_DIR_.'tendance/classes/ModelTendance.php');
}

class AdminTendanceController extends ModuleAdminController{
    public function __construct(){
        $this->table = "tendance";
        $this->className = "ModelTendance";
        $this->lang = false;
        $this->bootstrap = true;
        $this->fieldImageSettings = [
            [
                'name' => 'little_image_one',
                'dir' => 'tendance'
            ],
            [
                'name' => 'little_image_two',
                'dir' => 'tendance'
            ],
            [
                'name' => 'big_image',
                'dir' => 'tendance'
            ],
            [
                'name' => 'bandeau_image_one',
                'dir' => 'tendance'
            ],
            [
                'name' => 'bandeau_image_two',
                'dir' => 'tendance'
            ],
            [
                'name' => 'bandeau_image_three',
                'dir' => 'tendance'
            ],
            [
                'name' => 'bandeau_image_four',
                'dir' => 'tendance'
            ],
        ];
        $this->fields_list = array(
            'id_tendance' => array(
                'title' => $this->l('Titre'),
                'width' => 'auto'
            ),
            'left' => array(
                'title' => $this->l('Droite'),
                'width' => 'auto',
                'type' => 'bool'
            ),
            'active' => array(
                'title' => $this->l('Actif'),
                'width' => 'auto',
                'type' => 'bool'
            ),
        );

        //Ajout d'actions sur chaque ligne
        $this->addRowAction('edit');
        $this->addRowAction('delete');
        parent::__construct();
    }
    /**
    * Surcharge de la fonction de traduction sur PS 1.7 et supérieur.
    * La fonction globale ne fonctionne pas
    * @param type $string
    * @param type $class
    * @param type $addslashes
    * @param type $htmlentities
    * @return type
    */
    public function initPageHeaderToolbar()
    {
        
        //Bouton d'ajout
        $this->page_header_toolbar_btn['new'] = array(
            'href' => self::$currentIndex . '&add' . $this->table . '&token=' . $this->token,
            'desc' => $this->module->l('Ajouter un block'),
            'icon' => 'process-icon-new'
        );
        
        parent::initPageHeaderToolbar();
    }
    protected function l($string, $class = null, $addslashes = false, $htmlentities = true)
    {
        if ( _PS_VERSION_ >= '1.7') {
            return Context::getContext()->getTranslator()->trans($string);
        } else {
            return parent::l($string, $class, $addslashes, $htmlentities);
        }
    }
    
    public function renderForm(){
        if(!($tendance = $this->loadObject(true))){
            return;
        }

        $image = _PS_IMG_DIR_.'tendance/'.$tendance->id.'.jpg';
        $img_url = ImageManager::thumbnail($image, $this->table.'_'.(int)$tendance->id.'.'.$this->imageType, 350,
            $this->imageType, true, true);
        $image_size = file_exists($image) ? filesize($image) / 1000 : false;

        $little_image_one = _PS_IMG_DIR_.'tendance/'.$tendance->id.'-little-image-one.jpg';
        $little_image_one_url = ImageManager::thumbnail($little_image_one, $this->table.'_'.(int)$tendance->id.'-little-image-one.jpg.'.$this->imageType, 350,
            $this->imageType, true, true);
        $little_image_one_size = file_exists($little_image_one) ? filesize($little_image_one) / 1000 : false;

        //Définition du formulaire d'édition
        $this->fields_form = [
            'tinymce' => true,
            //Entête
            'legend' => [
                'title' => $this->module->l('Edit Sample'),
                'icon' => 'icon-cog'
            ],
            //Champs
            'input' => [
                [
                    'type' => 'text', //Type de champ
                    'label' => $this->module->l('Titre'), //Label
                    'name' => 'title', //Nom
                    'required' => true, //Requis ou non
                    'hint' => $this->module->l('Titre à afficher') //Indication complémentaires de saisie
                ],
                [
                    'type' => 'textarea',
                    // 'autoload_rte' => true, //Flag pour éditeur Wysiwyg
                    'label' => $this->module->l('Description'),
                    'name' => 'description',
                    'required' => true,
                    'empty_message' => $this->module->l('Description'),
                ],
                [
                    'type' => 'file',
                    'label' => $this->l('Petite image une'),
                    'name' => 'little_image_one',
                    'image' => $img_url ? $img_url :false,
                    'size' => $image_size,
                    'display_image' => true,
                    'col' => 6,
                    'hint' => $this->l('Première image du bandeau')
                ],
                [
                    'type' => 'file',
                    'label' => $this->l('Petite image deux'),
                    'name' => 'little_image_two',
                    'image' => $little_image_one_url ? $little_image_one_url :false,
                    'size' => $little_image_one_size,
                    'display_image' => true,
                    'col' => 6,
                    'hint' => $this->l('Deuxième image du bandeau')
                ],
                [
                    'type' => 'file',
                    'label' => $this->l('Petite image trois'),
                    'name' => 'big_image',
                    // 'image' => $img_url ? $img_url :false,
                    // 'size' => $image_size,
                    'display_image' => true,
                    'col' => 6,
                    'hint' => $this->l('Grosse image')
                ],
                [
                    'type' => 'file',
                    'label' => $this->l('Première image du bandeau du bas'),
                    'name' => 'bandeau_image_one',
                    // 'image' => $img_url ? $img_url :false,
                    // 'size' => $image_size,
                    'display_image' => true,
                    'col' => 6,
                    'hint' => $this->l('Première image du bandeau du bas')
                ],
                [
                    'type' => 'file',
                    'label' => $this->l('Deuxième image du bandeau du bas'),
                    'name' => 'bandeau_image_two',
                    // 'image' => $img_url ? $img_url :false,
                    // 'size' => $image_size,
                    'display_image' => true,
                    'col' => 6,
                    'hint' => $this->l('Deuxième image du bandeau du bas')
                ],
                [
                    'type' => 'file',
                    'label' => $this->l('Troisième image du bandeau du bas'),
                    'name' => 'bandeau_image_three',
                    // 'image' => $img_url ? $img_url :false,
                    // 'size' => $image_size,
                    'display_image' => true,
                    'col' => 6,
                    'hint' => $this->l('Troisième image du bandeau du bas')
                ],
                [
                    'type' => 'file',
                    'label' => $this->l('Quatrième image du bandeau du bas'),
                    'name' => 'bandeau_image_four',
                    // 'image' => $img_url ? $img_url :false,
                    // 'size' => $image_size,
                    'display_image' => true,
                    'col' => 6,
                    'hint' => $this->l('Quatrième image du bandeau du bas')
                ],
                [
                    'type' => 'switch',
                    'label' => $this->module->l('Droite'),
                    'name' => 'left',
                    'class' => 'input fixed-width-sm',
                    'required' => true,
                    'empty_message' => $this->module->l('Droite ou gauche ?'),
                    'values' => [
                        [
                            'id' => 'active_on',
                            'value' => 1,
                            'label' => $this->l('Actif')
                        ],
                        [
                            'id' => 'active_off',
                            'value' => 0,
                            'label' => $this->l('Désactivé')
                        ]
                    ]
                ],
                [
                    'type' => 'switch',
                    'label' => $this->module->l('Actif'),
                    'name' => 'active',
                    'class' => 'input fixed-width-sm',
                    'required' => true,
                    'empty_message' => $this->module->l('Droite ou gauche ?'),
                    'values' => [
                        [
                            'id' => 'active_on',
                            'value' => 1,
                            'label' => $this->l('Actif')
                        ],
                        [
                            'id' => 'active_off',
                            'value' => 0,
                            'label' => $this->l('Désactivé')
                        ]
                    ]
                ],
            ],
            //Boutton de soumission
        'submit' => [
            'title' => $this->l('Save'), //On garde volontairement la traduction de l'admin par défaut
            'name' => 'submit_form'
            ]
        ];
        
        if (Tools::isSubmit('submit_form')){	
            $target_dir =  _PS_IMG_DIR_.'tendance/';
            foreach($_FILES as $img){
                $target_file = $target_dir . basename($img["name"]);	
                $check = getimagesize($img["tmp_name"]);
                if($check !== false){
                    $uploadOk = 1;
                }else{
                    $uploadOk = 0;
                }
                if(file_exists($target_file)) {
					$uploadOk = 0;
                }
                if (move_uploaded_file($img["tmp_name"], $target_file)) {
                    $file_location = basename($img["name"]);
                } 
            }
        }
        return parent::renderForm();
    }
   
}

 

Share this post


Link to post
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...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More