Gombi92 Posted February 5, 2015 Share Posted February 5, 2015 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 Link to comment Share on other sites More sharing options...
UmbertoD Posted February 17, 2015 Share Posted February 17, 2015 (edited) 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 February 17, 2015 by UmbertoD (see edit history) Link to comment Share on other sites More sharing options...
Skygraphism Posted February 18, 2015 Share Posted February 18, 2015 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 More sharing options...
fred-vinapresta Posted February 19, 2015 Share Posted February 19, 2015 Hi Skygraphism, did you disable smarty cache in the performance admin after you did the modifcations? Link to comment Share on other sites More sharing options...
Skygraphism Posted February 19, 2015 Share Posted February 19, 2015 (edited) 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 February 19, 2015 by Skygraphism (see edit history) Link to comment Share on other sites More sharing options...
UmbertoD Posted February 24, 2015 Share Posted February 24, 2015 (edited) 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 February 25, 2015 by UmbertoD (see edit history) 1 Link to comment Share on other sites More sharing options...
Skygraphism Posted February 25, 2015 Share Posted February 25, 2015 (edited) 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 // Uploaded File 'fileUpload' => $fileUpload, The error says : [8] Undefined variable: fileUpload And I don't understand where the problem is... Edited February 25, 2015 by Skygraphism (see edit history) Link to comment Share on other sites More sharing options...
UmbertoD Posted February 25, 2015 Share Posted February 25, 2015 You're welcome! I realized I left some coding mistakes in the first review, I've just made some modifications in my post, check the codes again. Link to comment Share on other sites More sharing options...
Skygraphism Posted February 25, 2015 Share Posted February 25, 2015 Thanks for the corrections! I made the modifications in all files, but the error is always here. Link to comment Share on other sites More sharing options...
UmbertoD Posted February 25, 2015 Share Posted February 25, 2015 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 More sharing options...
Skygraphism Posted February 25, 2015 Share Posted February 25, 2015 The variable is correctly declared in the files. The file is uploading to the server and the filename is in the SQL. I don't see where the problem should come....but in the customer's details (in the back office) the filename should be appaear somewhere? Link to comment Share on other sites More sharing options...
UmbertoD Posted February 25, 2015 Share Posted February 25, 2015 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 More sharing options...
Skygraphism Posted February 26, 2015 Share Posted February 26, 2015 (edited) 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 ) Edited February 26, 2015 by Skygraphism (see edit history) Link to comment Share on other sites More sharing options...
Skygraphism Posted March 25, 2015 Share Posted March 25, 2015 Hi guys! I come back to you because I have an other question about the uploaded file. In the Back Office, in the customer's details, what I should do if I want to upload a new file by the Back Office? Link to comment Share on other sites More sharing options...
tamuztech Posted June 5, 2015 Share Posted June 5, 2015 (edited) 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 July 4, 2015 by tamuztech (see edit history) Link to comment Share on other sites More sharing options...
serms81 Posted July 21, 2015 Share Posted July 21, 2015 (edited) 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 July 21, 2015 by serms81 (see edit history) Link to comment Share on other sites More sharing options...
serms81 Posted July 21, 2015 Share Posted July 21, 2015 If input file uploader in customer registration were optional, Can we upload it later for a customer at the BO customer view? How could we upload it from BO customer view? any idea? Link to comment Share on other sites More sharing options...
Deiindd Posted September 1, 2015 Share Posted September 1, 2015 (edited) Can I add this to Order return? Please help Edited September 2, 2015 by Deiindd (see edit history) Link to comment Share on other sites More sharing options...
ggyyvv Posted September 11, 2015 Share Posted September 11, 2015 Not work in version 1.6.0.14 please help Link to comment Share on other sites More sharing options...
ggyyvv Posted September 11, 2015 Share Posted September 11, 2015 and please answer, where is to insert <form method="post" id="create-account_form" ... enctype="multipart/form-data"> how it looks like a fully to the standard theme (1.6.0.14) Link to comment Share on other sites More sharing options...
Deiindd Posted September 21, 2015 Share Posted September 21, 2015 anyone? Link to comment Share on other sites More sharing options...
Aletren Posted June 10, 2016 Share Posted June 10, 2016 (edited) ? Edited June 10, 2016 by lefi0528 (see edit history) Link to comment Share on other sites More sharing options...
Aletren Posted June 10, 2016 Share Posted June 10, 2016 and please answer, where is to insert <form method="post" id="create-account_form" ... enctype="multipart/form-data"> how it looks like a fully to the standard theme (1.6.0.14) please Did you find the answer ? Link to comment Share on other sites More sharing options...
Sridhar09 Posted July 30, 2016 Share Posted July 30, 2016 (edited) 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 July 30, 2016 by Sridhar09 (see edit history) Link to comment Share on other sites More sharing options...
Shara_AC Posted August 3, 2019 Share Posted August 3, 2019 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 More sharing options...
tarekoxygene Posted January 13, 2021 Share Posted January 13, 2021 On 8/3/2019 at 7:13 AM, yahyaefendy said: 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! My be you omitted $customer->save(); Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now