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