In example below customer must(not required) upload agreement
file what must be less then 10Mb
and only .jpeg
, .jpg
, .png
or .pdf
format.
<?php
/**
* Customer account controller
*
* @category Vendor
* @package Vendor_StoreCustomer
*/
require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AccountController.php');
class Vendor_StoreCustomer_AccountController extends Mage_Customer_AccountController
{
/**
* Alloweed extensions for upload customer agreement file
*
* @var array
*/
private $allowedExtensions = array('jpg', 'jpeg', 'png', 'pdf');
/**
* Max agreement file upload size in Mb, 10 by default
*
* @var string
*/
private $maxUploadSize = 10;
/**
* Create customer account action
*/
public function createPostAction()
{
$errUrl = $this->_getUrl('*/*/create', array('_secure' => true));
if (!$this->_validateFormKey()) {
$this->_redirectError($errUrl);
return;
}
/** @var $session Mage_Customer_Model_Session */
$session = $this->_getSession();
if ($session->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
if (!$this->getRequest()->isPost()) {
$this->_redirectError($errUrl);
return;
}
$customer = $this->_getCustomer();
try {
$errors = $this->_getCustomerErrors($customer);
$errors = $this->vallidateUploadFile(array('agreement'), $errors);
if (empty($errors)) {
$customer->cleanPasswordsValidationData();
$customer->save();
$this->_dispatchRegisterSuccess($customer);
$this->_successProcessRegistration($customer);
return;
} else {
$this->_addSessionError($errors);
}
} catch (Mage_Core_Exception $e) {
$session->setCustomerFormData($this->getRequest()->getPost());
if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
$url = $this->_getUrl('customer/account/forgotpassword');
$message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
} else {
$message = $this->_escapeHtml($e->getMessage());
}
$session->addError($message);
} catch (Exception $e) {
$session->setCustomerFormData($this->getRequest()->getPost());
$session->addException($e, $this->__('Cannot save the customer.'));
}
$this->_redirectError($errUrl);
}
/**
* Validate upload files
*
* @param array $fields
* @param array $errors
*
* @return array
*/
private function vallidateUploadFile($fields, $errors)
{
if ( ! is_array($fields) ) {
$fields = array();
}
if(!is_array($errors)) {
$errors = array();
}
foreach ($fields as $field) {
if ( isset($_FILES[$field]) ) {
$fileSize = $_FILES[$field]['size'];
if ( ($fileSize / ( 1024 * 1024 )) > $this->maxUploadSize ) {
$errors[] = $this->__('You cannot upload file bigger then 10Mb.');
}
$fileExtension = pathinfo($_FILES[$field]['name'], PATHINFO_EXTENSION);
if( $fileSize && ! in_array($fileExtension, $this->allowedExtensions) ) {
$errors[] = $this->__('Wrong agreement file extension. Allowed extension are: %s.', implode(', ', $this->allowedExtensions));
}
}
}
return $errors;
}
}
$errors = $this->vallidateUploadFile(array('agreement'), $errors);
/**
* Validate upload files
*
* @param array $fields
* @param array $errors
*
* @return array
*/
private function vallidateUploadFile($fields, $errors)
{
if ( ! is_array($fields) ) {
$fields = array();
}
if(!is_array($errors)) {
$errors = array();
}
foreach ($fields as $field) {
if ( isset($_FILES[$field]) ) {
$fileSize = $_FILES[$field]['size'];
if ( ($fileSize / ( 1024 * 1024 )) > $this->maxUploadSize ) {
$errors[] = $this->__('You cannot upload file bigger then 10Mb.');
}
$fileExtension = pathinfo($_FILES[$field]['name'], PATHINFO_EXTENSION);
if( $fileSize && ! in_array($fileExtension, $this->allowedExtensions) ) {
$errors[] = $this->__('Wrong agreement file extension. Allowed extension are: %s.', implode(', ', $this->allowedExtensions));
}
}
}
return $errors;
}