* @copyright PrestaShop * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0 * @version 1.3 * */ include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php'); class AdminCustomers extends AdminTab { public function __construct() { $this->table = 'customer'; $this->className = 'Customer'; $this->lang = false; $this->edit = true; $this->view = true; $this->delete = true; $this->deleted = true; $this->_select = '(YEAR(CURRENT_DATE)-YEAR(`birthday`)) - (RIGHT(CURRENT_DATE, 5) $this->l('M'), 2 => $this->l('F'), 9 => $this->l('?')); $this->fieldsDisplay = array( 'id_customer' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), 'id_gender' => array('title' => $this->l('Gender'), 'width' => 25, 'align' => 'center', 'icon' => array(1 => 'male.gif', 2 => 'female.gif', 'default' => 'unknown.gif'), 'orderby' => false, 'type' => 'select', 'select' => $genders, 'filter_key' => 'a!id_gender'), 'lastname' => array('title' => $this->l('Last Name'), 'width' => 80), 'firstname' => array('title' => $this->l('First name'), 'width' => 60), 'email' => array('title' => $this->l('E-mail address'), 'width' => 120, 'maxlength' => 19), 'age' => array('title' => $this->l('Age'), 'width' => 30, 'search' => false), 'active' => array('title' => $this->l('Enabled'), 'width' => 25, 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false), 'newsletter' => array('title' => $this->l('News.'), 'width' => 25, 'align' => 'center', 'type' => 'bool', 'icon' => array(0 => 'disabled.gif', 1 => 'enabled.gif'), 'orderby' => false), 'optin' => array('title' => $this->l('Opt.'), 'width' => 25, 'align' => 'center', 'type' => 'bool', 'icon' => array(0 => 'disabled.gif', 1 => 'enabled.gif'), 'orderby' => false), 'date_add' => array('title' => $this->l('Registration'), 'width' => 60, 'type' => 'date'), 'connect' => array('title' => $this->l('Connection'), 'width' => 60, 'type' => 'datetime', 'search' => false)); $this->optionTitle = $this->l('Customers options'); $this->_fieldsOptions = array( 'PS_PASSWD_TIME_FRONT' => array('title' => $this->l('Password regenerate:'), 'desc' => $this->l('Security minimum time to wait for regenerate a new password'), 'cast' => 'intval', 'size' => 5, 'type' => 'text', 'suffix' => ' '.$this->l('minutes')) ); parent::__construct(); } public function postProcess() { global $currentIndex; if (Tools::getValue('submitAdd'.$this->table)) { $groupList = Tools::getValue('groupBox'); /* Checking fields validity */ $this->validateRules(); if (!sizeof($this->_errors)) { $id = intval(Tools::getValue('id_'.$this->table)); if (isset($id) AND !empty($id)) { if ($this->tabAccess['edit'] !== '1') $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.'); else { $object = new $this->className($id); if (Validate::isLoadedObject($object)) { $customer_email = strval(Tools::getValue('email')); // check if e-mail already used if ($customer_email != $object->email) { $customer = new Customer(); $customer->getByEmail($customer_email); if ($customer->id) $this->_errors[] = Tools::displayError('an account already exists for this e-mail address:').' '.$customer_email; } if ($object->getNeedDNI() AND Tools::getValue('dni') != NULL AND !Validate::isDni(Tools::getValue('dni'))) $this->_errors[] = Tools::displayError('identification number is incorrect or already used'); if (!is_array($groupList) OR sizeof($groupList) == 0) $this->_errors[] = Tools::displayError('customer must be in at least one group'); else if (!in_array(Tools::getValue('id_default_group'), $groupList)) $this->_errors[] = Tools::displayError('default customer group must be selected on group box'); // Updating customer's group if (!sizeof($this->_errors)) { $object->cleanGroups(); if (is_array($groupList) AND sizeof($groupList) > 0) $object->addGroups($groupList); } } else $this->_errors[] = Tools::displayError('an error occurred while loading object').' '.$this->table.' '.Tools::displayError('(cannot load object)'); } } else { if ($this->tabAccess['add'] === '1') { $object = new $this->className(); $this->copyFromPost($object, $this->table); if (!$object->add()) $this->_errors[] = Tools::displayError('an error occurred while creating object').' '.$this->table.' ('.mysql_error().')'; elseif (($_POST[$this->identifier] = $object->id /* voluntary */) AND $this->postImage($object->id) AND !sizeof($this->_errors) AND $this->_redirect) { // Add Associated groups $group_list = Tools::getValue('groupBox'); if (is_array($group_list) && sizeof($group_list) > 0) $object->addGroups($group_list); $parent_id = intval(Tools::getValue('id_parent', 1)); // Save and stay on same form if (Tools::isSubmit('submitAdd'.$this->table.'AndStay')) Tools::redirectAdmin($currentIndex.'&'.$this->identifier.'='.$object->id.'&conf=3&update'.$this->table.'&token='.$this->token); // Save and back to parent if (Tools::isSubmit('submitAdd'.$this->table.'AndBackToParent')) Tools::redirectAdmin($currentIndex.'&'.$this->identifier.'='.$parent_id.'&conf=3&token='.$this->token); // Default behavior (save and back) Tools::redirectAdmin($currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$this->token); } } else $this->_errors[] = Tools::displayError('You do not have permission to add anything here.'); } } } return parent::postProcess(); } public function viewcustomer() { global $currentIndex, $cookie; $irow = 0; $configurations = Configuration::getMultiple(array('PS_LANG_DEFAULT', 'PS_CURRENCY_DEFAULT')); $defaultLanguage = intval($configurations['PS_LANG_DEFAULT']); $defaultCurrency = intval($configurations['PS_CURRENCY_DEFAULT']); $customer = $this->loadObject(); $customerStats = $customer->getStats(); $addresses = $customer->getAddresses($defaultLanguage); $products = $customer->getBoughtProducts(); $discounts = Discount::getCustomerDiscounts($defaultLanguage, $customer->id, false, false); $orders = Order::getCustomerOrders($customer->id); $carts = Cart::getCustomerCarts($customer->id); $groups = $customer->getGroups(); $referrers = Referrer::getReferrers($customer->id); echo '
'.$customer->firstname.' '.$customer->lastname.'
'.$customer->email.'

'.$this->l('ID:').' '.sprintf('%06d', $customer->id).($customer->dni != NULL ? ' | '.$this->l('DNI:').' '.$customer->dni : '').'
'.$this->l('Registration date:').' '.Tools::displayDate($customer->date_add, intval($cookie->id_lang), true).'
'.$this->l('Last visit:').' '.($customerStats['last_visit'] ? Tools::displayDate($customerStats['last_visit'], intval($cookie->id_lang), true) : $this->l('never')).'
'.$this->l('Newsletter:').' '.($customer->newsletter ? '' : '').'
'.$this->l('Opt-in:').' '.($customer->optin ? '' : '').'
'.$this->l('Age:').' '.$customerStats['age'].' '.((!empty($customer->birthday['age'])) ? '('.Tools::displayDate($customer->birthday, intval($cookie->id_lang)).')' : $this->l('unknown')).'

'.$this->l('Last update:').' '.Tools::displayDate($customer->date_upd, intval($cookie->id_lang), true).'
'.$this->l('Status:').' '.($customer->active ? '' : '').'
 
'; // display hook specified to this page : AdminCustomers if (($hook = Module::hookExec('adminCustomers', array('id_customer' => $customer->id))) !== false) echo '
'.$hook.'
'; echo '

'.$this->l('Groups').' ('.sizeof($groups).')

'; if ($groups AND sizeof($groups)) { echo ' '; $tokenGroups = Tools::getAdminToken('AdminGroups'.intval(Tab::getIdFromClassName('AdminGroups')).intval($cookie->id_employee)); foreach ($groups AS $group) { $objGroup = new Group($group); echo ' '; } echo '
'.$this->l('ID').' '.$this->l('Name').' '.$this->l('Actions').'
'.$objGroup->id.' '.$objGroup->name[$defaultLanguage].'
'; } echo '
 
'; echo '

'.$this->l('Orders').' ('.sizeof($orders).')

'; if ($orders AND sizeof($orders)) { echo ' '; $tokenOrders = Tools::getAdminToken('AdminOrders'.intval(Tab::getIdFromClassName('AdminOrders')).intval($cookie->id_employee)); foreach ($orders AS $order) echo ' '; echo '
'.$this->l('ID').' '.$this->l('Date').' '.$this->l('Quantity').' '.$this->l('Total').' '.$this->l('Payment').' '.$this->l('State').' '.$this->l('Actions').'
'.sprintf('%06d', $order['id_order']).' '.Tools::displayDate($order['date_add'], intval($cookie->id_lang), true).' '.$order['nb_products'].' '.Tools::displayPrice($order['total_paid'], new Currency(intval($order['id_currency']))).' '.$order['payment'].' '.$order['order_state'].'
'; } else echo $customer->firstname.' '.$customer->lastname.' '.$this->l('has placed no orders yet'); if ($products AND sizeof($products)) { echo '
 

'.$this->l('Products').' ('.sizeof($products).')

'; $tokenOrders = Tools::getAdminToken('AdminOrders'.intval(Tab::getIdFromClassName('AdminOrders')).intval($cookie->id_employee)); foreach ($products AS $product) echo ' '; echo '
'.$this->l('Date').' '.$this->l('Name').' '.$this->l('Quantity').' '.$this->l('Actions').'
'.Tools::displayDate($product['date_add'], intval($cookie->id_lang), true).' '.$product['product_name'].' '.$product['product_quantity'].'
'; } echo '
 

'.$this->l('Addresses').' ('.sizeof($addresses).')

'; if (sizeof($addresses)) { echo ' '; $tokenAddresses = Tools::getAdminToken('AdminAddresses'.intval(Tab::getIdFromClassName('AdminAddresses')).intval($cookie->id_employee)); foreach ($addresses AS $address) echo ' '; echo '
'.$this->l('Company').' '.$this->l('Name').' '.$this->l('Address').' '.$this->l('Country').' '.$this->l('Phone number(s)').' '.$this->l('Actions').'
'.($address['company'] ? $address['company'] : '--').' '.$address['firstname'].' '.$address['lastname'].' '.$address['address1'].($address['address2'] ? ' '.$address['address2'] : '').' '.$address['postcode'].' '.$address['city'].' '.$address['country'].' '.($address['phone'] ? ($address['phone'].($address['phone_mobile'] ? '
'.$address['phone_mobile'] : '')) : ($address['phone_mobile'] ? '
'.$address['phone_mobile'] : '--')).'
'; } else echo $customer->firstname.' '.$customer->lastname.' '.$this->l('has registered no addresses yet').'.'; echo '
 

'.$this->l('Discounts').' ('.sizeof($discounts).')

'; if (sizeof($discounts)) { echo ' '; $tokenDiscounts = Tools::getAdminToken('AdminDiscounts'.intval(Tab::getIdFromClassName('AdminDiscounts')).intval($cookie->id_employee)); foreach ($discounts AS $discount) { echo ' '; } echo '
'.$this->l('ID').' '.$this->l('Code').' '.$this->l('Type').' '.$this->l('Value').' '.$this->l('Qty available').' '.$this->l('Status').' '.$this->l('Actions').'
'.$discount['id_discount'].' '.$discount['name'].' '.$discount['type'].' '.$discount['value'].' '.$discount['quantity_for_user'].' '.$this->l('Status').'
'; } else echo $customer->firstname.' '.$customer->lastname.' '.$this->l('has no discount vouchers').'.'; echo '
 
'; echo '

'.$this->l('Carts').' ('.sizeof($carts).')

'; if ($carts AND sizeof($carts)) { echo ' '; $tokenCarts = Tools::getAdminToken('AdminCarts'.intval(Tab::getIdFromClassName('AdminCarts')).intval($cookie->id_employee)); foreach ($carts AS $cart) { $cartI = new Cart(intval($cart['id_cart'])); $summary = $cartI->getSummaryDetails(); $currency = new Currency(intval($cart['id_currency'])); $carrier = new Carrier(intval($cart['id_carrier'])); echo ' '; } echo '
'.$this->l('ID').' '.$this->l('Date').' '.$this->l('Total').' '.$this->l('Carrier').' '.$this->l('Actions').'
'.sprintf('%06d', $cart['id_cart']).' '.Tools::displayDate($cart['date_add'], intval($cookie->id_lang), true).' '.Tools::displayPrice($summary['total_price'], $currency).' '.$carrier->name.'
'; } else echo $this->l('No cart available').'.'; echo '
 
'; /* Last connections */ $connections = $customer->getLastConnections(); if (sizeof($connections)) { echo '

'.$this->l('Last connections').'

'; foreach ($connections as $connection) echo ''; echo '
'.$this->l('Date').' '.$this->l('Pages viewed').' '.$this->l('Total time').' '.$this->l('Origin').' '.$this->l('IP Address').'
'.Tools::displayDate($connection['date_add'], intval($cookie->id_lang), true).' '.intval($connection['pages']).' '.$connection['time'].' '.($connection['http_referer'] ? preg_replace('/^www./', '', parse_url($connection['http_referer'], PHP_URL_HOST)) : $this->l('Direct link')).' '.$connection['ipaddress'].'
 
'; } if (sizeof($referrers)) { echo '

'.$this->l('Referrers').'

'; foreach ($referrers as $referrer) echo ''; echo '
'.$this->l('Date').' '.$this->l('Name').'
'.Tools::displayDate($referrer['date_add'], intval($cookie->id_lang), true).' '.$referrer['name'].'
 
'; } echo ' '.$this->l('Back to customer list').'
'; } public function displayForm($isMainTab = true) { global $currentIndex; parent::displayForm(); $obj = $this->loadObject(true); if ($obj->id) $need_identifcation_number = $obj->getNeedDNI(); $birthday = explode('-', $this->getFieldValue($obj, 'birthday')); $customer_groups = Tools::getValue('groupBox', $obj->getGroups()); $groups = Group::getGroups($this->_defaultFormLanguage, true); echo '
'.($obj->id ? '' : '').'
'.$this->l('Customer').'
getFieldValue($obj, 'id_gender') == 1 ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'id_gender') == 2 ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'id_gender') == 9 OR !$this->getFieldValue($obj, 'id_gender')) ? 'checked="checked" ' : '').'/>
* '.$this->l('Invalid characters:').' 0-9!<>,;?=+()@#"�{}_$%: 
* '.$this->l('Forbidden characters:').' 0-9!<>,;?=+()@#"�{}_$%: 
'.(!$obj->id ? '*' : '').'

'.($obj->id ? $this->l('Leave blank if no change') : $this->l('5 characters min., only letters, numbers, or').' -_').'

*
'; $sl_year = ($this->getFieldValue($obj, 'birthday')) ? $birthday[0] : 0; $years = Tools::dateYears(); $sl_month = ($this->getFieldValue($obj, 'birthday')) ? $birthday[1] : 0; $months = Tools::dateMonths(); $sl_day = ($this->getFieldValue($obj, 'birthday')) ? $birthday[2] : 0; $days = Tools::dateDays(); $tab_months = array( $this->l('January'), $this->l('February'), $this->l('March'), $this->l('April'), $this->l('May'), $this->l('June'), $this->l('July'), $this->l('August'), $this->l('September'), $this->l('October'), $this->l('November'), $this->l('December')); echo '
'; if (isset($need_identifcation_number) AND $need_identifcation_number) echo '

'.$this->l('DNI / NIF / NIE').'

'; echo '
getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/>

'.$this->l('Allow or disallow this customer to log in').'

getFieldValue($obj, 'newsletter') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'newsletter') ? 'checked="checked" ' : '').'/>

'.$this->l('Customer will receive your newsletter via e-mail').'

getFieldValue($obj, 'optin') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'optin') ? 'checked="checked" ' : '').'/>

'.$this->l('Customer will receive your ads via e-mail').'

'.$this->l('The group from which apply none cumulative rules (e.g., price display method, reduction)').'

'; if (sizeof($groups)) { echo ' '; $irow = 0; foreach ($groups as $group) { echo ' '; } echo '
'.$this->l('ID').' '.$this->l('Group name').'
'.' '.$group['id_group'].'

'.$this->l('Mark all checkbox(es) of groups to which the customer is to be member').' *

'; } else echo '

'.$this->l('No group created').'

'; echo '
* '.$this->l('Required field').'
'; } public function getList($id_lang, $orderBy = NULL, $orderWay = NULL, $start = 0, $limit = NULL) { global $cookie; return parent::getList(intval($cookie->id_lang), !Tools::getValue($this->table.'Orderby') ? 'date_add' : NULL, !Tools::getValue($this->table.'Orderway') ? 'DESC' : NULL); } public function beforeDelete($object) { return $object->isUsed(); } } ?>