Tomasz Gałkowski
Tomasz Gałkowski

Reputation: 1929

File upload in module configuration panel, Prestashop

I'm trying to create a XML import module that will convert given file to CSV format and then use that CSV to import categories and products.

I have a working configuration page made with getContent() it basically calls a method that generates this form via $helper->generateForm(). $helper is a HelperForm() object.

protected function getConfigForm()
{
    return array(
        'form' => array(
            'legend' => array(
            'title' => $this->l('Settings'),
            'icon' => 'icon-cogs',
            ),
            'input' => array(
                array(
                    'type' => 'file',
                    'label' => $this->l('XML file'),
                    'name' => 'XMLIMPORT_XML_FILE',
                    'desc' => $this->l('Select file you wish to import.'),
                    'required' => true
                ),
                array(
                    'col' => 3,
                    'type' => 'text',
                    'prefix' => '<i class="icon icon-envelope"></i>',
                    'desc' => $this->l('Enter a valid email address'),
                    'name' => 'XMLIMPORT_LINES',
                    'label' => $this->l('Records per file'),
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
            ),
        ),
    );
}

I need to get this data to my XML converter. How do I upload a file (around 10-20MB) to Prestashop to be then able to do other stuff with it? How to save it permanently on the server?

I tried doing this:

return array(
    'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null),
    'XMLIMPORT_LINES' => Configuration::get('XMLIMPORT_LINES', 1000)
);

And after that this:

$form_values = $this->getConfigFormValues(); // returned array from above

foreach (array_keys($form_values) as $key)
    Configuration::updateValue($key, Tools::getValue($key));

And later using my own class for XML conversion like this, hoping that it will give me file handle.

$xml_converter = new XMLToCSVConverter(Configuration::get('XMLIMPORT_XML_FILE'), 'output', 'example_products.php');

Apparently it didn't as nothing happens. The class itself is working fine outside of Prestashop module. The constructor is __construct($xml_file, $csv_filename, $template_file).

I need to pass the file I upload to that constructor. I've been struggling for days now.

@edit: I can see the contents of the file inside the HTTP call when submit is clicked. But how do I pass that file to my class?

Upvotes: 0

Views: 3732

Answers (2)

Gofenice Technologies
Gofenice Technologies

Reputation: 171

prestashop handles the image upload with ImageManager class, this class contains more methods which are useful for handling image upload, resize etc.. so its better refer the default homeslider module for the image upload using a module. This module is handling the image upload process in postProcess method with the help of ImageManager class, this class methods will do the all the processes related to upload.

Upvotes: 0

gskema
gskema

Reputation: 3211

As far as I remember 'type' => 'file', doesn't actually save any values in the database. This type is only meant to output a file field in your form.

After submitting, you should then do you custom processing with $_FILES['XMLIMPORT_XML_FILE'] : move to upload/ or whereever you want.

'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null), won't return you anything. After uploading you my wish to save the uploaded file path here, but it won't show up next time in the form unless you build the output yourself.

Module configratuon is meant to save text config values. Handling files is trickier and you have to do them yourself each time.

EDIT:

To intercept the saving process, look up the submit button name and make an if statement:

public function getContent() {
    if(Tools::isSubmit('submitButtonName')) {
      error_log(print_r($_FILES, 1));
    }
}

There's probably a function postProcess which does the same (it looks like you copied the methods from a default module).

Upvotes: 1

Related Questions