Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 107.08 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('_PS_VERSION_'))
  4.     exit;
  5.  
  6. class AdvancedExport extends Module
  7. {
  8.     public $hasAttr;
  9.     public $lastElement;
  10.     public $rowsNumber;
  11.     public $link;
  12.     private $selected_cat;
  13.     private $export_types =  array('products', 'orders', 'categories', 'manufacturers', 'newsletters', 'suppliers', 'customers' ), $counter = null; // put it here
  14.     public $products = array(
  15.         array('name' => 'Product Id','field' =>  'id_product', 'database' => 'products', 'alias' => 'p'),
  16.         array('name' => 'Product Reference','field' =>  'reference', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  17.         array('name' => 'Name','field' =>  'name', 'database' => 'products_lang', 'alias' => 'pl'),
  18.         array('name' => 'Short Description','field' =>  'description_short', 'database' => 'products_lang', 'alias' => 'pl'),
  19.         array('name' => 'Long Description','field' =>  'description', 'database' => 'products_lang', 'alias' => 'pl'),
  20.         array('name' => 'Quantity','field' =>  'quantity', 'database' => 'other', 'attribute' => true),
  21.         array('name' => 'Price','field' =>  'price', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  22.         array('name' => 'Price Catalogue TTC','field' =>  'price_tax_nodiscount', 'database' => 'other', 'attribute' => true),
  23.         array('name' => 'Price Tax','field' =>  'price_tax', 'database' => 'other', 'attribute' => true),
  24.         array('name' => 'Wholesale Price','field' =>  'wholesale_price', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  25.         array('name' => 'Supplier Id (default)','field' =>  'id_supplier', 'database' => 'products', 'alias' => 'p'),
  26.         array('name' => 'Suppliers Ids','field' =>  'id_supplier_all', 'database' => 'other', 'attribute' => true),
  27.         array('name' => 'Supplier Name (default)','field' =>  'supplier_name', 'as' => true, 'database' => 'supplier', 'alias' => 's'),
  28.         array('name' => 'Supplier Names','field' =>  'supplier_name_all', 'database' => 'other', 'attribute' => true),
  29.         array('name' => 'Manufacturer Id','field' =>  'id_manufacturer', 'database' => 'products', 'alias' => 'p'),
  30.         array('name' => 'Manufacturer Name','field' =>  'manufacturer_name', 'database' => 'other'),
  31.         array('name' => 'Tax Id Rules Group','field' =>  'id_tax_rules_group', 'database' => 'products', 'alias' => 'p'),
  32.         array('name' => 'Tax Rate','field' =>  'tax_rate', 'database' => 'other'),
  33.         array('name' => 'Default Category Id','field' =>  'id_category_default', 'database' => 'products', 'alias' => 'p'),
  34.         array('name' => 'Default Category Name','field' =>  'nameCategoryDefault', 'database' => 'other'),
  35.         array('name' => 'Categories Names','field' =>  'categories_names', 'database' => 'other'),
  36.         array('name' => 'Categories Ids','field' =>  'categories_ids', 'database' => 'other'),
  37.         array('name' => 'On Sale','field' =>  'on_sale', 'database' => 'products', 'alias' => 'p'),
  38.         array('name' => 'EAN 13','field' =>  'ean13', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  39.         array('name' => 'Supplier Reference','field' =>  'supplier_reference', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  40.         array('name' => 'Date Added','field' =>  'date_add', 'database' => 'products', 'alias' => 'p'),
  41.         array('name' => 'Date Update','field' =>  'date_upd', 'database' => 'products', 'alias' => 'p'),
  42.         array('name' => 'Active','field' =>  'active', 'database' => 'products', 'alias' => 'p'),
  43.         array('name' => 'Meta Title','field' =>  'meta_title', 'database' => 'products_lang', 'alias' => 'pl'),
  44.         array('name' => 'Meta Description','field' =>  'meta_description', 'database' => 'products_lang', 'alias' => 'pl'),
  45.         array('name' => 'Meta Keywords','field' =>  'meta_keywords', 'database' => 'products_lang', 'alias' => 'pl'),
  46.         array('name' => 'Available Now','field' =>  'available_now', 'database' => 'products_lang', 'alias' => 'pl'),
  47.         array('name' => 'Available Later','field' =>  'available_later', 'database' => 'products_lang', 'alias' => 'pl'),      
  48.         array('name' => 'Tags','field' =>  'tags', 'database' => 'other'),
  49.         array('name' => 'Accessories','field' =>  'accessories', 'database' => 'other'),
  50.         array('name' => 'Images','field' =>  'images', 'database' => 'other', 'attribute' => true),
  51.         array('name' => 'Online only','field' =>  'online_only', 'database' => 'products', 'alias' => 'p'),
  52.         array('name' => 'Upc','field' =>  'upc', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  53.         array('name' => 'Ecotax','field' =>  'ecotax', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  54.         array('name' => 'Unity','field' =>  'unity', 'database' => 'products', 'alias' => 'p'),
  55.         array('name' => 'Unit Price Ratio','field' =>  'unit_price_ratio', 'database' => 'products', 'alias' => 'p'),
  56.         array('name' => 'Minimal Quantity','field' =>  'minimal_quantity', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  57.         array('name' => 'Additional Shipping Cost','field' =>  'additional_shipping_cost', 'database' => 'products', 'alias' => 'p'),
  58.         array('name' => 'Location','field' =>  'location', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  59.         array('name' => 'Width','field' =>  'width', 'database' => 'products', 'alias' => 'p'),
  60.         array('name' => 'Height','field' =>  'height', 'database' => 'products', 'alias' => 'p'),
  61.         array('name' => 'Depth','field' =>  'depth', 'database' => 'products', 'alias' => 'p'),
  62.         array('name' => 'Weight','field' =>  'weight', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
  63.         array('name' => 'Out Of Stock','field' =>  'out_of_stock', 'database' => 'products', 'alias' => 'p'),
  64.         array('name' => 'Quantity Discount','field' =>  'quantity_discount', 'database' => 'products', 'alias' => 'p'),
  65.         array('name' => 'Customizable','field' =>  'customizable', 'database' => 'products', 'alias' => 'p'),
  66.         array('name' => 'Uploadable Files','field' =>  'uploadable_files', 'database' => 'products', 'alias' => 'p'),
  67.         array('name' => 'Text Fields','field' =>  'text_fields', 'database' => 'products', 'alias' => 'p'),
  68.         array('name' => 'Available For Order','field' =>  'available_for_order', 'database' => 'products', 'alias' => 'p'),
  69.         array('name' => 'Condition','field' =>  'condition', 'database' => 'products', 'alias' => 'p'),
  70.         array('name' => 'Show Price','field' =>  'show_price', 'database' => 'products', 'alias' => 'p'),
  71.         array('name' => 'Indexed','field' =>  'indexed', 'database' => 'products', 'alias' => 'p'),
  72.         array('name' => 'Cache Is Pack','field' =>  'cache_is_pack', 'database' => 'products', 'alias' => 'p'),
  73.         array('name' => 'Cache Has Attachments','field' =>  'cache_has_attachments', 'database' => 'products', 'alias' => 'p'),
  74.         array('name' => 'Cache Default Attribute','field' =>  'cache_default_attribute', 'database' => 'products', 'alias' => 'p'),
  75.         array('name' => 'Link Rewrite','field' =>  'link_rewrite', 'database' => 'products_lang', 'alias' => 'pl'),
  76.         array('name' => 'Url Product','field' =>  'url_product', 'database' => 'other'),
  77.         array('name' => 'Features','field' =>  'features', 'database' => 'other'),
  78.         array('name' => 'Attributes','field' =>  'attributes_name', 'database' => 'other', 'attribute' => true)
  79.     );
  80.     public $orders = array(
  81.         //PS_ORDER
  82.         array('name' => 'Order No', 'field' => 'id_order', 'database' => 'orders', "alias" => "o"),
  83.         array('name' => 'Reference', 'field' => 'reference', 'database' => 'orders', "alias" => "o", 'attribute' => true),
  84.         array('name' => 'Code (voucher)', 'field' => 'code', 'database' => 'other'),
  85.         //SHOP
  86.         array('name' => 'Payment module', 'field' => 'module', 'database' => 'orders',  "alias" => "o"),
  87.         array('name' => 'Payment', 'field' => 'payment', 'database' => 'orders',  "alias" => "o"),
  88.         array('name' => 'Total paid', 'field' => 'total_paid', 'database' => 'orders',  "alias" => "o"),
  89.         array('name' => 'Total paid tax incl', 'field' => 'total_paid_tax_incl', 'database' => 'orders', "alias" => "o"),
  90.         array('name' => 'Total paid tax excl', 'field' => 'total_paid_tax_excl', 'database' => 'orders', "alias" => "o"),
  91.         array('name' => 'Total products with tax', 'field' => 'total_products_wt', 'database' => 'orders', "alias" => "o"),
  92.         array('name' => 'Total paid real', 'field' => 'total_paid_real', 'database' => 'orders', "alias" => "o"),
  93.         array('name' => 'Total products', 'field' => 'total_products', 'database' => 'orders', "alias" => "o"),
  94.         array('name' => 'Total shipping', 'field' => 'total_shipping', 'database' => 'orders', "alias" => "o"),
  95.         array('name' => 'Total wrapping', 'field' => 'total_wrapping', 'database' => 'orders', "alias" => "o"),
  96.         array('name' => 'Shipping number', 'field' => 'shipping_number', 'database' => 'orders', "alias" => "o"),
  97.         array('name' => 'Delivery number', 'field' => 'delivery_number', 'database' => 'orders', "alias" => "o"),
  98.         array('name' => 'Invoice number', 'field' => 'invoice_number', 'database' => 'orders', "alias" => "o"),
  99.         array('name' => 'Invoice date', 'field' => 'invoice_date', 'database' => 'orders', "alias" => "o"),
  100.         array('name' => 'Delivery date', 'field' => 'delivery_date', 'database' => 'orders', "alias" => "o"),
  101.         array('name' => 'Date added', 'field' => 'date_add', 'database' => 'orders', "alias" => "o"),
  102.         array('name' => 'Date updated', 'field' => 'date_upd', 'database' => 'orders', "alias" => "o"),
  103.         array('name' => 'Total discounts', 'field' => 'total_discounts', 'database' => 'orders', "alias" => "o"),
  104.         array('name' => 'Gift message', 'field' => 'gift_message', 'database' => 'orders', "alias" => "o"),
  105.         array('name' => 'Valid', 'field' => 'valid', 'database' => 'orders', "alias" => "o"),
  106.         array('name' => 'Carrier id', 'field' => 'id_carrier', 'database' => 'orders', "alias" => "o"),
  107.         array('name' => 'Customer id', 'field' => 'id_customer', 'database' => 'orders', "alias" => "o"),
  108.         array('name' => 'Recycled packaging', 'field' => 'recyclable', 'database' => 'orders', "alias" => "o"),
  109.         array('name' => 'Gift wrapping', 'field' => 'gift', 'database' => 'orders', "alias" => "o"),
  110.         array('name' => 'Customization', 'field' => 'customization', 'database' => 'other', "alias" => "o"),
  111.         //PS_CUSTOMER
  112.         array('name' => 'Customer Firstname', 'field' => 'firstname', 'database' => 'customer', "alias" => "cu"),
  113.         array('name' => 'Customer Lastname', 'field' => 'lastname', 'database' => 'customer', "alias" => "cu"),
  114.         array('name' => 'Customer Email', 'field' => 'email', 'database' => 'customer', "alias" => "cu"),
  115.         array('name' => 'Customer id language', 'field' => 'id_lang', 'database' => 'customer', "alias" => "cu"),
  116.         //array("name" => "codice target", "field" => "codice_target", "database" => "customer", "alias" => "cu"), //aggiunto codice target
  117.         //PS_ADRESS
  118.         array('name' => 'Delivery Gender', 'field' => 'delivery_name', 'as' => true, 'database' => 'gender', "alias" => "gl"),
  119.         array('name' => 'Delivery Company Name', 'field' => 'company', 'database' => 'address', "alias" => "a"),
  120.         array('name' => 'Delivery Firstname', 'field' => 'delivery_firstname', 'as' => true, 'database' => 'address', "alias" => "a"),
  121.         array('name' => 'Delivery Lastname', 'field' => 'delivery_lastname', 'as' => true, 'database' => 'address', "alias" => "a"),
  122.         array('name' => 'Delivery address line 1', 'field' => 'delivery_address1', 'as' => true, 'database' => 'address', "alias" => "a"),
  123.         array('name' => 'Delivery address line 2', 'field' => 'delivery_address2', 'as' => true, 'database' => 'address', "alias" => "a"),
  124.         array('name' => 'Delivery postcode', 'field' => 'delivery_postcode', 'as' => true, 'database' => 'address', "alias" => "a"),
  125.         array('name' => 'Delivery city', 'field' => 'delivery_city', 'as' => true, 'database' => 'address', "alias" => "a"),
  126.         array('name' => 'Delivery phone', 'field' => 'delivery_phone', 'as' => true, 'database' => 'address', "alias" => "a"),
  127.         array('name' => 'Delivery phone(mobile)', 'field' => 'delivery_phone_mobile', 'as' => true, 'database' => 'address', "alias" => "a"),
  128.         array('name' => 'Delivery VAT', 'field' => 'delivery_vat_number', 'as' => true, 'database' => 'address', "alias" => "a"),
  129.         array('name' => 'Delivery DNI', 'field' => 'delivery_dni', 'as' => true, 'database' => 'address', "alias" => "a"),
  130.         //PS_STATE
  131.         array('name' => 'Delivery country iso code', 'field' => 'iso_code', 'database' => 'country', 'alias' => 'co'),
  132.         array('name' => 'Delivery state', 'field' => 'state_name', 'as' => true, 'database' => 'state', 'alias' => 's'),
  133.         //PS_COUNTRY_LANG
  134.         array('name' => 'Delivery country', 'field' => 'country_name', 'as' => true, 'database' => 'country_lang', 'alias' => 'cl'),
  135.         //PS_ADRESS
  136.         array('name' => 'Invoice address line 1', 'field' => 'invoice_address1', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  137.         array('name' => 'Invoice address line 2', 'field' => 'invoice_address2', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  138.         array('name' => 'Invoice postcode', 'field' => 'invoice_postcode', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  139.         array('name' => 'Invoice city', 'field' => 'invoice_city', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  140.         array('name' => 'Invoice phone', 'field' => 'invoice_phone', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  141.         array('name' => 'Invoice phone (mobile)', 'field' => 'invoice_phone_mobile', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  142.         array('name' => 'Invoice gender', 'field' => 'invoice_name', 'as' => true, 'database' => 'gender', "alias" => "inv_gl"),
  143.         array('name' => 'Invoice firstname', 'field' => 'invoice_firstname', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  144.         array('name' => 'Invoice lastname', 'field' => 'invoice_lastname', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  145.         array('name' => 'Invoice company name', 'field' => 'invoice_company', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
  146.         //ORDER_PAYMENT
  147.         array('name' => 'Transaction Id', 'field' => 'transaction_id', 'database' => 'order_payment', "alias" => "op"),
  148.         //PS_CARRIER
  149.         array('name' => 'Name carrier', 'field' => 'carrier_name', 'as' => true, 'database' => 'carrier', "alias" => "ca"),
  150.         //PS_ORDER_DETAIL
  151.         array('name' => 'Product ID', 'field' => 'product_id', 'database' => 'order_detail', "alias" => "od"),
  152.         array('name' => 'Product Ref', 'field' => 'product_reference', 'database' => 'order_detail', "alias" => "od"),
  153.         array('name' => 'Product Name', 'field' => 'product_name', 'database' => 'order_detail', "alias" => "od"),
  154.         array('name' => 'Product Price', 'field' => 'product_price', 'database' => 'order_detail', "alias" => "od"),
  155.         array('name' => 'Product Quantity', 'field' => 'product_quantity', 'database' => 'order_detail', "alias" => "od"),
  156.         array('name' => 'Shop name', 'field' => 'shop_name', 'database' => 'shop', 'as' => true, 'alias' => 'sh'),
  157.  
  158.         array('name' => 'Message', 'field' => 'message', 'database' => 'message', 'alias' => 'm'),
  159.         array('name' => 'Order currency', 'field' => 'currency_iso_code', 'database' => 'currency', 'as' => true, 'alias' => 'cur'),
  160.     );
  161.     public $categories = array(
  162.         array("name" => "Id category", "field" => "id_category", "database" => "category", "alias" => "c"),
  163.         array("name" => "Id parent", "field" => "id_parent", "database" => "category", "alias" => "c"),
  164.         array("name" => "Id shop default", "field" => "id_shop_default", "database" => "category", "alias" => "c"),
  165.         array("name" => "Level depth", "field" => "level_depth", "database" => "category", "alias" => "c"),
  166.         array("name" => "nleft", "field" => "nleft", "database" => "category", "alias" => "c"),
  167.         array("name" => "nright", "field" => "nright", "database" => "category", "alias" => "c"),
  168.         array("name" => "active", "field" => "active", "database" => "category", "alias" => "c"),
  169.         array("name" => "Is root category", "field" => "is_root_category", "database" => "category", "alias" => "c"),
  170.         array("name" => "Id group", "field" => "id_group", "database" => "other"),
  171.         array("name" => "Id shop", "field" => "id_shop", "database" => "category_lang", "alias" => "cl"),
  172.         array("name" => "Name", "field" => "name", "database" => "category_lang", "alias" => "cl"),
  173.         array("name" => "Description", "field" => "description", "database" => "category_lang", "alias" => "cl"),
  174.         array("name" => "Link rewrite", "field" => "link_rewrite", "database" => "category_lang", "alias" => "cl"),
  175.         array("name" => "Meta title", "field" => "meta_title", "database" => "category_lang", "alias" => "cl"),
  176.         array("name" => "Meta keywords", "field" => "meta_keywords", "database" => "category_lang", "alias" => "cl"),
  177.         array("name" => "Meta description", "field" => "meta_description", "database" => "category_lang", "alias" => "cl"),
  178.         array("name" => "Position", "field" => "position", "database" => "category_shop", "alias" => "category_shop")
  179.     );
  180.     public $manufacturers = array(
  181.         array("name" =>"id manufacturer", "field" => "id_manufacturer", "database" => "manufacturer", "alias" => "m"),
  182.         array("name" =>"name", "field" => "name", "database" => "manufacturer", "alias" => "m"),
  183.         array("name" =>"active", "field" => "active", "database" => "manufacturer", "alias" => "m"),
  184.         array("name" =>"description", "field" => "description", "database" => "manufacturer", "alias" => "ml"),
  185.         array("name" =>"short description", "field" => "short_description", "database" => "manufacturer", "alias" => "ml"),
  186.         array("name" =>"meta title", "field" => "meta_title", "database" => "manufacturer", "alias" => "ml"),
  187.         array("name" =>"meta keywords", "field" => "meta_keywords", "database" => "manufacturer", "alias" => "ml"),
  188.         array("name" =>"meta description", "field" => "meta_description", "database" => "manufacturer", "alias" => "ml"),
  189.         array("name" =>"id shop", "field" => "id_shop", "database" => "manufacturer", "alias" => "manufacturer_shop"),
  190.         );
  191.     public $suppliers = array(
  192.         array("name" =>"id supplier", "field" => "id_supplier", "database" => "supplier", "alias" => "s"),
  193.         array("name" =>"name", "field" => "name", "database" => "supplier", "alias" => "s"),
  194.         array("name" =>"active", "field" => "active", "database" => "supplier", "alias" => "s"),
  195.         array("name" =>"description", "field" => "description", "database" => "supplier", "alias" => "sl"),
  196.         array("name" =>"meta title", "field" => "meta_title", "database" => "supplier", "alias" => "sl"),
  197.         array("name" =>"meta keywords", "field" => "meta_keywords", "database" => "supplier", "alias" => "sl"),
  198.         array("name" =>"meta description", "field" => "meta_description", "database" => "supplier", "alias" => "sl"),
  199.         array("name" =>"id shop", "field" => "id_shop", "database" => "supplier", "alias" => "supplier_shop"),
  200.         );
  201.     public $customers = array(
  202.         array("name" => "id customer", "field" => "id_customer", "database" => "customer", "alias" => "c"),
  203.         array("name" => "codice target", "field" => "codice_target", "database" => "customer", "alias" => "c"), //aggiunto codice target
  204.         array("name" => "id gender", "field" => "id_gender", "database" => "customer", "alias" => "c"),
  205.         array("name" => "company", "field" => "company", "database" => "customer", "alias" => "c"),
  206.         array("name" => "siret", "field" => "siret", "database" => "customer", "alias" => "c"),
  207.         array("name" => "ape", "field" => "ape", "database" => "customer", "alias" => "c"),
  208.         array("name" => "firstname", "field" => "firstname", "database" => "customer", "alias" => "c"),
  209.         array("name" => "lastname", "field" => "lastname", "database" => "customer", "alias" => "c"),
  210.         array("name" => "email", "field" => "email", "database" => "customer", "alias" => "c"),
  211.         array("name" => "birthday", "field" => "birthday", "database" => "customer", "alias" => "c"),
  212.         array("name" => "newsletter", "field" => "newsletter", "database" => "customer", "alias" => "c"),
  213.         array("name" => "website", "field" => "website", "database" => "customer", "alias" => "c"),
  214.         array("name" => "password", "field" => "passwd", "database" => "customer", "alias" => "c"),
  215.  
  216.         array("name" => "address company", "field" => "address_company", "database" => "address", 'as' => true, "alias" => "a"),
  217.         array("name" => "address firstname", "field" => "address_firstname", "database" => "address", 'as' => true, "alias" => "a"),
  218.         array("name" => "address lastname", "field" => "address_lastname", "database" => "address", 'as' => true, "alias" => "a"),
  219.         array("name" => "address address1", "field" => "address1", "database" => "address", "alias" => "a"),
  220.         array("name" => "address address2", "field" => "address2", "database" => "address", "alias" => "a"),
  221.         array("name" => "address postcode", "field" => "postcode", "database" => "address", "alias" => "a"),
  222.         array("name" => "address city", "field" => "city", "database" => "address", "alias" => "a"),
  223.         array("name" => "address other", "field" => "other", "database" => "address", "alias" => "a"),
  224.         array("name" => "address phone", "field" => "phone", "database" => "address", "alias" => "a"),
  225.         array("name" => "address phone_mobile", "field" => "phone_mobile", "database" => "address", "alias" => "a"),
  226.         array("name" => "address vat_number", "field" => "vat_number", "database" => "address", "alias" => "a"),
  227.         array("name" => "address dni", "field" => "dni", "database" => "address", "alias" => "a"),
  228.         array("name" => "address active", "field" => "active", "database" => "address", "alias" => "a"),
  229.         );
  230.     public $newsletters = array(
  231.         array("name" => "Email", "field" => "email", "database" => "newsletter"),
  232.         array("name" => "Date add", "field" => "newsletter_date_add", "database" => "newsletter"),
  233.         array("name" => "Ip", "field" => "ip_registration_newsletter", "database" => "newsletter"),
  234.         array("name" => "Referer", "field" => "http_referer", "database" => "newsletter"),
  235.         array("name" => "Active", "field" => "active", "database" => "newsletter")
  236.     );
  237.     public $switch;
  238.     public $showTimeAndMemory;
  239.  
  240.     public function __construct()
  241.     {
  242.         $this->name = "advancedexport";
  243.         $this->tab = "administration";
  244.         $this->bootstrap  = true;
  245.         $this->author = "Algol Consulting";
  246.         $this->version = "4.2.0";
  247.         $this->displayName = $this->l('Esportazione Avanzata');
  248.         $this->description = $this->l('Strumento esportazione avanzata');
  249.         $this->module_key = "a3895af3e1e55fa47a756b6e973e77fe";
  250.         $this->link = new Link();
  251.         $this->showTimeAndMemory = false;
  252.         $this->switch = (_PS_VERSION_ >= 1.6 ? "switch": "radio");
  253.         parent::__construct();
  254.  
  255.         $path = dirname(__FILE__);
  256.  
  257.         if (strpos(__FILE__, 'Module.php') !== false)
  258.             $path .= '/../modules/'.$this->name;
  259.  
  260.         include_once($path.'/classes/AdvancedExportClass.php');
  261.     }
  262.  
  263.     public function install()
  264.     {
  265.          if(!$this->CreateTables()
  266.             || !Configuration::updateGlobalValue("AdvancedExport_CURRENT", 0)
  267.              || !Configuration::updateGlobalValue("AdvancedExport_TOTAL", 0)
  268.             || !Configuration::updateGlobalValue("ADVANCEDEXPORT_SECURE_KEY", Tools::strtoupper(Tools::passwdGen(16))))
  269.             return false;
  270.  
  271.          if (!parent::install())
  272.             return false;
  273.  
  274.          return true;
  275.     }
  276.  
  277.     public function CreateTables()
  278.     {
  279.         //for test create tmp table
  280.         $table_name =  _DB_PREFIX_ . "advancedexport";
  281.  
  282.         $query = "CREATE TABLE IF NOT EXISTS `".$table_name."` (
  283.             `id_advancedexport` int(10) unsigned NOT NULL auto_increment,
  284.             `type` varchar(200) NOT NULL,
  285.             `name` varchar(200) NOT NULL,
  286.             `delimiter` varchar(255) NOT NULL,
  287.             `separator` varchar(255) NOT NULL,
  288.             `id_lang` int(10) NOT NULL,
  289.             `charset` varchar(255) NOT NULL,
  290.             `add_header` BOOL NOT NULL DEFAULT 0,
  291.             `decimal_separator` varchar(10) NOT NULL,
  292.             `decimal_round` int(10) NOT NULL,
  293.             `strip_tags` BOOL NOT NULL DEFAULT 0,
  294.             `only_new` BOOL NOT NULL DEFAULT 0,
  295.             `date_from` varchar(255) NOT NULL,
  296.             `date_to` varchar(255) NOT NULL,
  297.            `last_exported_id` int(10) NOT NULL DEFAULT 0,
  298.            `start_id` int(10) NOT NULL DEFAULT 0,
  299.             `end_id` int(10) NOT NULL DEFAULT 0,
  300.             `save_type` int(10) NOT NULL DEFAULT 0,
  301.             `filename` varchar(255) NOT NULL,
  302.             `image_type` varchar(255) NOT NULL,
  303.             `email` varchar(255) NOT NULL,
  304.             `ftp_hostname` varchar(255) NOT NULL,
  305.             `ftp_user_name` varchar(255) NOT NULL,
  306.             `ftp_user_pass` varchar(255) NOT NULL,
  307.             `fields` text  NOT NULL,
  308.             PRIMARY KEY  (`id_advancedexport`)
  309.             ) ENGINE="._MYSQL_ENGINE_." DEFAULT CHARSET=utf8";
  310.  
  311.         if(!$this->DbExecute($query))
  312.             return false;
  313.  
  314.         return true;
  315.     }
  316.  
  317.     public function DbExecute($query)
  318.     {
  319.         return Db::getInstance()->Execute($query);
  320.     }
  321.  
  322.     public function uninstall()
  323.     {
  324.         if(!$this->RemoveTables()
  325.             || !Configuration::deleteByName("AdvancedExport_CURRENT")
  326.             || !Configuration::deleteByName("AdvancedExport_TOTAL"))  //install tables
  327.             return false;
  328.  
  329.         if (!parent::uninstall())
  330.             return false;
  331.  
  332.         return true;
  333.     }
  334.  
  335.     public function RemoveTables()
  336.     {
  337.         //remove main table
  338.         if(!$this->DbExecute("DROP TABLE IF EXISTS `"._DB_PREFIX_."advancedexport`"))
  339.             return false;
  340.         return true;
  341.     }
  342.  
  343.     public function getContent()
  344.     {
  345.         $this->_html = '';
  346.         $this->_postProcess();
  347.  
  348.         if ($this->getValue('add_model') || Tools::isSubmit('updateadvancedexport')  || Tools::isSubmit('btnSubmit'))
  349.         {
  350.             $this->_html .= $this->displayAddModelForm($helper = new HelperForm());
  351.         }
  352.         else
  353.             $this->_html .= $this->displayIndexForm();
  354.  
  355.         $this->addHeader();
  356.         return $this->_html;
  357.     }
  358.  
  359.     public function addHeader()
  360.     {
  361.         $this->addCSS($this->_path.'views/css/admin.css');
  362.         $this->addCSS($this->_path.'views/css/jquery.percentageloader-0.1.css');
  363.  
  364.         if(_PS_VERSION_ >= 1.6) {
  365.             $this->addJS(_PS_JS_DIR_ . 'jquery/ui/jquery.ui.sortable.min.js');
  366.             $this->addJS($this->_path.'views/js/admin.js');
  367.         } else {
  368.             $this->addJS($this->_path.'views/js/jquery-ui-1.10.4.custom.min.js');
  369.             $this->addJS($this->_path.'views/js/admin15.js');
  370.         }
  371.  
  372.         $this->addJS($this->_path.'views/js/jquery.percentageloader-0.1.min.js');
  373.         $this->addJS($this->_path.'views/js/jquery.bsmselect.js');
  374.         $this->addJS($this->_path.'views/js/jquery.bsmselect.compatibility.js');
  375.         $this->addJS($this->_path.'views/js/jquery.bsmselect.sortable.js');
  376.  
  377.         $this->addJS($this->_path.'views/js/jquery.cooki-plugin.js');
  378.     }
  379.  
  380.     public function _postProcess()
  381.     {
  382.         if($this->getValue("ajax"))
  383.             $this->hookAjaxCall();
  384.  
  385.         if ($this->_postValidation() == false)
  386.             return false;
  387.  
  388.         $errors = '';
  389.  
  390.         if (Tools::isSubmit('btnSubmit') )
  391.         {
  392.             if($this->saveModel())
  393.                 $this->redirect('saveModelConfirmation');
  394.         }
  395.         else if(Tools::isSubmit('deleteadvancedexport'))
  396.         {
  397.             if(!$this->deleteModel())
  398.                 $this->redirect('deleteModelConfirmation');
  399.         }
  400.         elseif ($this->isSubmit('duplicateadvancedexport') && $this->getValue('id_advancedexport'))
  401.         {
  402.             $time_start = microtime(true);  //debug
  403.  
  404.             $this->getExportType($this->getValue('id_advancedexport'));
  405.  
  406.             if($this->showTimeAndMemory) // debug
  407.                 $this->showTimeAndMemoryUsage($time_start); // debug
  408.             else
  409.                 $this->redirect('exportConfirmation');
  410.         }
  411.         elseif ($this->isSubmit('viewfiles') && $this->getValue('url'))
  412.         {
  413.             $this->getFile($this->getValue('url'));
  414.         }
  415.         elseif ($this->isSubmit('deletefiles') && $url = $this->getValue('url'))
  416.         {
  417.             $this->deleteFile($url);
  418.             $this->redirect('deleteFileConfirmation');
  419.         }
  420.         elseif (Tools::isSubmit('saveModelConfirmation'))
  421.             $this->_html .= $this->displayConfirmation($this->l('Model save successfully.'));
  422.         elseif (Tools::isSubmit('deleteModelConfirmation'))
  423.             $this->_html .= $this->displayConfirmation($this->l('Model delete successfully.'));
  424.         elseif (Tools::isSubmit('deleteFileConfirmation'))
  425.             $this->_html .= $this->displayConfirmation($this->l('File delete successfully.'));
  426.         elseif (Tools::isSubmit('exportConfirmation'))
  427.             $this->_html .= $this->displayConfirmation($this->l('Export finish successfully.'));
  428.  
  429.         if ($errors)
  430.             $this->_html .= $this->displayError($errors);
  431.     }
  432.  
  433.     protected function _postValidation()
  434.     {
  435.         $this->_errors = array();
  436.  
  437.         if (Tools::isSubmit('btnSubmit') )
  438.         {
  439.             $fields = Tools::getValue('fields');
  440.             if (!is_array($fields))
  441.                       $this->_errors[] = $this->l('You must choose at least one field.');
  442.             if (!Validate::isName(Tools::getValue('name')) || Tools::getValue('name') == '')
  443.                       $this->_errors[] = $this->l('Invalid or empty name.');
  444.             if (Tools::getValue('filename') != '' && !Validate::isName(Tools::getValue('filename')))
  445.                       $this->_errors[] = $this->l('Invalid file name.');
  446.             if (Tools::getValue('date_from') != '' && !Validate::isDate(Tools::getValue('date_from')))
  447.                       $this->_errors[] = $this->l('Invalid date from.');
  448.             if (Tools::getValue('date_to') != '' && !Validate::isDate(Tools::getValue('date_to')))
  449.                       $this->_errors[] = $this->l('Invalid date to.');
  450.             if (Tools::getValue('start_id') != '' && !Validate::isInt(Tools::getValue('start_id')))
  451.                       $this->_errors[] = $this->l('Invalid begin id field.');
  452.             if (Tools::getValue('end_id') != '' && !Validate::isInt(Tools::getValue('end_id')))
  453.                       $this->_errors[] = $this->l('Invalid finish id field.');
  454.         }
  455.  
  456.         if (count($this->_errors))
  457.         {
  458.             foreach ($this->_errors as $err)
  459.             $this->_html .= '<div class="alert alert-danger">'.$err.'</div>';
  460.  
  461.             return false;
  462.         }
  463.  
  464.         return true;
  465.     }
  466.  
  467.     public function saveModel()
  468.     {
  469.         $functionName = ($this->getValue('type') ? $this->getValue('type')  : '').'FormFields';
  470.         $specific = $this->$functionName();
  471.  
  472.         $to_serialize = null;
  473.         foreach ($specific as $key => $value) {
  474.             $trimmed = str_replace('[]', '', $value['name']);
  475.             if($this->getValue($trimmed) != "")
  476.                 $to_serialize[$value['name']] = $this->getValue($trimmed);
  477.         }
  478.  
  479.         $AdvancedExport = new AdvancedExportClass($this->getValue('id_advancedexport'));
  480.         $AdvancedExport->copyFromPost();
  481.         $AdvancedExport->fields = Tools::jsonEncode($to_serialize);
  482.         $AdvancedExport->save();
  483.  
  484.         return true;
  485.     }
  486.  
  487.     public function getValue($value)
  488.     {
  489.         return Tools::getValue($value);
  490.     }
  491.  
  492.     public function redirect($action)
  493.     {
  494.         Tools::redirectAdmin(AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&'.$action);
  495.     }
  496.  
  497.     public function deleteModel()
  498.     {
  499.         $AdvancedExport = new AdvancedExportClass($this->getValue('id_advancedexport'));
  500.         $AdvancedExport->delete();
  501.  
  502.         return true;
  503.     }
  504.  
  505.     public function addCSS($path)
  506.     {
  507.         return $this->context->controller->addCSS($path);
  508.     }
  509.  
  510.     public function addJS($path)
  511.     {
  512.         return $this->context->controller->addJS($path);
  513.     }
  514.  
  515.     public function displayAddModelForm($helper)
  516.     {
  517.         $type = null;
  518.         if($id = Tools::getValue("id_advancedexport"))
  519.         {
  520.             $aec = new AdvancedExportClass($id);
  521.             if(is_object($aec))
  522.                 $type = $aec->type;
  523.         }
  524.         else
  525.         {
  526.             $type = Tools::getValue("type");
  527.         }
  528.  
  529.         $helper->module = $this;
  530.         $helper->show_toolbar = true;
  531.         $helper->table = 'advancedexport';
  532.         $helper->id = (int)$id;
  533.         $helper->default_form_language = (int)Configuration::get("PS_LANG_DEFAULT");
  534.         $helper->allow_employee_form_lang = (int)Configuration::get("PS_LANG_DEFAULT");
  535.         $helper->submit_action = 'btnSubmit';
  536.         $helper->token = $this->getValue('token');
  537.         $helper->currentIndex = $this->getAdminLink().'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
  538.         $helper->fields_value = $this->getModelFieldsValues($type);
  539.  
  540.         return $helper->generateForm( $this->ModelFormFields($type) );
  541.     }
  542.  
  543.     public function getAdminLink()
  544.     {
  545.         return $this->context->link->getAdminLink('AdminModules', false);
  546.     }
  547.  
  548.     public function getModelFieldsValues($type)
  549.     {
  550.         $fields = array('id_advancedexport', 'type', 'name', 'delimiter', 'separator', 'id_lang', 'charset', 'add_header', 'decimal_separator', 'decimal_round', 'strip_tags', 'file_type', 'only_new', 'save_type', 'shops', 'image_type', 'filename', 'email', 'ftp_hostname', 'ftp_user_pass', 'ftp_user_name', 'date_from', 'date_to', 'start_id', 'end_id');
  551.         $fields_specific = null;
  552.         $fields_value = null;
  553.  
  554.         if($this->getValue('id_advancedexport'))
  555.         {
  556.             $ae = new AdvancedExportClass($this->getValue('id_advancedexport'));
  557.             $fields_specific = Tools::jsonDecode($ae->fields, true);
  558.         }
  559.         foreach ($fields as $field)
  560.         {
  561.             if (Tools::getValue($field))
  562.                 $fields_value[$field] = Tools::getValue($field);
  563.             else if (isset($ae) && isset($ae->$field))
  564.                 $fields_value[$field] = $ae->$field;
  565.             else
  566.                 $fields_value[$field] = '';
  567.           }
  568.  
  569.           $functionName = ($type ? $type : 'products').'FormFields';
  570.           $specific = $this->$functionName();
  571.  
  572.           foreach ($specific as $value)
  573.           {
  574.             if(isset($fields_specific[$value['name']]))
  575.             {
  576.                 $fields_value[$value['name']] = $fields_specific[$value['name']];
  577.             }
  578.             else
  579.             {
  580.                 $fields_value[$value['name']] = null;
  581.              }
  582.           }
  583.  
  584.         if(isset($fields_specific["categories"])) {
  585.             $this->selected_cat = $fields_specific["categories"];
  586.         }
  587.  
  588.           return $fields_value;
  589.     }
  590.  
  591.     public function ModelFormFields($type)
  592.     {
  593.         $functionName = ($type ? $type : 'products').'FormFields';
  594.         $result = null;
  595.  
  596.         $result[0] = array(
  597.             'form' => array(
  598.                 'legend' => array(
  599.                     'title' => $this->l('Settings Form'),
  600.                     'icon' => 'icon-envelope'
  601.                 ),
  602.                 'input' => array(
  603.                 array(
  604.                     'type' => 'hidden',
  605.                     'name' => 'id_advancedexport'
  606.                 ),
  607.                 array(
  608.                     'type' => 'hidden',
  609.                     'name' => 'type'
  610.                 ),
  611.                 array(
  612.                     'type' => 'text',
  613.                     'label' => $this->l('Name'),
  614.                     'name' => 'name',
  615.                     'required' => true,
  616.                     'desc' =>$this->l('Settings insternal name')
  617.                 ),
  618.                 array(
  619.                     'type' => 'text',
  620.                     'label' => $this->l('File name'),
  621.                     'name' => 'filename',
  622.                     'desc' =>$this->l('You can set name for file or leave blank name will be given by system.')
  623.                 ),
  624.                 array(
  625.                     'type' => 'select',
  626.                     'label' => $this->l('Delimiter'),
  627.                     'name' => 'delimiter',
  628.                     'options' => array(
  629.                         'query' => array(
  630.                             array(
  631.                                 'value' => ",",
  632.                                 'name' => "[,] ".$this->l("comma")
  633.                             ),
  634.                             array(
  635.                                 'value' => ";",
  636.                                 'name' => "[;] ". $this->l("semi-colons")
  637.                             ),
  638.                             array(
  639.                                 'value' => ":",
  640.                                 'name' => "[:] ". $this->l("colons")
  641.                             ),
  642.                             array(
  643.                                 'value' => "|",
  644.                                 'name' => "[|] ". $this->l("pipes")
  645.                             ),
  646.                             array(
  647.                                 'value' => "~",
  648.                                 'name' => "[~] ". $this->l("tilde")
  649.                             )
  650.                         ),
  651.                         'id' => 'value',
  652.                         'name' => 'name'
  653.                     ),
  654.                     'desc' =>$this->l('Separrator for each line in csv file')
  655.                 ),
  656.                 array(
  657.                     'type' => 'select',
  658.                     'label' => $this->l('Separator'),
  659.                     'name' => 'separator',
  660.                     'options' => array(
  661.                         'query' => array(
  662.                             array(
  663.                                 'value' => '&#34;',
  664.                                 'name' => '["] '.$this->l("quotation marks")
  665.                             ),
  666.                             array(
  667.                                 'value' => "'",
  668.                                 'name' => "['] ". $this->l("single quotation marks")
  669.                             )
  670.                         ),
  671.                         'id' => 'value',
  672.                         'name' => 'name'
  673.                     ),
  674.                     'desc' =>$this->l('Separrator for each value in csv file')
  675.                 ),
  676.                 array(
  677.                     'type' => 'select',
  678.                     'label' => $this->l('Language'),
  679.                     'name' => 'id_lang',
  680.                     'col' => '4',
  681.                     'options' => array(
  682.                         'query' => Language::getLanguages(),
  683.                         'id' => 'id_lang',
  684.                         'name' => 'name'
  685.                     ),
  686.                     'desc' =>$this->l('Default utf-8.')
  687.  
  688.                 ),
  689.                 array(
  690.                     'type' => 'select',
  691.                     'label' => $this->l('Encoding type'),
  692.                     'name' => 'charset',
  693.                     'col' => '4',
  694.                     'options' => array(
  695.                         'query' => $this->getCharsets(),
  696.                         'id' => 'name',
  697.                         'name' => 'name'
  698.                     ),
  699.                     'desc' =>$this->l('Default utf-8.')
  700.  
  701.                 ),
  702.                 array(
  703.                     'type' => 'select',
  704.                     'label' => $this->l('Decimal separator'),
  705.                     'name' => 'decimal_separator',
  706.                     'options' => array(
  707.                         'query' => array(
  708.                             array(
  709.                                 'value' => ',',
  710.                                 'name' => '[,] '.$this->l("comma")
  711.                             ),
  712.                             array(
  713.                                 'value' => ".",
  714.                                 'name' => "[.] ". $this->l("dot")
  715.                             )
  716.                         ),
  717.                         'default' => array(
  718.                             'label' => $this->l('default'),
  719.                             'value' => -1
  720.                         ),
  721.                         'id' => 'value',
  722.                         'name' => 'name'
  723.                     )
  724.                 ),
  725.                 array(
  726.                     'type' => 'select',
  727.                     'label' => $this->l('Round values'),
  728.                     'name' => 'decimal_round',
  729.                     'options' => array(
  730.                         'default' => array(
  731.                             'label' => $this->l('default'),
  732.                             'value' => '-1'
  733.                         ),
  734.                         'query' => array(
  735.                             array(
  736.                                 'value' => '0',
  737.                                 'label' => '0'
  738.                             ),
  739.                             array(
  740.                                 'value' => '1',
  741.                                 'label' => '1'
  742.                             ),
  743.                             array(
  744.                                 'value' => '2',
  745.                                 'label' => '2'
  746.                             ),
  747.                             array(
  748.                                 'value' => '3',
  749.                                 'label' => '3'
  750.                             ),
  751.                             array(
  752.                                 'value' => '4',
  753.                                 'label' => '4'
  754.                             ),
  755.                             array(
  756.                                 'value' => '5',
  757.                                 'label' => '5'
  758.                             ),
  759.                             array(
  760.                                 'value' => '6',
  761.                                 'label' => '6'
  762.                             )
  763.                         ),
  764.                         'id' => 'value',
  765.                         'name' => 'label'
  766.                     )
  767.                 ),
  768.                 array(
  769.                     'type' => 'select',
  770.                     'label' => $this->l('Save type'),
  771.                     'name' => 'save_type',
  772.                     'default' => '0',
  773.                     'options' => array(
  774.                         'query' => $this->getSaveTypes(),
  775.                         'id' => 'id',
  776.                         'name' => 'name'
  777.                     ),
  778.                     'desc' =>$this->l('Save your file, sent to server or email.')
  779.  
  780.                 ),
  781.                 array(
  782.                     'type' => 'text',
  783.                     'label' => $this->l('Email'),
  784.                     'name' => 'email',
  785.                     'class' => 'process2'
  786.                 ),
  787.                 array(
  788.                     'type' => 'text',
  789.                     'label' => $this->l('Hostname'),
  790.                     'name' => 'ftp_hostname',
  791.                     'class' => 'process1'
  792.                 ),
  793.                 array(
  794.                     'type' => 'text',
  795.                     'label' => $this->l('Username'),
  796.                     'name' => 'ftp_user_name',
  797.                     'class' => 'process1'
  798.                 ),
  799.                 array(
  800.                     'type' => 'text',
  801.                     'label' => $this->l('Password'),
  802.                     'name' => 'ftp_user_pass',
  803.                     'class' => 'process1'
  804.                 ),
  805.                 array(
  806.                     'type' => $this->switch,
  807.                     'label' => $this->l('Display labels'),
  808.                     'name' => 'add_header',
  809.                     'class' => 't',
  810.                     'is_bool' => true,
  811.                     'values' => array(
  812.                         array(
  813.                             'id' => 'active_on',
  814.                             'value' => 1,
  815.                             'label' => $this->l('Yes')
  816.                         ),
  817.                         array(
  818.                             'id' => 'active_off',
  819.                             'value' => 0,
  820.                             'label' => $this->l('No')
  821.                         )
  822.                     ),
  823.                 ),
  824.                 array(
  825.                     'type' => $this->switch,
  826.                     'label' => $this->l('Strip tags'),
  827.                     'name' => 'strip_tags',
  828.                     'class' => 't',
  829.                     'is_bool' => true,
  830.                     'values' => array(
  831.                         array(
  832.                             'id' => 'active_on',
  833.                             'value' => 1,
  834.                             'label' => $this->l('Yes')
  835.                         ),
  836.                         array(
  837.                             'id' => 'active_off',
  838.                             'value' => 0,
  839.                             'label' => $this->l('No')
  840.                         )
  841.                     ),
  842.                 ),
  843.                 array(
  844.                     'type' => $this->switch,
  845.                     'label' => $this->l('Export not exported'),
  846.                     'name' => 'only_new',
  847.                     'class' => 't',
  848.                     'is_bool' => true,
  849.                     'desc' =>$this->l('Export not exported yet.'),
  850.                     'values' => array(
  851.                         array(
  852.                             'id' => 'active_on',
  853.                             'value' => 1,
  854.                             'label' => $this->l('Yes')
  855.                          ),
  856.                         array(
  857.                             'id' => 'active_off',
  858.                             'value' => 0,
  859.                             'label' => $this->l('No')
  860.                          )
  861.                     ),
  862.                 ),
  863.                 array(
  864.                     'type' => 'text',
  865.                     'label' => $this->l('Begin id'),
  866.                     'name' => 'start_id',
  867.                     'desc' =>$this->l('You can specyify start id number.')
  868.                 ),
  869.                 array(
  870.                     'type' => 'text',
  871.                     'label' => $this->l('Finish id'),
  872.                     'name' => 'end_id',
  873.                     'desc' =>$this->l('You can specify end id number.')
  874.                 ),
  875.                 array(
  876.                     'type' => (_PS_VERSION_ >= 1.6 ? 'datetime' : 'date'),
  877.                     'label' => $this->l('From'),
  878.                     'name' => 'date_from',
  879.                     'size' => 10,
  880.                     'maxlength' => 10,
  881.                     'desc' =>$this->l('Format: 2011-12-31 HH-MM-SS(inclusive).')
  882.                 ),
  883.                 array(
  884.                     'type' => (_PS_VERSION_ >= 1.6 ? 'datetime' : 'date'),
  885.                     'label' => $this->l('To'),
  886.                     'name' => 'date_to',
  887.                     'size' => 10,
  888.                     'maxlength' => 10,
  889.                     'desc' =>$this->l('Format: 2012-12-31 HH-MM-SS(inclusive).')
  890.                 )
  891.             ),
  892.             'submit' => array(
  893.                 'title' => $this->l('Save'),
  894.              ),
  895.             'buttons' => array(
  896.                     'cancelBlock' => array(
  897.                         'title' => $this->l('Cancel'),
  898.                         'href' => $this->getAdminUrl(),
  899.                         'icon' => 'process-icon-cancel'
  900.                     )
  901.                 )
  902.             ),
  903.          );
  904.  
  905.         if($type != "orders" && $type != "newsletters" && $type != "customers") {
  906.             $result[0]['form']['input'][] = array(
  907.                 'type' => 'select',
  908.                 'label' => $this->l('Image type'),
  909.                 'name' => 'image_type',
  910.                 'col' => '4',
  911.                 'options' => array(
  912.                     'query' => ImageType::getImagesTypes($type),
  913.                     'id' => 'name',
  914.                     'name' => 'name'
  915.                 ),
  916.             );
  917.         }
  918.  
  919.         $specific = $this->$functionName();
  920.         $result[0]['form']['input'] = array_merge( $result[0]['form']['input'], $specific);
  921.  
  922.         return $result;
  923.     }
  924.  
  925.     public function getCharsets()
  926.     {
  927.         return array(
  928.             array('id' => 1, 'name' => "UTF-8"),
  929.             array('id' => 2, 'name' => "ISO-8859-1"),
  930.             array('id' => 3, 'name' => "GB2312"),
  931.             array('id' => 4, 'name' => "Windows-1251"),
  932.             array('id' => 5, 'name' => "Windows-1252"),
  933.             array('id' => 6, 'name' => "Shift JIS"),
  934.             array('id' => 7, 'name' => "GBK"),
  935.             array('id' => 8, 'name' => "Windows-1256"),
  936.             array('id' => 9, 'name' => "ISO-8859-2"),
  937.             array('id' => 10, 'name' => "EUC-JP"),
  938.             array('id' => 11, 'name' => "ISO-8859-15"),
  939.             array('id' => 12, 'name' => "ISO-8859-9"),
  940.             array('id' => 13, 'name' => "Windows-1250"),
  941.             array('id' => 14, 'name' => "Windows-1254"),
  942.             array('id' => 15, 'name' => "EUC-KR"),
  943.             array('id' => 16, 'name' => "Big5"),
  944.             array('id' => 17, 'name' => "Windows-874"),
  945.             array('id' => 18, 'name' => "US-ASCII"),
  946.             array('id' => 19, 'name' => "TIS-620"),
  947.             array('id' => 20, 'name' => "ISO-8859-7"),
  948.             array('id' => 21, 'name' => "Windows-1255"),
  949.         );
  950.     }
  951.  
  952.     public function getSaveTypes()
  953.     {
  954.         return array(
  955.             array('id' => 0, 'name' => $this->l("Save to disc"), 'short_name' => $this->l("disc")),
  956.             array('id' => 1, 'name' => $this->l("Save to server"), 'short_name' => $this->l("ftp")),
  957.             array('id' => 2, 'name' => $this->l("Sent to emal"), 'short_name' => $this->l("email"))
  958.         );
  959.     }
  960.  
  961.     public function getFileTypes()
  962.     {
  963.         return array(
  964.             array('id' => 1, 'name' => "csv"),
  965.             array('id' => 2, 'name' => "xml")
  966.         );
  967.     }
  968.  
  969.     public function getAdminUrl()
  970.     {
  971.         return AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules');
  972.     }
  973.  
  974.     public  function isFeatureActive()
  975.     {
  976.         return Shop::isFeatureActive();
  977.     }
  978.  
  979.     public function isSubmit($value)
  980.     {
  981.         return Tools::isSubmit($value);
  982.     }
  983.  
  984.     public function getExportType($id_advancedexport)
  985.     {
  986.         Configuration::updateGlobalValue("AdvancedExport_CURRENT", 0);
  987.  
  988.         $ae = new AdvancedExportClass($id_advancedexport);
  989.         $this->createExportFile($ae);
  990.     }
  991.  
  992.     public function showTimeAndMemoryUsage($time_start)
  993.     {
  994.         echo "</br></br></br></br></br></br></br><b>Using <span style='font-size:20px;'>". memory_get_peak_usage(1). "</span> bytes of ram.</b>";
  995.         $time_end = microtime(true);
  996.         //dividing with 60 will give the execution time in minutes other wise seconds
  997.         $execution_time = ($time_end - $time_start)/60;
  998.         //execution time of the script
  999.         echo '</br><b>Total Execution Time: <span style="font-size:20px;"> '.$execution_time.'</span> Mins</b>';
  1000.     }
  1001.  
  1002.     public function createExportFile($ae)
  1003.     {
  1004.         ini_set('memory_limit','725M');
  1005.         $ae->fields = Tools::jsonDecode($ae->fields, true);
  1006.         $sorted_fields = $this->getLabelsAndFields($ae->type, $ae->fields);
  1007.  
  1008.         $functionName = $ae->type.'Query';
  1009.         $elements = $this->$functionName($ae, $sorted_fields);
  1010.         //total number for progress bar
  1011.         Configuration::updateGlobalValue("AdvancedExport_TOTAL", DB::getInstance()->numRows());
  1012.  
  1013.         $url = $this->writeToFile($ae, $sorted_fields, $elements);
  1014.  
  1015.         $this->saveLastId($ae, $this->lastElement);
  1016.  
  1017.         $this->processFile($ae->save_type, $url, $ae);
  1018.     }
  1019.  
  1020.     public function getLabelsAndFields($type, $fields)
  1021.     {
  1022.         if($fields)
  1023.         {
  1024.             set_time_limit(0);
  1025.  
  1026.             foreach ($fields['fields[]'] as $key => $field)
  1027.             {
  1028.                 $fields['allexportfields'][] = $this->{$type}[$field]['field'];
  1029.                 $fields['labels'][] = $this->{$type}[$field]['name'];
  1030.  
  1031.                 if($this->{$type}[$field]['database'] == 'other')
  1032.                 {
  1033.                     $fields['otherfields'][$this->{$type}[$field]['field']] = $this->{$type}[$field]['field'];
  1034.                 }
  1035.                 else
  1036.                 {
  1037.                     //JeΕ›li jest podany alias w array
  1038.                     //to uΕΌyj ten alias jako prefix w nazwie tabeli
  1039.                     //wraz z kropkΔ….
  1040.                     $alias = (isset($this->{$type}[$field]['alias']) ?  $this->{$type}[$field]['alias'].'.' : '');
  1041.  
  1042.                     //jeΕ›li wartoΕ›Δ‡ as jest false
  1043.                     //to utwΓ³rz polecenie as
  1044.                     //wymagane przy nazwach pΓ³l ktΓ³re siΔ™powtarzajΔ…
  1045.                     if (isset($this->{$type}[$field]['as']) && $this->{$type}[$field]['as']) {
  1046.                         $fields['sqlfields'][] = $alias."`".Tools::substr(strstr($this->{$type}[$field]['field'], '_'), Tools::strlen('_'))."` as " .$this->{$type}[$field]['field']."";
  1047.                     }else {
  1048.                         $fields['sqlfields'][] = $alias . "`" . $this->{$type}[$field]['field'] . "`";
  1049.                     }
  1050.                 }
  1051.  
  1052.                 if(isset($this->{$type}[$field]['attribute']) && $this->{$type}[$field]['attribute'] == true)
  1053.                 {
  1054.                     $fields['attribute_fields'][] = $this->{$type}[$field]['field'];
  1055.                 }
  1056.  
  1057.                 if($this->{$type}[$field]['database'] == "order_detail")
  1058.                 {
  1059.                     $fields['order_detail'] = true;
  1060.                     $fields['sqlfields'][] = "od.`product_id`";
  1061.                     $fields['sqlfields'][] = "od.`product_attribute_id`";
  1062.                     $fields['sqlfields'][] = "o.`id_cart`";
  1063.                 }
  1064.             }
  1065.         }
  1066.  
  1067.         return $fields;
  1068.     }
  1069.  
  1070.     //manufacturer name
  1071.  
  1072.     public function writeToFile($ae, $sorted_fields, $elements)
  1073.     {
  1074.         $url = $this->getFileUrl($ae->filename, $ae->type);
  1075.  
  1076.         $file = @fopen( $url, 'w' );
  1077.         //add labels for export data ==== Nascosto da Dario
  1078.         //if($ae->add_header)
  1079.           //$this->filewrite($ae, $sorted_fields, $file);
  1080.  
  1081.         $i = 0;
  1082.         while ($element = $this->nextRow($elements))
  1083.         {
  1084.             if ($i == $this->rowsNumber - 1)
  1085.             {
  1086.                 $this->lastElement = $element;
  1087.             }
  1088.  
  1089.             Configuration::updateGlobalValue("AdvancedExport_CURRENT", (string)$i);
  1090.             //progress bar
  1091.             $this->getDataFromObject($element, $file, $sorted_fields, $ae);
  1092.             $i++; //progress bar
  1093.         }
  1094.  
  1095.         //close file
  1096.         if($file)
  1097.             fclose($file);
  1098.  
  1099.         return $url;
  1100.     }
  1101.  
  1102.     public function nextRow ($elements)
  1103.     {
  1104.         return Db::getInstance(_PS_USE_SQL_SLAVE_)->nextRow($elements);
  1105.     }
  1106.  
  1107.     public function hookAjaxCall()
  1108.     {
  1109.         header("Cache-Control: no-cache, must-revalidate");
  1110.         header("Expires: 0");
  1111.  
  1112.         $action = Tools::getValue('action');
  1113.  
  1114.         switch ($action) {
  1115.             case "getCurrentIndex":
  1116.                 $response = array(
  1117.                     'total' => (int)Configuration::get("AdvancedExport_TOTAL"),
  1118.                     'current' => (int)Configuration::get("AdvancedExport_CURRENT")
  1119.                 );
  1120.                 echo Tools::jsonEncode($response);
  1121.                 break;
  1122.         }
  1123.     }
  1124.  
  1125.     public function getFileUrl($filename, $type)
  1126.     {
  1127.         //open file for write
  1128.         if($filename == null || $filename == "")
  1129.             $filename = $type.date('Y-m-d_His').".csv";
  1130.         else
  1131.             $filename = $filename.".csv";
  1132.  
  1133.         $url = _PS_ROOT_DIR_.'/modules/advancedexport/csv/'.$type;
  1134.         if (!is_dir($url)) {
  1135.             mkdir($url);
  1136.         }
  1137.  
  1138.         return $url.'/'.$filename;
  1139.     }
  1140.  
  1141.     public function getDataFromObject($element, $file, $sorted_fields, $ae)
  1142.     {
  1143.         $id_object = $element[$this->getId($ae->type)];
  1144.         //instance od product object
  1145.         $obj = $this->getObject($id_object, $ae->type);
  1146.         //has attributes clean varible
  1147.         $this->hasAttr = 0;
  1148.         if(isset($sorted_fields['otherfields']))
  1149.         {
  1150.             foreach ($sorted_fields['otherfields'] as $key => $value) {
  1151.                 $run = $ae->type.'_'.$value;
  1152.                 $element[$value] = $this->$run($obj, $ae, $element);
  1153.             }
  1154.         }
  1155.  
  1156.         if($ae->type == "products" && $ae->fields['attributes'] && isset($sorted_fields['attribute_fields']))
  1157.             $this->processWithAttributes($obj, $element, $file, $sorted_fields, $ae);
  1158.  
  1159.         if($this->hasAttr == 0)
  1160.             $this->fputToFile($file, $sorted_fields['allexportfields'], $element, $ae);
  1161.     }
  1162.  
  1163.     //get default category name
  1164.  
  1165.     public function getObject($id_object, $type, $full = false)
  1166.     {
  1167.         switch ($type) {
  1168.             case 'categories':
  1169.                 $type = new Category($id_object);
  1170.                 break;
  1171.             case 'manufacturers':
  1172.                 $type = new Manufacturer($id_object);
  1173.                 break;
  1174.             case 'newsletters':
  1175.                 $type = null;
  1176.                 break;
  1177.             case 'orders':
  1178.                 $type = new Order($id_object);
  1179.                 break;
  1180.             case 'products':
  1181.                 $type = new Product($id_object, $full);
  1182.                 break;
  1183.             case 'suppliers':
  1184.                 $type = new Supplier($id_object);
  1185.                 break;
  1186.             default:
  1187.                 $type = null;
  1188.                 break;
  1189.         }
  1190.  
  1191.         return $type;
  1192.     }
  1193.  
  1194.     //get images url
  1195.  
  1196.     public function processWithAttributes($obj, $element, $file, $sorted_fields, $ae)
  1197.     {
  1198.         $combArray = null;
  1199.         $combArray = $this->getProductCombination($obj, $ae);
  1200.        
  1201.         if (isset($combArray)){
  1202.             $this->hasAttr = 1;
  1203.             foreach ($combArray AS $id_products_attribute => $products_attribute) {
  1204.                 $list = null;
  1205.                 $elementCopy = null;
  1206.                 $elementCopy = $element;
  1207.  
  1208.                 foreach ($sorted_fields['attribute_fields'] as $key => $value) {
  1209.                     $run = 'combination_' . $value;
  1210.                     $elementCopy[$value] = $this->$run($obj, $products_attribute, $ae);
  1211.                 }
  1212.  
  1213.                 $this->fputToFile($file, $sorted_fields['allexportfields'], $elementCopy, $ae);
  1214.             }
  1215.         }
  1216.     }
  1217.  
  1218.     //tags
  1219.  
  1220.     //$dati_csv = $readyForExport[$value];
  1221.     //echo " <script type=\"text/javascript\">alert('$dati_csv')</script>";
  1222.  
  1223. private $count = null; // put this within class
  1224.  
  1225.         public function fputToFile($file, $allexportfields, $object, $ae)
  1226.         {
  1227.             if($allexportfields && $file && $object && $ae)
  1228.             {
  1229.                 //one ready for export product
  1230.                 $readyForExport = array();
  1231.  
  1232.  
  1233.                 //put in correct sort order
  1234.                 foreach ($allexportfields as $value)
  1235.                 {
  1236.                     $object = $this->processDecimalSettings($object, $ae, $value);
  1237.                     $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);
  1238.  
  1239.                 }
  1240.  
  1241.                 //print_r('The id_order is: ' . $readyForExport['id_order']); // see if this is empty $readyForExport['id_order']
  1242.  
  1243.                 $this->counter[$readyForExport['id_order']] = (!empty($this->counter[$readyForExport['id_order']])) ? ++$this->counter[$readyForExport['id_order']] : 1; // try here
  1244.                 $readyForExport['orderLine'] = $this->counter[$readyForExport['id_order']]; // and try here
  1245.                
  1246.                 //print_r('The id_order is added: ' . $readyForExport['orderLine']); // see if it is added
  1247.  
  1248.                 //echo '<pre>' . var_dump($readyForExport) . '</pre>';
  1249.  
  1250.                 // modifiche === Dario === prezzo
  1251.                 $newPrice = substr($readyForExport['product_price'], 0, strpos($readyForExport['product_price'], "."));
  1252.                 $readyForExport['product_price'] = $newPrice;
  1253.  
  1254.                 // === data
  1255.  
  1256.                 $newDateAdd = new DateTime($readyForExport['date_add']);
  1257.                 $readyForExport['date_add'] = $newDateAdd->format('d/m/Y');
  1258.  
  1259.  
  1260.                 // aggiungo 21 giorni - 3 settimane - alla data di acquisto
  1261.                 $date_mod = clone $newDateAdd;
  1262.                 $date_mod->add(new DateInterval('P21D'));
  1263.                 $readyForExport['delivery_date'] = $date_mod->format('d/m/Y');
  1264.  
  1265.                 // === data invoice
  1266.                 $newDateInvoice = clone $newDateAdd;
  1267.                 $readyForExport['invoice_date'] = $newDateAdd->format('d/m/Y');
  1268.  
  1269.                 //scambio l'id customer con il codice_target
  1270.  
  1271.                 //$readyForExport['codice_target'] = 8989;
  1272.  
  1273.                 $textTarget = (string)$readyForExport['codice_target'];
  1274.  
  1275.                 $readyForExport['id_customer'] = $textTarget;
  1276.  
  1277.                 // aggiungo gli zeri davanti al customer id
  1278.                 $id_count = strlen($readyForExport['id_customer']);            
  1279.                 if ($id_count == 1) {
  1280.                    
  1281.                     $newCustomer = "0000000".$readyForExport['id_customer'];
  1282.                     $readyForExport['id_customer'] = $newCustomer;
  1283.                    
  1284.                 }elseif ($id_count == 2) {
  1285.                    
  1286.                     $newCustomer = "000000".$readyForExport['id_customer'];
  1287.                     $readyForExport['id_customer'] = $newCustomer;
  1288.                    
  1289.                 }elseif ($id_count == 3) {
  1290.                    
  1291.                     $newCustomer = "00000".$readyForExport['id_customer'];
  1292.                     $readyForExport['id_customer'] = $newCustomer;
  1293.                    
  1294.                 }elseif ($id_count == 4) {
  1295.                     $newCustomer = "0000".$readyForExport['id_customer'];
  1296.                     $readyForExport['id_customer'] = $newCustomer;
  1297.                    
  1298.                 }elseif ($id_count == 5) {
  1299.                     $newCustomer = "000".$readyForExport['id_customer'];
  1300.                     $readyForExport['id_customer'] = $newCustomer;
  1301.                    
  1302.                 }elseif ($id_count == 6) {
  1303.                     $newCustomer = "00".$readyForExport['id_customer'];
  1304.                     $readyForExport['id_customer'] = $newCustomer;
  1305.                    
  1306.                 }
  1307.  
  1308.                 // elaboro lo SKU
  1309.  
  1310.                 $textSku = (string)$readyForExport['product_name'];
  1311.                
  1312.                 $newSku_1 = $readyForExport['product_name'];
  1313.  
  1314.                 $newSku_1 = substr($newSku_1,0,4);
  1315.                 $newSku_2 = "/".substr($textSku,-4,4);
  1316.  
  1317.                 $newSku_tot = $newSku_1.$newSku_2;
  1318.                
  1319.                 $newSku_tot = str_replace(' ', '', $newSku_tot);
  1320.                 $newSku_tot = str_replace('-', '', $newSku_tot);
  1321.                 $newSku_tot = str_replace('co', '', $newSku_tot);
  1322.                
  1323.                 $newSku_tot = str_replace('e', '', $newSku_tot);
  1324.                 $newSku_tot = str_replace('r', '', $newSku_tot);
  1325.  
  1326.                 $readyForExport['product_name'] = $newSku_tot;
  1327.  
  1328.  
  1329.  
  1330.                 // aggiungo un campo fisso
  1331.                 $readyForExport['causale'] = "NR";
  1332.  
  1333.                 // aggiungo un campo fisso
  1334.                 $readyForExport['ORCL'] = "ORCL";
  1335.            
  1336.                 //$readyForExport['G'] = "";
  1337.                 $readyForExport['J'] = "";
  1338.                 $readyForExport['K'] = "";
  1339.                 $readyForExport['L'] = "";
  1340.                 $readyForExport['M'] = "";
  1341.                 $readyForExport['N'] = "";
  1342.                 $readyForExport['P'] = "";
  1343.                 $readyForExport['Q'] = "";
  1344.                 $readyForExport['R'] = "30";
  1345.  
  1346.                 $index_arr=array("id_customer","date_add","ORCL","product_name","causale","product_quantity","product_price","delivery_date","id_order","J","K","L","M","N","orderLine","P","Q","R");
  1347.  
  1348.                 //riordino i campi in base a come li dispongo nella variabile $index_arr
  1349.                 $arr_t=array();
  1350.                 foreach($index_arr as $i=>$v) {
  1351.                     foreach($readyForExport as $k=>$b) {
  1352.                         if ($k==$v) $arr_t[$k]=$b;
  1353.                     }
  1354.                 }
  1355.                 $readyForExport=$arr_t;
  1356.  
  1357.             //write into csv line by line
  1358.             fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
  1359.         }
  1360.     }
  1361.  
  1362.     //tags
  1363.  
  1364.     public function saveLastId($ae, $myLastElement)
  1365.     {
  1366.         if($ae->only_new && isset($myLastElement[$this->getId($ae->type)]))
  1367.         {
  1368.             $ae->last_exported_id = $myLastElement[$this->getId($ae->type)];
  1369.             $ae->fields = Tools::jsonEncode($ae->fields);
  1370.             $ae->save();
  1371.         }
  1372.  
  1373.         return $ae;
  1374.     }
  1375.  
  1376.     public function getId ($type)
  1377.     {
  1378.         $id = '';
  1379.         switch ($type) {
  1380.             case "categories":
  1381.                 $id = "id_category";
  1382.                 break;
  1383.             case "customers":
  1384.                 $id = "id_customer";
  1385.                 break;
  1386.             case "manufacturers":
  1387.                 $id = "id_manufacturer";
  1388.                 break;
  1389.             case "newsletters":
  1390.                 $id = "id";
  1391.                 break;
  1392.             case "orders":
  1393.                 $id = "id_order";
  1394.                 break;
  1395.             case "products":
  1396.                 $id = "id_product";
  1397.                 break;
  1398.             case "suppliers":
  1399.                 $id = "id_supplier";
  1400.                 break;
  1401.         }
  1402.         return $id;
  1403.     }
  1404.  
  1405.  
  1406.     public function processFile($process, $url, $ae)
  1407.     {
  1408.         switch ($process)
  1409.         {
  1410.             case 0:
  1411.             break;
  1412.             case 1:
  1413.                 $this->ftpFile($ae->ftp_hostname, $ae->ftp_user_name, $ae->ftp_user_pass, $url);
  1414.             break;
  1415.             case 2:
  1416.                 $this->sentFile($url, $ae->email);
  1417.             break;
  1418.             default:
  1419.             break;
  1420.         }
  1421.     }
  1422.  
  1423.     public function ftpFile($ftp_hostname, $ftp_user_name, $ftp_user_pass, $export_file )
  1424.     {
  1425.         // open some file for reading
  1426.         $file = basename($export_file);
  1427.         $fp = fopen($export_file, 'r');
  1428.  
  1429.         // set up basic connection
  1430.         $conn = ftp_connect($ftp_hostname);
  1431.  
  1432.         // login with username and password
  1433.         $login_result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
  1434.  
  1435.         // try to upload $file
  1436.         if (ftp_fput($conn, $file, $fp, FTP_BINARY))
  1437.             echo "Successfully uploaded $file\n";
  1438.         else
  1439.             echo "There was a problem while uploading $file\n";
  1440.  
  1441.         // close the connection and the file handler
  1442.         ftp_close($conn);
  1443.         fclose($fp);
  1444.     }
  1445.  
  1446.             public function sentFile($export_file, $email)
  1447.             {
  1448.                 $file_attachement = null;
  1449.                 $file_attachement['content'] = Tools::file_get_contents($export_file);
  1450.                 $file_attachement['name'] = "export file";
  1451.                 $file_attachement['mime'] = 'text/csv';
  1452.  
  1453.                 $id_lang = $this->getConfiguration("PS_LANG_DEFAULT");
  1454.                 Mail::Send($id_lang, 'index', 'index', null, $email, null, null, "advanced export", $file_attachement, null, dirname(__FILE__).'/mails/');
  1455.             }
  1456.  
  1457.     public function getConfiguration($value)
  1458.     {
  1459.         return (int)Configuration::get($value);
  1460.     }
  1461.  
  1462.     public function displayIndexForm()
  1463.     {
  1464.         $html = '<script type="text/javascript">
  1465.            var urlJson = "index.php?controller=AdminModules&configure=advancedexport&module_name=advancedexport&token='.Tools::getAdminTokenLite("AdminModules").'";
  1466.        </script>';
  1467.         $html .= '<div class="row">';
  1468.             $html .= '<div class="col-lg-12">';
  1469.                 $html .= '<div id="loader-container">';
  1470.                     $html .= '<div id="topLoader"> </div>';
  1471.                 $html .= '</div>';
  1472.                 $html .= '<div class="row">';
  1473.                     $html .= '<div class="col-lg-2 col-md-3">';
  1474.                         $html .= '<div class="list-group">';
  1475.                             foreach ($this->export_types as $key => $value)
  1476.                             {
  1477.                                 $html .= '<a href="#" id="'.$value.'" class="list-group-item">'.$value.'</a>';
  1478.                             }
  1479.                         $html .= '</div>';
  1480.                     $html .= '</div>';
  1481.                     $html .= '<div class="form-horizontal col-lg-10">';
  1482.                         $html .= '<div class="list-group">';
  1483.  
  1484.                             foreach ($this->export_types as $key => $value)
  1485.                             {
  1486.                                 $html .= "<div class='product-tab-content hide' alt='$value'>";
  1487.                                 $html .= $this->displayModelListForm($helper = new HelperList(), $value);
  1488.                                 $html .= $this->displayFilesForm($helper = new HelperList(), $value);
  1489.                                 $html .= '</div>';
  1490.                             }
  1491.                         $html .= '</div>';
  1492.                     $html .= '</div>';
  1493.                 $html .= '</div>';
  1494.             $html .= '</div>';
  1495.         $html .= '</div>';
  1496.  
  1497.         return $html;
  1498.  
  1499.  
  1500.  
  1501.     }
  1502.  
  1503.     public function displayModelListForm($helper, $type)
  1504.     {
  1505.         $this->fields_list = $this->listFieldsForm();
  1506.         $links = $this->getLinks($type);
  1507.         $this->_display = 'index';
  1508.         $helper->module = $this;
  1509.         $helper->identifier = 'id_advancedexport';
  1510.         $helper->actions = array('edit', 'duplicate', 'delete');
  1511.         $helper->show_toolbar = true;
  1512.         //$helper->simple_header = true;
  1513.         $helper->shopLinkType = '';
  1514.         $helper->listTotal = count($links);
  1515.         $helper->toolbar_btn = $this->initToolbar($type);
  1516.         $helper->title = $type." models";
  1517.         $helper->table = 'advancedexport';
  1518.         $helper->token = Tools::getAdminTokenLite('AdminModules');
  1519.         $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
  1520.         $helper->tpl_vars['version'] = false;
  1521.         return $helper->generateList($links, $this->fields_list);
  1522.     }
  1523.  
  1524.     public function listFieldsForm()
  1525.     {
  1526.         return array(
  1527.             'id_advancedexport' => array(
  1528.                 'title' => $this->l('ID'),
  1529.                 'width' => 20
  1530.             ),
  1531.             'name' => array(
  1532.                 'title' => $this->l('Name'),
  1533.                 'width' => 100
  1534.             ),
  1535.             'cron_url' => array(
  1536.                 'title' => $this->l('Cron url'),
  1537.                 'type' => 'html'
  1538.             ),
  1539.             'save_type' => array(
  1540.                 'title' => $this->l('Save Type'),
  1541.                 'width' => 30
  1542.             ),
  1543.             'only_new' => array(
  1544.                 'title' => $this->l('New entries'),
  1545.                 'active' => 'only_new',
  1546.                 'type' => 'bool',
  1547.                 'align' => 'center',
  1548.                 'width' => 30,
  1549.                 'orderby' => false
  1550.             )
  1551.         );
  1552.     }
  1553.  
  1554.     //get features
  1555.  
  1556.     public function DbExecuteS($query)
  1557.     {
  1558.         return Db::getInstance()->ExecuteS( $query );
  1559.     }
  1560.  
  1561.  
  1562.     //price tax
  1563.  
  1564.     public function initToolbar($type)
  1565.     {
  1566.         $current_index = AdminController::$currentIndex;
  1567.         $token = Tools::getAdminTokenLite('AdminModules');
  1568.  
  1569.         $back = Tools::safeOutput(Tools::getValue('back', ''));
  1570.  
  1571.         if (!isset($back) || empty($back))
  1572.             $back = $current_index.'&configure='.$this->name.'&token='.$token;
  1573.  
  1574.         switch ($this->_display)
  1575.         {
  1576.             case 'add':
  1577.                 $this->toolbar_btn['cancel'] = array(
  1578.                 'href' => $back,
  1579.                 'desc' => $this->l('Cancel')
  1580.             );
  1581.             break;
  1582.             case 'edit':
  1583.                 $this->toolbar_btn['cancel'] = array(
  1584.                 'href' => $back,
  1585.                 'desc' => $this->l('Cancel')
  1586.             );
  1587.             break;
  1588.             case 'index':
  1589.                 $this->toolbar_btn['new'] = array(
  1590.                     'href' => $current_index.'&configure='.$this->name.'&token='.$token.'&add_model=1&type='.$type,
  1591.                     'desc' => $this->l('Add new')
  1592.                                            );
  1593.              break;
  1594.              default:
  1595.             break;
  1596.         }
  1597.  
  1598.         return $this->toolbar_btn;
  1599.     }
  1600.  
  1601.     //price tax without discount
  1602.  
  1603.     public function displayFilesForm($helper, $type)
  1604.     {
  1605.         $this->fields_list = $this->filesFieldsForm();
  1606.         $files = $this->getFiles($type);
  1607.         $this->_display = 'index';
  1608.         $helper->module = $this;
  1609.         $helper->identifier = 'url';
  1610.         $helper->simple_header = true;
  1611.         $helper->actions = array('view', 'delete');
  1612.         $helper->show_toolbar = true;
  1613.         $helper->shopLinkType = '';
  1614.         $helper->listTotal = count($files);
  1615.         $helper->title = $type.' files ';
  1616.         $helper->table = 'files';
  1617.         $helper->token = Tools::getAdminTokenLite('AdminModules');
  1618.         $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
  1619.  
  1620.         return $helper->generateList($files, $this->fields_list);
  1621.     }
  1622.  
  1623.     public function format_size($size) {
  1624.         $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
  1625.         if ($size == 0) { return('n/a'); } else {
  1626.             return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
  1627.     }
  1628.  
  1629.     public function filesFieldsForm()
  1630.     {
  1631.         return array(
  1632.             'id_file' => array(
  1633.                 'title' => $this->l('ID'),
  1634.                 'width' => 25
  1635.             ),
  1636.             'name' => array(
  1637.                 'title' => $this->l('Name'),
  1638.                 'width' => 100
  1639.             ),
  1640.             'filesize' => array(
  1641.                 'title' => $this->l('File size'),
  1642.                 'width' => 100
  1643.             )
  1644.         );
  1645.      }
  1646.  
  1647.     //wholesale_price
  1648.  
  1649.     public function getFiles($type)
  1650.     {
  1651.         $dirname = _PS_ROOT_DIR_.'/modules/advancedexport/csv/'.$type.'/';
  1652.         $files = glob($dirname.'*.{csv}', GLOB_BRACE);
  1653.  
  1654.         $result = array();
  1655.         $lp = 1;
  1656.  
  1657.         if($files){
  1658.             foreach ($files as $key => $value)
  1659.             {
  1660.                 $result[] = array(
  1661.                     'id_file' => $lp,
  1662.                     'name' => basename($value),
  1663.                     'filesize' => $this->format_size(filesize($value)),
  1664.                     'url' => urlencode($type.'/'.basename($value))
  1665.                 );
  1666.                 $lp++;
  1667.             }
  1668.         }
  1669.         return $result;
  1670.     }
  1671.  
  1672.  
  1673.     public function getFile($url)
  1674.     {
  1675.         $dir = (string)realpath(dirname(__FILE__).'/csv/'.$url);
  1676.         if(file_exists($dir))
  1677.         {
  1678.             header('Content-Description: File Transfer');
  1679.             header('Content-Type: application/csv');
  1680.             header('Content-Disposition: attachment; filename='.basename($dir));
  1681.             header('Content-Transfer-Encoding: binary');
  1682.             header('Expires: 0');
  1683.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  1684.             header('Pragma: public');
  1685.             header('Content-Length: ' . filesize($dir));
  1686.             ob_clean();
  1687.             flush();
  1688.             readfile($dir);
  1689.             exit;
  1690.         }
  1691.     }
  1692.  
  1693.     public function cronTask()
  1694.     {
  1695.         Context::getContext()->link = new Link();
  1696.         $id = Tools::getValue('id');
  1697.         $this->getExportType($id);
  1698.     }
  1699.  
  1700.     public function deleteFile($url)
  1701.     {
  1702.         $dir = (string)realpath(dirname(__FILE__).'/csv/'.$url);
  1703.         if(file_exists($dir))
  1704.             unlink($dir);
  1705.     }
  1706.  
  1707.     public function productsFormFields()
  1708.     {
  1709.         $fields = array(
  1710.             array(
  1711.                 'type'  => 'bsmselect',
  1712.                 'label' => $this->l('Products fields'),
  1713.                 'name'  => 'fields[]',
  1714.                 'id'  => 'fields',
  1715.                 'class' => 'sminit, ds-select',
  1716.                 'multiple' => true,
  1717.                 'options' => array(
  1718.                     'query' => $this->addToFieldId($this->products),
  1719.                     'id' => 'id',
  1720.                     'name' => 'name'
  1721.                 )
  1722.             ),
  1723.             array(
  1724.                 'type' => $this->switch,
  1725.                 'label' => $this->l('Active products only'),
  1726.                 'name' => 'active',
  1727.                 'class' => 't',
  1728.                 'is_bool' => true,
  1729.                 'values' => array(
  1730.                     array(
  1731.                         'id' => 'active_on',
  1732.                         'value' => 1,
  1733.                         'label' => $this->l('Yes')
  1734.                      ),
  1735.                     array(
  1736.                         'id' => 'active_off',
  1737.                         'value' => 0,
  1738.                         'label' => $this->l('No')
  1739.                      )
  1740.                 ),
  1741.             ),
  1742.             array(
  1743.                 'type' => $this->switch,
  1744.                 'label' => $this->l('Products out of stock'),
  1745.                 'name' => 'out_of_stock',
  1746.                 'class' => 't',
  1747.                 'is_bool' => true,
  1748.                 'values' => array(
  1749.                     array(
  1750.                         'id' => 'active_on',
  1751.                         'value' => 1,
  1752.                         'label' => $this->l('Yes')
  1753.                      ),
  1754.                     array(
  1755.                         'id' => 'active_off',
  1756.                         'value' => 0,
  1757.                         'label' => $this->l('No')
  1758.                      )
  1759.                 ),
  1760.             ),
  1761.             array(
  1762.                 'type' => $this->switch,
  1763.                 'label' => $this->l('Products with ean13'),
  1764.                 'name' => 'ean',
  1765.                 'class' => 't',
  1766.                 'is_bool' => true,
  1767.                 'values' => array(
  1768.                     array(
  1769.                         'id' => 'active_on',
  1770.                         'value' => 1,
  1771.                         'label' => $this->l('Yes')
  1772.                      ),
  1773.                     array(
  1774.                         'id' => 'active_off',
  1775.                         'value' => 0,
  1776.                         'label' => $this->l('No')
  1777.                      )
  1778.                 ),
  1779.             ),
  1780.             array(
  1781.                 'type' => $this->switch,
  1782.                 'label' => $this->l('Export attributes'),
  1783.                 'name' => 'attributes',
  1784.                 'is_bool' => true,
  1785.                 'class' => 't',
  1786.                 'desc' =>$this->l('Each combination will be exported in new line and specific values will be overwrite'),
  1787.                 'values' => array(
  1788.                     array(
  1789.                         'id' => 'active_on',
  1790.                         'value' => 1,
  1791.                         'label' => $this->l('Yes')
  1792.                      ),
  1793.                     array(
  1794.                         'id' => 'active_off',
  1795.                         'value' => 0,
  1796.                         'label' => $this->l('No')
  1797.                     )
  1798.                 ),
  1799.             ),
  1800.             array(
  1801.                 'type'  => 'select',
  1802.                 'label' => $this->l('Suppliers'),
  1803.                 'name'  => 'suppliers[]',
  1804.                 'id'  => 'suppliers',
  1805.                 'class' => 'ds-select',
  1806.                 'multiple' => true,
  1807.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  1808.                 'options' => array(
  1809.                     'query' => Supplier::getSuppliers(false, $this->getConfiguration('PS_LANG_DEFAULT')),
  1810.                     'id' => 'id_supplier',
  1811.                     'name' => 'name'
  1812.                 )
  1813.             ),
  1814.             array(
  1815.                 'type'  => 'select',
  1816.                 'label' => $this->l('Manufacturers'),
  1817.                 'name'  => 'manufacturers[]',
  1818.                 'id'  => 'manufacturers',
  1819.                 'class' => 'ds-select',
  1820.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  1821.                 'multiple' => true,
  1822.                 'options' => array(
  1823.                     'query' => Manufacturer::getManufacturers(false, $this->getConfiguration('PS_LANG_DEFAULT')),
  1824.                     'id' => 'id_manufacturer',
  1825.                     'name' => 'name'
  1826.                 )
  1827.             )
  1828.         );
  1829.         if(_PS_VERSION_ >= 1.6) {
  1830.             $fields[] = array(
  1831.                 'type' => 'categories',
  1832.                 'label' => $this->l('Categories'),
  1833.                 'name' => 'categories',
  1834.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  1835.                 'tree' => array(
  1836.                     'id' => 'categories-tree',
  1837.                     'selected_categories' => $this->selected_cat,
  1838.                     'use_search' => true,
  1839.                     'use_checkbox' => true
  1840.                 )
  1841.             );
  1842.         }
  1843.         else
  1844.         {
  1845.             $root_category = Category::getRootCategory();
  1846.             $root_category = array('id_category' => $root_category->id, 'name' => $root_category->name);
  1847.  
  1848.             $fields[] = array(
  1849.                 'type' => 'categories',
  1850.                 'label' => $this->l('Parent category:'),
  1851.                 'name' => 'categories',
  1852.                 'values' => array(
  1853.                     'trads' => array(
  1854.                         'Root' => $root_category,
  1855.                         'selected' => $this->l('Selected'),
  1856.                         'Collapse All' => $this->l('Collapse All'),
  1857.                         'Expand All' => $this->l('Expand All'),
  1858.                         'Check All' => $this->l('Check All'),
  1859.                         'Uncheck All' => $this->l('Uncheck All'),
  1860.                     ),
  1861.                     'selected_cat' => ($this->selected_cat ? $this->selected_cat : array()),
  1862.                     'input_name' => 'categories[]',
  1863.                     'disabled_categories' => array(),
  1864.                     'use_checkbox' => true,
  1865.                     'use_radio' => false,
  1866.                     'use_search' => false,
  1867.                     'top_category' => Category::getTopCategory(),
  1868.                     'use_context' => true,
  1869.                 )
  1870.             );
  1871.         }
  1872.  
  1873.         return $fields;
  1874.     }
  1875.  
  1876.     public function addToFieldId($fields)
  1877.     {
  1878.         for ($i = 0; $i < count($fields); $i++) {
  1879.             //add id
  1880.             $fields[$i]['id'] = $i;
  1881.         }
  1882.  
  1883.         return $fields;
  1884.     }
  1885.  
  1886.     public function productsQuery($ae, $set)
  1887.     {
  1888.         $sql = 'SELECT p.`id_product` '.(empty($set['sqlfields']) ? "" : ', '.implode(', ', $set['sqlfields'])).
  1889.             ' FROM '._DB_PREFIX_.'product as p'.
  1890.             Shop::addSqlAssociation('product', 'p').'
  1891.            LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` '.Shop::addSqlRestrictionOnLang('pl').')
  1892.            LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (p.`id_supplier` = s.`id_supplier`)
  1893.            LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)'.
  1894.             (isset($set['categories']) && $set['categories']   ? ' LEFT JOIN `'._DB_PREFIX_.'category_product` c ON (c.`id_product` = p.`id_product`)' : '').'
  1895.             WHERE pl.`id_lang` = '.(int)$ae->id_lang.
  1896.             (isset($ae->only_new) && $ae->only_new && $ae->last_exported_id ? ' AND p.`id_product` > '.$ae->last_exported_id : '').
  1897.             ($ae->only_new == false && $ae->start_id ? ' AND p.`id_product` >= '.$ae->start_id : '').
  1898.             ($ae->only_new == false && $ae->end_id ? ' AND p.`id_product` <= '.$ae->end_id : '').
  1899.             (isset($set['categories']) && $set['categories']  ? ' AND c.`id_category` IN ('.implode(",", $set['categories'] ).')' : '').
  1900.             (isset($set['suppliers[]'] ) && $set['suppliers[]']     ? ' AND p.`id_supplier` IN ('.implode(",", $set['suppliers[]']).')' : '').
  1901.             (isset($set['manufacturers[]']) && $set['manufacturers[]']  ? ' AND p.`id_manufacturer` IN ('.implode(",",$set['manufacturers[]']).')' : '').
  1902.             (isset($set['active']) && $set['active']  ? ' AND product_shop.`active` = 1' : '').
  1903.             (isset($set['out_of_stock']) && $set['out_of_stock']  ? ' AND p.`quantity` <= 0' : '').
  1904.             (isset($set['ean13']) && $set['ean13']  ? ' AND p.`ean13` != ""' : '').
  1905.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND p.`date_add` >= "'.($ae->date_from).'"' : '').
  1906.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND p.`date_add` <= "'.($ae->date_to).'"' : '').
  1907.             ' GROUP BY p.`id_product`';
  1908.  
  1909.         $result = $this->query($sql);
  1910.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  1911.         return $result;
  1912.     }
  1913.  
  1914.     public function ExecuteS($sql)
  1915.     {
  1916.         return Db::getInstance()->ExecuteS($sql);
  1917.     }
  1918.  
  1919.     public function Execute($sql)
  1920.     {
  1921.         return Db::getInstance()->Execute($sql);
  1922.     }
  1923.  
  1924.     public function query($sql_query)
  1925.     {
  1926.         return Db::getInstance(_PS_USE_SQL_SLAVE_)->query($sql_query);
  1927.     }
  1928.  
  1929.     public function products_attributes_name($obj)
  1930.     {
  1931.         return null;
  1932.     }
  1933.  
  1934.     public function products_supplier_name_all($obj)
  1935.     {
  1936.         $sups = $this->executeS('
  1937.         SELECT DISTINCT(s.`name`)
  1938.         FROM `'._DB_PREFIX_.'product_supplier` ps
  1939.         LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (ps.`id_supplier`= s.`id_supplier`)
  1940.         LEFT JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  1941.         WHERE ps.`id_product` = '.$obj->id);
  1942.         $suppliers = array();
  1943.         foreach($sups as $sup) {
  1944.             $suppliers[] = $sup['name'];
  1945.         }
  1946.  
  1947.         return implode(',', $suppliers);
  1948.     }
  1949.  
  1950.     public function products_id_supplier_all($obj)
  1951.     {
  1952.         $sups = $this->executeS('
  1953.         SELECT DISTINCT(ps.`id_supplier`)
  1954.         FROM `'._DB_PREFIX_.'product_supplier` ps
  1955.         JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  1956.         WHERE ps.`id_product` = '.$obj->id);
  1957.         $suppliers = array();
  1958.         foreach($sups as $sup) {
  1959.             $suppliers[] = $sup['id_supplier'];
  1960.         }
  1961.         return implode(',', $suppliers);
  1962.     }
  1963.  
  1964.     public function products_features($obj, $ae)
  1965.     {
  1966.         $features = $obj->getFrontFeaturesStatic($ae->id_lang, $obj->id);
  1967.         $feats = array();
  1968.         foreach($features as $feature) {
  1969.             $feats[] = $feature['name'] .'-'. $feature['value'];
  1970.         }
  1971.         return implode(",", $feats);
  1972.     }
  1973.  
  1974.     public function products_price_tax($obj)
  1975.     {
  1976.         return $obj->getPrice(true);
  1977.     }
  1978.  
  1979.     public function products_tax_rate($obj)
  1980.     {
  1981.         return $obj->getTaxesRate();
  1982.     }
  1983.  
  1984.     public function products_quantity($obj)
  1985.     {
  1986.         return Product::getQuantity((int)$obj->id);
  1987.     }
  1988.  
  1989.     public function products_price_tax_nodiscount($obj)
  1990.     {
  1991.         return $obj->getPrice(true, null, 6, null, false, false);
  1992.     }
  1993.  
  1994.     public function products_url_product($obj, $ae)
  1995.     {
  1996.         $category = Category::getLinkRewrite((int)$obj->id_category_default, (int)$ae->id_lang);
  1997.         return $this->context->link->getProductLink((int)$obj->id, $obj->link_rewrite[$ae->id_lang], $category);
  1998.     }
  1999.  
  2000.     public function products_manufacturer_name($obj)
  2001.     {
  2002.         return $obj->getWsManufacturerName();
  2003.     }
  2004.  
  2005.     public function products_categories_names($obj, $ae)
  2006.     {
  2007.         $categories = $obj->getCategories();
  2008.         $cats = array();
  2009.         foreach($categories as $cat) {
  2010.             $category = new Category($cat, $ae->id_lang);
  2011.             $cats[] = $category->name;
  2012.         }
  2013.         return implode(",", $cats);
  2014.     }
  2015.  
  2016.     public function products_categories_ids($obj, $ae)
  2017.     {
  2018.         $categories = $obj->getCategories();
  2019.         $cats = array();
  2020.         foreach($categories as $cat) {
  2021.             $category = new Category($cat, $ae->id_lang);
  2022.             $cats[] = $category->id;
  2023.         }
  2024.         return implode(",", $cats);
  2025.     }
  2026.  
  2027.     public function products_nameCategoryDefault($obj, $ae)
  2028.     {
  2029.         $category = new Category($obj->id_category_default, $ae->id_lang);
  2030.         return $category->name;
  2031.     }
  2032.  
  2033.     public function products_images($obj, $ae)
  2034.     {
  2035.         $imagelinks = array();
  2036.         $images = $obj->getImages($obj->id);
  2037.         foreach($images as $image) {
  2038.             $imagelinks[] = $this->link->getImageLink($obj->link_rewrite[$ae->id_lang], $obj->id .'-'. $image['id_image'], $ae->image_type);
  2039.         }
  2040.         return implode(",", $imagelinks);
  2041.     }
  2042.  
  2043.     public function products_tags($obj)
  2044.     {
  2045.         return $obj->getTags(1);
  2046.     }
  2047.  
  2048.     public function products_accessories($obj, $ae)
  2049.     {
  2050.         if($accessories = $obj->getAccessories($ae->id_lang, false)) {
  2051.             foreach($accessories as $value)
  2052.             {
  2053.                 $accessories[] = $value['reference'];
  2054.             }
  2055.             return implode(',', $accessories);
  2056.         } else {
  2057.             return '';
  2058.         }
  2059.     }
  2060.  
  2061.     public function getProductCombination($obj, $ae)
  2062.     {
  2063.         $combArray = null;
  2064.         $groups = null;
  2065.  
  2066.         $combinaisons = $obj->getAttributeCombinations((int)($ae->id_lang));
  2067.         if (is_array($combinaisons))
  2068.         {
  2069.             $combinationImages = $obj->getCombinationImages((int)($ae->id_lang));
  2070.  
  2071.             foreach ($combinaisons AS $k => $combinaison)
  2072.             {
  2073.                 $combArray[$combinaison['id_product_attribute']]['wholesale_price'] = $combinaison['wholesale_price'];
  2074.                 $combArray[$combinaison['id_product_attribute']]['price'] = $combinaison['price'];
  2075.                 $combArray[$combinaison['id_product_attribute']]['weight'] = $combinaison['weight'];
  2076.                 $combArray[$combinaison['id_product_attribute']]['unit_impact'] = $combinaison['unit_price_impact'];
  2077.                 $combArray[$combinaison['id_product_attribute']]['reference'] = $combinaison['reference'];
  2078.                 $combArray[$combinaison['id_product_attribute']]['supplier_reference'] = $combinaison['supplier_reference'];
  2079.                 $combArray[$combinaison['id_product_attribute']]['ean13'] = $combinaison['ean13'];
  2080.                 $combArray[$combinaison['id_product_attribute']]['upc'] = $combinaison['upc'];
  2081.                 $combArray[$combinaison['id_product_attribute']]['minimal_quantity'] = $combinaison['minimal_quantity'];
  2082.                 $combArray[$combinaison['id_product_attribute']]['location'] = $combinaison['location'];
  2083.                 $combArray[$combinaison['id_product_attribute']]['quantity'] = $combinaison['quantity'];
  2084.                 $combArray[$combinaison['id_product_attribute']]['id_image'] = isset($combinationImages[$combinaison['id_product_attribute']][0]['id_image']) ? $combinationImages[$combinaison['id_product_attribute']][0]['id_image'] : 0;
  2085.                 //$combArray[$combinaison['id_products_attribute']]['default_on'] = $combinaison['default_on'];
  2086.                 $combArray[$combinaison['id_product_attribute']]['ecotax'] = $combinaison['ecotax'];
  2087.                 $combArray[$combinaison['id_product_attribute']]['id_product_attribute'] = $combinaison['id_product_attribute'];
  2088.                 $combArray[$combinaison['id_product_attribute']]['attributes'][] = array($combinaison['group_name'], $combinaison['attribute_name'], $combinaison['id_attribute']);
  2089.                 if ($combinaison['is_color_group'])
  2090.                     $groups[$combinaison['id_attribute_group']] = $combinaison['group_name'];
  2091.             }
  2092.         }
  2093.         return $combArray;
  2094.     }
  2095.  
  2096.     public function combination_supplier_name_all($obj, $product_attribute, $ae)
  2097.     {
  2098.         $sups = $this->executeS('
  2099.            SELECT DISTINCT(s.`name`)
  2100.            FROM `'._DB_PREFIX_.'product_supplier` ps
  2101.            LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (ps.`id_supplier`= s.`id_supplier`)
  2102.            LEFT JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  2103.            WHERE ps.`id_product` = '.$obj->id.' AND ps.id_product_attribute='.(int)$product_attribute['id_product_attribute']);
  2104.         $suppliers = array();
  2105.         foreach($sups as $sup) {
  2106.             $suppliers[] = $sup['name'];
  2107.         }
  2108.         return implode(',', $suppliers);
  2109.     }
  2110.  
  2111.     public function combination_id_supplier_all($obj, $product_attribute)
  2112.     {
  2113.         $sups = $this->executeS('
  2114.             SELECT DISTINCT(ps.`id_supplier`)
  2115.             FROM `'._DB_PREFIX_.'product_supplier` ps
  2116.             JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  2117.             WHERE ps.`id_product` = '.$obj->id.' AND ps.id_product_attribute='.(int)$product_attribute['id_product_attribute']);
  2118.  
  2119.         $suppliers = array();
  2120.         foreach($sups as $sup)
  2121.             $suppliers[] = $sup['id_supplier'];
  2122.  
  2123.         return implode(',', $suppliers);
  2124.     }
  2125.  
  2126.     public function combination_attributes_name($obj, $products_attribute)
  2127.     {
  2128.         $name = null;
  2129.         foreach ($products_attribute['attributes'] AS $attribute)
  2130.         {
  2131.             $name .= addslashes(htmlspecialchars($attribute[0])).': '.addslashes(htmlspecialchars($attribute[1])).' ';
  2132.         }
  2133.         return Tools::stripslashes($name);
  2134.     }
  2135.  
  2136.     public function combination_wholesale_price($obj, $product_attribute)
  2137.     {
  2138.         return $product_attribute['wholesale_price'];
  2139.     }
  2140.  
  2141.     public function combination_price($obj, $product_attribute)
  2142.     {
  2143.         return Product::getPriceStatic((int)$obj->id, false, (int)$product_attribute['id_product_attribute']);
  2144.     }
  2145.  
  2146.     public function combination_weight($obj, $product_attribute)
  2147.     {
  2148.         return $product_attribute['weight'];
  2149.     }
  2150.  
  2151.     public function combination_price_tax($obj, $product_attribute)
  2152.     {
  2153.         return $obj->getPrice(true, (int)$product_attribute['id_product_attribute']);
  2154.     }
  2155.  
  2156.     public function combination_price_tax_nodiscount($obj, $product_attribute)
  2157.     {
  2158.         return $obj->getPrice(true, (int)$product_attribute['id_product_attribute'], 2, null, false, false);
  2159.     }
  2160.  
  2161.     public function combination_unit_impact($obj, $products_attribute)
  2162.     {
  2163.         return $products_attribute['unit_impact'];
  2164.     }
  2165.  
  2166.     public function combination_reference($obj, $products_attribute)
  2167.     {
  2168.         return $products_attribute['reference'];
  2169.     }
  2170.  
  2171.     public function combination_supplier_reference($obj, $products_attribute)
  2172.     {
  2173.         return $products_attribute['supplier_reference'];
  2174.     }
  2175.  
  2176.     public function combination_ean13($obj, $products_attribute)
  2177.     {
  2178.         return $products_attribute['ean13'];
  2179.     }
  2180.  
  2181.     public function combination_upc($obj, $products_attribute)
  2182.     {
  2183.         return $products_attribute['upc'];
  2184.     }
  2185.  
  2186.     public function combination_minimal_quantity($obj, $products_attribute)
  2187.     {
  2188.         return $products_attribute['minimal_quantity'];
  2189.     }
  2190.  
  2191.     public function combination_location($obj, $products_attribute)
  2192.     {
  2193.         return $products_attribute['location'];
  2194.     }
  2195.  
  2196.     public function combination_quantity($obj, $products_attribute)
  2197.     {
  2198.         return $products_attribute['quantity'];
  2199.     }
  2200.  
  2201.     public function combination_ecotax($obj, $products_attribute)
  2202.     {
  2203.         return $products_attribute['ecotax'];
  2204.     }
  2205.  
  2206.     public function combination_images($obj, $product_attribute, $ae)
  2207.     {
  2208.         $attrImage = ($product_attribute['id_image'] ? new Image($product_attribute['id_image']) : null);
  2209.         if($attrImage){
  2210.             return $this->link->getImageLink($obj->link_rewrite[$ae->id_lang], $obj->id .'-'. $attrImage->id, $ae->image_type);
  2211.         }
  2212.     }
  2213.  
  2214.     public function ordersFormFields()
  2215.     {
  2216.         $fields = array(
  2217.             array(
  2218.                 'type'  => 'bsmselect',
  2219.                 'label' => $this->l('Orders fields'),
  2220.                 'name'  => 'fields[]',
  2221.                 'id'  => 'fields',
  2222.                 'class' => 'ds-select',
  2223.                 'multiple' => true,
  2224.                 'options' => array(
  2225.                     'query' => $this->addToFieldId($this->orders),
  2226.                     'id' => 'id',
  2227.                     'name' => 'name'
  2228.                 )
  2229.             ),
  2230.             array(
  2231.                 'type'  => 'select',
  2232.                 'label' => $this->l('Customers groups'),
  2233.                 'name'  => 'groups[]',
  2234.                 'id' => 'groups',
  2235.                 'class' => 'ds-select',
  2236.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2237.                 'multiple' => true,
  2238.                 'options' => array(
  2239.                     'query' => Group::getGroups($this->getConfiguration('PS_LANG_DEFAULT')),
  2240.                     'id' => 'id_group',
  2241.                     'name' => 'name'
  2242.                 )
  2243.             ),
  2244.             array(
  2245.                 'type'  => 'select',
  2246.                 'label' => $this->l('Payments'),
  2247.                 'name'  => 'payments[]',
  2248.                 'id'  => 'payments',
  2249.                 'class' => 'ds-select',
  2250.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2251.                 'multiple' => true,
  2252.                 'options' => array(
  2253.                     'query' => PaymentModule::getInstalledPaymentModules(),
  2254.                     'id' => 'name',
  2255.                     'name' => 'name'
  2256.                 )
  2257.             ),
  2258.             array(
  2259.                 'type'  => 'select',
  2260.                 'label' => $this->l('Carrier type'),
  2261.                 'name'  => 'carriers[]',
  2262.                 'id' => 'carriers',
  2263.                 'class' => 'ds-select',
  2264.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2265.                 'multiple' => true,
  2266.                 'options' => array(
  2267.                     'query' => Carrier::getCarriers($this->getConfiguration('PS_LANG_DEFAULT')),
  2268.                     'id' => 'id_carrier',
  2269.                     'name' => 'name'
  2270.                 )
  2271.             ),
  2272.             array(
  2273.                 'type'  => 'select',
  2274.                 'label' => $this->l('Order state'),
  2275.                 'name'  => 'state[]',
  2276.                 'id' => 'state',
  2277.                 'class' => 'ds-select',
  2278.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2279.                 'multiple' => true,
  2280.                 'options' => array(
  2281.                     'query' => OrderState::getOrderStates($this->getConfiguration('PS_LANG_DEFAULT')),
  2282.                     'id' => 'id_order_state',
  2283.                     'name' => 'name'
  2284.                 )
  2285.             )
  2286.         );
  2287.  
  2288.         return $fields;
  2289.     }
  2290.  
  2291.     public function ordersQuery($ae, $sorted_fields)
  2292.     {
  2293.         $sql = 'SELECT o.`id_order` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2294.                FROM '._DB_PREFIX_.'orders o
  2295.                LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON ( od.`id_order` = o.`id_order` )
  2296.                LEFT JOIN `'._DB_PREFIX_.'shop` sh ON ( o.`id_shop` = sh.`id_shop` )
  2297.                LEFT JOIN `'._DB_PREFIX_.'customer` cu ON ( o.`id_customer` = cu.`id_customer` )
  2298.                LEFT JOIN `'._DB_PREFIX_.'gender_lang` gl ON ( cu.`id_gender` = gl.`id_gender` AND gl.`id_lang` = 1)
  2299.                LEFT JOIN `'._DB_PREFIX_.'gender_lang` inv_gl ON ( cu.`id_gender` = inv_gl.`id_gender` AND inv_gl.`id_lang` = 1)
  2300.                LEFT JOIN `'._DB_PREFIX_.'address` a ON ( a.`id_address` = o.`id_address_delivery` )
  2301.                LEFT JOIN `'._DB_PREFIX_.'address` inv_a ON ( inv_a.`id_address` = o.`id_address_invoice` )
  2302.                LEFT JOIN `'._DB_PREFIX_.'state` s ON ( s.`id_state` = a.`id_state` )
  2303.                LEFT JOIN `'._DB_PREFIX_.'country` co ON ( co.`id_country` = a.`id_country` )
  2304.                LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON ( cl.`id_country` = co.`id_country` )
  2305.                LEFT JOIN `'._DB_PREFIX_.'carrier` ca ON ( ca.`id_carrier` = o.`id_carrier` )
  2306.                LEFT JOIN `'._DB_PREFIX_.'order_payment` op ON ( op.`order_reference` = o.`reference` )
  2307.                LEFT JOIN `'._DB_PREFIX_.'message` m ON ( m.`id_order` = o.`id_order` )
  2308.                LEFT JOIN `'._DB_PREFIX_.'currency` cur ON ( o.`id_currency` = cur.`id_currency` )
  2309.                WHERE cl.`id_lang`= '.$ae->id_lang.
  2310.                 (isset($ae->only_new) && $ae->only_new ? ' AND o.`id_order` > '.$ae->last_exported_id : '').
  2311.                 ($ae->only_new == false && $ae->start_id ? ' AND o.`id_order` >= '.$ae->start_id : '').
  2312.                 ($ae->only_new == false && $ae->end_id ? ' AND o.`id_order` <= '.$ae->end_id : '').
  2313.                 (isset($sorted_fields['groups[]']) && $sorted_fields['groups[]'] ? ' AND cu.`id_default_group` IN ('.implode(', ', $sorted_fields['groups[]']).')' : '').
  2314.                 (isset($sorted_fields['payments[]']) && $sorted_fields['payments[]'] ? ' AND o.`module` IN ("'.implode('", "', $sorted_fields['payments[]']).'")' : '').
  2315.                 (isset($sorted_fields['carriers[]']) && $sorted_fields['carriers[]'] ? ' AND o.`id_carrier` IN ('.implode(', ', $sorted_fields['carriers[]']).')' : '').
  2316.                 (isset($sorted_fields['state[]']) && $sorted_fields['state[]'] ? ' AND o.`current_state` IN ('.implode(', ', $sorted_fields['state[]']).')' : '').
  2317.                 (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND o.`date_add` >= "'.($ae->date_from).'"' : '').
  2318.                 (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND o.`date_add` <= "'.($ae->date_to).'"' : '')
  2319.                              .Shop::addSqlRestriction(false, 'o').
  2320.                 ' GROUP BY '.(isset($sorted_fields['order_detail']) && $sorted_fields['order_detail']  ? 'od.`id_order_detail`' : 'o.`id_order`');
  2321.  
  2322.         $result = $this->query($sql);
  2323.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2324.         return $result;
  2325.     }
  2326.  
  2327.     public function orders_code($obj)
  2328.     {
  2329.         $result = $obj->getCartRules();
  2330.         $codes = array();
  2331.         foreach($result as $res) {
  2332.             $codes[] = $res['name'];
  2333.         }
  2334.         return implode(',', $codes);
  2335.     }
  2336.  
  2337.     public function orders_customization($obj,$ae, $element)
  2338.     {
  2339.         $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  2340.                 SELECT cud.`value`, cu.`quantity`
  2341.                 FROM `'._DB_PREFIX_.'customization` cu
  2342.                 INNER JOIN `'._DB_PREFIX_.'customized_data` cud ON (cud.`id_customization` = cu.`id_customization`)
  2343.                 WHERE cu.`id_product` = '.(int)($element['product_id']).' AND cu.`id_product_attribute` = '.(int)($element['product_attribute_id']).'  AND cu.`id_cart` = '.(int)($element['id_cart']) );
  2344.         $cud = array();
  2345.         foreach($result as $res)
  2346.             $cud[] = 'value:'.$res['value'] . ' ' . 'quantity:' . $res['quantity'];
  2347.  
  2348.         return implode(',', $cud);
  2349.     }
  2350.  
  2351.     public function categoriesQuery($ae, $sorted_fields)
  2352.     {
  2353.         $sql = 'SELECT c.`id_category` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2354.            FROM `'._DB_PREFIX_.'category` c
  2355.             '.Shop::addSqlAssociation('category', 'c').'
  2356.             LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').'
  2357.             WHERE 1'.($ae->id_lang ? ' AND `id_lang` = '.(int)$ae->id_lang : '').
  2358.             (isset($ae->only_new) && $ae->only_new ? ' AND c.`id_category` > '.$ae->last_exported_id : '').
  2359.             ($ae->only_new == false && $ae->start_id ? ' AND c.`id_category` >= '.$ae->start_id : '').
  2360.             ($ae->only_new == false && $ae->end_id ? ' AND c.`id_category` <= '.$ae->end_id : '').
  2361.             (isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND c.`active` = 1' : '').
  2362.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND c.`date_add` >= "'.($ae->date_from).'"' : '').
  2363.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND c.`date_add` <= "'.($ae->date_to).'"' : '').'
  2364.             GROUP BY c.id_category
  2365.             ORDER BY c.`level_depth` ASC, category_shop.`position` ASC';
  2366.         $result = $this->query($sql);
  2367.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2368.         return $result;
  2369.     }
  2370.  
  2371.     public function categories_id_group($obj)
  2372.     {
  2373.         $result = $this->executeS('
  2374.             SELECT cg.`id_group`
  2375.             FROM '._DB_PREFIX_.'category_group cg
  2376.             WHERE cg.`id_category` = '.(int)$obj->id);
  2377.         $groups = null;
  2378.         foreach ($result as $group)
  2379.             $groups = $group['id_group'];
  2380.  
  2381.         return $groups;
  2382.     }
  2383.  
  2384.     public function categoriesFormFields()
  2385.     {
  2386.         $fields = array(
  2387.             array(
  2388.                 'type'  => 'bsmselect',
  2389.                 'label' => $this->l('Orders fields'),
  2390.                 'name'  => 'fields[]',
  2391.                 'id'  => 'fields',
  2392.                 'class' => 'sminit, ds-select',
  2393.                 'multiple' => true,
  2394.                 'options' => array(
  2395.                     'query' => $this->addToFieldId($this->categories),
  2396.                     'id' => 'id',
  2397.                     'name' => 'name'
  2398.                 )
  2399.             ),
  2400.             array(
  2401.                 'type' => $this->switch,
  2402.                 'label' => $this->l('Only active'),
  2403.                 'name' => 'active',
  2404.                 'class' => 't',
  2405.                 'is_bool' => true,
  2406.                 'values' => array(
  2407.                     array(
  2408.                         'id' => 'active_on',
  2409.                         'value' => 1,
  2410.                         'label' => $this->l('Yes')
  2411.                     ),
  2412.                     array(
  2413.                         'id' => 'active_off',
  2414.                         'value' => 0,
  2415.                         'label' => $this->l('No')
  2416.                     )
  2417.                 ),
  2418.             )
  2419.         );
  2420.  
  2421.         return $fields;
  2422.     }
  2423.  
  2424.     public function manufacturersQuery($ae, $sorted_fields)
  2425.     {
  2426.         $sql = 'SELECT m.`id_manufacturer` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2427.         FROM `'._DB_PREFIX_.'manufacturer` m
  2428.         '.Shop::addSqlAssociation('manufacturer', 'm').'
  2429.         INNER JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)$ae->id_lang.')'.'
  2430.            WHERE 1'.(isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND m.`active` = 1' : '').
  2431.             (isset($ae->only_new) && $ae->only_new ? ' AND m.`id_manufacturer` > '.$ae->last_exported_id : '').
  2432.             ($ae->only_new == false && $ae->start_id ? ' AND m.`id_manufacturer` >= '.$ae->start_id : '').
  2433.             ($ae->only_new == false && $ae->end_id ? ' AND m.`id_manufacturer` <= '.$ae->end_id : '').
  2434.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND m.`date_add` >= "'.($ae->date_from).'"' : '').
  2435.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND m.`date_add` <= "'.($ae->date_to).'"' : '');
  2436.         $result = $this->query($sql);
  2437.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2438.         return $result;
  2439.     }
  2440.  
  2441.     public function manufacturersFormFields()
  2442.     {
  2443.         $fields = array(
  2444.             array(
  2445.                 'type' => 'bsmselect',
  2446.                 'label' => $this->l('Manufacturers fields'),
  2447.                 'name' => 'fields[]',
  2448.                 'id' => 'fields',
  2449.                 'class' => 'ds-select',
  2450.                 'multiple' => true,
  2451.                 'options' => array(
  2452.                     'query' => $this->addToFieldId($this->manufacturers),
  2453.                     'id' => 'id',
  2454.                     'name' => 'name'
  2455.                 )
  2456.             ),
  2457.             array(
  2458.                 'type' => $this->switch,
  2459.                 'label' => $this->l('Only active'),
  2460.                 'name' => 'active',
  2461.                 'class' => 't',
  2462.                 'is_bool' => true,
  2463.                 'values' => array(
  2464.                     array(
  2465.                         'id' => 'active_on',
  2466.                         'value' => 1,
  2467.                         'label' => $this->l('Yes')
  2468.                     ),
  2469.                     array(
  2470.                         'id' => 'active_off',
  2471.                         'value' => 0,
  2472.                         'label' => $this->l('No')
  2473.                     )
  2474.                 ),
  2475.             )
  2476.         );
  2477.  
  2478.         return $fields;
  2479.     }
  2480.  
  2481.     public function suppliersQuery($ae, $sorted_fields)
  2482.     {
  2483.         $sql = 'SELECT s.`id_supplier` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2484.         FROM `'._DB_PREFIX_.'supplier` s
  2485.         '.Shop::addSqlAssociation('supplier', 's').'
  2486.         INNER JOIN `'._DB_PREFIX_.'supplier_lang` sl ON (s.`id_supplier` = sl.`id_supplier` AND sl.`id_lang` = '.(int)$ae->id_lang.')'.'
  2487.            WHERE 1'.(isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND s.`active` = 1' : '').
  2488.             (isset($ae->only_new) && $ae->only_new ? ' AND s.`id_supplier` > '.$ae->last_exported_id : '').
  2489.             ($ae->only_new == false && $ae->start_id ? ' AND s.`id_supplier` >= '.$ae->start_id : '').
  2490.             ($ae->only_new == false && $ae->end_id ? ' AND s.`id_supplier` <= '.$ae->end_id : '').
  2491.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND s.`date_add` >= "'.($ae->date_from).'"' : '').
  2492.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND s.`date_add` <= "'.($ae->date_to).'"' : '');
  2493.         $result = $this->query($sql);
  2494.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2495.         return $result;
  2496.     }
  2497.  
  2498.     public function suppliersFormFields()
  2499.     {
  2500.         $fields = array(
  2501.             array(
  2502.                 'type' => 'bsmselect',
  2503.                 'label' => $this->l('Orders fields'),
  2504.                 'name' => 'fields[]',
  2505.                 'id' => 'fields',
  2506.                 'class' => 'sminit, ds-select',
  2507.                 'multiple' => true,
  2508.                 'options' => array(
  2509.                     'query' => $this->addToFieldId($this->suppliers),
  2510.                     'id' => 'id',
  2511.                     'name' => 'name'
  2512.                 )
  2513.             ),
  2514.             array(
  2515.                 'type' => $this->switch,
  2516.                 'label' => $this->l('Only active'),
  2517.                 'name' => 'active',
  2518.                 'class' => 't',
  2519.                 'is_bool' => true,
  2520.                 'values' => array(
  2521.                     array(
  2522.                         'id' => 'active_on',
  2523.                         'value' => 1,
  2524.                         'label' => $this->l('Yes')
  2525.                     ),
  2526.                     array(
  2527.                         'id' => 'active_off',
  2528.                         'value' => 0,
  2529.                         'label' => $this->l('No')
  2530.                     )
  2531.                 ),
  2532.             )
  2533.         );
  2534.  
  2535.         return $fields;
  2536.     }
  2537.  
  2538.     public function customersFormFields()
  2539.     {
  2540.         $fields = array(
  2541.             array(
  2542.                 'type' => 'bsmselect',
  2543.                 'label' => $this->l('Customers fields'),
  2544.                 'name' => 'fields[]',
  2545.                 'id' => 'fields',
  2546.                 'class' => 'sminit, ds-select',
  2547.                 'multiple' => true,
  2548.                 'options' => array(
  2549.                     'query' => $this->addToFieldId($this->customers),
  2550.                     'id' => 'id',
  2551.                     'name' => 'name'
  2552.                 )
  2553.  
  2554.             ),
  2555.             array(
  2556.                 'type' => $this->switch,
  2557.                 'label' => $this->l('Only active'),
  2558.                 'name' => 'active',
  2559.                 'class' => 't',
  2560.                 'is_bool' => true,
  2561.                 'values' => array(
  2562.                     array(
  2563.                         'id' => 'active_on',
  2564.                         'value' => 1,
  2565.                         'label' => $this->l('Yes')
  2566.                     ),
  2567.                     array(
  2568.                         'id' => 'active_off',
  2569.                         'value' => 0,
  2570.                         'label' => $this->l('No')
  2571.                     )
  2572.                 ),
  2573.             )
  2574.         );
  2575.  
  2576.         return $fields;
  2577.     }
  2578.  
  2579.     public function customersQuery($ae, $sorted_fields)
  2580.     {
  2581.         $sql = 'SELECT c.`id_customer` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'  
  2582.                 FROM '._DB_PREFIX_.'customer c
  2583.                LEFT JOIN `'._DB_PREFIX_.'address` a ON ( a.`id_customer` = c.`id_customer` )
  2584.                 WHERE 1'.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).
  2585.             (isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND c.`active` = 1' : '').
  2586.             (isset($ae->only_new) && $ae->only_new ? ' AND c.`id_customer` > '.$ae->last_exported_id : '').
  2587.             ($ae->only_new == false && $ae->start_id ? ' AND c.`id_customer` >= '.$ae->start_id : '').
  2588.             ($ae->only_new == false && $ae->end_id ? ' AND c.`id_customer` <= '.$ae->end_id : '').
  2589.             (isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND c.`active` = 1' : '').
  2590.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND c.`date_add` >= "'.($ae->date_from).'"' : '').
  2591.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND c.`date_add` <= "'.($ae->date_to).'"' : '');
  2592.         $result = $this->query($sql);
  2593.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2594.         return $result;
  2595.     }
  2596.  
  2597.  
  2598.     public function newslettersQuery($ae, $sorted_fields)
  2599.     {
  2600.         $sql = 'SELECT n.`id` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2601.                 FROM '._DB_PREFIX_.'newsletter as n
  2602.                 WHERE 1'.(isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND n.`active` = 1' : '').
  2603.                 (isset($ae->only_new) && $ae->only_new ? ' AND n.`id` > '.$ae->last_exported_id : '').
  2604.                 ($ae->only_new == false && $ae->start_id ? ' AND n.`id` >= '.$ae->start_id : '').
  2605.                 ($ae->only_new == false && $ae->end_id ? ' AND n.`id` <= '.$ae->end_id : '').
  2606.                 (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND n.`newsletter_date_add` >= "'.($ae->date_from).'"' : '').
  2607.                 (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND n.`newsletter_date_add` <= "'.($ae->date_to).'"' : '').'
  2608.                 AND n.`id_shop` = '.$this->context->shop->id;
  2609.         $result = $this->query($sql);
  2610.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2611.         return $result;
  2612.     }
  2613.  
  2614.     public function newslettersFormFields()
  2615.     {
  2616.         $fields = array(
  2617.             array(
  2618.                 'type' => 'bsmselect',
  2619.                 'label' => $this->l('Newsletters fields'),
  2620.                 'name' => 'fields[]',
  2621.                 'id' => 'fields',
  2622.                 'class' => 'sminit, ds-select',
  2623.                 'multiple' => true,
  2624.                 'options' => array(
  2625.                     'query' => $this->addToFieldId($this->newsletters),
  2626.                     'id' => 'id',
  2627.                     'name' => 'name'
  2628.                 )
  2629.             ),
  2630.             array(
  2631.                 'type' => $this->switch,
  2632.                 'label' => $this->l('Only active'),
  2633.                 'class' => 't',
  2634.                 'name' => 'active',
  2635.                 'is_bool' => true,
  2636.                 'values' => array(
  2637.                     array(
  2638.                         'id' => 'active_on',
  2639.                         'value' => 1,
  2640.                         'label' => $this->l('Yes')
  2641.                     ),
  2642.                     array(
  2643.                         'id' => 'active_off',
  2644.                         'value' => 0,
  2645.                         'label' => $this->l('No')
  2646.                     )
  2647.                 ),
  2648.             )
  2649.         );
  2650.  
  2651.         return $fields;
  2652.     }
  2653.  
  2654.     public function getLanguages()
  2655.     {
  2656.         return Language::getLanguages();
  2657.     }
  2658.  
  2659.     /**
  2660.      * Add key to array element as id
  2661.      * for select control
  2662.      * @param array $array
  2663.      * @return array $array
  2664.      */
  2665.     public function addKeyAsArrayElementId($array)
  2666.     {
  2667.         for ($i=0; $i < count($array); $i++) {
  2668.             $array[$i]['id'] = $i;
  2669.         }
  2670.  
  2671.         return $array;
  2672.     }
  2673.  
  2674.     /**
  2675.      * @param $object
  2676.      * @param $ae
  2677.      * @param $value
  2678.      * @return mixed
  2679.      */
  2680.     public function processDecimalSettings($object, $ae, $value)
  2681.     {
  2682.         if ($this->is_decimal($object[$value]))
  2683.         {
  2684.             //this have to be first becasue if we change separator
  2685.             //the value is not recognise
  2686.             if($ae->decimal_round != -1 && $ae->decimal_round != "-1")
  2687.                 $object[$value] = Tools::ps_round((float)$object[$value], $ae->decimal_round);
  2688.  
  2689.             if($ae->decimal_separator != -1 && $ae->decimal_separator != "-1")
  2690.             {
  2691.                 $object[$value] = str_replace(',', $ae->decimal_separator, $object[$value]);
  2692.                 $object[$value] = str_replace('.', $ae->decimal_separator, $object[$value]);
  2693.             }
  2694.         }
  2695.         if ($ae->strip_tags)
  2696.             $object[$value] = strip_tags($object[$value]);
  2697.         return $object;
  2698.     }
  2699.  
  2700.     public function is_decimal( $val )
  2701.     {
  2702.         return is_numeric( $val ) && strpos( $val, "." ) !== false;
  2703.     }
  2704.  
  2705.     /**
  2706.      * @param $type
  2707.      * @return array
  2708.      */
  2709.     public function getLinks($type)
  2710.     {
  2711.         $links = $this->DbExecuteS("select * from " . _DB_PREFIX_ . "advancedexport where type = '$type'");
  2712.         for ($i = 0; $i < count($links); $i++) {
  2713.             $links[$i]['cron_url'] = "<span class='cron_url'>http://" . $_SERVER['HTTP_HOST'] . __PS_BASE_URI__ . "modules/advancedexport/cron.php?secure_key=".Configuration::get('ADVANCEDEXPORT_SECURE_KEY')."&id=" . $links[$i]['id_advancedexport']."   </span><span class='cron_button'>copy to clipboard</span>";
  2714.             $type = $this->getSaveTypes();
  2715.             $links[$i]['save_type'] = $type[$links[$i]['save_type']]['short_name'];
  2716.         }
  2717.         return $links;
  2718.     }
  2719.  
  2720.     /**
  2721.      * @param $ae
  2722.      * @param $sorted_fields
  2723.      * @param $file
  2724.      */
  2725.     public function filewrite($ae, $sorted_fields, $file)
  2726.     {
  2727.         fwrite($file, implode($ae->delimiter, $sorted_fields['labels']) . "\r\n");
  2728.     }
  2729. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement