diff --git a/admin-dev/tabs/AdminCustomers.php b/admin-dev/tabs/AdminCustomers.php deleted file mode 100644 index 2ef20ef63..000000000 --- a/admin-dev/tabs/AdminCustomers.php +++ /dev/null @@ -1,884 +0,0 @@ - -* @copyright 2007-2011 PrestaShop SA -* @version Release: $Revision: 7320 $ -* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) -* International Registered Trademark & Property of PrestaShop SA -*/ - -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->requiredDatabase = true; - - $this->_select = '(YEAR(CURRENT_DATE)-YEAR(`birthday`)) - (RIGHT(CURRENT_DATE, 5) 'unknown.gif'); - $genders = array(0 => $this->l('?')); - foreach (Gender::getGenders() as $gender) - { - $genders_icon[$gender->id] = '../genders/'.$gender->id.'.jpg'; - $genders[$gender->id] = $gender->name; - } - - $this->fieldsDisplay = array( - 'id_customer' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), - 'id_gender' => array( - 'title' => $this->l('Gender'), - 'width' => 30, - 'align' => 'center', - 'icon' => $genders_icon, - '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', 'callback' => 'printNewsIcon', 'orderby' => false), - 'optin' => array('title' => $this->l('Opt.'), 'width' => 25, 'align' => 'center', 'type' => 'bool', 'callback' => 'printOptinIcon', 'orderby' => false), - 'date_add' => array('title' => $this->l('Registration'), 'width' => 30, 'type' => 'date', 'align' => 'right'), - 'connect' => array('title' => $this->l('Connection'), 'width' => 60, 'type' => 'datetime', 'search' => false), - ); - - $this->shopLinkType = 'shop'; - $this->shopShareDatas = Shop::SHARE_CUSTOMER; - - $this->optionsList = array( - 'general' => array( - 'title' => $this->l('Customers options'), - 'fields' => array( - 'PS_PASSWD_TIME_FRONT' => array('title' => $this->l('Regenerate password:'), 'desc' => $this->l('Security minimum time to wait to regenerate the password'),'validation' => 'isUnsignedInt', 'cast' => 'intval', 'size' => 5, 'type' => 'text', 'suffix' => ' '.$this->l('minutes')) - ), - ), - ); - - parent::__construct(); - } - - public function postProcess() - { - if (Tools::isSubmit('submitDel'.$this->table) OR Tools::isSubmit('delete'.$this->table)) - { - $deleteForm = ' -
-
'.$this->l('How do you want to delete your customer(s)?').' - '.$this->l('You have two ways to delete a customer, please choose what you want to do.').' -

- - -

-

- - -

'; - foreach ($_POST as $key => $value) - if (is_array($value)) - foreach ($value as $val) - $deleteForm .= ''; - else - $deleteForm .= ''; - $deleteForm .= '
-
-
-
 
'; - } - - if (Tools::getValue('submitAdd'.$this->table)) - { - $groupList = Tools::getValue('groupBox'); - - /* Checking fields validity */ - $this->validateRules(); - if (!sizeof($this->_errors)) - { - $id = (int)(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 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 (!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 in 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); - $shop = new Shop((int)$object->id_shop); - $object->id_group_shop = (int)$shop->id_group_shop; - if (!$object->add()) - $this->_errors[] = Tools::displayError('An error occurred while creating object.').' '.$this->table.' ('.Db::getInstance()->getMsgError().')'; - 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, true); - $parent_id = (int)(Tools::getValue('id_parent', 1)); - // Save and stay on same form - if (Tools::isSubmit('submitAdd'.$this->table.'AndStay')) - Tools::redirectAdmin(self::$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(self::$currentIndex.'&'.$this->identifier.'='.$parent_id.'&conf=3&token='.$this->token); - // Default behavior (save and back) - Tools::redirectAdmin(self::$currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$this->token); - } - } - else - $this->_errors[] = Tools::displayError('You do not have permission to add here.'); - } - } - } - elseif (Tools::isSubmit('delete'.$this->table) AND $this->tabAccess['delete'] === '1') - { - switch (Tools::getValue('deleteMode')) - { - case 'real': - $this->deleted = false; - Discount::deleteByIdCustomer((int)(Tools::getValue('id_customer'))); - break; - case 'deleted': - $this->deleted = true; - break; - default: - echo $deleteForm; - if (isset($_POST['delete'.$this->table])) - unset($_POST['delete'.$this->table]); - if (isset($_GET['delete'.$this->table])) - unset($_GET['delete'.$this->table]); - break; - } - } - elseif (Tools::isSubmit('submitDel'.$this->table) AND $this->tabAccess['delete'] === '1') - { - switch (Tools::getValue('deleteMode')) - { - case 'real': - $this->deleted = false; - foreach (Tools::getValue('customerBox') as $id_customer) - Discount::deleteByIdCustomer((int)($id_customer)); - break; - case 'deleted': - $this->deleted = true; - break; - default: - echo $deleteForm; - if (isset($_POST['submitDel'.$this->table])) - unset($_POST['submitDel'.$this->table]); - if (isset($_GET['submitDel'.$this->table])) - unset($_GET['submitDel'.$this->table]); - break; - } - } - elseif (Tools::isSubmit('submitGuestToCustomer') AND Tools::getValue('id_customer')) - { - if ($this->tabAccess['edit'] === '1') - { - $customer = new Customer((int)Tools::getValue('id_customer')); - if (!Validate::isLoadedObject($customer)) - $this->_errors[] = Tools::displayError('This customer does not exist.'); - if(Customer::customerExists($customer->email)) - $this->_errors[] = Tools::displayError('This customer already exist as non-guest.'); - elseif ($customer->transformToCustomer(Tools::getValue('id_lang', Configuration::get('PS_LANG_DEFAULT')))) - Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$customer->id.'&conf=3&token='.$this->token); - else - $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); - } - else - $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); - }elseif (Tools::isSubmit('changeNewsletterVal') AND Tools::getValue('id_customer')) - { - $id_customer = (int)Tools::getValue('id_customer'); - $customer = new Customer($id_customer); - if (!Validate::isLoadedObject($customer)) - $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); - $update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET newsletter = '.($customer->newsletter ? 0 : 1).' WHERE `id_customer` = '.(int)($customer->id)); - if (!$update) - $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); - Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); - - }elseif (Tools::isSubmit('changeOptinVal') AND Tools::getValue('id_customer')) - { - $id_customer = (int)Tools::getValue('id_customer'); - $customer = new Customer($id_customer); - if (!Validate::isLoadedObject($customer)) - $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); - $update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET optin = '.($customer->optin ? 0 : 1).' WHERE `id_customer` = '.(int)($customer->id)); - if (!$update) - $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); - Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); - } - - return parent::postProcess(); - } - - public function viewcustomer() - { - $irow = 0; - $defaultLanguage = $this->context->language->id; - if (!($customer = $this->loadObject())) - return; - $customerStats = $customer->getStats(); - $addresses = $customer->getAddresses($defaultLanguage); - $products = $customer->getBoughtProducts(); - $discounts = Discount::getCustomerDiscounts($defaultLanguage, $customer->id, false, false); - $orders = Order::getCustomerOrders($customer->id, true); - $carts = Cart::getCustomerCarts($customer->id); - $groups = $customer->getGroups(); - $messages = CustomerThread::getCustomerMessages((int)$customer->id); - $referrers = Referrer::getReferrers($customer->id); - if ($totalCustomer = Db::getInstance()->getValue('SELECT SUM(total_paid_real) FROM '._DB_PREFIX_.'orders WHERE id_customer = '.(int)$customer->id.' AND valid = 1')) - { - Db::getInstance()->getValue('SELECT SQL_CALC_FOUND_ROWS COUNT(*) FROM '._DB_PREFIX_.'orders WHERE valid = 1 GROUP BY id_customer HAVING SUM(total_paid_real) > '.(int)$totalCustomer); - $countBetterCustomers = (int)Db::getInstance()->getValue('SELECT FOUND_ROWS()') + 1; - } - else - $countBetterCustomers = '-'; - - $gender = new Gender($customer->id_gender); - - echo ' -
- '.$customer->firstname.' '.$customer->lastname.' -
- '.$customer->email.'

- '.$this->l('ID:').' '.sprintf('%06d', $customer->id).'
- '.$this->l('Registration date:').' '.Tools::displayDate($customer->date_add, $this->context->language->id, true).'
- '.$this->l('Last visit:').' '.($customerStats['last_visit'] ? Tools::displayDate($customerStats['last_visit'], $this->context->language->id, true) : $this->l('never')).'
- '.($countBetterCustomers != '-' ? $this->l('Rank: #').' '.(int)$countBetterCustomers.'
' : '') - .(Shop::isFeatureActive() ? '
'.$this->l('Shop:').' '.Shop::getInstance($customer->id_shop)->name : '').' -
-
-
- -
- '.$this->l('Newsletter:').' '.($customer->newsletter ? '' : '').'
- '.$this->l('Opt-in:').' '.($customer->optin ? '' : '').'
- '.$this->l('Age:').' '.$customerStats['age'].' '.((!empty($customer->birthday['age'])) ? '('.Tools::displayDate($customer->birthday, $this->context->language->id).')' : $this->l('unknown')).'

- '.$this->l('Last update:').' '.Tools::displayDate($customer->date_upd, $this->context->language->id, true).'
- '.$this->l('Status:').' '.($customer->active ? '' : ''); - if ($customer->isGuest()) - { - echo ' -
- '.$this->l('This customer is registered as').' '.$this->l('guest').''; - if(!Customer::customerExists($customer->email)) - { - echo ' -
- -

- '.$this->l('This feature generates a random password and sends an e-mail to the customer').' -
'; - } - else - echo '
'.$this->l('A registered customer account exists with the same email address').''; - echo ' -
- '; - } - echo ' -
-
 
'; - - echo '
'.$this->l('Add a private note').' -

'.$this->l('This note will be displayed to all the employees but not to the customer.').'

-
-
- - -
-
-
 
- '; - - - echo '

'.$this->l('Messages').' ('.sizeof($messages).')

'; - if (sizeof($messages)) - { - echo ' - - - - - - '; - foreach ($messages AS $message) - echo ' - - - - '; - echo '
'.$this->l('Status').''.$this->l('Message').''.$this->l('Sent on').'
'.$message['status'].''.substr(strip_tags(html_entity_decode($message['message'], ENT_NOQUOTES, 'UTF-8')), 0, 75).'...'.Tools::displayDate($message['date_add'], $this->context->language->id, true).'
-
 
'; - } - else - echo $customer->firstname.' '.$customer->lastname.' '.$this->l('has never contacted you.'); - - // display hook specified to this page : AdminCustomers - if (($hook = Module::hookExec('adminCustomers', array('id_customer' => $customer->id))) !== false) - echo '
'.$hook.'
'; - echo '
 
'; - - echo '

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

'; - if ($groups AND sizeof($groups)) - { - echo ' - - - - - - '; - $tokenGroups = Tools::getAdminToken('AdminGroups'.(int)(Tab::getIdFromClassName('AdminGroups')).(int)$this->context->employee->id); - 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)) - { - $totalOK = 0; - $ordersOK = array(); - $ordersKO = array(); - $tokenOrders = Tools::getAdminToken('AdminOrders'.(int)Tab::getIdFromClassName('AdminOrders').(int)$this->context->employee->id); - foreach ($orders AS $order) - if ($order['valid']) - { - $ordersOK[] = $order; - $totalOK += $order['total_paid_real']; - } - else - $ordersKO[] = $order; - $orderHead = ' - - - - - - - - - - '; - $orderFoot = '
'.$this->l('ID').''.$this->l('Date').''.$this->l('Products').''.$this->l('Total paid').''.$this->l('Payment').''.$this->l('State').''.$this->l('Actions').'
'; - if ($countOK = sizeof($ordersOK)) - { - echo '

'.$this->l('Valid orders:').' '.$countOK.' '.$this->l('for').' '.Tools::displayPrice($totalOK, $this->context->currency->id).'

'.$orderHead; - foreach ($ordersOK AS $order) - echo ' - '.$order['id_order'].' - '.Tools::displayDate($order['date_add'], $this->context->language->id).' - '.$order['nb_products'].' - '.Tools::displayPrice($order['total_paid_real'], new Currency((int)($order['id_currency']))).' - '.$order['payment'].' - '.$order['order_state'].' - - '; - echo $orderFoot.'
'; - } - if ($countKO = sizeof($ordersKO)) - { - echo '

'.$this->l('Invalid orders:').' '.$countKO.'

'.$orderHead; - foreach ($ordersKO AS $order) - echo ' - - '.$order['id_order'].' - '.Tools::displayDate($order['date_add'], $this->context->language->id).' - '.$order['nb_products'].' - '.Tools::displayPrice($order['total_paid_real'], new Currency((int)($order['id_currency']))).' - '.$order['payment'].' - '.$order['order_state'].' - - '; - echo $orderFoot.'
 
'; - } - } - else - echo $customer->firstname.' '.$customer->lastname.' '.$this->l('has not placed any orders yet'); - - if ($products AND sizeof($products)) - { - echo '
 
-

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

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

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

'; - if (sizeof($addresses)) - { - echo ' - - - - - - - - - '; - $tokenAddresses = Tools::getAdminToken('AdminAddresses'.(int)(Tab::getIdFromClassName('AdminAddresses')).(int)$this->context->employee->id); - 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 not registered any addresses yet').'.'; - echo '
 
-

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

'; - if (sizeof($discounts)) - { - echo ' - - - - - - - - - - '; - $tokenDiscounts = Tools::getAdminToken('AdminDiscounts'.(int)(Tab::getIdFromClassName('AdminDiscounts')).(int)$this->context->employee->id); - 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'.(int)(Tab::getIdFromClassName('AdminCarts')).(int)$this->context->employee->id); - foreach ($carts AS $cart) - { - $cartI = new Cart((int)($cart['id_cart'])); - $summary = $cartI->getSummaryDetails(); - $currency = new Currency((int)($cart['id_currency'])); - $carrier = new Carrier((int)($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'], $this->context->language->id, true).''.Tools::displayPrice($summary['total_price'], $currency).''.$carrier->name.'
'; - } - else - echo $this->l('No cart available').'.'; - echo '
'; - - $sql = 'SELECT DISTINCT id_product, c.id_cart, c.id_shop, cp.id_shop AS cp_id_shop - FROM '._DB_PREFIX_.'cart_product cp - JOIN '._DB_PREFIX_.'cart c ON (c.id_cart = cp.id_cart) - WHERE c.id_customer = '.(int)$customer->id.' - AND cp.id_product NOT IN ( - SELECT product_id - FROM '._DB_PREFIX_.'orders o - JOIN '._DB_PREFIX_.'order_detail od ON (o.id_order = od.id_order) - WHERE o.valid = 1 AND o.id_customer = '.(int)$customer->id.' - )'; - $interested = Db::getInstance()->executeS($sql); - if (count($interested)) - { - echo '
-

'.$this->l('Products').' ('.count($interested).')

- '; - foreach ($interested as $p) - { - $product = new Product($p['id_product'], false, $this->context->language->id, $p['id_shop']); - $url = $this->context->link->getProductLink($product->id, $product->link_rewrite, Category::getLinkRewrite($product->id_category_default, $this->context->language->id), null, null, $p['cp_id_shop']); - echo ' - - - - - '; - } - echo '
'.(int)$product->id.''.Tools::htmlentitiesUTF8($product->name).'
'; - } - - 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'], $this->context->language->id, true).''.(int)($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').'

- - - - - '.((Shop::isFeatureActive())? '' : '').' - '; - foreach ($referrers as $referrer) - echo ' - - - '.((Shop::isFeatureActive())? '' : '').' - '; - echo '
'.$this->l('Date').''.$this->l('Name').''.$this->l('Shop').'
'.Tools::displayDate($referrer['date_add'], $this->context->language->id, true).''.$referrer['name'].''.$referrer['shop_name'].'
 
'; - } - echo ' '.$this->l('Back to customer list').'
'; - } - - public function displayForm($isMainTab = true) - { - parent::displayForm(); - - if (!($obj = $this->loadObject(true))) - return; - - $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').' - -
'; - foreach (Gender::getGenders() as $gender) - { - echo 'getFieldValue($obj, 'id_gender') == $gender->id ? 'checked="checked" ' : '').'/>'; - echo '   '; - } - echo ' getFieldValue($obj, 'id_gender') == 9 || !$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 ' -
- - - -
'; - 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 will be as applied by default.').'
-
- -
'; - if (sizeof($groups)) - { - echo ' - - - - - - '; - $irow = 0; - foreach ($groups as $group) - { - echo ' - - - - - '; - } - echo ' -
'.$this->l('ID').''.$this->l('Group name').'
'.''.$group['id_group'].'
-

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

- '; - } else - echo '

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

'; - echo ' -
'; - if (Shop::isFeatureActive()) - { - $shops = Shop::getShops(); - echo ' - -
- '; - } - echo ' -
- -
-
* '.$this->l('Required field').'
-
-
'; - } - - public function getList($id_lang, $orderBy = NULL, $orderWay = NULL, $start = 0, $limit = NULL, $id_lang_shop = NULL) - { - return parent::getList(Context::getContext()->language->id, !Tools::getValue($this->table.'Orderby') ? 'date_add' : NULL, !Tools::getValue($this->table.'Orderway') ? 'DESC' : NULL); - } - - public function beforeDelete($object) - { - return $object->isUsed(); - } -} - - diff --git a/admin-dev/themes/template/customers/form.tpl b/admin-dev/themes/template/customers/form.tpl new file mode 100644 index 000000000..4a1dc4f6c --- /dev/null +++ b/admin-dev/themes/template/customers/form.tpl @@ -0,0 +1,300 @@ +{* +* 2007-2011 PrestaShop +* +* NOTICE OF LICENSE +* +* This source file is subject to the Academic Free License (AFL 3.0) +* that is bundled with this package in the file LICENSE.txt. +* It is also available through the world-wide-web at this URL: +* http://opensource.org/licenses/afl-3.0.php +* If you did not receive a copy of the license and are unable to +* obtain it through the world-wide-web, please send an email +* to license@prestashop.com so we can send you a copy immediately. +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade PrestaShop to newer +* versions in the future. If you wish to customize PrestaShop for your +* needs please refer to http://www.prestashop.com for more information. +* +* @author PrestaShop SA +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} + +{if $firstCall} + + +{/if} +{if isset($fields.title)}

{$fields.title}

{/if} +
+ {if $form_id} + + {/if} +
+ {foreach $fields as $key => $field} + {if $key == 'legend'} + + {if isset($field.image)}{$field.title}{/if} + {$field.title} + + {elseif $key == 'input'} + {foreach $field as $input} + {if $input.name == 'id_state'} +
+ {/if} + {if isset($input.label)} + + {/if} + {if $input.type == 'hidden'} + + {else} +
+ {if $input.type == 'text'} + {if isset($input.lang) && isset($input.attributeLang)} + {foreach $languages as $language} +
+ + {if isset($input.hint)}{$input.hint} {/if} +
+ {/foreach} + {if count($languages) > 1} +
+ +
+
+ {l s='Choose language:'}

+ {foreach $languages as $language} + {$language.name} + {/foreach} +
+ {/if} + {else} + + {if isset($input.hint)}{$input.hint} {/if} + {/if} + {elseif $input.type == 'select'} + {if isset($input.options.query) && !$input.options.query && isset($input.empty_message)} + {$input.empty_message} + {$input.required = false} + {$input.p = null} + {else} + + {if isset($input.hint)}{$input.hint} {/if} + {/if} + {elseif $input.type == 'radio'} + {foreach $input.values as $value} + + + {if isset($input.br) && $input.br}
{/if} + {/foreach} + {elseif $input.type == 'textarea'} + {if isset($input.lang) && isset($input.attributeLang)} + {foreach $languages as $language} +
+ +
+ {/foreach} + {if count($languages) > 1} +
+ +
+
+ {l s='Choose language:'}

+ {foreach $languages as $language} + {$language.name} + {/foreach} +
+ {/if} + {else} + + {/if} + {elseif $input.type == 'checkbox'} + {foreach $input.values.query as $value} + {assign var=id_checkbox value=$input.name|cat:'_'|cat:$value[$input.values.id]} + +
+ {/foreach} + {elseif $input.type == 'file'} + + + {elseif $input.type == 'password'} + + {elseif $input.type == 'birthday'} + {foreach $input.options as $key => $select} + + {/foreach} + {elseif $input.type == 'group'} + {assign var=groups value=$input.values} + {include file='form_group.tpl'} + {elseif $input.type == 'shop' OR $input.type == 'group_shop'} + {include file='form_shop.tpl'} + {elseif $input.type == 'asso_shop' && isset($asso_shop) && $asso_shop} + +
+ {$asso_shop} +
+ {/if} + {if isset($input.required) && $input.required} *{/if} + {if isset($input.p)} +

+ {if is_array($input.p)} + {foreach $input.p as $p} + {if is_array($p)} + {$p.text}
+ {else} + {$p}
+ {/if} + {/foreach} + {else} + {$input.p} + {/if} +

+ {/if} + {if isset($languages)}
{/if} +
+ {/if} + {if $input.name == 'id_state'} +
+ {/if} + {/foreach} + {elseif $key == 'submit'} +
+ +
+ {/if} + {/foreach} + {if $required_fields} +
* {l s ='Required field'}
+ {/if} +
+
+ +

+{if $firstCall && !$no_back} + {if $back} + {l s='Back'} + {else} + {l s='Back to list'} + {/if} +
+{/if} diff --git a/admin-dev/themes/template/customers/view.tpl b/admin-dev/themes/template/customers/view.tpl new file mode 100644 index 000000000..6b4606fca --- /dev/null +++ b/admin-dev/themes/template/customers/view.tpl @@ -0,0 +1,411 @@ +{* +* 2007-2011 PrestaShop +* +* NOTICE OF LICENSE +* +* This source file is subject to the Academic Free License (AFL 3.0) +* that is bundled with this package in the file LICENSE.txt. +* It is also available through the world-wide-web at this URL: +* http://opensource.org/licenses/afl-3.0.php +* If you did not receive a copy of the license and are unable to +* obtain it through the world-wide-web, please send an email +* to license@prestashop.com so we can send you a copy immediately. +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade PrestaShop to newer +* versions in the future. If you wish to customize PrestaShop for your +* needs please refer to http://www.prestashop.com for more information. +* +* @author PrestaShop SA +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} + + +
+
+ + + +
+ + {$customer->firstname} {$customer->lastname} + +
+ {$customer->email} +

+ {l s='ID:'} {$customer->id|string_format:"%06d"}
+ {l s='Registration date:'} {$registration_date}
+ {l s='Last visit:'} {if $customer_stats['last_visit']}{$last_visit}{else}{l s='never'}{/if}
+ {if $count_better_customers != '-'}{l s='Rank: #'} {$count_better_customers}
{/if} + {if $shop_is_feature_active}{l s='Shop:'} {$name_shop}
{/if} +
+ +
+
+ + + +
+ {l s='Newsletter:'} {if $customer->newsletter}{else}{/if}
+ {l s='Opt-in:'} {if $customer->optin}{else}{/if}
+ {l s='Age:'} {$customer_stats['age']} {if isset($customer->birthday['age'])}({$customer_birthday}){else}{l s='unknown'}{/if}

+ {l s='Last update:'} {$last_update}
+ {l s='Status:'} {if $customer->active}{else}{/if} + + {if $customer->isGuest()} +
+ {l s='This customer is registered as'} {l s='guest'} + {if !$customer_exists} +
+ +

+ {l s='This feature generates a random password and sends an e-mail to the customer'} +
+ {else} +
{l s='A registered customer account exists with the same email address'} + {/if} +
+ {/if} +
+ +
 
+ +
+ + {l s='Add a private note'} + +

{l s='This note will be displayed to all the employees but not to the customer.'}

+
+
+ + +
+
+ +
 
+ + +

{l s='Messages'} ({count($messages)})

+{if count($messages)} + + + + + + + {foreach $messages AS $message} + + + + + + {/foreach} +
{l s='Status'}{l s='Message'}{l s='Sent on'}
{$message['status']} + + {$message['message']}... + + {$message['date_add']}
+
 
+{else} + {$customer->firstname} {$customer->lastname} {l s='has never contacted you'} +{/if} + +{* display hook specified to this page : AdminCustomers *} +{if $hook !== false} +
{$hook}
+{/if} + +
 
+ +

{l s='Groups'} ({count($groups)})

+{if $groups AND count($groups)} + + + + + + + {foreach $groups AS $key => $group} + + + + + + {/foreach} +
{l s='ID'}{l s='Name'}{l s='Actions'}
{$group['id_group']}{$group['name']}
+{/if} +
 
+ + +

{l s='Orders'} ({count($orders)})

+{if $orders AND count($orders)} + {assign var=count_ok value=count($orders_ok)} + {if $count_ok} +
+

+ {l s='Valid orders:'} {$count_ok} {l s='for'} {$total_ok} +

+ + + + + + + + + + + {foreach $orders_ok AS $key => $order} + + + + + + + + + + {/foreach} +
{l s='ID'}{l s='Date'}{l s='Products'}{l s='Total paid'}{l s='Payment'}{l s='State'}{l s='Actions'}
{$order['id_order']}{$order['date_add']}{$order['nb_products']}{$order['total_paid_real']}{$order['payment']}{$order['order_state']}
+
+ {/if} + {assign var=count_ko value=count($orders_ko)} + {if $count_ko} +
+

{l s='Invalid orders:'} {$count_ko}

+ + + + + + + + + + + {foreach $orders_ko AS $key => $order} + + + + + + + + + + {/foreach} +
{l s='ID'}{l s='Date'}{l s='Products'}{l s='Total paid'}{l s='Payment'}{l s='State'}{l s='Actions'}
{$order['id_order']}{$order['date_add']}{$order['nb_products']}{$order['total_paid_real']}{$order['payment']}{$order['order_state']}
+
+
 
+ {/if} +{else} + {$customer->firstname} {$customer->lastname} {l s='has not placed any orders yet'} +{/if} + +{if $products AND count($products)} +
 
+

{l s='Products'} ({count($products)})

+ + + + + + + + {foreach $products AS $key => $product} + + + + + + + {/foreach} +
{l s='Date'}{l s='Name'}{l s='Quantity'}{l s='Actions'}
{$product['date_add']}{$product['product_name']}{$product['product_quantity']}
+{/if} +
 
+ +

{l s='Addresses'} ({count($addresses)})

+{if count($addresses)} + + + + + + + + + + {foreach $addresses AS $key => $address} + + + + + + + + + {/foreach} +
{l s='Company'}{l s='Name'}{l s='Address'}{l s='Country'}{l s='Phone number(s)'}{l s='Actions'}
{if $address['company']}{$address['company']}{else}--{/if}{$address['firstname']} {$address['lastname']}{$address['address1']} {if $address['address2']}{$address['address2']}{/if} {$address['postcode']} {$address['city']}{$address['country']} + {if $address['phone']} + {$address['phone']} + {if $address['phone_mobile']}
{$address['phone_mobile']}{/if} + {else} + {if $address['phone_mobile']}
{$address['phone_mobile']}{else}--{/if} + {/if} +
+ + +
+{else} + {$customer->firstname} {$customer->lastname} {l s='has not registered any addresses yet'} +{/if} + +
 
+

{l s='Discounts'} ({count($discounts)})

+{if count($discounts)} + + + + + + + + + + + {foreach $discounts AS $key => $discount} + + + + + + + + + + {/foreach} +
{l s='ID'}{l s='Code'}{l s='Type'}{l s='Value'}{l s='Qty available'}{l s='Status'}{l s='Actions'}
{$discount['id_discount']}{$discount['name']}{$discount['type']}{$discount['value']}{$discount['quantity_for_user']}{l s='Status'} + + +
+{else} + {$customer->firstname} {$customer->lastname} {l s='has no discount vouchers'}. +{/if} +
 
+ +
+

{l s='Carts'} ({count($carts)})

+ {if $carts AND count($carts)} + + + + + + + + + {foreach $carts AS $key => $cart} + + + + + + + + {/foreach} +
{l s='ID'}{l s='Date'}{l s='Total'}{l s='Carrier'}{l s='Actions'}
{$cart['id_cart']}{$cart['date_add']}{$cart['total_price']}{$cart['name']}
+ {else} + {l s='No cart available'}. + {/if} +
+ +{if count($interested)} +
+

{l s='Products'} ({count($interested)})

+ + {foreach $interested as $key => $p} + + + + + + {/foreach} +
{$p['id']}{$p['name']}
+
+{/if} + +
 
+ +{* Last connections *} +{if count($connections)} +

{l s='Last connections'}

+ + + + + + + + + {foreach $connections as $connection} + + + + + + + + {/foreach} +
{l s='Date'}{l s='Pages viewed'}{l s='Total time'}{l s='Origin'}{l s='IP Address'}
{$connection['date_add']}{$connection['pages']}{$connection['time']}{$connection['http_referer']}{$connection['ipaddress']}
+
 
+{/if} + +{if count($referrers)} +

{l s='Referrers'}

+ + + + + {if $shop_is_feature_active}{/if} + + {foreach $referrers as $referrer} + + + + {if $shop_is_feature_active}{/if} + + {/foreach} +
{l s='Date'}{l s='Name'}{l s='Shop'}
{$referrer['date_add']}{$referrer['name']}{$referrer['shop_name']}
+
 
+{/if} + {l s='Back to customer list'}
\ No newline at end of file diff --git a/admin-dev/themes/template/list_content.tpl b/admin-dev/themes/template/list_content.tpl index 5b7128daf..95e6f8482 100644 --- a/admin-dev/themes/template/list_content.tpl +++ b/admin-dev/themes/template/list_content.tpl @@ -98,7 +98,7 @@ {/foreach} {if $shop_link_type} - {$tr.shop_short_name} + {if isset($tr.shop_short_name)}{$tr.shop_short_name}{else}{$tr.shop_name}{/if} {/if} {if $has_actions} diff --git a/classes/AdminController.php b/classes/AdminController.php index 0c382a4c8..62a0c4df8 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -1495,7 +1495,7 @@ class AdminControllerCore extends Controller $select_shop = ', shop.name as shop_name '; $join_shop = ' LEFT JOIN '._DB_PREFIX_.$this->shopLinkType.' shop ON a.id_'.$this->shopLinkType.' = shop.id_'.$this->shopLinkType; - $where_shop = $this->context->shop->sqlRestriction($this->shopShareDatas, 'a', $this->shopLinkType); + $where_shop = $this->context->shop->addSqlRestriction($this->shopShareDatas, 'a', $this->shopLinkType); } $assos = Shop::getAssoTables(); diff --git a/classes/Customer.php b/classes/Customer.php index f14e7922a..6d3f524e9 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -27,94 +27,108 @@ class CustomerCore extends ObjectModel { - public $id; + public $id; - public $id_shop; + public $id_shop; - public $id_group_shop; + public $id_group_shop; /** @var string Secure key */ - public $secure_key; + public $secure_key; /** @var string protected note */ - public $note; + public $note; /** @var integer Gender ID */ - public $id_gender = 0; + public $id_gender = 0; /** @var integer Default group ID */ - public $id_default_group = _PS_DEFAULT_CUSTOMER_GROUP_; + public $id_default_group = _PS_DEFAULT_CUSTOMER_GROUP_; /** @var string Lastname */ - public $lastname; + public $lastname; /** @var string Firstname */ - public $firstname; + public $firstname; /** @var string Birthday (yyyy-mm-dd) */ - public $birthday = NULL; + public $birthday = null; /** @var string e-mail */ - public $email; + public $email; /** @var boolean Newsletter subscription */ - public $newsletter; + public $newsletter; /** @var string Newsletter ip registration */ - public $ip_registration_newsletter; + public $ip_registration_newsletter; /** @var string Newsletter ip registration */ - public $newsletter_date_add; + public $newsletter_date_add; /** @var boolean Opt-in subscription */ - public $optin; + public $optin; /** @var integer Password */ - public $passwd; + public $passwd; /** @var datetime Password */ - public $last_passwd_gen; + public $last_passwd_gen; /** @var boolean Status */ - public $active = true; + public $active = true; /** @var boolean Status */ - public $is_guest = 0; + public $is_guest = 0; /** @var boolean True if carrier has been deleted (staying in database as deleted) */ - public $deleted = 0; + public $deleted = 0; /** @var string Object creation date */ - public $date_add; + public $date_add; /** @var string Object last modification date */ - public $date_upd; + public $date_upd; - public $years; - public $days; - public $months; + public $years; + public $days; + public $months; /** @var int customer id_country as determined by geolocation */ - public $geoloc_id_country; + public $geoloc_id_country; /** @var int customer id_state as determined by geolocation */ - public $geoloc_id_state; + public $geoloc_id_state; /** @var string customer postcode as determined by geolocation */ - public $geoloc_postcode; + public $geoloc_postcode; /** @var boolean is the customer logged in */ - public $logged = 0; + public $logged = 0; /** @var int id_guest meaning the guest table, not the guest customer */ - public $id_guest; + public $id_guest; protected $tables = array ('customer'); - protected $fieldsRequired = array('lastname', 'passwd', 'firstname', 'email'); - protected $fieldsSize = array('lastname' => 32, 'passwd' => 32, 'firstname' => 32, 'email' => 128, 'note' => 65000); - protected $fieldsValidate = array('secure_key' => 'isMd5', 'lastname' => 'isName', 'firstname' => 'isName', 'email' => 'isEmail', 'passwd' => 'isPasswd', - 'id_gender' => 'isUnsignedId', 'birthday' => 'isBirthDate', 'newsletter' => 'isBool', 'optin' => 'isBool', 'active' => 'isBool', 'note' => 'isCleanHtml', 'is_guest' => 'isBool', 'id_shop' => 'isUnsignedId','id_group_shop' => 'isUnsignedId',); + protected $fieldsRequired = array('lastname', 'passwd', 'firstname', 'email'); + protected $fieldsSize = array('lastname' => 32, 'passwd' => 32, 'firstname' => 32, 'email' => 128, 'note' => 65000); + protected $fieldsValidate = array( + 'secure_key' => 'isMd5', + 'lastname' => 'isName', + 'firstname' => 'isName', + 'email' => 'isEmail', + 'passwd' => 'isPasswd', + 'id_gender' => 'isUnsignedId', + 'birthday' => 'isBirthDate', + 'newsletter' => 'isBool', + 'optin' => 'isBool', + 'active' => 'isBool', + 'note' => 'isCleanHtml', + 'is_guest' => 'isBool', + 'id_shop' => 'isUnsignedId', + 'id_group_shop' => 'isUnsignedId' + ); - protected $webserviceParameters = array( + protected $webserviceParameters = array( 'fields' => array( 'id_default_group' => array('xlink_resource' => 'groups'), 'newsletter_date_add' => array(), @@ -126,8 +140,8 @@ class CustomerCore extends ObjectModel ), ); - protected $table = 'customer'; - protected $identifier = 'id_customer'; + protected $table = 'customer'; + protected $identifier = 'id_customer'; protected static $_defaultGroupId = array(); protected static $_customerHasAddress = array(); @@ -137,69 +151,69 @@ class CustomerCore extends ObjectModel { $this->validateFields(); if (isset($this->id)) - $fields['id_customer'] = (int)($this->id); + $fields['id_customer'] = (int)$this->id; $fields['id_shop'] = (int)$this->id_shop; $fields['id_group_shop'] = (int)$this->id_group_shop; $fields['secure_key'] = pSQL($this->secure_key); $fields['note'] = pSQL($this->note, true); - $fields['id_gender'] = (int)($this->id_gender); - $fields['id_default_group'] = (int)($this->id_default_group); + $fields['id_gender'] = (int)$this->id_gender; + $fields['id_default_group'] = (int)$this->id_default_group; $fields['lastname'] = pSQL($this->lastname); $fields['firstname'] = pSQL($this->firstname); $fields['birthday'] = pSQL($this->birthday); $fields['email'] = pSQL($this->email); - $fields['newsletter'] = (int)($this->newsletter); + $fields['newsletter'] = (int)$this->newsletter; $fields['newsletter_date_add'] = pSQL($this->newsletter_date_add); $fields['ip_registration_newsletter'] = pSQL($this->ip_registration_newsletter); - $fields['optin'] = (int)($this->optin); + $fields['optin'] = (int)$this->optin; $fields['passwd'] = pSQL($this->passwd); $fields['last_passwd_gen'] = pSQL($this->last_passwd_gen); - $fields['active'] = (int)($this->active); + $fields['active'] = (int)$this->active; $fields['date_add'] = pSQL($this->date_add); $fields['date_upd'] = pSQL($this->date_upd); - $fields['is_guest'] = (int)($this->is_guest); - $fields['deleted'] = (int)($this->deleted); + $fields['is_guest'] = (int)$this->is_guest; + $fields['deleted'] = (int)$this->deleted; return $fields; } - public function add($autodate = true, $nullValues = true) + public function add($autodate = true, $null_values = true) { $this->id_shop = ($this->id_shop) ? $this->id_shop : Context::getContext()->shop->getID(); $this->id_group_shop = ($this->id_group_shop) ? $this->id_group_shop : Context::getContext()->shop->getGroupID(); - $this->birthday = (empty($this->years) ? $this->birthday : (int)($this->years).'-'.(int)($this->months).'-'.(int)($this->days)); + $this->birthday = (empty($this->years) ? $this->birthday : (int)$this->years.'-'.(int)$this->months.'-'.(int)$this->days); $this->secure_key = md5(uniqid(rand(), true)); $this->last_passwd_gen = date('Y-m-d H:i:s', strtotime('-'.Configuration::get('PS_PASSWD_TIME_FRONT').'minutes')); if (empty($this->id_default_group)) $this->id_default_group = 1; /* Can't create a guest customer, if this feature is disabled */ - if ($this->is_guest AND !Configuration::get('PS_GUEST_CHECKOUT_ENABLED')) + if ($this->is_guest && !Configuration::get('PS_GUEST_CHECKOUT_ENABLED')) return false; - if (!parent::add($autodate, $nullValues)) + if (!parent::add($autodate, $null_values)) return false; - $row = array('id_customer' => (int)($this->id), 'id_group' => (int)$this->id_default_group); + $row = array('id_customer' => (int)$this->id, 'id_group' => (int)$this->id_default_group); return Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT'); } - public function update($nullValues = false) + public function update() { $this->birthday = (empty($this->years) ? $this->birthday : (int)$this->years.'-'.(int)$this->months.'-'.(int)$this->days); - if ($this->newsletter AND !$this->newsletter_date_add) + if ($this->newsletter && !$this->newsletter_date_add) $this->newsletter_date_add = date('Y-m-d H:i:s'); return parent::update(true); } public function delete() { - $addresses = $this->getAddresses((int)(Configuration::get('PS_LANG_DEFAULT'))); - foreach ($addresses AS $address) + $addresses = $this->getAddresses((int)Configuration::get('PS_LANG_DEFAULT')); + foreach ($addresses as $address) { - $obj = new Address((int)($address['id_address'])); + $obj = new Address((int)$address['id_address']); $obj->delete(); } - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)($this->id)); - Discount::deleteByIdCustomer((int)($this->id)); + Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)$this->id); + Discount::deleteByIdCustomer((int)$this->id); return parent::delete(); } @@ -208,7 +222,7 @@ class CustomerCore extends ObjectModel * * @return array Customers */ - static public function getCustomers(Shop $shop = null) + public static function getCustomers(Shop $shop = null) { if (!$shop) $shop = Context::getContext()->shop; @@ -227,16 +241,16 @@ class CustomerCore extends ObjectModel * @param string $passwd Password is also checked if specified * @return Customer instance */ - public function getByEmail($email, $passwd = NULL, Shop $shop = null) + public function getByEmail($email, $passwd = null, Shop $shop = null) { - if (!Validate::isEmail($email) OR ($passwd AND !Validate::isPasswd($passwd))) + if (!Validate::isEmail($email) || ($passwd && !Validate::isPasswd($passwd))) die (Tools::displayError()); if (!$shop) $shop = Context::getContext()->shop; $sql = 'SELECT * - FROM `'._DB_PREFIX_ .'customer` + FROM `'._DB_PREFIX_.'customer` WHERE `active` = 1 AND `email` = \''.pSQL($email).'\' '.$shop->addSqlRestriction(Shop::SHARE_CUSTOMER).' @@ -248,7 +262,7 @@ class CustomerCore extends ObjectModel if (!$result) return false; $this->id = $result['id_customer']; - foreach ($result AS $key => $value) + foreach ($result as $key => $value) if (key_exists($key, $this)) $this->{$key} = $value; @@ -267,7 +281,7 @@ class CustomerCore extends ObjectModel $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT `id_customer` FROM `'._DB_PREFIX_.'customer` - WHERE `id_customer` = \''.(int)($id_customer).'\' + WHERE `id_customer` = \''.(int)$id_customer.'\' AND active = 1 AND `deleted` = 0'); if (isset($result['id_customer'])) @@ -280,10 +294,10 @@ class CustomerCore extends ObjectModel * * @param string $email e-mail * @param $return_id boolean - * @param $ignoreGuest boolean, to exclude guest customer + * @param $ignore_guest boolean, to exclude guest customer * @return Customer ID if found, false otherwise */ - static public function customerExists($email, $return_id = false, $ignoreGuest = true, Shop $shop = null) + public static function customerExists($email, $return_id = false, $ignore_guest = true, Shop $shop = null) { if (!Validate::isEmail($email)) die (Tools::displayError()); @@ -295,7 +309,7 @@ class CustomerCore extends ObjectModel FROM `'._DB_PREFIX_.'customer` WHERE `email` = \''.pSQL($email).'\' '.$shop->addSqlRestriction(Shop::SHARE_CUSTOMER). - ($ignoreGuest ? 'AND `is_guest` = 0' : ''); + ($ignore_guest ? 'AND `is_guest` = 0' : ''); $result = Db::getInstance()->getRow($sql); if ($return_id) @@ -317,8 +331,8 @@ class CustomerCore extends ObjectModel self::$_customerHasAddress[$id_customer] = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT `id_address` FROM `'._DB_PREFIX_.'address` - WHERE `id_customer` = '.(int)($id_customer).' - AND `id_address` = '.(int)($id_address).' + WHERE `id_customer` = '.(int)$id_customer.' + AND `id_address` = '.(int)$id_address.' AND `deleted` = 0'); } return self::$_customerHasAddress[$id_customer]; @@ -343,7 +357,7 @@ class CustomerCore extends ObjectModel LEFT JOIN `'._DB_PREFIX_.'country` c ON (a.`id_country` = c.`id_country`) LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country`) LEFT JOIN `'._DB_PREFIX_.'state` s ON (s.`id_state` = a.`id_state`) - WHERE `id_lang` = '.(int)($id_lang).' AND `id_customer` = '.(int)($this->id).' AND a.`deleted` = 0'; + WHERE `id_lang` = '.(int)$id_lang.' AND `id_customer` = '.(int)$this->id.' AND a.`deleted` = 0'; return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); } @@ -356,10 +370,11 @@ class CustomerCore extends ObjectModel public static function getAddressesTotalById($id_customer) { return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' - SELECT COUNT(`id_address`) - FROM `'._DB_PREFIX_.'address` - WHERE `id_customer` = '.(int)($id_customer).' - AND `deleted` = 0'); + SELECT COUNT(`id_address`) + FROM `'._DB_PREFIX_.'address` + WHERE `id_customer` = '.(int)$id_customer.' + AND `deleted` = 0' + ); } /** @@ -370,7 +385,7 @@ class CustomerCore extends ObjectModel */ public static function checkPassword($id_customer, $passwd) { - if (!Validate::isUnsignedId($id_customer) OR !Validate::isMd5($passwd)) + if (!Validate::isUnsignedId($id_customer) || !Validate::isMd5($passwd)) die (Tools::displayError()); $sql = 'SELECT `id_customer` @@ -412,19 +427,19 @@ class CustomerCore extends ObjectModel $result = Db::getInstance()->getRow(' SELECT COUNT(`id_order`) AS nb_orders, SUM(`total_paid` / o.`conversion_rate`) AS total_orders FROM `'._DB_PREFIX_.'orders` o - WHERE o.`id_customer` = '.(int)($this->id).' + WHERE o.`id_customer` = '.(int)$this->id.' AND o.valid = 1'); $result2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT MAX(c.`date_add`) AS last_visit FROM `'._DB_PREFIX_.'guest` g LEFT JOIN `'._DB_PREFIX_.'connections` c ON c.id_guest = g.id_guest - WHERE g.`id_customer` = '.(int)($this->id)); + WHERE g.`id_customer` = '.(int)$this->id); $result3 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT (YEAR(CURRENT_DATE)-YEAR(c.`birthday`)) - (RIGHT(CURRENT_DATE, 5)id); $result['last_visit'] = $result2['last_visit']; $result['age'] = ($result3['age'] != date('Y') ? $result3['age'] : '--'); @@ -432,17 +447,17 @@ class CustomerCore extends ObjectModel } public function getLastConnections() - { + { return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT c.date_add, COUNT(cp.id_page) AS pages, TIMEDIFF(MAX(cp.time_end), c.date_add) as time, http_referer,INET_NTOA(ip_address) as ipaddress FROM `'._DB_PREFIX_.'guest` g LEFT JOIN `'._DB_PREFIX_.'connections` c ON c.id_guest = g.id_guest LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections - WHERE g.`id_customer` = '.(int)($this->id).' + WHERE g.`id_customer` = '.(int)$this->id.' GROUP BY c.`id_connections` ORDER BY c.date_add DESC LIMIT 10'); - } + } /* * Specify if a customer already in base @@ -453,7 +468,7 @@ class CustomerCore extends ObjectModel // DEPRECATED public function customerIdExists($id_customer) { - return self::customerIdExistsStatic((int)($id_customer)); + return self::customerIdExistsStatic((int)$id_customer); } public static function customerIdExistsStatic($id_customer) @@ -461,22 +476,33 @@ class CustomerCore extends ObjectModel $row = Db::getInstance()->getRow(' SELECT `id_customer` FROM '._DB_PREFIX_.'customer c - WHERE c.`id_customer` = '.(int)($id_customer)); + WHERE c.`id_customer` = '.(int)$id_customer); return isset($row['id_customer']); } public function cleanGroups() { - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)($this->id)); + Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)$this->id); } public function addGroups($groups) { foreach ($groups as $group) { - $row = array('id_customer' => (int)($this->id), 'id_group' => (int)($group)); - Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT'); + $groups_customers = $this->getGroups(); + if (count($groups_customers) == 0) + { + $row = array('id_customer' => (int)$this->id, 'id_group' => (int)$group); + Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT'); + } + else + foreach ($groups_customers as $group_customers) + if ($group_customers != $group) + { + $row = array('id_customer' => (int)$this->id, 'id_group' => (int)$group); + Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT'); + } } } @@ -513,7 +539,7 @@ class CustomerCore extends ObjectModel return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT * FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order - WHERE o.valid = 1 AND o.`id_customer` = '.(int)($this->id)); + WHERE o.valid = 1 AND o.`id_customer` = '.(int)$this->id); } public static function getDefaultGroupId($id_customer) @@ -521,21 +547,30 @@ class CustomerCore extends ObjectModel if (!Group::isFeatureActive()) return 1; - if (!isset(self::$_defaultGroupId[(int)($id_customer)])) - self::$_defaultGroupId[(int)($id_customer)] = Db::getInstance()->getValue('SELECT `id_default_group` FROM `'._DB_PREFIX_.'customer` WHERE `id_customer` = '.(int)$id_customer); - return self::$_defaultGroupId[(int)($id_customer)]; + if (!isset(self::$_defaultGroupId[(int)$id_customer])) + self::$_defaultGroupId[(int)$id_customer] = Db::getInstance()->getValue(' + SELECT `id_default_group` + FROM `'._DB_PREFIX_.'customer` + WHERE `id_customer` = '.(int)$id_customer + ); + return self::$_defaultGroupId[(int)$id_customer]; } - static public function getCurrentCountry($id_customer, Cart $cart = null) + public static function getCurrentCountry($id_customer, Cart $cart = null) { if (!$cart) $cart = Context::getContext()->cart; - if (!$cart OR !$cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}) - $id_address = (int)(Db::getInstance()->getValue('SELECT `id_address` FROM `'._DB_PREFIX_.'address` WHERE `id_customer` = '.(int)($id_customer).' AND `deleted` = 0 ORDER BY `id_address`')); + if (!$cart || !$cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}) + $id_address = (int)Db::getInstance()->getValue(' + SELECT `id_address` + FROM `'._DB_PREFIX_.'address` + WHERE `id_customer` = '.(int)$id_customer.' + AND `deleted` = 0 ORDER BY `id_address`' + ); else $id_address = $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}; $ids = Address::getCountryAndState($id_address); - return (int)($ids['id_country'] ? $ids['id_country'] : Configuration::get('PS_COUNTRY_DEFAULT')); + return (int)$ids['id_country'] ? $ids['id_country'] : Configuration::get('PS_COUNTRY_DEFAULT'); } public function toggleStatus() @@ -546,7 +581,7 @@ class CustomerCore extends ObjectModel return Db::getInstance()->execute(' UPDATE `'.pSQL(_DB_PREFIX_.$this->table).'` SET `date_upd` = NOW() - WHERE `'.pSQL($this->identifier).'` = '.(int)($this->id)); + WHERE `'.pSQL($this->identifier).'` = '.(int)$this->id); } @@ -555,7 +590,7 @@ class CustomerCore extends ObjectModel return (bool)$this->is_guest; } - public function transformToCustomer($id_lang, $password = NULL) + public function transformToCustomer($id_lang, $password = null) { if (!$this->isGuest()) return false; @@ -575,32 +610,19 @@ class CustomerCore extends ObjectModel '{passwd}' => $password ); - Mail::Send((int)$id_lang, 'guest_to_customer', Mail::l('Your guest account has been transformed to customer account'), $vars, $this->email, $this->firstname.' '.$this->lastname); + Mail::Send( + (int)$id_lang, + 'guest_to_customer', + Mail::l('Your guest account has been transformed to customer account'), + $vars, + $this->email, + $this->firstname.' '.$this->lastname + ); return true; } return false; } - public static function printNewsIcon($id_customer, $tr) - { - $customer = new Customer($tr['id_customer']); - if (!Validate::isLoadedObject($customer)) - die(Tools::displayError('Invalid objects')); - echo ''. - ($customer->newsletter ? '' : ''). - ''; - } - - public static function printOptinIcon($id_customer, $tr) - { - $customer = new Customer($tr['id_customer']); - if (!Validate::isLoadedObject($customer)) - die(Tools::displayError('Invalid objects')); - echo ''. - ($customer->optin ? '' : ''). - ''; - } - public function setWsPasswd($passwd) { if ($this->id != 0) @@ -617,16 +639,16 @@ class CustomerCore extends ObjectModel * Check customer informations and return customer validity * * @since 1.5.0 - * @param boolean $withGuest + * @param boolean $with_guest * @return boolean customer validity */ - public function isLogged($withGuest = false) + public function isLogged($with_guest = false) { - if (!$withGuest AND $this->is_guest == 1) + if (!$with_guest && $this->is_guest == 1) return false; /* Customer is valid only if it can be load and if object password is the same as database one */ - if ($this->logged == 1 AND $this->id AND Validate::isUnsignedId($this->id) AND self::checkPassword($this->id, $this->passwd)) + if ($this->logged == 1 && $this->id && Validate::isUnsignedId($this->id) && self::checkPassword($this->id, $this->passwd)) return true; return false; } diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php new file mode 100644 index 000000000..82d7aee86 --- /dev/null +++ b/controllers/admin/AdminCustomersController.php @@ -0,0 +1,798 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class AdminCustomersControllerCore extends AdminController +{ + public function __construct() + { + $this->table = 'customer'; + $this->className = 'Customer'; + $this->lang = false; + $this->deleted = true; + + $this->addRowAction('edit'); + $this->addRowAction('view'); + $this->addRowAction('delete'); + + $this->requiredDatabase = true; + + $this->context = Context::getContext(); + + $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?'))); + + $this->default_form_language = $this->context->language->id; + + $genders_icon = array('default' => 'unknown.gif'); + $genders = array(0 => $this->l('?')); + foreach (Gender::getGenders() as $gender) + { + $genders_icon[$gender->id] = '../genders/'.$gender->id.'.jpg'; + $genders[$gender->id] = $gender->name; + } + + $this->fieldsDisplay = array( + 'id_customer' => array( + 'title' => $this->l('ID'), + 'align' => 'center', + 'width' => 25 + ), + 'id_gender' => array( + 'title' => $this->l('Gender'), + 'width' => 30, + 'align' => 'center', + 'icon' => $genders_icon, + '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', + 'callback' => 'printNewsIcon', + 'orderby' => false + ), + 'optin' => array( + 'title' => $this->l('Opt.'), + 'width' => 25, + 'align' => 'center', + 'type' => 'bool', + 'callback' => 'printOptinIcon', + 'orderby' => false + ), + 'date_add' => array( + 'title' => $this->l('Registration'), + 'width' => 30, + 'type' => 'date', + 'align' => 'right' + ), + 'connect' => array( + 'title' => $this->l('Connection'), + 'width' => 60, + 'type' => 'datetime', + 'search' => false + ) + ); + + $this->shopLinkType = 'shop'; + $this->shopShareDatas = Shop::SHARE_CUSTOMER; + + $this->options = array( + 'general' => array( + 'title' => $this->l('Customers options'), + 'fields' => array( + 'PS_PASSWD_TIME_FRONT' => array( + 'title' => $this->l('Regenerate password:'), + 'desc' => $this->l('Security minimum time to wait to regenerate the password'), + 'validation' => 'isUnsignedInt', + 'cast' => 'intval', + 'size' => 5, + 'type' => 'text', + 'suffix' => ' '.$this->l('minutes') + ) + ), + 'submit' => array() + ) + ); + + parent::__construct(); + } + + public function initList() + { + $this->_select = '(YEAR(CURRENT_DATE)-YEAR(`birthday`)) - (RIGHT(CURRENT_DATE, 5)loadObject(true))) + return; + + $genders = Gender::getGenders(); + $total_genders = count($genders); + $list_genders = array(); + for ($i = 0; $i < $total_genders; $i++) + { + $list_genders[$i]['id'] = 'gender_'.$genders[$i]->id; + $list_genders[$i]['value'] = $genders[$i]->id; + $list_genders[$i]['label'] = $genders[$i]->name; + } + $list_genders[$i]['id'] = 'gender_unknown'; + $list_genders[$i]['value'] = 0; + $list_genders[$i]['label'] = $this->l('Unknown'); + + $years = Tools::dateYears(); + $months = Tools::dateMonths(); + $days = Tools::dateDays(); + + $groups = Group::getGroups($this->default_form_language, true); + + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Customer'), + 'image' => '../img/admin/tab-customers.gif' + ), + 'input' => array( + array( + 'type' => 'radio', + 'label' => $this->l('Gender:'), + 'name' => 'id_gender', + 'required' => false, + 'class' => 't', + 'values' => $list_genders + ), + array( + 'type' => 'text', + 'label' => $this->l('Last name:'), + 'name' => 'lastname', + 'size' => 33, + 'required' => true, + 'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()@#"�{}_$%:' + ), + array( + 'type' => 'text', + 'label' => $this->l('First name:'), + 'name' => 'firstname', + 'size' => 33, + 'required' => true, + 'hint' => $this->l('Forbidden characters:').' 0-9!<>,;?=+()@#"�{}_$%:' + ), + array( + 'type' => 'password', + 'label' => $this->l('Password:'), + 'name' => 'passwd', + 'size' => 33, + 'p' => ($obj->id ? $this->l('Leave blank if no change') : $this->l('5 characters min., only letters, numbers, or').' -_') + ), + array( + 'type' => 'text', + 'label' => $this->l('E-mail address:'), + 'name' => 'email', + 'size' => 33, + 'required' => true + ), + array( + 'type' => 'birthday', + 'label' => $this->l('Birthday:'), + 'name' => 'birthday', + 'options' => array( + 'days' => $days, + 'months' => $months, + 'years' => $years + ) + ), + array( + 'type' => 'radio', + 'label' => $this->l('Status:'), + 'name' => 'active', + 'required' => false, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'active_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'active_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Allow or disallow this customer to log in') + ), + array( + 'type' => 'radio', + 'label' => $this->l('Newsletter:'), + 'name' => 'newsletter', + 'required' => false, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'newsletter_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'newsletter_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Customer will receive your newsletter via e-mail') + ), + array( + 'type' => 'radio', + 'label' => $this->l('Opt-in:'), + 'name' => 'optin', + 'required' => false, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'optin_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'optin_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Customer will receive your ads via e-mail') + ), + array( + 'type' => 'select', + 'label' => $this->l('Default group:'), + 'name' => 'id_default_group', + 'options' => array( + 'query' => $groups, + 'id' => 'id_group', + 'name' => 'name' + ), + 'hint' => $this->l('The group will be as applied by default.') + ), + array( + 'type' => 'group', + 'label' => $this->l('Group access:'), + 'name' => 'groupBox', + 'values' => $groups, + 'required' => true, + 'p' => $this->l('Check all the box(es) of groups of which the customer is to be a member') + ) + ) + ); + + if (Shop::isFeatureActive()) + { + $this->fields_form['input'][] = array( + 'type' => 'select', + 'label' => $this->l('Shop:'), + 'name' => 'id_shop', + 'options' => array( + 'query' => Shop::getShops(), + 'id' => 'id_shop', + 'name' => 'name' + ), + ); + } + + $this->fields_form['submit'] = array( + 'title' => $this->l(' Save '), + 'class' => 'button' + ); + + $birthday = explode('-', $this->getFieldValue($obj, 'birthday')); + + $this->fields_value = array( + 'years' => $this->getFieldValue($obj, 'birthday') ? $birthday[0] : 0, + 'months' => $this->getFieldValue($obj, 'birthday') ? $birthday[1] : 0, + 'days' => $this->getFieldValue($obj, 'birthday') ? $birthday[2] : 0, + 'id_shop' => (int)Configuration::get('PS_SHOP_DEFAULT') + ); + + // Added values of object Group + $customer_groups = $obj->getGroups(); + $customer_groups_ids = array(); + if (is_array($customer_groups)) + foreach ($customer_groups as $customer_group) + $customer_groups_ids[] = $customer_group; + foreach ($groups as $group) + $this->fields_value['groupBox_'.$group['id_group']] = Tools::getValue('groupBox_'.$group['id_group'], in_array($group['id_group'], $customer_groups_ids)); + + return parent::initForm(); + } + + public function initContent() + { + if ($this->display == 'view') + return $this->initView(); + parent::initContent(); + } + + public function initView() + { + if (!($customer = $this->loadObject())) + return; + + $gender = new Gender($customer->id_gender); + $gender_image = $gender->getImage(); + + $customer_stats = $customer->getStats(); + $sql = 'SELECT SUM(total_paid_real) FROM '._DB_PREFIX_.'orders WHERE id_customer = %d AND valid = 1'; + if ($total_customer = Db::getInstance()->getValue(sprintf($sql, $customer->id))) + { + $sql = 'SELECT SQL_CALC_FOUND_ROWS COUNT(*) FROM '._DB_PREFIX_.'orders WHERE valid = 1 GROUP BY id_customer HAVING SUM(total_paid_real) > %d'; + Db::getInstance()->getValue(sprintf($sql, (int)$total_customer)); + $count_better_customers = (int)Db::getInstance()->getValue('SELECT FOUND_ROWS()') + 1; + } + else + $count_better_customers = '-'; + + $orders = Order::getCustomerOrders($customer->id, true); + $total_orders = count($orders); + for ($i = 0; $i < $total_orders; $i++) + { + $orders[$i]['date_add'] = Tools::displayDate($orders[$i]['date_add'], $this->context->language->id); + $orders[$i]['total_paid_real'] = Tools::displayPrice($orders[$i]['total_paid_real'], new Currency((int)$orders[$i]['id_currency'])); + } + + $messages = CustomerThread::getCustomerMessages((int)$customer->id); + $total_messages = count($messages); + for ($i = 0; $i < $total_messages; $i++) + { + $messages[$i]['message'] = substr(strip_tags(html_entity_decode($messages[$i]['message'], ENT_NOQUOTES, 'UTF-8')), 0, 75); + $messages[$i]['date_add'] = Tools::displayDate($messages[$i]['date_add'], $this->context->language->id, true); + } + + $groups = Group::getGroups($this->default_form_language, true); + $total_groups = count($groups); + for ($i = 0; $i < $total_groups; $i++) + { + $group = new Group($groups[$i]); + $groups[$i]['id_group'] = $group->id; + $groups[$i]['name'] = $group->name[$this->default_form_language]; + } + + $total_ok = 0; + $orders_ok = array(); + $orders_ko = array(); + foreach ($orders as $order) + { + if ($order['valid']) + { + $orders_ok[] = $order; + $total_ok += $order['total_paid_real']; + } + else + $orders_ko[] = $order; + } + + $products = $customer->getBoughtProducts(); + $total_products = count($products); + for ($i = 0; $i < $total_products; $i++) + $products[$i]['date_add'] = Tools::displayDate($products[$i]['date_add'], $this->default_form_language, true); + + $carts = Cart::getCustomerCarts($customer->id); + $total_carts = count($carts); + for ($i = 0; $i < $total_carts; $i++) + { + $cart = new Cart((int)$carts[$i]['id_cart']); + $summary = $cart->getSummaryDetails(); + $currency = new Currency((int)$carts[$i]['id_currency']); + $carrier = new Carrier((int)$carts[$i]['id_carrier']); + $carts[$i]['id_cart'] = sprintf('%06d', $carts[$i]['id_cart']); + $carts[$i]['date_add'] = Tools::displayDate($carts[$i]['date_add'], $this->default_form_language, true); + $carts[$i]['total_price'] = Tools::displayPrice($summary['total_price'], $currency); + $carts[$i]['name'] = $carrier->name; + } + + $sql = 'SELECT DISTINCT id_product, c.id_cart, c.id_shop, cp.id_shop AS cp_id_shop + FROM '._DB_PREFIX_.'cart_product cp + JOIN '._DB_PREFIX_.'cart c ON (c.id_cart = cp.id_cart) + WHERE c.id_customer = '.(int)$customer->id.' + AND cp.id_product NOT IN ( + SELECT product_id + FROM '._DB_PREFIX_.'orders o + JOIN '._DB_PREFIX_.'order_detail od ON (o.id_order = od.id_order) + WHERE o.valid = 1 AND o.id_customer = '.(int)$customer->id.' + )'; + $interested = Db::getInstance()->executeS($sql); + $total_interested = count($interested); + for ($i = 0; $i < $total_interested; $i++) + { + $product = new Product($interested[$i]['id_product'], false, $this->default_form_language, $interested[$i]['id_shop']); + $interested[$i]['url'] = $this->context->link->getProductLink( + $product->id, + $product->link_rewrite, + Category::getLinkRewrite($product->id_category_default, $this->default_form_language), + null, + null, + $interested[$i]['cp_id_shop'] + ); + $interested[$i]['id'] = (int)$product->id; + $interested[$i]['name'] = Tools::htmlentitiesUTF8($product->name); + } + + $connections = $customer->getLastConnections(); + $total_connections = count($connections); + for ($i = 0; $i < $total_connections; $i++) + { + $connections[$i]['date_add'] = Tools::displayDate($connections[$i]['date_add'], $this->default_form_language, true); + $connections[$i]['http_referer'] = $connections[$i]['http_referer'] ? + preg_replace('/^www./', '', parse_url($connections[$i]['http_referer'], PHP_URL_HOST)) : + $this->l('Direct link'); + } + + $referrers = Referrer::getReferrers($customer->id); + $total_referrers = count($referrers); + for ($i = 0; $i < $total_referrers; $i++) + $referrers[$i]['date_add'] = Tools::displayDate($referrers[$i]['date_add'], $this->default_form_language, true); + + $this->context->smarty->assign(array( + 'table' => $this->table, + 'current' => self::$currentIndex, + 'token' => $this->token, + 'content' => $this->content, + 'customer' => $customer, + 'gender_image' => $gender_image, + + // General information of the customer + 'registration_date' => Tools::displayDate($customer->date_add, $this->default_form_language, true), + 'customer_stats' => $customer_stats, + 'last_visit' => Tools::displayDate($customer_stats['last_visit'], $this->default_form_language, true), + 'count_better_customers' => $count_better_customers, + 'shop_is_feature_active' => Shop::isFeatureActive(), + 'name_shop' => Shop::getInstance($customer->id_shop)->name, + 'customer_birthday' => Tools::displayDate($customer->birthday, $this->default_form_language), + 'last_update' => Tools::displayDate($customer->date_upd, $this->default_form_language, true), + 'customer_exists' => Customer::customerExists($customer->email), + 'id_lang' => (int)(count($orders) ? $orders[0]['id_lang'] : Configuration::get('PS_LANG_DEFAULT')), + + // Add a Private note + 'customer_note' => Tools::htmlentitiesUTF8($customer->note), + + // Messages + 'messages' => $messages, + + // Display hook specified to this page : AdminCustomers + 'hook' => Module::hookExec('adminCustomers', array('id_customer' => $customer->id)), + + // Groups + 'groups' => $groups, + + // Orders + 'orders' => $orders, + 'orders_ok' => $orders_ok, + 'orders_ko' => $orders_ko, + 'total_ok' => Tools::displayPrice($total_ok, $this->context->currency->id), + + // Products + 'products' => $products, + + // Addresses + 'addresses' => $customer->getAddresses($this->default_form_language), + + // Discounts + 'discounts' => Discount::getCustomerDiscounts($this->default_form_language, $customer->id, false, false), + + // Carts + 'carts' => $carts, + + // Interested + 'interested' => $interested, + + // Connections + 'connections' => $connections, + + // Referrers + 'referrers' => $referrers, + )); + } + + public function postProcess() + { + /** + * Todo : Where it's used? + */ + if (Tools::isSubmit('submitDel'.$this->table) || Tools::isSubmit('delete'.$this->table)) + { + $delete_form = ' +
+
'.$this->l('How do you want to delete your customer(s)?').' + '.$this->l('You have two ways to delete a customer, please choose what you want to do.').' +

+ + +

+

+ + +

'; + foreach ($_POST as $key => $value) + if (is_array($value)) + foreach ($value as $val) + $delete_form .= ''; + else + $delete_form .= ''; + $delete_form .= '
+
+
+
 
'; + } + + if (Tools::getValue('submitAdd'.$this->table)) + { + /* Checking fields validity */ + $this->validateRules(); + if (!count($this->_errors)) + { + $id = (int)Tools::getValue('id_'.$this->table); + $group_list = Tools::getValue('groupBox'); + + //Update Object + if (isset($id) && !empty($id)) + { + if ($this->tabAccess['edit'] !== '1') + $this->_errors[] = Tools::displayError('You do not have permission to edit 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 (!is_array($group_list) || count($group_list) == 0) + $this->_errors[] = Tools::displayError('Customer must be in at least one group.'); + else + if (!in_array(Tools::getValue('id_default_group'), $group_list)) + $this->_errors[] = Tools::displayError('Default customer group must be selected in group box.'); + + // Updating customer's group + if (!count($this->_errors)) + { + $object->cleanGroups(); + if (is_array($group_list) && count($group_list) > 0) + $object->addGroups($group_list); + } + } + else + $this->_errors[] = Tools::displayError('An error occurred while loading object.').' + '.$this->table.' '.Tools::displayError('(cannot load object)'); + } + } + + //Create Object + else + { + if ($this->tabAccess['add'] === '1') + { + $object = new $this->className(); + $this->copyFromPost($object, $this->table); + $shop = new Shop((int)$object->id_shop); + $object->id_group_shop = (int)$shop->id_group_shop; + if (!$object->add()) + $this->_errors[] = Tools::displayError('An error occurred while creating object.').' + '.$this->table.' ('.Db::getInstance()->getMsgError().')'; + else if (($_POST[$this->identifier] = $object->id /* voluntary */) && + $this->postImage($object->id) && !count($this->_errors) && + $this->_redirect) + { + // Add Associated groups + $group_list = Tools::getValue('groupBox'); + if (is_array($group_list) && count($group_list) > 0) + $object->addGroups($group_list); + $parent_id = (int)Tools::getValue('id_parent', 1); + // Save and stay on same form + if (Tools::isSubmit('submitAdd'.$this->table.'AndStay')) + Tools::redirectAdmin(self::$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(self::$currentIndex.'&'.$this->identifier.'='.$parent_id.'&conf=3&token='.$this->token); + // Default behavior (save and back) + Tools::redirectAdmin(self::$currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$this->token); + } + } + else + $this->_errors[] = Tools::displayError('You do not have permission to add here.'); + } + } + } + else if (Tools::isSubmit('delete'.$this->table) && $this->tabAccess['delete'] === '1') + { + switch (Tools::getValue('deleteMode')) + { + case 'real': + $this->deleted = false; + Discount::deleteByIdCustomer((int)Tools::getValue('id_customer')); + break; + case 'deleted': + $this->deleted = true; + break; + default: + echo $delete_form; + if (isset($_POST['delete'.$this->table])) + unset($_POST['delete'.$this->table]); + if (isset($_GET['delete'.$this->table])) + unset($_GET['delete'.$this->table]); + break; + } + } + else if (Tools::isSubmit('submitDel'.$this->table) && $this->tabAccess['delete'] === '1') + { + switch (Tools::getValue('deleteMode')) + { + case 'real': + $this->deleted = false; + foreach (Tools::getValue('customerBox') as $id_customer) + Discount::deleteByIdCustomer((int)$id_customer); + break; + case 'deleted': + $this->deleted = true; + break; + default: + echo $delete_form; + if (isset($_POST['submitDel'.$this->table])) + unset($_POST['submitDel'.$this->table]); + if (isset($_GET['submitDel'.$this->table])) + unset($_GET['submitDel'.$this->table]); + break; + } + } + else if (Tools::isSubmit('submitGuestToCustomer') && Tools::getValue('id_customer')) + { + if ($this->tabAccess['edit'] === '1') + { + $customer = new Customer((int)Tools::getValue('id_customer')); + if (!Validate::isLoadedObject($customer)) + $this->_errors[] = Tools::displayError('This customer does not exist.'); + if (Customer::customerExists($customer->email)) + $this->_errors[] = Tools::displayError('This customer already exist as non-guest.'); + else if ($customer->transformToCustomer(Tools::getValue('id_lang', Configuration::get('PS_LANG_DEFAULT')))) + Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$customer->id.'&conf=3&token='.$this->token); + else + $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); + } + else + $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); + } + else if (Tools::isSubmit('changeNewsletterVal') && Tools::getValue('id_customer')) + { + $id_customer = (int)Tools::getValue('id_customer'); + $customer = new Customer($id_customer); + if (!Validate::isLoadedObject($customer)) + $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); + $update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET newsletter = '.($customer->newsletter ? 0 : 1).' WHERE `id_customer` = '.(int)$customer->id); + if (!$update) + $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); + Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); + } + else if (Tools::isSubmit('changeOptinVal') && Tools::getValue('id_customer')) + { + $id_customer = (int)Tools::getValue('id_customer'); + $customer = new Customer($id_customer); + if (!Validate::isLoadedObject($customer)) + $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); + $update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET optin = '.($customer->optin ? 0 : 1).' WHERE `id_customer` = '.(int)$customer->id); + if (!$update) + $this->_errors[] = Tools::displayError('An error occurred while updating customer.'); + Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); + } + + return parent::postProcess(); + } + + public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = null) + { + return parent::getList( + Context::getContext()->language->id, + !Tools::getValue($this->table.'Orderby') ? 'date_add' : null, + !Tools::getValue($this->table.'Orderway') ? 'DESC' : null + ); + } + + public function beforeDelete($object) + { + return $object->isUsed(); + } + + public static function printNewsIcon($id_customer, $tr) + { + $customer = new Customer($tr['id_customer']); + if (!Validate::isLoadedObject($customer)) + return; + return ' + '.($customer->newsletter ? '' : ''). + ''; + } + + public static function printOptinIcon($id_customer, $tr) + { + $customer = new Customer($tr['id_customer']); + if (!Validate::isLoadedObject($customer)) + return; + return ' + '.($customer->optin ? '' : ''). + ''; + } + +} + +