Jump to content

How to add an input file uploader in customer registration form v1.6


Recommended Posts

I need to add an input file uploader in customer registration (see the jp).

 

Indeed, my customers need to proove that they are pros to access the website content.

 

This field would be required to validate their account creation.

 

I've spend about 3 days (dealing with authentification.tpl, customer.php overriding...)  but still don't know how.

 

Please, help xx

post-910487-0-68859400-1423149106_thumb.png

Link to comment
Share on other sites

  • 2 weeks later...

The registration form is located in ./themes/YOUR-THEME/authentication.tpl, you could make the implementation as following, right after the Birthdate field, around line 130:

<div class="form-group">
    <label for="fileUpload">{l s='Select file'} <sup>*</sup></label>
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    <input type="file" class="required form-control" id="fileUpload" name="fileUpload" />
</div>

Beside of this, I think you already know how to add the field in the Customer Class and Controllers(AdminCustomersController.php, AuthController.php, Customer.php).

 

I've put this code right after the $customer->birthday validation at lines ~550 and ~420 to run the uploader in AuthController.php

if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name']))
{
	$extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.png', '.jpeg', '.gif', '.jpg');
	$filename = uniqid().basename($_FILES['fileUpload']['name']);
	$filename = str_replace(' ', '-', $filename);
	$filename = strtolower($filename);
	$filename = filter_var($filename, FILTER_SANITIZE_STRING);

	$_FILES['fileUpload']['name'] = $filename;

	$uploader = new UploaderCore();
	$uploader->upload($_FILES['fileUpload']);

	$customer->fileUpload = $filename;
}
Edited by UmbertoD (see edit history)
Link to comment
Share on other sites

Hi! 
At first , excuse me for my possibly bad english ! 

I have the same problem asGombi92 and test your solution UmbertoD but it doesn't work. The file uploader doesn't appear in my form.

 

I use the version 1.6 of Prestashop so maybe I need to do something else ? 

And i don't understand where the file is upload with your solution ?

Thanks for your answer.  

Link to comment
Share on other sites

Hi ! thanks for your answer. 

But I think the problem is here : 

 

UmbertoD said : 

Beside of this, I think you already know how to add the field in the Customer Class and Controllers(AdminCustomersController.php, AuthController.php, Customer.php).

 

And I don't modify anything in the Customer Class what should I add in this file?  

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

We basically need to make the new field act the same way as the existing ones.

 

First of all, we need to declare and create the field in the working side of the PrestaShop, naming it as fileUpload.

 

Let's run this statement on SQL to create the field in the customer table to store the filename.

ALTER TABLE `ps_customer` ADD `fileUpload` VARCHAR(100) NOT NULL;

In ./classes/Customer.php around line ~30 we'll find the declared variables. Add the new one like this:

class CustomerCore extends ObjectModel
{
	public $id;

	public $fileUpload;

	public $id_shop;

...

then in line ~192 add the new field like this:

'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
'fileUpload' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
),

Now, inside ./controllers/front/AuthController.php around line ~379 we prepare the information:

// Preparing customer
$customer = new Customer();
$lastnameAddress = Tools::getValue('lastname');
$firstnameAddress = Tools::getValue('firstname');
$_POST['lastname'] = Tools::getValue('customer_lastname', $lastnameAddress);
$_POST['firstname'] = Tools::getValue('customer_firstname', $firstnameAddress);

// Add this line
$fileUpload = Tools::getValue('fileUpload');

... 

around line ~423 we'll find the birthday validation, here we need to make the file validation. I made something simple but works fine:

$customer->firstname = Tools::ucwords($customer->firstname);
$customer->birthday = (empty($_POST['years']) ? '' : (int)Tools::getValue('years').'-'.(int)Tools::getValue('months').'-'.(int)Tools::getValue('days'));
if (!Validate::isBirthDate($customer->birthday))
    $this->errors[] = Tools::displayError('Invalid date of birth.');

// Add this code...
//$customer->fileUpload = 'N/A';
// If you want the uploader as OPTIONAL, remove the comment of the line above.
$file = $_FILES['fileUpload'];
$allowed = array('txt', 'rtf', 'doc', 'docx', 'pdf', 'png', 'jpeg', 'gif', 'jpg');
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
if ( file_exists($file['tmp_name']) && in_array($extension, $allowed) )
{
$filename = uniqid()."-".basename($file['name']);
$filename = str_replace(' ', '-', $filename);
$filename = strtolower($filename);
$filename = filter_var($filename, FILTER_SANITIZE_STRING);

$file['name'] = $filename;

$uploader = new UploaderCore();
$uploader->upload($file);

$customer->fileUpload = $filename;
}

 

The same way, in the line ~552, leaving it like this:

$customer->birthday = (empty($_POST['years']) ? '' : (int)Tools::getValue('years').'-'.(int)Tools::getValue('months').'-'.(int)Tools::getValue('days'));
if (!Validate::isBirthDate($customer->birthday))
		$this->errors[] = Tools::displayError('Invalid date of birth');

// Add this code...
//$customer->fileUpload = 'N/A';
// If you want the uploader as OPTIONAL, remove the comment of the line above.
$file = $_FILES['fileUpload'];
$allowed = array('txt', 'rtf', 'doc', 'docx', 'pdf', 'png', 'jpeg', 'gif', 'jpg');
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
if ( file_exists($file['tmp_name']) && in_array($extension, $allowed) )
{
$filename = uniqid()."-".basename($file['name']);
$filename = str_replace(' ', '-', $filename);
$filename = strtolower($filename);
$filename = filter_var($filename, FILTER_SANITIZE_STRING);

$file['name'] = $filename;

$uploader = new UploaderCore();
$uploader->upload($file);

$customer->fileUpload = $filename;
} 

This way the files will be uploaded in the ./upload/ folder, if you want to specify another subfolder just insert the path in $uploader->upload('my-path/'.$_FILES['fileUpload']);

 

To add the input fields in the registration form open ./themes/YOUR-THEME/authentication.tpl around ~182 and ~509 right after the birthday fields.

<div class="form-group">
    <label for="fileUpload">{l s='Select file'} <sup>*</sup></label>
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    <input type="file" class="required form-control" id="fileUpload" name="fileUpload" />
</div>

Don't forget to configure the form to send multimedia information on every <form> tags like the following code: (depends on theme)

<form method="post" id="create-account_form" ... enctype="multipart/form-data">

To show the filename in the client list open ./controllers/admin/AdminCustomersController.php around line ~159 and add:

'connect' => array(
	'title' => $this->l('Last visit'),
	'type' => 'datetime',
	'search' => false,
	'havingFilter' => true
),
'fileUpload' => array(
	'title' => $this->l('Uploaded File'),
	'type' => 'text',
	'search' => true,
	'havingFilter' => true
)
));

In the same file, around ~821:

// Connections
'connections' => $connections,
// Uploaded File
'fileUpload' => $fileUpload,
// Referrers
'referrers' => $referrers,
'show_toolbar' => true

Remember to clean the cache of Prestashop after uploading the modified files. I was based on the lastest PrestaShop 1.6.0.11.

 

If you want to show the files inside the Customer View you need to modify ./admin/themes/default/template/controllers/customers/helpers/view/view.tpl

 

Hope this can be helpful, I've tried to make it simple to implement.

 

Edit: I made some improval modifications in the codes.

Edited by UmbertoD (see edit history)
  • Like 1
Link to comment
Share on other sites

Thank you very much UmbertoD!!!! 

 

it's work for registration! 

 

I had an error but it was simply because in Customers.php and AuthController.php you don't write "uploadFile" and in the others "fileUpload". After changing this it was okay for the registration. 

 

But now when I want to see the detail of a customer in the back office I have an error about the line 

  1. // Uploaded File
    'fileUpload' => $fileUpload,

The error says : [8] Undefined variable: fileUpload

 

And I don't understand where the problem is...

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

Make sure the variable is correctly declared in Customer.php and AdminCustomersController.php, also check the field in the SQL table to be correctly defined as fileUpload.

 

Is the file currently uploading to the server? Is the filename in the fileUpload SQL field?

Link to comment
Share on other sites

At this point it should appear in the Customers list, not details.

 

To show the file inside the customer's details we should then modify ./admin-folder/themes/default/template/controllers/customers/helpers/view/view.tpl

 

Add a new panel div like this, wherever you want:

<div class="panel">
    <div class="panel-heading">
        <i class="icon-shopping-cart"></i> {l s='Uploaded File'}
    </div>
    {if $customer->fileUpload}
    <a href="../../../upload/{$customer->fileUpload}" target="_blank">Open file</a>
    {else}
    {l s='%1$s %2$s has not uploaded any files yet' sprintf=[$customer->firstname, $customer->lastname]}
    {/if}
</div>

This is a simple example using the variables.

Link to comment
Share on other sites

It work. 

 

It would be more simply to find the file now :) thanks ;) 

But I still have the message error, it seems to be not very important because all work but I'm just curious to understand why this error is detected.....

I join a printscreen of this message . 

The line where there is the bug is : 

'fileUpload' => $fileUpload,

(if nobody have any answer don't worry it doesn't really matter because all the rest work :)  ) 

post-402170-0-28221200-1424936352_thumb.jpg

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

  • 4 weeks later...
  • 2 months later...

Hi guys! nice work, I have a question, like a required field not work the validation...what must i do?

 

Customer.php

 

'fileUpload' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 100),

 

authentication.tpl 

 

<div class="required form-group">
    <label for="fileUpload">{l s='Imagen'} <sup>*</sup></label>
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"/>
    <input type="file" class="is_required validate form-control" data-validate="isGenericName" id="fileUpload" name="fileUpload" value="{if isset($smarty.post.fileUpload)}{$smarty.post.fileUpload}{/if}" />
    </div>

 

thanks

 

 prestashop_1.6.0.14

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

  • 1 month later...

We basically need to make the new field act the same way as the existing ones.

 

First of all, we need to declare and create the field in the working side of the PrestaShop, naming it as fileUpload.

 

Let's run this statement on SQL to create the field in the customer table to store the filename.

ALTER TABLE `ps_customer` ADD `fileUpload` VARCHAR(100) NOT NULL;

In ./classes/Customer.php around line ~30 we'll find the declared variables. Add the new one like this:

class CustomerCore extends ObjectModel
{
	public $id;

	public $fileUpload;

	public $id_shop;

...

then in line ~192 add the new field like this:

'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
'fileUpload' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
),

Now, inside ./controllers/front/AuthController.php around line ~379 we prepare the information:

// Preparing customer
$customer = new Customer();
$lastnameAddress = Tools::getValue('lastname');
$firstnameAddress = Tools::getValue('firstname');
$_POST['lastname'] = Tools::getValue('customer_lastname', $lastnameAddress);
$_POST['firstname'] = Tools::getValue('customer_firstname', $firstnameAddress);

// Add this line
$fileUpload = Tools::getValue('fileUpload');

... 

around line ~423 we'll find the birthday validation, here we need to make the file validation. I made something simple but works fine:

$customer->firstname = Tools::ucwords($customer->firstname);
$customer->birthday = (empty($_POST['years']) ? '' : (int)Tools::getValue('years').'-'.(int)Tools::getValue('months').'-'.(int)Tools::getValue('days'));
if (!Validate::isBirthDate($customer->birthday))
    $this->errors[] = Tools::displayError('Invalid date of birth.');

// Add this code...
//$customer->fileUpload = 'N/A';
// If you want the uploader as OPTIONAL, remove the comment of the line above.
$file = $_FILES['fileUpload'];
$allowed = array('txt', 'rtf', 'doc', 'docx', 'pdf', 'png', 'jpeg', 'gif', 'jpg');
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
if ( file_exists($file['tmp_name']) && in_array($extension, $allowed) )
{
$filename = uniqid()."-".basename($file['name']);
$filename = str_replace(' ', '-', $filename);
$filename = strtolower($filename);
$filename = filter_var($filename, FILTER_SANITIZE_STRING);

$file['name'] = $filename;

$uploader = new UploaderCore();
$uploader->upload($file);

$customer->fileUpload = $filename;
}

 

The same way, in the line ~552, leaving it like this:

$customer->birthday = (empty($_POST['years']) ? '' : (int)Tools::getValue('years').'-'.(int)Tools::getValue('months').'-'.(int)Tools::getValue('days'));
if (!Validate::isBirthDate($customer->birthday))
		$this->errors[] = Tools::displayError('Invalid date of birth');

// Add this code...
//$customer->fileUpload = 'N/A';
// If you want the uploader as OPTIONAL, remove the comment of the line above.
$file = $_FILES['fileUpload'];
$allowed = array('txt', 'rtf', 'doc', 'docx', 'pdf', 'png', 'jpeg', 'gif', 'jpg');
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
if ( file_exists($file['tmp_name']) && in_array($extension, $allowed) )
{
$filename = uniqid()."-".basename($file['name']);
$filename = str_replace(' ', '-', $filename);
$filename = strtolower($filename);
$filename = filter_var($filename, FILTER_SANITIZE_STRING);

$file['name'] = $filename;

$uploader = new UploaderCore();
$uploader->upload($file);

$customer->fileUpload = $filename;
} 

This way the files will be uploaded in the ./upload/ folder, if you want to specify another subfolder just insert the path in $uploader->upload('my-path/'.$_FILES['fileUpload']);

 

To add the input fields in the registration form open ./themes/YOUR-THEME/authentication.tpl around ~182 and ~509 right after the birthday fields.

<div class="form-group">
    <label for="fileUpload">{l s='Select file'} <sup>*</sup></label>
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    <input type="file" class="required form-control" id="fileUpload" name="fileUpload" />
</div>

Don't forget to configure the form to send multimedia information on every <form> tags like the following code: (depends on theme)

<form method="post" id="create-account_form" ... enctype="multipart/form-data">

To show the filename in the client list open ./controllers/admin/AdminCustomersController.php around line ~159 and add:

'connect' => array(
	'title' => $this->l('Last visit'),
	'type' => 'datetime',
	'search' => false,
	'havingFilter' => true
),
'fileUpload' => array(
	'title' => $this->l('Uploaded File'),
	'type' => 'text',
	'search' => true,
	'havingFilter' => true
)
));

In the same file, around ~821:

// Connections
'connections' => $connections,
// Uploaded File
'fileUpload' => $fileUpload,
// Referrers
'referrers' => $referrers,
'show_toolbar' => true

Remember to clean the cache of Prestashop after uploading the modified files. I was based on the lastest PrestaShop 1.6.0.11.

 

If you want to show the files inside the Customer View you need to modify ./admin/themes/default/template/controllers/customers/helpers/view/view.tpl

 

Hope this can be helpful, I've tried to make it simple to implement.

 

Edit: I made some improval modifications in the codes.

 

-------------------------------------------------

 

Awesome contribution!

 

There is a problem with my project about this...

 

 

For my optional fileUpload field, PrestaShop ALWAYS writes the 'N/A' string at DB.

 

I've observed that the IF condition:

if ( file_exists($file['tmp_name']) && in_array($extension, $allowed) )

has always a FALSE return value.

 

If I change the IF condition to:

if ( file_exists($file['tmp_name']) )

I have the same FALSE result.

 

I have file type inputs in my registration form, I have values on my DB and the BO looks great... but I can not upload a file.

 

I'm testing fileUpload with 70Kb PNG files to the default upload folder.

 

What can I do?

 

 

---

I solved it......

 

"What can I do?"???  DAMN IT!!!!

 

JUST ENSURE THAT YOU...

 

Don't forget to configure the form to send multimedia information on every <form> tags like the following code: (depends on theme)

<form method="post" ... enctype="multipart/form-data">
Edited by serms81 (see edit history)
Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...
  • 2 weeks later...
  • 8 months later...
  • 1 month later...
if(Tools::getValue('taxExempt'))
		{
			$file = $_FILES['taxExempt'];
			$allowed = array('pdf', 'png', 'jpeg', 'gif', 'jpg');
			$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
			if ( file_exists($file['tmp_name']) && in_array($extension, $allowed) )
			{		
				$filename = uniqid()."-".basename($file['name']);
				$filename = str_replace(' ', '-', $filename);
				$filename = strtolower($filename);
				$filename = filter_var($filename, FILTER_SANITIZE_STRING);
 
				$file['name'] = $filename;
 
				$uploader = new UploaderCore();
				$uploader->upload($file);
 
				$this->customer->taxExempt = $filename;
			}
		}

I have added this code to IdentityController.php in the function "public function postProcess". I have also added the "taxExempt" form uploader field in identity.tpl. 

This is already working in the authorization page, So I am pretty sure it is not an issue with the database or Customer.php.

But this code does not work. Any ideas??

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

  • 3 years later...
On 2/17/2015 at 4:13 PM, UmbertoD said:

The registration form is located in ./themes/YOUR-THEME/authentication.tpl, you could make the implementation as following, right after the Birthdate field, around line 130:


<div class="form-group">
    <label for="fileUpload">{l s='Select file'} <sup>*</sup></label>
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    <input type="file" class="required form-control" id="fileUpload" name="fileUpload" />
</div>

Beside of this, I think you already know how to add the field in the Customer Class and Controllers(AdminCustomersController.php, AuthController.php, Customer.php).

 

I've put this code right after the $customer->birthday validation at lines ~550 and ~420 to run the uploader in AuthController.php


if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name']))
{
	$extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.png', '.jpeg', '.gif', '.jpg');
	$filename = uniqid().basename($_FILES['fileUpload']['name']);
	$filename = str_replace(' ', '-', $filename);
	$filename = strtolower($filename);
	$filename = filter_var($filename, FILTER_SANITIZE_STRING);

	$_FILES['fileUpload']['name'] = $filename;

	$uploader = new UploaderCore();
	$uploader->upload($_FILES['fileUpload']);

	$customer->fileUpload = $filename;
}

sorry in advance because my english is bad.

From the syntax I have a problem. The filename is not saved to the database. What should i do?

Please help me!

Link to comment
Share on other sites

  • 1 year later...

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...