Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 107.48 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.  
  1224. private $count = null; // put this within class
  1225.  
  1226.         public function fputToFile($file, $allexportfields, $object, $ae)
  1227.         {
  1228.             if($allexportfields && $file && $object && $ae)
  1229.             {
  1230.                 //one ready for export product
  1231.                 $readyForExport = array();
  1232.  
  1233.  
  1234.                 //put in correct sort order
  1235.                 foreach ($allexportfields as $value)
  1236.                 {
  1237.                     $object = $this->processDecimalSettings($object, $ae, $value);
  1238.                     $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);
  1239.  
  1240.                 }
  1241.                
  1242.                 $sql = "SELECT codice_target FROM customer";
  1243.                
  1244.                 $result = $this->query($sql);
  1245.  
  1246.                 if(!$result)
  1247.                 {
  1248.                 return false;
  1249.                 }
  1250.  
  1251.                 while($row = $result->fetch_assoc())
  1252.                 {
  1253.                 $rows[] = $row;
  1254.                 }
  1255.  
  1256.                 foreach ($rows as $readyForExport['codice_target'])
  1257.                 {
  1258.                 return $readyForExport['codice_target'];
  1259.                 }
  1260.  
  1261.                 $this->counter[$readyForExport['id_order']] = (!empty($this->counter[$readyForExport['id_order']])) ? ++$this->counter[$readyForExport['id_order']] : 1; // try here
  1262.                 $readyForExport['orderLine'] = $this->counter[$readyForExport['id_order']]; // and try here
  1263.                
  1264.                 //print_r('The id_order is added: ' . $readyForExport['orderLine']); // see if it is added
  1265.  
  1266.                 //echo '<pre>' . var_dump($readyForExport) . '</pre>';
  1267.  
  1268.                 // modifiche === Dario === prezzo
  1269.                 $newPrice = substr($readyForExport['product_price'], 0, strpos($readyForExport['product_price'], "."));
  1270.                 $readyForExport['product_price'] = $newPrice;
  1271.  
  1272.                 // === data
  1273.  
  1274.                 $newDateAdd = new DateTime($readyForExport['date_add']);
  1275.                 $readyForExport['date_add'] = $newDateAdd->format('d/m/Y');
  1276.  
  1277.  
  1278.                 // aggiungo 21 giorni - 3 settimane - alla data di acquisto
  1279.                 $date_mod = clone $newDateAdd;
  1280.                 $date_mod->add(new DateInterval('P21D'));
  1281.                 $readyForExport['delivery_date'] = $date_mod->format('d/m/Y');
  1282.  
  1283.                 // === data invoice
  1284.                 $newDateInvoice = clone $newDateAdd;
  1285.                 $readyForExport['invoice_date'] = $newDateAdd->format('d/m/Y');
  1286.  
  1287.                 //scambio l'id customer con il codice_target
  1288.  
  1289.                 //$readyForExport['codice_target'] = 8989;
  1290.  
  1291.                 $textTarget = (string)$readyForExport['codice_target'];
  1292.  
  1293.                 $readyForExport['id_customer'] = $textTarget;
  1294.  
  1295.                 // aggiungo gli zeri davanti al customer id
  1296.                 $id_count = strlen($readyForExport['id_customer']);            
  1297.                 if ($id_count == 1) {
  1298.                    
  1299.                     $newCustomer = "0000000".$readyForExport['id_customer'];
  1300.                     $readyForExport['id_customer'] = $newCustomer;
  1301.                    
  1302.                 }elseif ($id_count == 2) {
  1303.                    
  1304.                     $newCustomer = "000000".$readyForExport['id_customer'];
  1305.                     $readyForExport['id_customer'] = $newCustomer;
  1306.                    
  1307.                 }elseif ($id_count == 3) {
  1308.                    
  1309.                     $newCustomer = "00000".$readyForExport['id_customer'];
  1310.                     $readyForExport['id_customer'] = $newCustomer;
  1311.                    
  1312.                 }elseif ($id_count == 4) {
  1313.                     $newCustomer = "0000".$readyForExport['id_customer'];
  1314.                     $readyForExport['id_customer'] = $newCustomer;
  1315.                    
  1316.                 }elseif ($id_count == 5) {
  1317.                     $newCustomer = "000".$readyForExport['id_customer'];
  1318.                     $readyForExport['id_customer'] = $newCustomer;
  1319.                    
  1320.                 }elseif ($id_count == 6) {
  1321.                     $newCustomer = "00".$readyForExport['id_customer'];
  1322.                     $readyForExport['id_customer'] = $newCustomer;
  1323.                    
  1324.                 }
  1325.  
  1326.                 // elaboro lo SKU
  1327.  
  1328.                 $textSku = (string)$readyForExport['product_name'];
  1329.                
  1330.                 $newSku_1 = $readyForExport['product_name'];
  1331.  
  1332.                 $newSku_1 = substr($newSku_1,0,4);
  1333.                 $newSku_2 = "/".substr($textSku,-4,4);
  1334.  
  1335.                 $newSku_tot = $newSku_1.$newSku_2;
  1336.                
  1337.                 $newSku_tot = str_replace(' ', '', $newSku_tot);
  1338.                 $newSku_tot = str_replace('-', '', $newSku_tot);
  1339.                 $newSku_tot = str_replace('co', '', $newSku_tot);
  1340.                
  1341.                 $newSku_tot = str_replace('e', '', $newSku_tot);
  1342.                 $newSku_tot = str_replace('r', '', $newSku_tot);
  1343.  
  1344.                 $readyForExport['product_name'] = $newSku_tot;
  1345.  
  1346.  
  1347.  
  1348.                 // aggiungo un campo fisso
  1349.                 $readyForExport['causale'] = "NR";
  1350.  
  1351.                 // aggiungo un campo fisso
  1352.                 $readyForExport['ORCL'] = "ORCL";
  1353.            
  1354.                 //$readyForExport['G'] = "";
  1355.                 $readyForExport['J'] = "";
  1356.                 $readyForExport['K'] = "";
  1357.                 $readyForExport['L'] = "";
  1358.                 $readyForExport['M'] = "";
  1359.                 $readyForExport['N'] = "";
  1360.                 $readyForExport['P'] = "";
  1361.                 $readyForExport['Q'] = "";
  1362.                 $readyForExport['R'] = "30";
  1363.  
  1364.                 $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");
  1365.  
  1366.                 //riordino i campi in base a come li dispongo nella variabile $index_arr
  1367.                 $arr_t=array();
  1368.                 foreach($index_arr as $i=>$v) {
  1369.                     foreach($readyForExport as $k=>$b) {
  1370.                         if ($k==$v) $arr_t[$k]=$b;
  1371.                     }
  1372.                 }
  1373.                 $readyForExport=$arr_t;
  1374.  
  1375.             //write into csv line by line
  1376.             fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
  1377.         }
  1378.     }
  1379.  
  1380.     //tags
  1381.  
  1382.     public function saveLastId($ae, $myLastElement)
  1383.     {
  1384.         if($ae->only_new && isset($myLastElement[$this->getId($ae->type)]))
  1385.         {
  1386.             $ae->last_exported_id = $myLastElement[$this->getId($ae->type)];
  1387.             $ae->fields = Tools::jsonEncode($ae->fields);
  1388.             $ae->save();
  1389.         }
  1390.  
  1391.         return $ae;
  1392.     }
  1393.  
  1394.     public function getId ($type)
  1395.     {
  1396.         $id = '';
  1397.         switch ($type) {
  1398.             case "categories":
  1399.                 $id = "id_category";
  1400.                 break;
  1401.             case "customers":
  1402.                 $id = "id_customer";
  1403.                 break;
  1404.             case "manufacturers":
  1405.                 $id = "id_manufacturer";
  1406.                 break;
  1407.             case "newsletters":
  1408.                 $id = "id";
  1409.                 break;
  1410.             case "orders":
  1411.                 $id = "id_order";
  1412.                 break;
  1413.             case "products":
  1414.                 $id = "id_product";
  1415.                 break;
  1416.             case "suppliers":
  1417.                 $id = "id_supplier";
  1418.                 break;
  1419.         }
  1420.         return $id;
  1421.     }
  1422.  
  1423.  
  1424.     public function processFile($process, $url, $ae)
  1425.     {
  1426.         switch ($process)
  1427.         {
  1428.             case 0:
  1429.             break;
  1430.             case 1:
  1431.                 $this->ftpFile($ae->ftp_hostname, $ae->ftp_user_name, $ae->ftp_user_pass, $url);
  1432.             break;
  1433.             case 2:
  1434.                 $this->sentFile($url, $ae->email);
  1435.             break;
  1436.             default:
  1437.             break;
  1438.         }
  1439.     }
  1440.  
  1441.     public function ftpFile($ftp_hostname, $ftp_user_name, $ftp_user_pass, $export_file )
  1442.     {
  1443.         // open some file for reading
  1444.         $file = basename($export_file);
  1445.         $fp = fopen($export_file, 'r');
  1446.  
  1447.         // set up basic connection
  1448.         $conn = ftp_connect($ftp_hostname);
  1449.  
  1450.         // login with username and password
  1451.         $login_result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
  1452.  
  1453.         // try to upload $file
  1454.         if (ftp_fput($conn, $file, $fp, FTP_BINARY))
  1455.             echo "Successfully uploaded $file\n";
  1456.         else
  1457.             echo "There was a problem while uploading $file\n";
  1458.  
  1459.         // close the connection and the file handler
  1460.         ftp_close($conn);
  1461.         fclose($fp);
  1462.     }
  1463.  
  1464.             public function sentFile($export_file, $email)
  1465.             {
  1466.                 $file_attachement = null;
  1467.                 $file_attachement['content'] = Tools::file_get_contents($export_file);
  1468.                 $file_attachement['name'] = "export file";
  1469.                 $file_attachement['mime'] = 'text/csv';
  1470.  
  1471.                 $id_lang = $this->getConfiguration("PS_LANG_DEFAULT");
  1472.                 Mail::Send($id_lang, 'index', 'index', null, $email, null, null, "advanced export", $file_attachement, null, dirname(__FILE__).'/mails/');
  1473.             }
  1474.  
  1475.     public function getConfiguration($value)
  1476.     {
  1477.         return (int)Configuration::get($value);
  1478.     }
  1479.  
  1480.     public function displayIndexForm()
  1481.     {
  1482.         $html = '<script type="text/javascript">
  1483.            var urlJson = "index.php?controller=AdminModules&configure=advancedexport&module_name=advancedexport&token='.Tools::getAdminTokenLite("AdminModules").'";
  1484.        </script>';
  1485.         $html .= '<div class="row">';
  1486.             $html .= '<div class="col-lg-12">';
  1487.                 $html .= '<div id="loader-container">';
  1488.                     $html .= '<div id="topLoader"> </div>';
  1489.                 $html .= '</div>';
  1490.                 $html .= '<div class="row">';
  1491.                     $html .= '<div class="col-lg-2 col-md-3">';
  1492.                         $html .= '<div class="list-group">';
  1493.                             foreach ($this->export_types as $key => $value)
  1494.                             {
  1495.                                 $html .= '<a href="#" id="'.$value.'" class="list-group-item">'.$value.'</a>';
  1496.                             }
  1497.                         $html .= '</div>';
  1498.                     $html .= '</div>';
  1499.                     $html .= '<div class="form-horizontal col-lg-10">';
  1500.                         $html .= '<div class="list-group">';
  1501.  
  1502.                             foreach ($this->export_types as $key => $value)
  1503.                             {
  1504.                                 $html .= "<div class='product-tab-content hide' alt='$value'>";
  1505.                                 $html .= $this->displayModelListForm($helper = new HelperList(), $value);
  1506.                                 $html .= $this->displayFilesForm($helper = new HelperList(), $value);
  1507.                                 $html .= '</div>';
  1508.                             }
  1509.                         $html .= '</div>';
  1510.                     $html .= '</div>';
  1511.                 $html .= '</div>';
  1512.             $html .= '</div>';
  1513.         $html .= '</div>';
  1514.  
  1515.         return $html;
  1516.  
  1517.  
  1518.  
  1519.     }
  1520.  
  1521.     public function displayModelListForm($helper, $type)
  1522.     {
  1523.         $this->fields_list = $this->listFieldsForm();
  1524.         $links = $this->getLinks($type);
  1525.         $this->_display = 'index';
  1526.         $helper->module = $this;
  1527.         $helper->identifier = 'id_advancedexport';
  1528.         $helper->actions = array('edit', 'duplicate', 'delete');
  1529.         $helper->show_toolbar = true;
  1530.         //$helper->simple_header = true;
  1531.         $helper->shopLinkType = '';
  1532.         $helper->listTotal = count($links);
  1533.         $helper->toolbar_btn = $this->initToolbar($type);
  1534.         $helper->title = $type." models";
  1535.         $helper->table = 'advancedexport';
  1536.         $helper->token = Tools::getAdminTokenLite('AdminModules');
  1537.         $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
  1538.         $helper->tpl_vars['version'] = false;
  1539.         return $helper->generateList($links, $this->fields_list);
  1540.     }
  1541.  
  1542.     public function listFieldsForm()
  1543.     {
  1544.         return array(
  1545.             'id_advancedexport' => array(
  1546.                 'title' => $this->l('ID'),
  1547.                 'width' => 20
  1548.             ),
  1549.             'name' => array(
  1550.                 'title' => $this->l('Name'),
  1551.                 'width' => 100
  1552.             ),
  1553.             'cron_url' => array(
  1554.                 'title' => $this->l('Cron url'),
  1555.                 'type' => 'html'
  1556.             ),
  1557.             'save_type' => array(
  1558.                 'title' => $this->l('Save Type'),
  1559.                 'width' => 30
  1560.             ),
  1561.             'only_new' => array(
  1562.                 'title' => $this->l('New entries'),
  1563.                 'active' => 'only_new',
  1564.                 'type' => 'bool',
  1565.                 'align' => 'center',
  1566.                 'width' => 30,
  1567.                 'orderby' => false
  1568.             )
  1569.         );
  1570.     }
  1571.  
  1572.     //get features
  1573.  
  1574.     public function DbExecuteS($query)
  1575.     {
  1576.         return Db::getInstance()->ExecuteS( $query );
  1577.     }
  1578.  
  1579.  
  1580.     //price tax
  1581.  
  1582.     public function initToolbar($type)
  1583.     {
  1584.         $current_index = AdminController::$currentIndex;
  1585.         $token = Tools::getAdminTokenLite('AdminModules');
  1586.  
  1587.         $back = Tools::safeOutput(Tools::getValue('back', ''));
  1588.  
  1589.         if (!isset($back) || empty($back))
  1590.             $back = $current_index.'&configure='.$this->name.'&token='.$token;
  1591.  
  1592.         switch ($this->_display)
  1593.         {
  1594.             case 'add':
  1595.                 $this->toolbar_btn['cancel'] = array(
  1596.                 'href' => $back,
  1597.                 'desc' => $this->l('Cancel')
  1598.             );
  1599.             break;
  1600.             case 'edit':
  1601.                 $this->toolbar_btn['cancel'] = array(
  1602.                 'href' => $back,
  1603.                 'desc' => $this->l('Cancel')
  1604.             );
  1605.             break;
  1606.             case 'index':
  1607.                 $this->toolbar_btn['new'] = array(
  1608.                     'href' => $current_index.'&configure='.$this->name.'&token='.$token.'&add_model=1&type='.$type,
  1609.                     'desc' => $this->l('Add new')
  1610.                                            );
  1611.              break;
  1612.              default:
  1613.             break;
  1614.         }
  1615.  
  1616.         return $this->toolbar_btn;
  1617.     }
  1618.  
  1619.     //price tax without discount
  1620.  
  1621.     public function displayFilesForm($helper, $type)
  1622.     {
  1623.         $this->fields_list = $this->filesFieldsForm();
  1624.         $files = $this->getFiles($type);
  1625.         $this->_display = 'index';
  1626.         $helper->module = $this;
  1627.         $helper->identifier = 'url';
  1628.         $helper->simple_header = true;
  1629.         $helper->actions = array('view', 'delete');
  1630.         $helper->show_toolbar = true;
  1631.         $helper->shopLinkType = '';
  1632.         $helper->listTotal = count($files);
  1633.         $helper->title = $type.' files ';
  1634.         $helper->table = 'files';
  1635.         $helper->token = Tools::getAdminTokenLite('AdminModules');
  1636.         $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
  1637.  
  1638.         return $helper->generateList($files, $this->fields_list);
  1639.     }
  1640.  
  1641.     public function format_size($size) {
  1642.         $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
  1643.         if ($size == 0) { return('n/a'); } else {
  1644.             return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
  1645.     }
  1646.  
  1647.     public function filesFieldsForm()
  1648.     {
  1649.         return array(
  1650.             'id_file' => array(
  1651.                 'title' => $this->l('ID'),
  1652.                 'width' => 25
  1653.             ),
  1654.             'name' => array(
  1655.                 'title' => $this->l('Name'),
  1656.                 'width' => 100
  1657.             ),
  1658.             'filesize' => array(
  1659.                 'title' => $this->l('File size'),
  1660.                 'width' => 100
  1661.             )
  1662.         );
  1663.      }
  1664.  
  1665.     //wholesale_price
  1666.  
  1667.     public function getFiles($type)
  1668.     {
  1669.         $dirname = _PS_ROOT_DIR_.'/modules/advancedexport/csv/'.$type.'/';
  1670.         $files = glob($dirname.'*.{csv}', GLOB_BRACE);
  1671.  
  1672.         $result = array();
  1673.         $lp = 1;
  1674.  
  1675.         if($files){
  1676.             foreach ($files as $key => $value)
  1677.             {
  1678.                 $result[] = array(
  1679.                     'id_file' => $lp,
  1680.                     'name' => basename($value),
  1681.                     'filesize' => $this->format_size(filesize($value)),
  1682.                     'url' => urlencode($type.'/'.basename($value))
  1683.                 );
  1684.                 $lp++;
  1685.             }
  1686.         }
  1687.         return $result;
  1688.     }
  1689.  
  1690.  
  1691.     public function getFile($url)
  1692.     {
  1693.         $dir = (string)realpath(dirname(__FILE__).'/csv/'.$url);
  1694.         if(file_exists($dir))
  1695.         {
  1696.             header('Content-Description: File Transfer');
  1697.             header('Content-Type: application/csv');
  1698.             header('Content-Disposition: attachment; filename='.basename($dir));
  1699.             header('Content-Transfer-Encoding: binary');
  1700.             header('Expires: 0');
  1701.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  1702.             header('Pragma: public');
  1703.             header('Content-Length: ' . filesize($dir));
  1704.             ob_clean();
  1705.             flush();
  1706.             readfile($dir);
  1707.             exit;
  1708.         }
  1709.     }
  1710.  
  1711.     public function cronTask()
  1712.     {
  1713.         Context::getContext()->link = new Link();
  1714.         $id = Tools::getValue('id');
  1715.         $this->getExportType($id);
  1716.     }
  1717.  
  1718.     public function deleteFile($url)
  1719.     {
  1720.         $dir = (string)realpath(dirname(__FILE__).'/csv/'.$url);
  1721.         if(file_exists($dir))
  1722.             unlink($dir);
  1723.     }
  1724.  
  1725.     public function productsFormFields()
  1726.     {
  1727.         $fields = array(
  1728.             array(
  1729.                 'type'  => 'bsmselect',
  1730.                 'label' => $this->l('Products fields'),
  1731.                 'name'  => 'fields[]',
  1732.                 'id'  => 'fields',
  1733.                 'class' => 'sminit, ds-select',
  1734.                 'multiple' => true,
  1735.                 'options' => array(
  1736.                     'query' => $this->addToFieldId($this->products),
  1737.                     'id' => 'id',
  1738.                     'name' => 'name'
  1739.                 )
  1740.             ),
  1741.             array(
  1742.                 'type' => $this->switch,
  1743.                 'label' => $this->l('Active products only'),
  1744.                 'name' => 'active',
  1745.                 'class' => 't',
  1746.                 'is_bool' => true,
  1747.                 'values' => array(
  1748.                     array(
  1749.                         'id' => 'active_on',
  1750.                         'value' => 1,
  1751.                         'label' => $this->l('Yes')
  1752.                      ),
  1753.                     array(
  1754.                         'id' => 'active_off',
  1755.                         'value' => 0,
  1756.                         'label' => $this->l('No')
  1757.                      )
  1758.                 ),
  1759.             ),
  1760.             array(
  1761.                 'type' => $this->switch,
  1762.                 'label' => $this->l('Products out of stock'),
  1763.                 'name' => 'out_of_stock',
  1764.                 'class' => 't',
  1765.                 'is_bool' => true,
  1766.                 'values' => array(
  1767.                     array(
  1768.                         'id' => 'active_on',
  1769.                         'value' => 1,
  1770.                         'label' => $this->l('Yes')
  1771.                      ),
  1772.                     array(
  1773.                         'id' => 'active_off',
  1774.                         'value' => 0,
  1775.                         'label' => $this->l('No')
  1776.                      )
  1777.                 ),
  1778.             ),
  1779.             array(
  1780.                 'type' => $this->switch,
  1781.                 'label' => $this->l('Products with ean13'),
  1782.                 'name' => 'ean',
  1783.                 'class' => 't',
  1784.                 'is_bool' => true,
  1785.                 'values' => array(
  1786.                     array(
  1787.                         'id' => 'active_on',
  1788.                         'value' => 1,
  1789.                         'label' => $this->l('Yes')
  1790.                      ),
  1791.                     array(
  1792.                         'id' => 'active_off',
  1793.                         'value' => 0,
  1794.                         'label' => $this->l('No')
  1795.                      )
  1796.                 ),
  1797.             ),
  1798.             array(
  1799.                 'type' => $this->switch,
  1800.                 'label' => $this->l('Export attributes'),
  1801.                 'name' => 'attributes',
  1802.                 'is_bool' => true,
  1803.                 'class' => 't',
  1804.                 'desc' =>$this->l('Each combination will be exported in new line and specific values will be overwrite'),
  1805.                 'values' => array(
  1806.                     array(
  1807.                         'id' => 'active_on',
  1808.                         'value' => 1,
  1809.                         'label' => $this->l('Yes')
  1810.                      ),
  1811.                     array(
  1812.                         'id' => 'active_off',
  1813.                         'value' => 0,
  1814.                         'label' => $this->l('No')
  1815.                     )
  1816.                 ),
  1817.             ),
  1818.             array(
  1819.                 'type'  => 'select',
  1820.                 'label' => $this->l('Suppliers'),
  1821.                 'name'  => 'suppliers[]',
  1822.                 'id'  => 'suppliers',
  1823.                 'class' => 'ds-select',
  1824.                 'multiple' => true,
  1825.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  1826.                 'options' => array(
  1827.                     'query' => Supplier::getSuppliers(false, $this->getConfiguration('PS_LANG_DEFAULT')),
  1828.                     'id' => 'id_supplier',
  1829.                     'name' => 'name'
  1830.                 )
  1831.             ),
  1832.             array(
  1833.                 'type'  => 'select',
  1834.                 'label' => $this->l('Manufacturers'),
  1835.                 'name'  => 'manufacturers[]',
  1836.                 'id'  => 'manufacturers',
  1837.                 'class' => 'ds-select',
  1838.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  1839.                 'multiple' => true,
  1840.                 'options' => array(
  1841.                     'query' => Manufacturer::getManufacturers(false, $this->getConfiguration('PS_LANG_DEFAULT')),
  1842.                     'id' => 'id_manufacturer',
  1843.                     'name' => 'name'
  1844.                 )
  1845.             )
  1846.         );
  1847.         if(_PS_VERSION_ >= 1.6) {
  1848.             $fields[] = array(
  1849.                 'type' => 'categories',
  1850.                 'label' => $this->l('Categories'),
  1851.                 'name' => 'categories',
  1852.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  1853.                 'tree' => array(
  1854.                     'id' => 'categories-tree',
  1855.                     'selected_categories' => $this->selected_cat,
  1856.                     'use_search' => true,
  1857.                     'use_checkbox' => true
  1858.                 )
  1859.             );
  1860.         }
  1861.         else
  1862.         {
  1863.             $root_category = Category::getRootCategory();
  1864.             $root_category = array('id_category' => $root_category->id, 'name' => $root_category->name);
  1865.  
  1866.             $fields[] = array(
  1867.                 'type' => 'categories',
  1868.                 'label' => $this->l('Parent category:'),
  1869.                 'name' => 'categories',
  1870.                 'values' => array(
  1871.                     'trads' => array(
  1872.                         'Root' => $root_category,
  1873.                         'selected' => $this->l('Selected'),
  1874.                         'Collapse All' => $this->l('Collapse All'),
  1875.                         'Expand All' => $this->l('Expand All'),
  1876.                         'Check All' => $this->l('Check All'),
  1877.                         'Uncheck All' => $this->l('Uncheck All'),
  1878.                     ),
  1879.                     'selected_cat' => ($this->selected_cat ? $this->selected_cat : array()),
  1880.                     'input_name' => 'categories[]',
  1881.                     'disabled_categories' => array(),
  1882.                     'use_checkbox' => true,
  1883.                     'use_radio' => false,
  1884.                     'use_search' => false,
  1885.                     'top_category' => Category::getTopCategory(),
  1886.                     'use_context' => true,
  1887.                 )
  1888.             );
  1889.         }
  1890.  
  1891.         return $fields;
  1892.     }
  1893.  
  1894.     public function addToFieldId($fields)
  1895.     {
  1896.         for ($i = 0; $i < count($fields); $i++) {
  1897.             //add id
  1898.             $fields[$i]['id'] = $i;
  1899.         }
  1900.  
  1901.         return $fields;
  1902.     }
  1903.  
  1904.     public function productsQuery($ae, $set)
  1905.     {
  1906.         $sql = 'SELECT p.`id_product` '.(empty($set['sqlfields']) ? "" : ', '.implode(', ', $set['sqlfields'])).
  1907.             ' FROM '._DB_PREFIX_.'product as p'.
  1908.             Shop::addSqlAssociation('product', 'p').'
  1909.            LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` '.Shop::addSqlRestrictionOnLang('pl').')
  1910.            LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (p.`id_supplier` = s.`id_supplier`)
  1911.            LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)'.
  1912.             (isset($set['categories']) && $set['categories']   ? ' LEFT JOIN `'._DB_PREFIX_.'category_product` c ON (c.`id_product` = p.`id_product`)' : '').'
  1913.             WHERE pl.`id_lang` = '.(int)$ae->id_lang.
  1914.             (isset($ae->only_new) && $ae->only_new && $ae->last_exported_id ? ' AND p.`id_product` > '.$ae->last_exported_id : '').
  1915.             ($ae->only_new == false && $ae->start_id ? ' AND p.`id_product` >= '.$ae->start_id : '').
  1916.             ($ae->only_new == false && $ae->end_id ? ' AND p.`id_product` <= '.$ae->end_id : '').
  1917.             (isset($set['categories']) && $set['categories']  ? ' AND c.`id_category` IN ('.implode(",", $set['categories'] ).')' : '').
  1918.             (isset($set['suppliers[]'] ) && $set['suppliers[]']     ? ' AND p.`id_supplier` IN ('.implode(",", $set['suppliers[]']).')' : '').
  1919.             (isset($set['manufacturers[]']) && $set['manufacturers[]']  ? ' AND p.`id_manufacturer` IN ('.implode(",",$set['manufacturers[]']).')' : '').
  1920.             (isset($set['active']) && $set['active']  ? ' AND product_shop.`active` = 1' : '').
  1921.             (isset($set['out_of_stock']) && $set['out_of_stock']  ? ' AND p.`quantity` <= 0' : '').
  1922.             (isset($set['ean13']) && $set['ean13']  ? ' AND p.`ean13` != ""' : '').
  1923.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND p.`date_add` >= "'.($ae->date_from).'"' : '').
  1924.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND p.`date_add` <= "'.($ae->date_to).'"' : '').
  1925.             ' GROUP BY p.`id_product`';
  1926.  
  1927.         $result = $this->query($sql);
  1928.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  1929.         return $result;
  1930.     }
  1931.  
  1932.     public function ExecuteS($sql)
  1933.     {
  1934.         return Db::getInstance()->ExecuteS($sql);
  1935.     }
  1936.  
  1937.     public function Execute($sql)
  1938.     {
  1939.         return Db::getInstance()->Execute($sql);
  1940.     }
  1941.  
  1942.     public function query($sql_query)
  1943.     {
  1944.         return Db::getInstance(_PS_USE_SQL_SLAVE_)->query($sql_query);
  1945.     }
  1946.  
  1947.     public function products_attributes_name($obj)
  1948.     {
  1949.         return null;
  1950.     }
  1951.  
  1952.     public function products_supplier_name_all($obj)
  1953.     {
  1954.         $sups = $this->executeS('
  1955.         SELECT DISTINCT(s.`name`)
  1956.         FROM `'._DB_PREFIX_.'product_supplier` ps
  1957.         LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (ps.`id_supplier`= s.`id_supplier`)
  1958.         LEFT JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  1959.         WHERE ps.`id_product` = '.$obj->id);
  1960.         $suppliers = array();
  1961.         foreach($sups as $sup) {
  1962.             $suppliers[] = $sup['name'];
  1963.         }
  1964.  
  1965.         return implode(',', $suppliers);
  1966.     }
  1967.  
  1968.     public function products_id_supplier_all($obj)
  1969.     {
  1970.         $sups = $this->executeS('
  1971.         SELECT DISTINCT(ps.`id_supplier`)
  1972.         FROM `'._DB_PREFIX_.'product_supplier` ps
  1973.         JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  1974.         WHERE ps.`id_product` = '.$obj->id);
  1975.         $suppliers = array();
  1976.         foreach($sups as $sup) {
  1977.             $suppliers[] = $sup['id_supplier'];
  1978.         }
  1979.         return implode(',', $suppliers);
  1980.     }
  1981.  
  1982.     public function products_features($obj, $ae)
  1983.     {
  1984.         $features = $obj->getFrontFeaturesStatic($ae->id_lang, $obj->id);
  1985.         $feats = array();
  1986.         foreach($features as $feature) {
  1987.             $feats[] = $feature['name'] .'-'. $feature['value'];
  1988.         }
  1989.         return implode(",", $feats);
  1990.     }
  1991.  
  1992.     public function products_price_tax($obj)
  1993.     {
  1994.         return $obj->getPrice(true);
  1995.     }
  1996.  
  1997.     public function products_tax_rate($obj)
  1998.     {
  1999.         return $obj->getTaxesRate();
  2000.     }
  2001.  
  2002.     public function products_quantity($obj)
  2003.     {
  2004.         return Product::getQuantity((int)$obj->id);
  2005.     }
  2006.  
  2007.     public function products_price_tax_nodiscount($obj)
  2008.     {
  2009.         return $obj->getPrice(true, null, 6, null, false, false);
  2010.     }
  2011.  
  2012.     public function products_url_product($obj, $ae)
  2013.     {
  2014.         $category = Category::getLinkRewrite((int)$obj->id_category_default, (int)$ae->id_lang);
  2015.         return $this->context->link->getProductLink((int)$obj->id, $obj->link_rewrite[$ae->id_lang], $category);
  2016.     }
  2017.  
  2018.     public function products_manufacturer_name($obj)
  2019.     {
  2020.         return $obj->getWsManufacturerName();
  2021.     }
  2022.  
  2023.     public function products_categories_names($obj, $ae)
  2024.     {
  2025.         $categories = $obj->getCategories();
  2026.         $cats = array();
  2027.         foreach($categories as $cat) {
  2028.             $category = new Category($cat, $ae->id_lang);
  2029.             $cats[] = $category->name;
  2030.         }
  2031.         return implode(",", $cats);
  2032.     }
  2033.  
  2034.     public function products_categories_ids($obj, $ae)
  2035.     {
  2036.         $categories = $obj->getCategories();
  2037.         $cats = array();
  2038.         foreach($categories as $cat) {
  2039.             $category = new Category($cat, $ae->id_lang);
  2040.             $cats[] = $category->id;
  2041.         }
  2042.         return implode(",", $cats);
  2043.     }
  2044.  
  2045.     public function products_nameCategoryDefault($obj, $ae)
  2046.     {
  2047.         $category = new Category($obj->id_category_default, $ae->id_lang);
  2048.         return $category->name;
  2049.     }
  2050.  
  2051.     public function products_images($obj, $ae)
  2052.     {
  2053.         $imagelinks = array();
  2054.         $images = $obj->getImages($obj->id);
  2055.         foreach($images as $image) {
  2056.             $imagelinks[] = $this->link->getImageLink($obj->link_rewrite[$ae->id_lang], $obj->id .'-'. $image['id_image'], $ae->image_type);
  2057.         }
  2058.         return implode(",", $imagelinks);
  2059.     }
  2060.  
  2061.     public function products_tags($obj)
  2062.     {
  2063.         return $obj->getTags(1);
  2064.     }
  2065.  
  2066.     public function products_accessories($obj, $ae)
  2067.     {
  2068.         if($accessories = $obj->getAccessories($ae->id_lang, false)) {
  2069.             foreach($accessories as $value)
  2070.             {
  2071.                 $accessories[] = $value['reference'];
  2072.             }
  2073.             return implode(',', $accessories);
  2074.         } else {
  2075.             return '';
  2076.         }
  2077.     }
  2078.  
  2079.     public function getProductCombination($obj, $ae)
  2080.     {
  2081.         $combArray = null;
  2082.         $groups = null;
  2083.  
  2084.         $combinaisons = $obj->getAttributeCombinations((int)($ae->id_lang));
  2085.         if (is_array($combinaisons))
  2086.         {
  2087.             $combinationImages = $obj->getCombinationImages((int)($ae->id_lang));
  2088.  
  2089.             foreach ($combinaisons AS $k => $combinaison)
  2090.             {
  2091.                 $combArray[$combinaison['id_product_attribute']]['wholesale_price'] = $combinaison['wholesale_price'];
  2092.                 $combArray[$combinaison['id_product_attribute']]['price'] = $combinaison['price'];
  2093.                 $combArray[$combinaison['id_product_attribute']]['weight'] = $combinaison['weight'];
  2094.                 $combArray[$combinaison['id_product_attribute']]['unit_impact'] = $combinaison['unit_price_impact'];
  2095.                 $combArray[$combinaison['id_product_attribute']]['reference'] = $combinaison['reference'];
  2096.                 $combArray[$combinaison['id_product_attribute']]['supplier_reference'] = $combinaison['supplier_reference'];
  2097.                 $combArray[$combinaison['id_product_attribute']]['ean13'] = $combinaison['ean13'];
  2098.                 $combArray[$combinaison['id_product_attribute']]['upc'] = $combinaison['upc'];
  2099.                 $combArray[$combinaison['id_product_attribute']]['minimal_quantity'] = $combinaison['minimal_quantity'];
  2100.                 $combArray[$combinaison['id_product_attribute']]['location'] = $combinaison['location'];
  2101.                 $combArray[$combinaison['id_product_attribute']]['quantity'] = $combinaison['quantity'];
  2102.                 $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;
  2103.                 //$combArray[$combinaison['id_products_attribute']]['default_on'] = $combinaison['default_on'];
  2104.                 $combArray[$combinaison['id_product_attribute']]['ecotax'] = $combinaison['ecotax'];
  2105.                 $combArray[$combinaison['id_product_attribute']]['id_product_attribute'] = $combinaison['id_product_attribute'];
  2106.                 $combArray[$combinaison['id_product_attribute']]['attributes'][] = array($combinaison['group_name'], $combinaison['attribute_name'], $combinaison['id_attribute']);
  2107.                 if ($combinaison['is_color_group'])
  2108.                     $groups[$combinaison['id_attribute_group']] = $combinaison['group_name'];
  2109.             }
  2110.         }
  2111.         return $combArray;
  2112.     }
  2113.  
  2114.     public function combination_supplier_name_all($obj, $product_attribute, $ae)
  2115.     {
  2116.         $sups = $this->executeS('
  2117.            SELECT DISTINCT(s.`name`)
  2118.            FROM `'._DB_PREFIX_.'product_supplier` ps
  2119.            LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (ps.`id_supplier`= s.`id_supplier`)
  2120.            LEFT JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  2121.            WHERE ps.`id_product` = '.$obj->id.' AND ps.id_product_attribute='.(int)$product_attribute['id_product_attribute']);
  2122.         $suppliers = array();
  2123.         foreach($sups as $sup) {
  2124.             $suppliers[] = $sup['name'];
  2125.         }
  2126.         return implode(',', $suppliers);
  2127.     }
  2128.  
  2129.     public function combination_id_supplier_all($obj, $product_attribute)
  2130.     {
  2131.         $sups = $this->executeS('
  2132.             SELECT DISTINCT(ps.`id_supplier`)
  2133.             FROM `'._DB_PREFIX_.'product_supplier` ps
  2134.             JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`)
  2135.             WHERE ps.`id_product` = '.$obj->id.' AND ps.id_product_attribute='.(int)$product_attribute['id_product_attribute']);
  2136.  
  2137.         $suppliers = array();
  2138.         foreach($sups as $sup)
  2139.             $suppliers[] = $sup['id_supplier'];
  2140.  
  2141.         return implode(',', $suppliers);
  2142.     }
  2143.  
  2144.     public function combination_attributes_name($obj, $products_attribute)
  2145.     {
  2146.         $name = null;
  2147.         foreach ($products_attribute['attributes'] AS $attribute)
  2148.         {
  2149.             $name .= addslashes(htmlspecialchars($attribute[0])).': '.addslashes(htmlspecialchars($attribute[1])).' ';
  2150.         }
  2151.         return Tools::stripslashes($name);
  2152.     }
  2153.  
  2154.     public function combination_wholesale_price($obj, $product_attribute)
  2155.     {
  2156.         return $product_attribute['wholesale_price'];
  2157.     }
  2158.  
  2159.     public function combination_price($obj, $product_attribute)
  2160.     {
  2161.         return Product::getPriceStatic((int)$obj->id, false, (int)$product_attribute['id_product_attribute']);
  2162.     }
  2163.  
  2164.     public function combination_weight($obj, $product_attribute)
  2165.     {
  2166.         return $product_attribute['weight'];
  2167.     }
  2168.  
  2169.     public function combination_price_tax($obj, $product_attribute)
  2170.     {
  2171.         return $obj->getPrice(true, (int)$product_attribute['id_product_attribute']);
  2172.     }
  2173.  
  2174.     public function combination_price_tax_nodiscount($obj, $product_attribute)
  2175.     {
  2176.         return $obj->getPrice(true, (int)$product_attribute['id_product_attribute'], 2, null, false, false);
  2177.     }
  2178.  
  2179.     public function combination_unit_impact($obj, $products_attribute)
  2180.     {
  2181.         return $products_attribute['unit_impact'];
  2182.     }
  2183.  
  2184.     public function combination_reference($obj, $products_attribute)
  2185.     {
  2186.         return $products_attribute['reference'];
  2187.     }
  2188.  
  2189.     public function combination_supplier_reference($obj, $products_attribute)
  2190.     {
  2191.         return $products_attribute['supplier_reference'];
  2192.     }
  2193.  
  2194.     public function combination_ean13($obj, $products_attribute)
  2195.     {
  2196.         return $products_attribute['ean13'];
  2197.     }
  2198.  
  2199.     public function combination_upc($obj, $products_attribute)
  2200.     {
  2201.         return $products_attribute['upc'];
  2202.     }
  2203.  
  2204.     public function combination_minimal_quantity($obj, $products_attribute)
  2205.     {
  2206.         return $products_attribute['minimal_quantity'];
  2207.     }
  2208.  
  2209.     public function combination_location($obj, $products_attribute)
  2210.     {
  2211.         return $products_attribute['location'];
  2212.     }
  2213.  
  2214.     public function combination_quantity($obj, $products_attribute)
  2215.     {
  2216.         return $products_attribute['quantity'];
  2217.     }
  2218.  
  2219.     public function combination_ecotax($obj, $products_attribute)
  2220.     {
  2221.         return $products_attribute['ecotax'];
  2222.     }
  2223.  
  2224.     public function combination_images($obj, $product_attribute, $ae)
  2225.     {
  2226.         $attrImage = ($product_attribute['id_image'] ? new Image($product_attribute['id_image']) : null);
  2227.         if($attrImage){
  2228.             return $this->link->getImageLink($obj->link_rewrite[$ae->id_lang], $obj->id .'-'. $attrImage->id, $ae->image_type);
  2229.         }
  2230.     }
  2231.  
  2232.     public function ordersFormFields()
  2233.     {
  2234.         $fields = array(
  2235.             array(
  2236.                 'type'  => 'bsmselect',
  2237.                 'label' => $this->l('Orders fields'),
  2238.                 'name'  => 'fields[]',
  2239.                 'id'  => 'fields',
  2240.                 'class' => 'ds-select',
  2241.                 'multiple' => true,
  2242.                 'options' => array(
  2243.                     'query' => $this->addToFieldId($this->orders),
  2244.                     'id' => 'id',
  2245.                     'name' => 'name'
  2246.                 )
  2247.             ),
  2248.             array(
  2249.                 'type'  => 'select',
  2250.                 'label' => $this->l('Customers groups'),
  2251.                 'name'  => 'groups[]',
  2252.                 'id' => 'groups',
  2253.                 'class' => 'ds-select',
  2254.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2255.                 'multiple' => true,
  2256.                 'options' => array(
  2257.                     'query' => Group::getGroups($this->getConfiguration('PS_LANG_DEFAULT')),
  2258.                     'id' => 'id_group',
  2259.                     'name' => 'name'
  2260.                 )
  2261.             ),
  2262.             array(
  2263.                 'type'  => 'select',
  2264.                 'label' => $this->l('Payments'),
  2265.                 'name'  => 'payments[]',
  2266.                 'id'  => 'payments',
  2267.                 'class' => 'ds-select',
  2268.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2269.                 'multiple' => true,
  2270.                 'options' => array(
  2271.                     'query' => PaymentModule::getInstalledPaymentModules(),
  2272.                     'id' => 'name',
  2273.                     'name' => 'name'
  2274.                 )
  2275.             ),
  2276.             array(
  2277.                 'type'  => 'select',
  2278.                 'label' => $this->l('Carrier type'),
  2279.                 'name'  => 'carriers[]',
  2280.                 'id' => 'carriers',
  2281.                 'class' => 'ds-select',
  2282.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2283.                 'multiple' => true,
  2284.                 'options' => array(
  2285.                     'query' => Carrier::getCarriers($this->getConfiguration('PS_LANG_DEFAULT')),
  2286.                     'id' => 'id_carrier',
  2287.                     'name' => 'name'
  2288.                 )
  2289.             ),
  2290.             array(
  2291.                 'type'  => 'select',
  2292.                 'label' => $this->l('Order state'),
  2293.                 'name'  => 'state[]',
  2294.                 'id' => 'state',
  2295.                 'class' => 'ds-select',
  2296.                 'desc' =>$this->l('If you want all leave blank. All are exported by default.'),
  2297.                 'multiple' => true,
  2298.                 'options' => array(
  2299.                     'query' => OrderState::getOrderStates($this->getConfiguration('PS_LANG_DEFAULT')),
  2300.                     'id' => 'id_order_state',
  2301.                     'name' => 'name'
  2302.                 )
  2303.             )
  2304.         );
  2305.  
  2306.         return $fields;
  2307.     }
  2308.  
  2309.     public function ordersQuery($ae, $sorted_fields)
  2310.     {
  2311.         $sql = 'SELECT o.`id_order` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2312.                FROM '._DB_PREFIX_.'orders o
  2313.                LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON ( od.`id_order` = o.`id_order` )
  2314.                LEFT JOIN `'._DB_PREFIX_.'shop` sh ON ( o.`id_shop` = sh.`id_shop` )
  2315.                LEFT JOIN `'._DB_PREFIX_.'customer` cu ON ( o.`id_customer` = cu.`id_customer` )
  2316.                LEFT JOIN `'._DB_PREFIX_.'gender_lang` gl ON ( cu.`id_gender` = gl.`id_gender` AND gl.`id_lang` = 1)
  2317.                LEFT JOIN `'._DB_PREFIX_.'gender_lang` inv_gl ON ( cu.`id_gender` = inv_gl.`id_gender` AND inv_gl.`id_lang` = 1)
  2318.                LEFT JOIN `'._DB_PREFIX_.'address` a ON ( a.`id_address` = o.`id_address_delivery` )
  2319.                LEFT JOIN `'._DB_PREFIX_.'address` inv_a ON ( inv_a.`id_address` = o.`id_address_invoice` )
  2320.                LEFT JOIN `'._DB_PREFIX_.'state` s ON ( s.`id_state` = a.`id_state` )
  2321.                LEFT JOIN `'._DB_PREFIX_.'country` co ON ( co.`id_country` = a.`id_country` )
  2322.                LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON ( cl.`id_country` = co.`id_country` )
  2323.                LEFT JOIN `'._DB_PREFIX_.'carrier` ca ON ( ca.`id_carrier` = o.`id_carrier` )
  2324.                LEFT JOIN `'._DB_PREFIX_.'order_payment` op ON ( op.`order_reference` = o.`reference` )
  2325.                LEFT JOIN `'._DB_PREFIX_.'message` m ON ( m.`id_order` = o.`id_order` )
  2326.                LEFT JOIN `'._DB_PREFIX_.'currency` cur ON ( o.`id_currency` = cur.`id_currency` )
  2327.                WHERE cl.`id_lang`= '.$ae->id_lang.
  2328.                 (isset($ae->only_new) && $ae->only_new ? ' AND o.`id_order` > '.$ae->last_exported_id : '').
  2329.                 ($ae->only_new == false && $ae->start_id ? ' AND o.`id_order` >= '.$ae->start_id : '').
  2330.                 ($ae->only_new == false && $ae->end_id ? ' AND o.`id_order` <= '.$ae->end_id : '').
  2331.                 (isset($sorted_fields['groups[]']) && $sorted_fields['groups[]'] ? ' AND cu.`id_default_group` IN ('.implode(', ', $sorted_fields['groups[]']).')' : '').
  2332.                 (isset($sorted_fields['payments[]']) && $sorted_fields['payments[]'] ? ' AND o.`module` IN ("'.implode('", "', $sorted_fields['payments[]']).'")' : '').
  2333.                 (isset($sorted_fields['carriers[]']) && $sorted_fields['carriers[]'] ? ' AND o.`id_carrier` IN ('.implode(', ', $sorted_fields['carriers[]']).')' : '').
  2334.                 (isset($sorted_fields['state[]']) && $sorted_fields['state[]'] ? ' AND o.`current_state` IN ('.implode(', ', $sorted_fields['state[]']).')' : '').
  2335.                 (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND o.`date_add` >= "'.($ae->date_from).'"' : '').
  2336.                 (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND o.`date_add` <= "'.($ae->date_to).'"' : '')
  2337.                              .Shop::addSqlRestriction(false, 'o').
  2338.                 ' GROUP BY '.(isset($sorted_fields['order_detail']) && $sorted_fields['order_detail']  ? 'od.`id_order_detail`' : 'o.`id_order`');
  2339.  
  2340.         $result = $this->query($sql);
  2341.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2342.         return $result;
  2343.     }
  2344.  
  2345.     public function orders_code($obj)
  2346.     {
  2347.         $result = $obj->getCartRules();
  2348.         $codes = array();
  2349.         foreach($result as $res) {
  2350.             $codes[] = $res['name'];
  2351.         }
  2352.         return implode(',', $codes);
  2353.     }
  2354.  
  2355.     public function orders_customization($obj,$ae, $element)
  2356.     {
  2357.         $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  2358.                 SELECT cud.`value`, cu.`quantity`
  2359.                 FROM `'._DB_PREFIX_.'customization` cu
  2360.                 INNER JOIN `'._DB_PREFIX_.'customized_data` cud ON (cud.`id_customization` = cu.`id_customization`)
  2361.                 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']) );
  2362.         $cud = array();
  2363.         foreach($result as $res)
  2364.             $cud[] = 'value:'.$res['value'] . ' ' . 'quantity:' . $res['quantity'];
  2365.  
  2366.         return implode(',', $cud);
  2367.     }
  2368.  
  2369.     public function categoriesQuery($ae, $sorted_fields)
  2370.     {
  2371.         $sql = 'SELECT c.`id_category` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2372.            FROM `'._DB_PREFIX_.'category` c
  2373.             '.Shop::addSqlAssociation('category', 'c').'
  2374.             LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').'
  2375.             WHERE 1'.($ae->id_lang ? ' AND `id_lang` = '.(int)$ae->id_lang : '').
  2376.             (isset($ae->only_new) && $ae->only_new ? ' AND c.`id_category` > '.$ae->last_exported_id : '').
  2377.             ($ae->only_new == false && $ae->start_id ? ' AND c.`id_category` >= '.$ae->start_id : '').
  2378.             ($ae->only_new == false && $ae->end_id ? ' AND c.`id_category` <= '.$ae->end_id : '').
  2379.             (isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND c.`active` = 1' : '').
  2380.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND c.`date_add` >= "'.($ae->date_from).'"' : '').
  2381.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND c.`date_add` <= "'.($ae->date_to).'"' : '').'
  2382.             GROUP BY c.id_category
  2383.             ORDER BY c.`level_depth` ASC, category_shop.`position` ASC';
  2384.         $result = $this->query($sql);
  2385.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2386.         return $result;
  2387.     }
  2388.  
  2389.     public function categories_id_group($obj)
  2390.     {
  2391.         $result = $this->executeS('
  2392.             SELECT cg.`id_group`
  2393.             FROM '._DB_PREFIX_.'category_group cg
  2394.             WHERE cg.`id_category` = '.(int)$obj->id);
  2395.         $groups = null;
  2396.         foreach ($result as $group)
  2397.             $groups = $group['id_group'];
  2398.  
  2399.         return $groups;
  2400.     }
  2401.  
  2402.     public function categoriesFormFields()
  2403.     {
  2404.         $fields = array(
  2405.             array(
  2406.                 'type'  => 'bsmselect',
  2407.                 'label' => $this->l('Orders fields'),
  2408.                 'name'  => 'fields[]',
  2409.                 'id'  => 'fields',
  2410.                 'class' => 'sminit, ds-select',
  2411.                 'multiple' => true,
  2412.                 'options' => array(
  2413.                     'query' => $this->addToFieldId($this->categories),
  2414.                     'id' => 'id',
  2415.                     'name' => 'name'
  2416.                 )
  2417.             ),
  2418.             array(
  2419.                 'type' => $this->switch,
  2420.                 'label' => $this->l('Only active'),
  2421.                 'name' => 'active',
  2422.                 'class' => 't',
  2423.                 'is_bool' => true,
  2424.                 'values' => array(
  2425.                     array(
  2426.                         'id' => 'active_on',
  2427.                         'value' => 1,
  2428.                         'label' => $this->l('Yes')
  2429.                     ),
  2430.                     array(
  2431.                         'id' => 'active_off',
  2432.                         'value' => 0,
  2433.                         'label' => $this->l('No')
  2434.                     )
  2435.                 ),
  2436.             )
  2437.         );
  2438.  
  2439.         return $fields;
  2440.     }
  2441.  
  2442.     public function manufacturersQuery($ae, $sorted_fields)
  2443.     {
  2444.         $sql = 'SELECT m.`id_manufacturer` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2445.         FROM `'._DB_PREFIX_.'manufacturer` m
  2446.         '.Shop::addSqlAssociation('manufacturer', 'm').'
  2447.         INNER JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)$ae->id_lang.')'.'
  2448.            WHERE 1'.(isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND m.`active` = 1' : '').
  2449.             (isset($ae->only_new) && $ae->only_new ? ' AND m.`id_manufacturer` > '.$ae->last_exported_id : '').
  2450.             ($ae->only_new == false && $ae->start_id ? ' AND m.`id_manufacturer` >= '.$ae->start_id : '').
  2451.             ($ae->only_new == false && $ae->end_id ? ' AND m.`id_manufacturer` <= '.$ae->end_id : '').
  2452.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND m.`date_add` >= "'.($ae->date_from).'"' : '').
  2453.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND m.`date_add` <= "'.($ae->date_to).'"' : '');
  2454.         $result = $this->query($sql);
  2455.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2456.         return $result;
  2457.     }
  2458.  
  2459.     public function manufacturersFormFields()
  2460.     {
  2461.         $fields = array(
  2462.             array(
  2463.                 'type' => 'bsmselect',
  2464.                 'label' => $this->l('Manufacturers fields'),
  2465.                 'name' => 'fields[]',
  2466.                 'id' => 'fields',
  2467.                 'class' => 'ds-select',
  2468.                 'multiple' => true,
  2469.                 'options' => array(
  2470.                     'query' => $this->addToFieldId($this->manufacturers),
  2471.                     'id' => 'id',
  2472.                     'name' => 'name'
  2473.                 )
  2474.             ),
  2475.             array(
  2476.                 'type' => $this->switch,
  2477.                 'label' => $this->l('Only active'),
  2478.                 'name' => 'active',
  2479.                 'class' => 't',
  2480.                 'is_bool' => true,
  2481.                 'values' => array(
  2482.                     array(
  2483.                         'id' => 'active_on',
  2484.                         'value' => 1,
  2485.                         'label' => $this->l('Yes')
  2486.                     ),
  2487.                     array(
  2488.                         'id' => 'active_off',
  2489.                         'value' => 0,
  2490.                         'label' => $this->l('No')
  2491.                     )
  2492.                 ),
  2493.             )
  2494.         );
  2495.  
  2496.         return $fields;
  2497.     }
  2498.  
  2499.     public function suppliersQuery($ae, $sorted_fields)
  2500.     {
  2501.         $sql = 'SELECT s.`id_supplier` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2502.         FROM `'._DB_PREFIX_.'supplier` s
  2503.         '.Shop::addSqlAssociation('supplier', 's').'
  2504.         INNER JOIN `'._DB_PREFIX_.'supplier_lang` sl ON (s.`id_supplier` = sl.`id_supplier` AND sl.`id_lang` = '.(int)$ae->id_lang.')'.'
  2505.            WHERE 1'.(isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND s.`active` = 1' : '').
  2506.             (isset($ae->only_new) && $ae->only_new ? ' AND s.`id_supplier` > '.$ae->last_exported_id : '').
  2507.             ($ae->only_new == false && $ae->start_id ? ' AND s.`id_supplier` >= '.$ae->start_id : '').
  2508.             ($ae->only_new == false && $ae->end_id ? ' AND s.`id_supplier` <= '.$ae->end_id : '').
  2509.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND s.`date_add` >= "'.($ae->date_from).'"' : '').
  2510.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND s.`date_add` <= "'.($ae->date_to).'"' : '');
  2511.         $result = $this->query($sql);
  2512.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2513.         return $result;
  2514.     }
  2515.  
  2516.     public function suppliersFormFields()
  2517.     {
  2518.         $fields = array(
  2519.             array(
  2520.                 'type' => 'bsmselect',
  2521.                 'label' => $this->l('Orders fields'),
  2522.                 'name' => 'fields[]',
  2523.                 'id' => 'fields',
  2524.                 'class' => 'sminit, ds-select',
  2525.                 'multiple' => true,
  2526.                 'options' => array(
  2527.                     'query' => $this->addToFieldId($this->suppliers),
  2528.                     'id' => 'id',
  2529.                     'name' => 'name'
  2530.                 )
  2531.             ),
  2532.             array(
  2533.                 'type' => $this->switch,
  2534.                 'label' => $this->l('Only active'),
  2535.                 'name' => 'active',
  2536.                 'class' => 't',
  2537.                 'is_bool' => true,
  2538.                 'values' => array(
  2539.                     array(
  2540.                         'id' => 'active_on',
  2541.                         'value' => 1,
  2542.                         'label' => $this->l('Yes')
  2543.                     ),
  2544.                     array(
  2545.                         'id' => 'active_off',
  2546.                         'value' => 0,
  2547.                         'label' => $this->l('No')
  2548.                     )
  2549.                 ),
  2550.             )
  2551.         );
  2552.  
  2553.         return $fields;
  2554.     }
  2555.  
  2556.     public function customersFormFields()
  2557.     {
  2558.         $fields = array(
  2559.             array(
  2560.                 'type' => 'bsmselect',
  2561.                 'label' => $this->l('Customers fields'),
  2562.                 'name' => 'fields[]',
  2563.                 'id' => 'fields',
  2564.                 'class' => 'sminit, ds-select',
  2565.                 'multiple' => true,
  2566.                 'options' => array(
  2567.                     'query' => $this->addToFieldId($this->customers),
  2568.                     'id' => 'id',
  2569.                     'name' => 'name'
  2570.                 )
  2571.  
  2572.             ),
  2573.             array(
  2574.                 'type' => $this->switch,
  2575.                 'label' => $this->l('Only active'),
  2576.                 'name' => 'active',
  2577.                 'class' => 't',
  2578.                 'is_bool' => true,
  2579.                 'values' => array(
  2580.                     array(
  2581.                         'id' => 'active_on',
  2582.                         'value' => 1,
  2583.                         'label' => $this->l('Yes')
  2584.                     ),
  2585.                     array(
  2586.                         'id' => 'active_off',
  2587.                         'value' => 0,
  2588.                         'label' => $this->l('No')
  2589.                     )
  2590.                 ),
  2591.             )
  2592.         );
  2593.  
  2594.         return $fields;
  2595.     }
  2596.  
  2597.     public function customersQuery($ae, $sorted_fields)
  2598.     {
  2599.         $sql = 'SELECT c.`id_customer` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'  
  2600.                 FROM '._DB_PREFIX_.'customer c
  2601.                LEFT JOIN `'._DB_PREFIX_.'address` a ON ( a.`id_customer` = c.`id_customer` )
  2602.                 WHERE 1'.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).
  2603.             (isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND c.`active` = 1' : '').
  2604.             (isset($ae->only_new) && $ae->only_new ? ' AND c.`id_customer` > '.$ae->last_exported_id : '').
  2605.             ($ae->only_new == false && $ae->start_id ? ' AND c.`id_customer` >= '.$ae->start_id : '').
  2606.             ($ae->only_new == false && $ae->end_id ? ' AND c.`id_customer` <= '.$ae->end_id : '').
  2607.             (isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND c.`active` = 1' : '').
  2608.             (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND c.`date_add` >= "'.($ae->date_from).'"' : '').
  2609.             (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND c.`date_add` <= "'.($ae->date_to).'"' : '');
  2610.         $result = $this->query($sql);
  2611.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2612.         return $result;
  2613.     }
  2614.  
  2615.  
  2616.     public function newslettersQuery($ae, $sorted_fields)
  2617.     {
  2618.         $sql = 'SELECT n.`id` '.(empty($sorted_fields['sqlfields']) ? "" : ', '.implode(', ', $sorted_fields['sqlfields'])).'
  2619.                 FROM '._DB_PREFIX_.'newsletter as n
  2620.                 WHERE 1'.(isset($sorted_fields['active']) && $sorted_fields['active'] ? ' AND n.`active` = 1' : '').
  2621.                 (isset($ae->only_new) && $ae->only_new ? ' AND n.`id` > '.$ae->last_exported_id : '').
  2622.                 ($ae->only_new == false && $ae->start_id ? ' AND n.`id` >= '.$ae->start_id : '').
  2623.                 ($ae->only_new == false && $ae->end_id ? ' AND n.`id` <= '.$ae->end_id : '').
  2624.                 (isset($ae->date_from) && $ae->date_from &&  !$ae->only_new ? ' AND n.`newsletter_date_add` >= "'.($ae->date_from).'"' : '').
  2625.                 (isset($ae->date_to) && $ae->date_to &&  !$ae->only_new ? ' AND n.`newsletter_date_add` <= "'.($ae->date_to).'"' : '').'
  2626.                 AND n.`id_shop` = '.$this->context->shop->id;
  2627.         $result = $this->query($sql);
  2628.         $this->rowsNumber = $this->query("SELECT FOUND_ROWS()")->fetchColumn();
  2629.         return $result;
  2630.     }
  2631.  
  2632.     public function newslettersFormFields()
  2633.     {
  2634.         $fields = array(
  2635.             array(
  2636.                 'type' => 'bsmselect',
  2637.                 'label' => $this->l('Newsletters fields'),
  2638.                 'name' => 'fields[]',
  2639.                 'id' => 'fields',
  2640.                 'class' => 'sminit, ds-select',
  2641.                 'multiple' => true,
  2642.                 'options' => array(
  2643.                     'query' => $this->addToFieldId($this->newsletters),
  2644.                     'id' => 'id',
  2645.                     'name' => 'name'
  2646.                 )
  2647.             ),
  2648.             array(
  2649.                 'type' => $this->switch,
  2650.                 'label' => $this->l('Only active'),
  2651.                 'class' => 't',
  2652.                 'name' => 'active',
  2653.                 'is_bool' => true,
  2654.                 'values' => array(
  2655.                     array(
  2656.                         'id' => 'active_on',
  2657.                         'value' => 1,
  2658.                         'label' => $this->l('Yes')
  2659.                     ),
  2660.                     array(
  2661.                         'id' => 'active_off',
  2662.                         'value' => 0,
  2663.                         'label' => $this->l('No')
  2664.                     )
  2665.                 ),
  2666.             )
  2667.         );
  2668.  
  2669.         return $fields;
  2670.     }
  2671.  
  2672.     public function getLanguages()
  2673.     {
  2674.         return Language::getLanguages();
  2675.     }
  2676.  
  2677.     /**
  2678.      * Add key to array element as id
  2679.      * for select control
  2680.      * @param array $array
  2681.      * @return array $array
  2682.      */
  2683.     public function addKeyAsArrayElementId($array)
  2684.     {
  2685.         for ($i=0; $i < count($array); $i++) {
  2686.             $array[$i]['id'] = $i;
  2687.         }
  2688.  
  2689.         return $array;
  2690.     }
  2691.  
  2692.     /**
  2693.      * @param $object
  2694.      * @param $ae
  2695.      * @param $value
  2696.      * @return mixed
  2697.      */
  2698.     public function processDecimalSettings($object, $ae, $value)
  2699.     {
  2700.         if ($this->is_decimal($object[$value]))
  2701.         {
  2702.             //this have to be first becasue if we change separator
  2703.             //the value is not recognise
  2704.             if($ae->decimal_round != -1 && $ae->decimal_round != "-1")
  2705.                 $object[$value] = Tools::ps_round((float)$object[$value], $ae->decimal_round);
  2706.  
  2707.             if($ae->decimal_separator != -1 && $ae->decimal_separator != "-1")
  2708.             {
  2709.                 $object[$value] = str_replace(',', $ae->decimal_separator, $object[$value]);
  2710.                 $object[$value] = str_replace('.', $ae->decimal_separator, $object[$value]);
  2711.             }
  2712.         }
  2713.         if ($ae->strip_tags)
  2714.             $object[$value] = strip_tags($object[$value]);
  2715.         return $object;
  2716.     }
  2717.  
  2718.     public function is_decimal( $val )
  2719.     {
  2720.         return is_numeric( $val ) && strpos( $val, "." ) !== false;
  2721.     }
  2722.  
  2723.     /**
  2724.      * @param $type
  2725.      * @return array
  2726.      */
  2727.     public function getLinks($type)
  2728.     {
  2729.         $links = $this->DbExecuteS("select * from " . _DB_PREFIX_ . "advancedexport where type = '$type'");
  2730.         for ($i = 0; $i < count($links); $i++) {
  2731.             $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>";
  2732.             $type = $this->getSaveTypes();
  2733.             $links[$i]['save_type'] = $type[$links[$i]['save_type']]['short_name'];
  2734.         }
  2735.         return $links;
  2736.     }
  2737.  
  2738.     /**
  2739.      * @param $ae
  2740.      * @param $sorted_fields
  2741.      * @param $file
  2742.      */
  2743.     public function filewrite($ae, $sorted_fields, $file)
  2744.     {
  2745.         fwrite($file, implode($ae->delimiter, $sorted_fields['labels']) . "\r\n");
  2746.     }
  2747. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement