<?php
/*
* 2009-2014 Developpeur Web Freelance
* 
*  @author DWF <contact@dwf.fr>
*  @copyright  2009-2014 Developpeur Web Freelance
*/

if (!defined('_PS_VERSION_'))
	exit;

class dwfexportcustomers extends Module
{
	private $_langLabel = array();
	private $_directory;
	private $_filename;
	private $_filename_http;
	private $_fieldlist = array();
	private $_fieldnames = array(
						1 => array(
								'name'						=> 'id_customer',
								'backwardCompatibility' 	=> true), 
				        2 => array(
								'name'						=> 'id_shop_group',
								'backwardCompatibility' 	=> false),
				        3 => array(
								'name'						=> 'id_shop',
								'backwardCompatibility' 	=> false),
				        4 => array(
								'name'						=> 'id_gender',
								'backwardCompatibility' 	=> true),
						5 => array(
								'name'						=> 'id_default_group',
								'backwardCompatibility' 	=> true),
				        6 => array(
								'name'						=> 'id_lang',
								'backwardCompatibility' 	=> false),
				        7 => array(
								'name'						=> 'id_risk',
								'backwardCompatibility' 	=> false),
						8 => array(
								'name'						=> 'company',
								'backwardCompatibility' 	=> false),
				        9 => array(
								'name'						=> 'siret',
								'backwardCompatibility' 	=> false),
				        10 => array(
								'name'						=> 'ape',
								'backwardCompatibility' 	=> false),
				      	11 => array(
								'name'						=> 'default_group_name',
								'backwardCompatibility' 	=> true),
						12 => array(
								'name'						=> 'all_groups_id',
								'backwardCompatibility' 	=> true),
						13 => array(
								'name'						=> 'all_groups_name',
								'backwardCompatibility' 	=> true),
						14 => array(
								'name'						=> 'email',
								'backwardCompatibility' 	=> true),
						15 => array(
								'name'						=> 'passwd',
								'backwardCompatibility' 	=> true),
						16 => array(
								'name'						=> 'firstname',
								'backwardCompatibility' 	=> true),
						17 => array(
								'name'						=> 'lastname',
								'backwardCompatibility' 	=> true),
						18 => array(
								'name'						=> 'address1',
								'backwardCompatibility' 	=> true),
						19 => array(
								'name'						=> 'address2',
								'backwardCompatibility' 	=> true),
						20 => array(
								'name'						=> 'postcode',
								'backwardCompatibility' 	=> true),
						21 => array(
								'name'						=> 'city',
								'backwardCompatibility' 	=> true),
						22 => array(
								'name'						=> 'country',
								'backwardCompatibility' 	=> true),
						23 => array(
								'name'						=> 'state',
								'backwardCompatibility' 	=> true),
						24 => array(
								'name'						=> 'phone',
								'backwardCompatibility' 	=> true),
						25 => array(
								'name'						=> 'phone_mobile',
								'backwardCompatibility' 	=> true),
						26 => array(
								'name'						=> 'vat_number',
								'backwardCompatibility' 	=> true),
						27 => array(
								'name'						=> 'dni',
								'backwardCompatibility' 	=> true),
						28 => array(
								'name'						=> 'last_passwd_gen',
								'backwardCompatibility' 	=> true),
						29 => array(
								'name'						=> 'birthday',
								'backwardCompatibility' 	=> true),
						30 => array(
								'name'						=> 'newsletter',
								'backwardCompatibility' 	=> true),
						31 => array(
								'name'						=> 'ip_registration_newsletter',
								'backwardCompatibility' 	=> true),
						32 => array(
								'name'						=> 'newsletter_date_add',
								'backwardCompatibility' 	=> true),
						33 => array(
								'name'						=> 'optin',
								'backwardCompatibility' 	=> true),
				        34 => array(
								'name'						=> 'website',
								'backwardCompatibility' 	=> false),
				        35 => array(
								'name'						=> 'outstanding_allow_amount',
								'backwardCompatibility' 	=> false),
				        36 => array(
								'name'						=> 'show_public_prices',
								'backwardCompatibility' 	=> false),
				        37 => array(
								'name'						=> 'max_payment_days',
								'backwardCompatibility' 	=> false),
				        38 => array(
								'name'						=> 'secure_key',
								'backwardCompatibility' 	=> true),
						39 => array(
								'name'						=> 'note',
								'backwardCompatibility' 	=> true),
						40 => array(
								'name'						=> 'active',
								'backwardCompatibility' 	=> true),
						41 => array(
								'name'						=> 'is_guest',
								'backwardCompatibility' 	=> true),
						42 => array(
								'name'						=> 'deleted',
								'backwardCompatibility' 	=> true),
						43 => array(
								'name'						=> 'date_add',
								'backwardCompatibility' 	=> true),
						44 => array(
								'name'						=> 'date_upd',
								'backwardCompatibility' 	=> true),
						45 => array(
								'name'						=> 'id_addresses',
								'backwardCompatibility' 	=> true),
						46 => array(
								'name'						=> 'addresses_all',
								'backwardCompatibility' 	=> true),
					);

	
	function __construct() {
		if (version_compare(_PS_VERSION_,'1.6','>=')) {
			$this->bootstrap = true;
		}
		$this->name = 'dwfexportcustomers';
		$this->tab = 'export';
		$this->version = '1.4';
		$this->author = "DWF";
		$this->module_key = "c61beda72a12f803feedd75a4468cc4c";
	
		/* Call the parent construct for translations */
		parent::__construct();
		
		$this->page = basename(__FILE__, '.php');
		$this->displayName = $this->l('Customers Export');
		$this->description = $this->l('Export your Customers in CSV file.');
		
		/** Backward compatibility */
		require(_PS_MODULE_DIR_.$this->name.'/backward_compatibility/backward.php');
		
		$this->_filename = dirname($_SERVER['SCRIPT_FILENAME']).'/backups/dwfexportcustomers_'.strtolower(Language::getIsoById($this->context->language->id)).'.csv';
		$this->_filename_http = 'http://'.Configuration::get('PS_SHOP_DOMAIN').dirname($_SERVER['SCRIPT_NAME']).'/backup.php?filename=dwfexportcustomers_'.strtolower(Language::getIsoById($this->context->language->id)).'.csv';
		
		if(Configuration::get('MOD_DWFCUSTOMEREXP_FIELDLIST')) {
			$activefields = explode(',', Configuration::get('MOD_DWFCUSTOMEREXP_FIELDLIST_ON'));
			$fieldlist = explode(',', Configuration::get('MOD_DWFCUSTOMEREXP_FIELDLIST'));
			foreach ($fieldlist as $k => $field) {
				$this->_fieldlist[] = array(
											'fieldid'	=> $field,
											'fieldname'	=> $this->_fieldnames[$field]['name'],
											'position'	=> $k,
											'active'	=> (in_array($field, $activefields)?1:0)
				); 
			}
		}
		
	}

	function install() {
		$groups = Group::getGroups(intval(Configuration::get('PS_LANG_DEFAULT')));
		foreach ($groups as $grp) $arrGrp[] = $grp['id_group'];
		
		for($i=1;$i<=count($this->_fieldnames);$i++) {
			if(_PS_VERSION_ >= '1.5' || $this->_fieldnames[$i]['backwardCompatibility'] == true)
				$arrFields[] = $i;
		}
		
		if (!parent::install() ||
			!Configuration::updateValue('MOD_DWFCUSTOMEREXP_FIELDLIST', implode(',', $arrFields)) ||
			!Configuration::updateValue('MOD_DWFCUSTOMEREXP_FIELDLIST_ON', implode(',', $arrFields)) ||
			!Configuration::updateValue('MOD_DWFCUSTOMEREXP_GROUPS', implode(',', $arrGrp)))
			
			return false;
		
		return true;
	}
	
	  function uninstall() {
        if (!parent::uninstall() ||
	        !Configuration::deleteByName('MOD_DWFCUSTOMEREXP_FIELDLIST') ||
	        !Configuration::deleteByName('MOD_DWFCUSTOMEREXP_FIELDLIST_ON') ||
	        !Configuration::deleteByName('MOD_DWFCUSTOMEREXP_GROUPS'))	
			
        	return false;
		
        return true;		
    }

    protected function changePosition() {
    	
    	if (!Validate::isInt(Tools::getValue('position')) ||
    			Tools::getValue('location') != 0 ||
    			(Tools::getValue('way') != 0 && Tools::getValue('way') != 1))
    		Tools::displayError();
    
    	$this->_html .= 'pos change!';
    	
    	$fieldid = (int)Tools::getValue('fieldid');
    	$position = (int)Tools::getValue('position');
    	$location = (int)Tools::getValue('location');
    
    	if (Tools::getValue('way') == 0)
    		$new_position = $position + 1;
    	else if (Tools::getValue('way') == 1)
    		$new_position = $position - 1;
    
    	$invert = $this->_fieldlist[$new_position];
    	$this->_fieldlist[$new_position] = $this->_fieldlist[$position];
    	$this->_fieldlist[$position] = $invert;

    	foreach ($this->_fieldlist as $fields) {
    		$arrFieldList[] = $fields['fieldid'];
    	}
    	
    	Configuration::updateValue('MOD_DWFCUSTOMEREXP_FIELDLIST', implode(',', $arrFieldList));
    	Tools::redirectAdmin('index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'));
    }
    
    protected function updatePositionsDnd() {
    	if (Tools::getValue('dwfexportcustomers'))
    		$positions = Tools::getValue('dwfexportcustomers');
    	else
    		$positions = array();
    	
    	foreach ($positions as $position => $value) {
    		$pos = explode('_', $value);
    
    		if (isset($pos[2]))
    			$arrFieldList[] = $pos[2];
    	}
    	
    	Configuration::updateValue('MOD_DWFCUSTOMEREXP_FIELDLIST', implode(',', $arrFieldList));
    }
    
	private function _postValidation() {
		@unlink($this->_filename);
		$fp = fopen($this->_filename, 'wb');
		fclose($fp);
		if (!file_exists($this->_filename)) {
			$this->_html .= '<div class="warning confirm">'.$this->l('Cannot write').' '.realpath($this->_filename).'</div>';
			
			return false;
		}
	}
	
	private function _addToFeed($str) {
		if(file_exists($this->_filename)) {
			$fp = fopen($this->_filename, 'ab');
			fwrite($fp, $str, strlen($str));
			fclose($fp);
		}
	}
	
	private function _postProcess() {

		if ($this->_postValidation() === false)
			return false;

		$this->_errors = array();
		if (Tools::isSubmit('submitSaveDwfExportCustomers')) {
// 			$this->context->controller->getLanguages();
			Configuration::updateValue('MOD_DWFCUSTOMEREXP_GROUPS', implode(',', Tools::getValue('groupBox')));
			Configuration::updateValue('MOD_DWFCUSTOMEREXP_FIELDLIST_ON', implode(',', Tools::getValue('activeFields')));

			Tools::redirectAdmin('index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'));
		}
		elseif (Tools::isSubmit('submitExportDwfExportCustomers')) {
			
			set_time_limit(0);
			$id_lang = intval(Configuration::get('PS_LANG_DEFAULT'));
			
			$this->_html = '<h2>'.$this->displayName.'</h2>';
			
			if(!Configuration::get('MOD_DWFCUSTOMEREXP_GROUPS')) {
					
				$groupsString = Configuration::get('MOD_DWFCUSTOMEREXP_GROUPS');
					
				$sql='SELECT * FROM '._DB_PREFIX_.'customer c
					LEFT JOIN '._DB_PREFIX_.'customer_group cg ON(cg.id_customer=c.id_customer)
					LEFT JOIN '._DB_PREFIX_.'group_lang gl ON (gl.id_group=cg.id_group AND gl.id_lang="'.$id_lang.'")
					WHERE cg.id_group IN ('.$groupsString.')
					GROUP BY c.id_customer';
			}
			else {
				$sql='SELECT * FROM '._DB_PREFIX_.'customer c
					LEFT JOIN '._DB_PREFIX_.'customer_group cg ON(cg.id_customer=c.id_customer)
					LEFT JOIN '._DB_PREFIX_.'group_lang gl ON (gl.id_group=cg.id_group AND gl.id_lang="'.$id_lang.'")
					GROUP BY c.id_customer';
					
			}
				
			$customers=Db::getInstance()->executeS($sql);
			
			foreach ($this->_fieldlist as $field) {
				if($field['active']) $this->_addToFeed($field['fieldname'].';');
			}
			$this->_addToFeed("\n");

			foreach ($customers as $customer) {
				$sqlGroups='SELECT cg.id_group, gl.name FROM '._DB_PREFIX_.'customer_group cg
							LEFT JOIN '._DB_PREFIX_.'group_lang gl ON (gl.id_group=cg.id_group AND gl.id_lang="'.$id_lang.'")
							WHERE cg.id_customer="'.$customer['id_customer'].'"';
	
				$arrayGroups=Db::getInstance()->executeS($sqlGroups);
	
				$groups_id = array();
				$groups_name = array();
				foreach($arrayGroups as $group) {
					$groups_id[] = $group['id_group'];
					$groups_name[] = $group['name'];
				}
	
				$groups_id = implode('|', $groups_id);
				$groups_name = implode('|',$groups_name);

				$sqlAddresses='SELECT *, cl.name as country, s.name as state 
								FROM '._DB_PREFIX_.'address a
								LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country=a.id_country AND cl.id_lang="'.$id_lang.'")
								LEFT JOIN '._DB_PREFIX_.'state s ON (s.id_state=a.id_state)
								WHERE a.id_customer="'.$customer['id_customer'].'"
								ORDER BY a.id_address';
	
				$arrayAddresses = Db::getInstance()->executeS($sqlAddresses);
	
				$addresses_id = array();
				$addresses_all = array();
				$invoiceAddress = array();
				foreach($arrayAddresses as $address) {
					if(!$invoiceAddress)
						$invoiceAddress = $address;
					
					$addresses_id[] = $address['id_address'];
					$addresses_all[] = $address['firstname']." ".$address['lastname']." ".$address['address1']." ".$address['address2']." ".$address['postcode']." ".$address['city']." ".$address['country']." ".$address['state']." ".$address['phone']." ".$address['phone_mobile'];
				}
	
				$addresses_id = implode('|', $addresses_id);
				$addresses_all = implode('|', $addresses_all);
				
				foreach ($this->_fieldlist as $field) {
					if($field['active']) {
						switch ($field['fieldname']) {
							case "id_customer": 
								$this->_addToFeed('"'.$customer['id_customer'].'";');
								break;
							
							case "id_shop_group":
									$this->_addToFeed('"'.$customer['id_shop_group'].'";');
								break;
								
							case "id_shop":
									$this->_addToFeed('"'.$customer['id_shop'].'";');
								break;
	
							case "id_gender":
								$this->_addToFeed('"'.$customer['id_gender'].'";');
								break;
								
							case "id_default_group":
								$this->_addToFeed('"'.$customer['id_default_group'].'";');
								break;
							
							case "default_group_name":
								$this->_addToFeed('"'.$customer['name'].'";');
								break;
								
							case "all_groups_id":
								$this->_addToFeed('"'.$groups_id.'";');
								break;
								
							case "all_groups_name":
								$this->_addToFeed('"'.$groups_name.'";');
								break;
								
							case "id_lang":
									$this->_addToFeed('"'.$this->getLangLabel($customer['id_lang']).'";');
								break;
								
							case "id_risk":
									$this->_addToFeed('"'.$customer['id_risk'].'";');
								break;
								
							case "company":
									$this->_addToFeed('"'.$customer['company'].'";');
								break;
								
							case "siret":
									$this->_addToFeed('"'.$customer['siret'].'";');
								break;
								
							case "ape":
									$this->_addToFeed('"'.$customer['ape'].'";');
								break;
								
							case "email":
								$this->_addToFeed('"'.$customer['email'].'";');
								break;
								
							case "passwd":
								$this->_addToFeed('"'.$customer['passwd'].'";');
								break;
								
							case "firstname":
								$this->_addToFeed('"'.$customer['firstname'].'";');
								break;
							
							case "lastname":
								$this->_addToFeed('"'.$customer['lastname'].'";');
								break;
								
							case "address1":
								$this->_addToFeed('"'.$invoiceAddress['address1'].'";');
								break;
							
							case "address2":
								$this->_addToFeed('"'.$invoiceAddress['address2'].'";');
								break;
							
							case "postcode":
								$this->_addToFeed('"'.$invoiceAddress['postcode'].'";');
								break;
							
							case "city":
								$this->_addToFeed('"'.$invoiceAddress['city'].'";');
								break;
							
							case "country":
								$this->_addToFeed('"'.$invoiceAddress['country'].'";');
								break;

							case "state":
								$this->_addToFeed('"'.$invoiceAddress['state'].'";');
								break;

							case "phone":
								$this->_addToFeed('"'.$invoiceAddress['phone'].'";');
								break;
							
							case "phone_mobile":
								$this->_addToFeed('"'.$invoiceAddress['phone_mobile'].'";');
								break;
								
							case "vat_number":
								$this->_addToFeed('"'.$invoiceAddress['vat_number'].'";');
								break;
								
							case "dni":
								$this->_addToFeed('"'.$invoiceAddress['dni'].'";');
								break;
								
							case "last_passwd_gen":
								$this->_addToFeed('"'.$customer['last_passwd_gen'].'";');
								break;
								
							case "birthday":
								$this->_addToFeed('"'.$customer['birthday'].'";');
								break;
								
							case "newsletter":
								$this->_addToFeed('"'.$customer['newsletter'].'";');
								break;
								
							case "ip_registration_newsletter":
								$this->_addToFeed('"'.$customer['ip_registration_newsletter'].'";');
								break;
								
							case "newsletter_date_add":
								$this->_addToFeed('"'.$customer['newsletter_date_add'].'";');
								break;
								
							case "optin":
								$this->_addToFeed('"'.$customer['optin'].'";');
								break;
								
							case "website":
									$this->_addToFeed('"'.$customer['website'].'";');
								break;
								
							case "outstanding_allow_amount":
									$this->_addToFeed('"'.$customer['outstanding_allow_amount'].'";');
								break;
								
							case "show_public_prices":
									$this->_addToFeed('"'.$customer['show_public_prices'].'";');
								break;
								
							case "max_payment_days":
									$this->_addToFeed('"'.$customer['max_payment_days'].'";');
								break;
								
							case "secure_key":
								$this->_addToFeed('"'.$customer['secure_key'].'";');
								break;
								
							case "note":
								$this->_addToFeed('"'.$customer['note'].'";');
								break;
								
							case "active":
								$this->_addToFeed('"'.$customer['active'].'";');
								break;
							
							case "is_guest":
								$this->_addToFeed('"'.$customer['is_guest'].'";');
								break;
								
							case "deleted":
								$this->_addToFeed('"'.$customer['deleted'].'";');
								break;

							case "date_add":
								$this->_addToFeed('"'.$customer['date_add'].'";');
								break;
								
							case "date_upd":
								$this->_addToFeed('"'.$customer['date_upd'].'";');
								break;
							
							case "id_addresses":
								$this->_addToFeed('"'.$addresses_id.'";');
								break;
								
							case "addresses_all":
								$this->_addToFeed('"'.$addresses_all.'";');
								break;
						}
					}
				}
				$this->_addToFeed("\n");							
			}
			
			
						
			if(file_exists($this->_filename)) {
				$fp = fopen($this->_filename, 'rb');
				$fstat = fstat($fp);
				fclose($fp);
					
				$this->_html .= '
						<div class="'.(isset($this->bootstrap) && $this->bootstrap?'alert alert-success':'conf confirm').'">
							<p>'.$this->l('Your Customers file is online at the following address:').'<br />
						  		<a href="'.$this->_filename_http.'" target="_blanck">
						  			<strong>'.$this->_filename_http.'</strong>
						  		</a>
							</p>
						  	<p>'.$this->l('Update:').' <strong>'.date('d/m/Y H:i:s', $fstat['mtime']).'</strong></p>
						</div>';
			}
			

			$this->_html .= '<fieldset class="'.(isset($this->bootstrap) && $this->bootstrap?'alert alert-info':'info').'">
								<h3>'.$this->l('Infos on the exported fields').'</h3>
									<br>'.$this->l('To open CSV : Choose UTF8 encodage').'<br>
									<br>'.$this->l('The fields are separated by semicolon (;)').'
									<br>'.$this->l('Fields with multiple fields are separated by comma (,)').'
									<br>'.$this->l('The text separator is double quotes (")').'
									<br>'.$this->l('The Groups are represented like that "x|y|z..."').'
									<br>'.$this->l('If a customer has got several addresses they will be represented like that "address1|address2..."').'</br>
									<br>'.$this->l('The address format is : firstname lastname address1 address2 zipcode city country state phone mobile phone').'
							</fieldset> <hr />';
				
		}
		elseif (Tools::isSubmit('fieldid') && Tools::isSubmit('way') && Tools::isSubmit('position') && Tools::isSubmit('location'))
			$this->changePosition();
		elseif (Tools::isSubmit('updatePositions'))
			$this->updatePositionsDnd();
		
		if (count($this->_errors)) {
			foreach ($this->_errors as $err)
				$this->_html .= '<div class="alert error">'.$err.'</div>';
		}
	}
	
	public function getLangLabel($idLang) {
		if(isset($this->_langLabel[$idLang]))
			return $this->_langLabel[$idLang];
		else {
			$lang = new Language($idLang);
			if($lang->id) {
				$this->_langLabel[$lang->id] = $lang->iso_code;
				return $this->_langLabel[$lang->id];
			}
			else return null;
		}
	}
	
	public function getContent() {
		$this->_html = '';
		$this->_postProcess();
	
		$this->displayForm();
	
		return $this->_html;
	}

	private function displayForm() {
		global $currentIndex;
		
		if (_PS_VERSION_ >= '1.5') {
			$this->context->controller->addJqueryPlugin('tablednd');
			$this->context->controller->addJS($this->_path.'dwfexportcustomers-dnd.js');
			$this->context->controller->addCSS($this->_path.'dwfexportcustomers.css');
		}
		else {
			$this->_html .= '
					<script type="text/javascript" src="../js/jquery/jquery.tablednd_0_5.js"></script>
					<script type="text/javascript" src="'.$this->_path.'dwfexportcustomers-dnd.js"></script>
					<link type="text/css" rel="stylesheet" href="'.$this->_path.'dwfexportcustomers.css" />
					';
		}
		
		$id_lang = intval(Configuration::get('PS_LANG_DEFAULT'));
	
		$this->_html .= '
		<script type="text/javascript">
            var come_from = \''.$this->name.'\';
            var token = \''.Tools::getAdminTokenLite('AdminModules').'\';
            '.(_PS_VERSION_ < '1.5'?'var currentIndex = \''.$currentIndex.'\';':'').'
            var alternate = 1;
        </script>
		
		<form method="post">
	        <fieldset class="panel">
				<h3><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Export').'</h3>
				<h4>'.$this->l('To export with your parameters, please click on the button below :').'</h4>
				<button type="submit" class="btn btn-default" name="submitExportDwfExportCustomers">'.$this->l('Export').'</button>
			</fieldset>
					
			<hr />
			
			<fieldset class="panel">
				<h3><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</h3>';
					$groups=Group::getGroups($id_lang);
					if (sizeof($groups)) {
							
						$this->_html .= '
						<div class="form-group">		
						<h4>'.$this->l('Groups:').' </h4>
						<table cellspacing="0" cellpadding="0" class="table" id="'.$this->name.'-groups">
							<tr>
								<th><input type="checkbox" name="checkme" class="noborder" onclick="checkDelBoxes(this.form, \'groupBox[]\', this.checked)" /></th>
								<th>'.$this->l('ID').'</th>
								<th>'.$this->l('Group name').'</th>
							</tr>';
							$irow = 0;
							
							$activeGroups = explode(',', Configuration::get('MOD_DWFCUSTOMEREXP_GROUPS'));
							foreach ($groups as $group) {
								
								$this->_html .= '<tr class="'.($irow++ % 2 ? 'alt_row' : '').'">
									<td>'.'<input type="checkbox" name="groupBox[]" class="groupBox" id="groupBox_'.$group['id_group'].'" value="'.$group['id_group'].'"'.(in_array($group['id_group'], $activeGroups)?' checked="checked"':'').' /></td>
									<td>'.$group['id_group'].'</td>
									<td><label for="groupBox_'.$group['id_group'].'" class="t">'.$group['name'].'</label></td>
								</tr>';
							}
							
						$this->_html .= '</table>
						<p class="help-block">'.$this->l('Check all customers groups you want to export (by default all groups)').'</p>
						</div>';
					}
					else $this->_html .= '<p>'.$this->l('No group created').'</p>';
				  
	         $this->_html .= '
	         		<div class="form-group">
	         		<h4>'.$this->l('Choose the fields you want to export:').'</h4>
	         		<table cellspacing="0" cellpadding="0" class="table tableDnD cms" id="'.$this->name.'">
						<thead>
							<tr class="nodrag nodrop">
								<th><input type="checkbox" name="checkme" class="noborder" onclick="checkDelBoxes(this.form, \'activeFields[]\', this.checked)" /></th>
								<th>'.$this->l('Field name').'</th>
								<th>'.$this->l('Position').'</th>
							</tr>
						</thead>
						<tbody>
	         		';
	     
			foreach($this->_fieldlist as $key => $field) {
	      		$this->_html .= '
					<tr class="'.($key%2?'alt_row':'not_alt_row').' row_hover" id="tr_'.($key%2).'_'.$field['fieldid'].'_'.$field['position'].'">
						<td><input type="checkbox" name="activeFields[]" value="'.$field['fieldid'].'"'.($field['active'] == 1?' checked="checked"':'').' /></td>
						<td>'.$this->l($field['fieldname']).'</td>
						<td class="center pointer dragHandle" id="td_'.($key%2).'_'.$field['fieldname'].'">
							<a
								'.((($key == (sizeof($this->_fieldnames) - 1)) || (sizeof($this->_fieldnames) == 1))?' style="display: none;"':'').' 
								href="'.$currentIndex.'&configure=dwfexportcustomers&fieldid='.$field['fieldid'].'&way=1&position='.(int)($field['position'] + 1).'&location=0&token='.Tools::getAdminTokenLite('AdminModules').'">
								<img src="'._PS_ADMIN_IMG_.'down.gif" alt="'.$this->l('Down').'" title="'.$this->l('Down').'" />
							</a>
							<a
								'.((($field['position'] == 0) || ($key == 0))?' style="display: none;"':'').' 
								href="'.$currentIndex.'&configure=dwfexportcustomers&fieldid='.$field['fieldname'].'&way=0&position='.(int)($field['position'] - 1).'&location=0&token='.Tools::getAdminTokenLite('AdminModules').'">
								<img src="'._PS_ADMIN_IMG_.'up.gif" alt="'.$this->l('Up').'" title="'.$this->l('Up').'" />
							</a>
						</td>
					</tr>';
			
			}
			
			$this->_html .= '</tbody>
				</table>
			
				<button type="submit" name="submitSaveDwfExportCustomers" class="btn btn-default">'.$this->l('Submit').'</button>
				</div>
			</fieldset>
		</form>';
	}

}