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