· 8 years ago · Mar 20, 2018, 04:54 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 public $products = array(
15 array('name' => 'Product Id','field' => 'id_product', 'database' => 'products', 'alias' => 'p'),
16 array('name' => 'Product Reference','field' => 'reference', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
17 array('name' => 'Name','field' => 'name', 'database' => 'products_lang', 'alias' => 'pl'),
18 array('name' => 'Short Description','field' => 'description_short', 'database' => 'products_lang', 'alias' => 'pl'),
19 array('name' => 'Long Description','field' => 'description', 'database' => 'products_lang', 'alias' => 'pl'),
20 array('name' => 'Quantity','field' => 'quantity', 'database' => 'other', 'attribute' => true),
21 array('name' => 'Price','field' => 'price', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
22 array('name' => 'Price Catalogue TTC','field' => 'price_tax_nodiscount', 'database' => 'other', 'attribute' => true),
23 array('name' => 'Price Tax','field' => 'price_tax', 'database' => 'other', 'attribute' => true),
24 array('name' => 'Wholesale Price','field' => 'wholesale_price', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
25 array('name' => 'Supplier Id (default)','field' => 'id_supplier', 'database' => 'products', 'alias' => 'p'),
26 array('name' => 'Suppliers Ids','field' => 'id_supplier_all', 'database' => 'other', 'attribute' => true),
27 array('name' => 'Supplier Name (default)','field' => 'supplier_name', 'as' => true, 'database' => 'supplier', 'alias' => 's'),
28 array('name' => 'Supplier Names','field' => 'supplier_name_all', 'database' => 'other', 'attribute' => true),
29 array('name' => 'Manufacturer Id','field' => 'id_manufacturer', 'database' => 'products', 'alias' => 'p'),
30 array('name' => 'Manufacturer Name','field' => 'manufacturer_name', 'database' => 'other'),
31 array('name' => 'Tax Id Rules Group','field' => 'id_tax_rules_group', 'database' => 'products', 'alias' => 'p'),
32 array('name' => 'Tax Rate','field' => 'tax_rate', 'database' => 'other'),
33 array('name' => 'Default Category Id','field' => 'id_category_default', 'database' => 'products', 'alias' => 'p'),
34 array('name' => 'Default Category Name','field' => 'nameCategoryDefault', 'database' => 'other'),
35 array('name' => 'Categories Names','field' => 'categories_names', 'database' => 'other'),
36 array('name' => 'Categories Ids','field' => 'categories_ids', 'database' => 'other'),
37 array('name' => 'On Sale','field' => 'on_sale', 'database' => 'products', 'alias' => 'p'),
38 array('name' => 'EAN 13','field' => 'ean13', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
39 array('name' => 'Supplier Reference','field' => 'supplier_reference', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
40 array('name' => 'Date Added','field' => 'date_add', 'database' => 'products', 'alias' => 'p'),
41 array('name' => 'Date Update','field' => 'date_upd', 'database' => 'products', 'alias' => 'p'),
42 array('name' => 'Active','field' => 'active', 'database' => 'products', 'alias' => 'p'),
43 array('name' => 'Meta Title','field' => 'meta_title', 'database' => 'products_lang', 'alias' => 'pl'),
44 array('name' => 'Meta Description','field' => 'meta_description', 'database' => 'products_lang', 'alias' => 'pl'),
45 array('name' => 'Meta Keywords','field' => 'meta_keywords', 'database' => 'products_lang', 'alias' => 'pl'),
46 array('name' => 'Available Now','field' => 'available_now', 'database' => 'products_lang', 'alias' => 'pl'),
47 array('name' => 'Available Later','field' => 'available_later', 'database' => 'products_lang', 'alias' => 'pl'),
48 array('name' => 'Tags','field' => 'tags', 'database' => 'other'),
49 array('name' => 'Accessories','field' => 'accessories', 'database' => 'other'),
50 array('name' => 'Images','field' => 'images', 'database' => 'other', 'attribute' => true),
51 array('name' => 'Online only','field' => 'online_only', 'database' => 'products', 'alias' => 'p'),
52 array('name' => 'Upc','field' => 'upc', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
53 array('name' => 'Ecotax','field' => 'ecotax', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
54 array('name' => 'Unity','field' => 'unity', 'database' => 'products', 'alias' => 'p'),
55 array('name' => 'Unit Price Ratio','field' => 'unit_price_ratio', 'database' => 'products', 'alias' => 'p'),
56 array('name' => 'Minimal Quantity','field' => 'minimal_quantity', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
57 array('name' => 'Additional Shipping Cost','field' => 'additional_shipping_cost', 'database' => 'products', 'alias' => 'p'),
58 array('name' => 'Location','field' => 'location', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
59 array('name' => 'Width','field' => 'width', 'database' => 'products', 'alias' => 'p'),
60 array('name' => 'Height','field' => 'height', 'database' => 'products', 'alias' => 'p'),
61 array('name' => 'Depth','field' => 'depth', 'database' => 'products', 'alias' => 'p'),
62 array('name' => 'Weight','field' => 'weight', 'database' => 'products', 'alias' => 'p', 'attribute' => true),
63 array('name' => 'Out Of Stock','field' => 'out_of_stock', 'database' => 'products', 'alias' => 'p'),
64 array('name' => 'Quantity Discount','field' => 'quantity_discount', 'database' => 'products', 'alias' => 'p'),
65 array('name' => 'Customizable','field' => 'customizable', 'database' => 'products', 'alias' => 'p'),
66 array('name' => 'Uploadable Files','field' => 'uploadable_files', 'database' => 'products', 'alias' => 'p'),
67 array('name' => 'Text Fields','field' => 'text_fields', 'database' => 'products', 'alias' => 'p'),
68 array('name' => 'Available For Order','field' => 'available_for_order', 'database' => 'products', 'alias' => 'p'),
69 array('name' => 'Condition','field' => 'condition', 'database' => 'products', 'alias' => 'p'),
70 array('name' => 'Show Price','field' => 'show_price', 'database' => 'products', 'alias' => 'p'),
71 array('name' => 'Indexed','field' => 'indexed', 'database' => 'products', 'alias' => 'p'),
72 array('name' => 'Cache Is Pack','field' => 'cache_is_pack', 'database' => 'products', 'alias' => 'p'),
73 array('name' => 'Cache Has Attachments','field' => 'cache_has_attachments', 'database' => 'products', 'alias' => 'p'),
74 array('name' => 'Cache Default Attribute','field' => 'cache_default_attribute', 'database' => 'products', 'alias' => 'p'),
75 array('name' => 'Link Rewrite','field' => 'link_rewrite', 'database' => 'products_lang', 'alias' => 'pl'),
76 array('name' => 'Url Product','field' => 'url_product', 'database' => 'other'),
77 array('name' => 'Features','field' => 'features', 'database' => 'other'),
78 array('name' => 'Attributes','field' => 'attributes_name', 'database' => 'other', 'attribute' => true)
79 );
80 public $orders = array(
81 //PS_ORDER
82 array('name' => 'Order No', 'field' => 'id_order', 'database' => 'orders', "alias" => "o"),
83 array('name' => 'Reference', 'field' => 'reference', 'database' => 'orders', "alias" => "o", 'attribute' => true),
84 array('name' => 'Code (voucher)', 'field' => 'code', 'database' => 'other'),
85 //SHOP
86 array('name' => 'Payment module', 'field' => 'module', 'database' => 'orders', "alias" => "o"),
87 array('name' => 'Payment', 'field' => 'payment', 'database' => 'orders', "alias" => "o"),
88 array('name' => 'Total paid', 'field' => 'total_paid', 'database' => 'orders', "alias" => "o"),
89 array('name' => 'Total paid tax incl', 'field' => 'total_paid_tax_incl', 'database' => 'orders', "alias" => "o"),
90 array('name' => 'Total paid tax excl', 'field' => 'total_paid_tax_excl', 'database' => 'orders', "alias" => "o"),
91 array('name' => 'Total products with tax', 'field' => 'total_products_wt', 'database' => 'orders', "alias" => "o"),
92 array('name' => 'Total paid real', 'field' => 'total_paid_real', 'database' => 'orders', "alias" => "o"),
93 array('name' => 'Total products', 'field' => 'total_products', 'database' => 'orders', "alias" => "o"),
94 array('name' => 'Total shipping', 'field' => 'total_shipping', 'database' => 'orders', "alias" => "o"),
95 array('name' => 'Total wrapping', 'field' => 'total_wrapping', 'database' => 'orders', "alias" => "o"),
96 array('name' => 'Shipping number', 'field' => 'shipping_number', 'database' => 'orders', "alias" => "o"),
97 array('name' => 'Delivery number', 'field' => 'delivery_number', 'database' => 'orders', "alias" => "o"),
98 array('name' => 'Invoice number', 'field' => 'invoice_number', 'database' => 'orders', "alias" => "o"),
99 array('name' => 'Invoice date', 'field' => 'invoice_date', 'database' => 'orders', "alias" => "o"),
100 array('name' => 'Delivery date', 'field' => 'delivery_date', 'database' => 'orders', "alias" => "o"),
101 array('name' => 'Date added', 'field' => 'date_add', 'database' => 'orders', "alias" => "o"),
102 array('name' => 'Date updated', 'field' => 'date_upd', 'database' => 'orders', "alias" => "o"),
103 array('name' => 'Total discounts', 'field' => 'total_discounts', 'database' => 'orders', "alias" => "o"),
104 array('name' => 'Gift message', 'field' => 'gift_message', 'database' => 'orders', "alias" => "o"),
105 array('name' => 'Valid', 'field' => 'valid', 'database' => 'orders', "alias" => "o"),
106 array('name' => 'Carrier id', 'field' => 'id_carrier', 'database' => 'orders', "alias" => "o"),
107 array('name' => 'Customer id', 'field' => 'id_customer', 'database' => 'orders', "alias" => "o"),
108 array('name' => 'Recycled packaging', 'field' => 'recyclable', 'database' => 'orders', "alias" => "o"),
109 array('name' => 'Gift wrapping', 'field' => 'gift', 'database' => 'orders', "alias" => "o"),
110 array('name' => 'Customization', 'field' => 'customization', 'database' => 'other', "alias" => "o"),
111 //PS_CUSTOMER
112 array('name' => 'Customer Firstname', 'field' => 'firstname', 'database' => 'customer', "alias" => "cu"),
113 array('name' => 'Customer Lastname', 'field' => 'lastname', 'database' => 'customer', "alias" => "cu"),
114 array('name' => 'Customer Email', 'field' => 'email', 'database' => 'customer', "alias" => "cu"),
115 array('name' => 'Customer id language', 'field' => 'id_lang', 'database' => 'customer', "alias" => "cu"),
116 //PS_ADRESS
117 array('name' => 'Delivery Gender', 'field' => 'delivery_name', 'as' => true, 'database' => 'gender', "alias" => "gl"),
118 array('name' => 'Delivery Company Name', 'field' => 'company', 'database' => 'address', "alias" => "a"),
119 array('name' => 'Delivery Firstname', 'field' => 'delivery_firstname', 'as' => true, 'database' => 'address', "alias" => "a"),
120 array('name' => 'Delivery Lastname', 'field' => 'delivery_lastname', 'as' => true, 'database' => 'address', "alias" => "a"),
121 array('name' => 'Delivery address line 1', 'field' => 'delivery_address1', 'as' => true, 'database' => 'address', "alias" => "a"),
122 array('name' => 'Delivery address line 2', 'field' => 'delivery_address2', 'as' => true, 'database' => 'address', "alias" => "a"),
123 array('name' => 'Delivery postcode', 'field' => 'delivery_postcode', 'as' => true, 'database' => 'address', "alias" => "a"),
124 array('name' => 'Delivery city', 'field' => 'delivery_city', 'as' => true, 'database' => 'address', "alias" => "a"),
125 array('name' => 'Delivery phone', 'field' => 'delivery_phone', 'as' => true, 'database' => 'address', "alias" => "a"),
126 array('name' => 'Delivery phone(mobile)', 'field' => 'delivery_phone_mobile', 'as' => true, 'database' => 'address', "alias" => "a"),
127 array('name' => 'Delivery VAT', 'field' => 'delivery_vat_number', 'as' => true, 'database' => 'address', "alias" => "a"),
128 array('name' => 'Delivery DNI', 'field' => 'delivery_dni', 'as' => true, 'database' => 'address', "alias" => "a"),
129 //PS_STATE
130 array('name' => 'Delivery country iso code', 'field' => 'iso_code', 'database' => 'country', 'alias' => 'co'),
131 array('name' => 'Delivery state', 'field' => 'state_name', 'as' => true, 'database' => 'state', 'alias' => 's'),
132 //PS_COUNTRY_LANG
133 array('name' => 'Delivery country', 'field' => 'country_name', 'as' => true, 'database' => 'country_lang', 'alias' => 'cl'),
134 //PS_ADRESS
135 array('name' => 'Invoice address line 1', 'field' => 'invoice_address1', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
136 array('name' => 'Invoice address line 2', 'field' => 'invoice_address2', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
137 array('name' => 'Invoice postcode', 'field' => 'invoice_postcode', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
138 array('name' => 'Invoice city', 'field' => 'invoice_city', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
139 array('name' => 'Invoice phone', 'field' => 'invoice_phone', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
140 array('name' => 'Invoice phone (mobile)', 'field' => 'invoice_phone_mobile', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
141 array('name' => 'Invoice gender', 'field' => 'invoice_name', 'as' => true, 'database' => 'gender', "alias" => "inv_gl"),
142 array('name' => 'Invoice firstname', 'field' => 'invoice_firstname', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
143 array('name' => 'Invoice lastname', 'field' => 'invoice_lastname', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
144 array('name' => 'Invoice company name', 'field' => 'invoice_company', 'as' => true, 'database' => 'address', 'alias' => 'inv_a'),
145 //ORDER_PAYMENT
146 array('name' => 'Transaction Id', 'field' => 'transaction_id', 'database' => 'order_payment', "alias" => "op"),
147 //PS_CARRIER
148 array('name' => 'Name carrier', 'field' => 'carrier_name', 'as' => true, 'database' => 'carrier', "alias" => "ca"),
149 //PS_ORDER_DETAIL
150 array('name' => 'Product ID', 'field' => 'product_id', 'database' => 'order_detail', "alias" => "od"),
151 array('name' => 'Product Ref', 'field' => 'product_reference', 'database' => 'order_detail', "alias" => "od"),
152 array('name' => 'Product Name', 'field' => 'product_name', 'database' => 'order_detail', "alias" => "od"),
153 array('name' => 'Product Price', 'field' => 'product_price', 'database' => 'order_detail', "alias" => "od"),
154 array('name' => 'Product Quantity', 'field' => 'product_quantity', 'database' => 'order_detail', "alias" => "od"),
155 array('name' => 'Shop name', 'field' => 'shop_name', 'database' => 'shop', 'as' => true, 'alias' => 'sh'),
156
157 array('name' => 'Message', 'field' => 'message', 'database' => 'message', 'alias' => 'm'),
158 array('name' => 'Order currency', 'field' => 'currency_iso_code', 'database' => 'currency', 'as' => true, 'alias' => 'cur'),
159 );
160 public $categories = array(
161 array("name" => "Id category", "field" => "id_category", "database" => "category", "alias" => "c"),
162 array("name" => "Id parent", "field" => "id_parent", "database" => "category", "alias" => "c"),
163 array("name" => "Id shop default", "field" => "id_shop_default", "database" => "category", "alias" => "c"),
164 array("name" => "Level depth", "field" => "level_depth", "database" => "category", "alias" => "c"),
165 array("name" => "nleft", "field" => "nleft", "database" => "category", "alias" => "c"),
166 array("name" => "nright", "field" => "nright", "database" => "category", "alias" => "c"),
167 array("name" => "active", "field" => "active", "database" => "category", "alias" => "c"),
168 array("name" => "Is root category", "field" => "is_root_category", "database" => "category", "alias" => "c"),
169 array("name" => "Id group", "field" => "id_group", "database" => "other"),
170 array("name" => "Id shop", "field" => "id_shop", "database" => "category_lang", "alias" => "cl"),
171 array("name" => "Name", "field" => "name", "database" => "category_lang", "alias" => "cl"),
172 array("name" => "Description", "field" => "description", "database" => "category_lang", "alias" => "cl"),
173 array("name" => "Link rewrite", "field" => "link_rewrite", "database" => "category_lang", "alias" => "cl"),
174 array("name" => "Meta title", "field" => "meta_title", "database" => "category_lang", "alias" => "cl"),
175 array("name" => "Meta keywords", "field" => "meta_keywords", "database" => "category_lang", "alias" => "cl"),
176 array("name" => "Meta description", "field" => "meta_description", "database" => "category_lang", "alias" => "cl"),
177 array("name" => "Position", "field" => "position", "database" => "category_shop", "alias" => "category_shop")
178 );
179 public $manufacturers = array(
180 array("name" =>"id manufacturer", "field" => "id_manufacturer", "database" => "manufacturer", "alias" => "m"),
181 array("name" =>"name", "field" => "name", "database" => "manufacturer", "alias" => "m"),
182 array("name" =>"active", "field" => "active", "database" => "manufacturer", "alias" => "m"),
183 array("name" =>"description", "field" => "description", "database" => "manufacturer", "alias" => "ml"),
184 array("name" =>"short description", "field" => "short_description", "database" => "manufacturer", "alias" => "ml"),
185 array("name" =>"meta title", "field" => "meta_title", "database" => "manufacturer", "alias" => "ml"),
186 array("name" =>"meta keywords", "field" => "meta_keywords", "database" => "manufacturer", "alias" => "ml"),
187 array("name" =>"meta description", "field" => "meta_description", "database" => "manufacturer", "alias" => "ml"),
188 array("name" =>"id shop", "field" => "id_shop", "database" => "manufacturer", "alias" => "manufacturer_shop"),
189 );
190 public $suppliers = array(
191 array("name" =>"id supplier", "field" => "id_supplier", "database" => "supplier", "alias" => "s"),
192 array("name" =>"name", "field" => "name", "database" => "supplier", "alias" => "s"),
193 array("name" =>"active", "field" => "active", "database" => "supplier", "alias" => "s"),
194 array("name" =>"description", "field" => "description", "database" => "supplier", "alias" => "sl"),
195 array("name" =>"meta title", "field" => "meta_title", "database" => "supplier", "alias" => "sl"),
196 array("name" =>"meta keywords", "field" => "meta_keywords", "database" => "supplier", "alias" => "sl"),
197 array("name" =>"meta description", "field" => "meta_description", "database" => "supplier", "alias" => "sl"),
198 array("name" =>"id shop", "field" => "id_shop", "database" => "supplier", "alias" => "supplier_shop"),
199 );
200 public $customers = array(
201 array("name" => "id customer", "field" => "id_customer", "database" => "customer", "alias" => "c"),
202 array("name" => "id gender", "field" => "id_gender", "database" => "customer", "alias" => "c"),
203 array("name" => "company", "field" => "company", "database" => "customer", "alias" => "c"),
204 array("name" => "siret", "field" => "siret", "database" => "customer", "alias" => "c"),
205 array("name" => "ape", "field" => "ape", "database" => "customer", "alias" => "c"),
206 array("name" => "firstname", "field" => "firstname", "database" => "customer", "alias" => "c"),
207 array("name" => "lastname", "field" => "lastname", "database" => "customer", "alias" => "c"),
208 array("name" => "email", "field" => "email", "database" => "customer", "alias" => "c"),
209 array("name" => "birthday", "field" => "birthday", "database" => "customer", "alias" => "c"),
210 array("name" => "newsletter", "field" => "newsletter", "database" => "customer", "alias" => "c"),
211 array("name" => "website", "field" => "website", "database" => "customer", "alias" => "c"),
212 array("name" => "password", "field" => "passwd", "database" => "customer", "alias" => "c"),
213
214 array("name" => "address company", "field" => "address_company", "database" => "address", 'as' => true, "alias" => "a"),
215 array("name" => "address firstname", "field" => "address_firstname", "database" => "address", 'as' => true, "alias" => "a"),
216 array("name" => "address lastname", "field" => "address_lastname", "database" => "address", 'as' => true, "alias" => "a"),
217 array("name" => "address address1", "field" => "address1", "database" => "address", "alias" => "a"),
218 array("name" => "address address2", "field" => "address2", "database" => "address", "alias" => "a"),
219 array("name" => "address postcode", "field" => "postcode", "database" => "address", "alias" => "a"),
220 array("name" => "address city", "field" => "city", "database" => "address", "alias" => "a"),
221 array("name" => "address other", "field" => "other", "database" => "address", "alias" => "a"),
222 array("name" => "address phone", "field" => "phone", "database" => "address", "alias" => "a"),
223 array("name" => "address phone_mobile", "field" => "phone_mobile", "database" => "address", "alias" => "a"),
224 array("name" => "address vat_number", "field" => "vat_number", "database" => "address", "alias" => "a"),
225 array("name" => "address dni", "field" => "dni", "database" => "address", "alias" => "a"),
226 array("name" => "address active", "field" => "active", "database" => "address", "alias" => "a"),
227 );
228 public $newsletters = array(
229 array("name" => "Email", "field" => "email", "database" => "newsletter"),
230 array("name" => "Date add", "field" => "newsletter_date_add", "database" => "newsletter"),
231 array("name" => "Ip", "field" => "ip_registration_newsletter", "database" => "newsletter"),
232 array("name" => "Referer", "field" => "http_referer", "database" => "newsletter"),
233 array("name" => "Active", "field" => "active", "database" => "newsletter")
234 );
235 public $switch;
236 public $showTimeAndMemory;
237
238 public function __construct()
239 {
240 $this->name = "advancedexport";
241 $this->tab = "administration";
242 $this->bootstrap = true;
243 $this->author = "Algol Consulting";
244 $this->version = "4.2.0";
245 $this->displayName = $this->l('Esportazione Avanzata');
246 $this->description = $this->l('Strumento esportazione avanzata');
247 $this->module_key = "a3895af3e1e55fa47a756b6e973e77fe";
248 $this->link = new Link();
249 $this->showTimeAndMemory = false;
250 $this->switch = (_PS_VERSION_ >= 1.6 ? "switch": "radio");
251 parent::__construct();
252
253 $path = dirname(__FILE__);
254
255 if (strpos(__FILE__, 'Module.php') !== false)
256 $path .= '/../modules/'.$this->name;
257
258 include_once($path.'/classes/AdvancedExportClass.php');
259 }
260
261 public function install()
262 {
263 if(!$this->CreateTables()
264 || !Configuration::updateGlobalValue("AdvancedExport_CURRENT", 0)
265 || !Configuration::updateGlobalValue("AdvancedExport_TOTAL", 0)
266 || !Configuration::updateGlobalValue("ADVANCEDEXPORT_SECURE_KEY", Tools::strtoupper(Tools::passwdGen(16))))
267 return false;
268
269 if (!parent::install())
270 return false;
271
272 return true;
273 }
274
275 public function CreateTables()
276 {
277 //for test create tmp table
278 $table_name = _DB_PREFIX_ . "advancedexport";
279
280 $query = "CREATE TABLE IF NOT EXISTS `".$table_name."` (
281 `id_advancedexport` int(10) unsigned NOT NULL auto_increment,
282 `type` varchar(200) NOT NULL,
283 `name` varchar(200) NOT NULL,
284 `delimiter` varchar(255) NOT NULL,
285 `separator` varchar(255) NOT NULL,
286 `id_lang` int(10) NOT NULL,
287 `charset` varchar(255) NOT NULL,
288 `add_header` BOOL NOT NULL DEFAULT 0,
289 `decimal_separator` varchar(10) NOT NULL,
290 `decimal_round` int(10) NOT NULL,
291 `strip_tags` BOOL NOT NULL DEFAULT 0,
292 `only_new` BOOL NOT NULL DEFAULT 0,
293 `date_from` varchar(255) NOT NULL,
294 `date_to` varchar(255) NOT NULL,
295 `last_exported_id` int(10) NOT NULL DEFAULT 0,
296 `start_id` int(10) NOT NULL DEFAULT 0,
297 `end_id` int(10) NOT NULL DEFAULT 0,
298 `save_type` int(10) NOT NULL DEFAULT 0,
299 `filename` varchar(255) NOT NULL,
300 `image_type` varchar(255) NOT NULL,
301 `email` varchar(255) NOT NULL,
302 `ftp_hostname` varchar(255) NOT NULL,
303 `ftp_user_name` varchar(255) NOT NULL,
304 `ftp_user_pass` varchar(255) NOT NULL,
305 `fields` text NOT NULL,
306 PRIMARY KEY (`id_advancedexport`)
307 ) ENGINE="._MYSQL_ENGINE_." DEFAULT CHARSET=utf8";
308
309 if(!$this->DbExecute($query))
310 return false;
311
312 return true;
313 }
314
315 public function DbExecute($query)
316 {
317 return Db::getInstance()->Execute($query);
318 }
319
320 public function uninstall()
321 {
322 if(!$this->RemoveTables()
323 || !Configuration::deleteByName("AdvancedExport_CURRENT")
324 || !Configuration::deleteByName("AdvancedExport_TOTAL")) //install tables
325 return false;
326
327 if (!parent::uninstall())
328 return false;
329
330 return true;
331 }
332
333 public function RemoveTables()
334 {
335 //remove main table
336 if(!$this->DbExecute("DROP TABLE IF EXISTS `"._DB_PREFIX_."advancedexport`"))
337 return false;
338 return true;
339 }
340
341 public function getContent()
342 {
343 $this->_html = '';
344 $this->_postProcess();
345
346 if ($this->getValue('add_model') || Tools::isSubmit('updateadvancedexport') || Tools::isSubmit('btnSubmit'))
347 {
348 $this->_html .= $this->displayAddModelForm($helper = new HelperForm());
349 }
350 else
351 $this->_html .= $this->displayIndexForm();
352
353 $this->addHeader();
354 return $this->_html;
355 }
356
357 public function addHeader()
358 {
359 $this->addCSS($this->_path.'views/css/admin.css');
360 $this->addCSS($this->_path.'views/css/jquery.percentageloader-0.1.css');
361
362 if(_PS_VERSION_ >= 1.6) {
363 $this->addJS(_PS_JS_DIR_ . 'jquery/ui/jquery.ui.sortable.min.js');
364 $this->addJS($this->_path.'views/js/admin.js');
365 } else {
366 $this->addJS($this->_path.'views/js/jquery-ui-1.10.4.custom.min.js');
367 $this->addJS($this->_path.'views/js/admin15.js');
368 }
369
370 $this->addJS($this->_path.'views/js/jquery.percentageloader-0.1.min.js');
371 $this->addJS($this->_path.'views/js/jquery.bsmselect.js');
372 $this->addJS($this->_path.'views/js/jquery.bsmselect.compatibility.js');
373 $this->addJS($this->_path.'views/js/jquery.bsmselect.sortable.js');
374
375 $this->addJS($this->_path.'views/js/jquery.cooki-plugin.js');
376 }
377
378 public function _postProcess()
379 {
380 if($this->getValue("ajax"))
381 $this->hookAjaxCall();
382
383 if ($this->_postValidation() == false)
384 return false;
385
386 $errors = '';
387
388 if (Tools::isSubmit('btnSubmit') )
389 {
390 if($this->saveModel())
391 $this->redirect('saveModelConfirmation');
392 }
393 else if(Tools::isSubmit('deleteadvancedexport'))
394 {
395 if(!$this->deleteModel())
396 $this->redirect('deleteModelConfirmation');
397 }
398 elseif ($this->isSubmit('duplicateadvancedexport') && $this->getValue('id_advancedexport'))
399 {
400 $time_start = microtime(true); //debug
401
402 $this->getExportType($this->getValue('id_advancedexport'));
403
404 if($this->showTimeAndMemory) // debug
405 $this->showTimeAndMemoryUsage($time_start); // debug
406 else
407 $this->redirect('exportConfirmation');
408 }
409 elseif ($this->isSubmit('viewfiles') && $this->getValue('url'))
410 {
411 $this->getFile($this->getValue('url'));
412 }
413 elseif ($this->isSubmit('deletefiles') && $url = $this->getValue('url'))
414 {
415 $this->deleteFile($url);
416 $this->redirect('deleteFileConfirmation');
417 }
418 elseif (Tools::isSubmit('saveModelConfirmation'))
419 $this->_html .= $this->displayConfirmation($this->l('Model save successfully.'));
420 elseif (Tools::isSubmit('deleteModelConfirmation'))
421 $this->_html .= $this->displayConfirmation($this->l('Model delete successfully.'));
422 elseif (Tools::isSubmit('deleteFileConfirmation'))
423 $this->_html .= $this->displayConfirmation($this->l('File delete successfully.'));
424 elseif (Tools::isSubmit('exportConfirmation'))
425 $this->_html .= $this->displayConfirmation($this->l('Export finish successfully.'));
426
427 if ($errors)
428 $this->_html .= $this->displayError($errors);
429 }
430
431 protected function _postValidation()
432 {
433 $this->_errors = array();
434
435 if (Tools::isSubmit('btnSubmit') )
436 {
437 $fields = Tools::getValue('fields');
438 if (!is_array($fields))
439 $this->_errors[] = $this->l('You must choose at least one field.');
440 if (!Validate::isName(Tools::getValue('name')) || Tools::getValue('name') == '')
441 $this->_errors[] = $this->l('Invalid or empty name.');
442 if (Tools::getValue('filename') != '' && !Validate::isName(Tools::getValue('filename')))
443 $this->_errors[] = $this->l('Invalid file name.');
444 if (Tools::getValue('date_from') != '' && !Validate::isDate(Tools::getValue('date_from')))
445 $this->_errors[] = $this->l('Invalid date from.');
446 if (Tools::getValue('date_to') != '' && !Validate::isDate(Tools::getValue('date_to')))
447 $this->_errors[] = $this->l('Invalid date to.');
448 if (Tools::getValue('start_id') != '' && !Validate::isInt(Tools::getValue('start_id')))
449 $this->_errors[] = $this->l('Invalid begin id field.');
450 if (Tools::getValue('end_id') != '' && !Validate::isInt(Tools::getValue('end_id')))
451 $this->_errors[] = $this->l('Invalid finish id field.');
452 }
453
454 if (count($this->_errors))
455 {
456 foreach ($this->_errors as $err)
457 $this->_html .= '<div class="alert alert-danger">'.$err.'</div>';
458
459 return false;
460 }
461
462 return true;
463 }
464
465 public function saveModel()
466 {
467 $functionName = ($this->getValue('type') ? $this->getValue('type') : '').'FormFields';
468 $specific = $this->$functionName();
469
470 $to_serialize = null;
471 foreach ($specific as $key => $value) {
472 $trimmed = str_replace('[]', '', $value['name']);
473 if($this->getValue($trimmed) != "")
474 $to_serialize[$value['name']] = $this->getValue($trimmed);
475 }
476
477 $AdvancedExport = new AdvancedExportClass($this->getValue('id_advancedexport'));
478 $AdvancedExport->copyFromPost();
479 $AdvancedExport->fields = Tools::jsonEncode($to_serialize);
480 $AdvancedExport->save();
481
482 return true;
483 }
484
485 public function getValue($value)
486 {
487 return Tools::getValue($value);
488 }
489
490 public function redirect($action)
491 {
492 Tools::redirectAdmin(AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&'.$action);
493 }
494
495 public function deleteModel()
496 {
497 $AdvancedExport = new AdvancedExportClass($this->getValue('id_advancedexport'));
498 $AdvancedExport->delete();
499
500 return true;
501 }
502
503 public function addCSS($path)
504 {
505 return $this->context->controller->addCSS($path);
506 }
507
508 public function addJS($path)
509 {
510 return $this->context->controller->addJS($path);
511 }
512
513 public function displayAddModelForm($helper)
514 {
515 $type = null;
516 if($id = Tools::getValue("id_advancedexport"))
517 {
518 $aec = new AdvancedExportClass($id);
519 if(is_object($aec))
520 $type = $aec->type;
521 }
522 else
523 {
524 $type = Tools::getValue("type");
525 }
526
527 $helper->module = $this;
528 $helper->show_toolbar = true;
529 $helper->table = 'advancedexport';
530 $helper->id = (int)$id;
531 $helper->default_form_language = (int)Configuration::get("PS_LANG_DEFAULT");
532 $helper->allow_employee_form_lang = (int)Configuration::get("PS_LANG_DEFAULT");
533 $helper->submit_action = 'btnSubmit';
534 $helper->token = $this->getValue('token');
535 $helper->currentIndex = $this->getAdminLink().'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
536 $helper->fields_value = $this->getModelFieldsValues($type);
537
538 return $helper->generateForm( $this->ModelFormFields($type) );
539 }
540
541 public function getAdminLink()
542 {
543 return $this->context->link->getAdminLink('AdminModules', false);
544 }
545
546 public function getModelFieldsValues($type)
547 {
548 $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');
549 $fields_specific = null;
550 $fields_value = null;
551
552 if($this->getValue('id_advancedexport'))
553 {
554 $ae = new AdvancedExportClass($this->getValue('id_advancedexport'));
555 $fields_specific = Tools::jsonDecode($ae->fields, true);
556 }
557 foreach ($fields as $field)
558 {
559 if (Tools::getValue($field))
560 $fields_value[$field] = Tools::getValue($field);
561 else if (isset($ae) && isset($ae->$field))
562 $fields_value[$field] = $ae->$field;
563 else
564 $fields_value[$field] = '';
565 }
566
567 $functionName = ($type ? $type : 'products').'FormFields';
568 $specific = $this->$functionName();
569
570 foreach ($specific as $value)
571 {
572 if(isset($fields_specific[$value['name']]))
573 {
574 $fields_value[$value['name']] = $fields_specific[$value['name']];
575 }
576 else
577 {
578 $fields_value[$value['name']] = null;
579 }
580 }
581
582 if(isset($fields_specific["categories"])) {
583 $this->selected_cat = $fields_specific["categories"];
584 }
585
586 return $fields_value;
587 }
588
589 public function ModelFormFields($type)
590 {
591 $functionName = ($type ? $type : 'products').'FormFields';
592 $result = null;
593
594 $result[0] = array(
595 'form' => array(
596 'legend' => array(
597 'title' => $this->l('Settings Form'),
598 'icon' => 'icon-envelope'
599 ),
600 'input' => array(
601 array(
602 'type' => 'hidden',
603 'name' => 'id_advancedexport'
604 ),
605 array(
606 'type' => 'hidden',
607 'name' => 'type'
608 ),
609 array(
610 'type' => 'text',
611 'label' => $this->l('Name'),
612 'name' => 'name',
613 'required' => true,
614 'desc' =>$this->l('Settings insternal name')
615 ),
616 array(
617 'type' => 'text',
618 'label' => $this->l('File name'),
619 'name' => 'filename',
620 'desc' =>$this->l('You can set name for file or leave blank name will be given by system.')
621 ),
622 array(
623 'type' => 'select',
624 'label' => $this->l('Delimiter'),
625 'name' => 'delimiter',
626 'options' => array(
627 'query' => array(
628 array(
629 'value' => ",",
630 'name' => "[,] ".$this->l("comma")
631 ),
632 array(
633 'value' => ";",
634 'name' => "[;] ". $this->l("semi-colons")
635 ),
636 array(
637 'value' => ":",
638 'name' => "[:] ". $this->l("colons")
639 ),
640 array(
641 'value' => "|",
642 'name' => "[|] ". $this->l("pipes")
643 ),
644 array(
645 'value' => "~",
646 'name' => "[~] ". $this->l("tilde")
647 )
648 ),
649 'id' => 'value',
650 'name' => 'name'
651 ),
652 'desc' =>$this->l('Separrator for each line in csv file')
653 ),
654 array(
655 'type' => 'select',
656 'label' => $this->l('Separator'),
657 'name' => 'separator',
658 'options' => array(
659 'query' => array(
660 array(
661 'value' => '"',
662 'name' => '["] '.$this->l("quotation marks")
663 ),
664 array(
665 'value' => "'",
666 'name' => "['] ". $this->l("single quotation marks")
667 )
668 ),
669 'id' => 'value',
670 'name' => 'name'
671 ),
672 'desc' =>$this->l('Separrator for each value in csv file')
673 ),
674 array(
675 'type' => 'select',
676 'label' => $this->l('Language'),
677 'name' => 'id_lang',
678 'col' => '4',
679 'options' => array(
680 'query' => Language::getLanguages(),
681 'id' => 'id_lang',
682 'name' => 'name'
683 ),
684 'desc' =>$this->l('Default utf-8.')
685
686 ),
687 array(
688 'type' => 'select',
689 'label' => $this->l('Encoding type'),
690 'name' => 'charset',
691 'col' => '4',
692 'options' => array(
693 'query' => $this->getCharsets(),
694 'id' => 'name',
695 'name' => 'name'
696 ),
697 'desc' =>$this->l('Default utf-8.')
698
699 ),
700 array(
701 'type' => 'select',
702 'label' => $this->l('Decimal separator'),
703 'name' => 'decimal_separator',
704 'options' => array(
705 'query' => array(
706 array(
707 'value' => ',',
708 'name' => '[,] '.$this->l("comma")
709 ),
710 array(
711 'value' => ".",
712 'name' => "[.] ". $this->l("dot")
713 )
714 ),
715 'default' => array(
716 'label' => $this->l('default'),
717 'value' => -1
718 ),
719 'id' => 'value',
720 'name' => 'name'
721 )
722 ),
723 array(
724 'type' => 'select',
725 'label' => $this->l('Round values'),
726 'name' => 'decimal_round',
727 'options' => array(
728 'default' => array(
729 'label' => $this->l('default'),
730 'value' => '-1'
731 ),
732 'query' => array(
733 array(
734 'value' => '0',
735 'label' => '0'
736 ),
737 array(
738 'value' => '1',
739 'label' => '1'
740 ),
741 array(
742 'value' => '2',
743 'label' => '2'
744 ),
745 array(
746 'value' => '3',
747 'label' => '3'
748 ),
749 array(
750 'value' => '4',
751 'label' => '4'
752 ),
753 array(
754 'value' => '5',
755 'label' => '5'
756 ),
757 array(
758 'value' => '6',
759 'label' => '6'
760 )
761 ),
762 'id' => 'value',
763 'name' => 'label'
764 )
765 ),
766 array(
767 'type' => 'select',
768 'label' => $this->l('Save type'),
769 'name' => 'save_type',
770 'default' => '0',
771 'options' => array(
772 'query' => $this->getSaveTypes(),
773 'id' => 'id',
774 'name' => 'name'
775 ),
776 'desc' =>$this->l('Save your file, sent to server or email.')
777
778 ),
779 array(
780 'type' => 'text',
781 'label' => $this->l('Email'),
782 'name' => 'email',
783 'class' => 'process2'
784 ),
785 array(
786 'type' => 'text',
787 'label' => $this->l('Hostname'),
788 'name' => 'ftp_hostname',
789 'class' => 'process1'
790 ),
791 array(
792 'type' => 'text',
793 'label' => $this->l('Username'),
794 'name' => 'ftp_user_name',
795 'class' => 'process1'
796 ),
797 array(
798 'type' => 'text',
799 'label' => $this->l('Password'),
800 'name' => 'ftp_user_pass',
801 'class' => 'process1'
802 ),
803 array(
804 'type' => $this->switch,
805 'label' => $this->l('Display labels'),
806 'name' => 'add_header',
807 'class' => 't',
808 'is_bool' => true,
809 'values' => array(
810 array(
811 'id' => 'active_on',
812 'value' => 1,
813 'label' => $this->l('Yes')
814 ),
815 array(
816 'id' => 'active_off',
817 'value' => 0,
818 'label' => $this->l('No')
819 )
820 ),
821 ),
822 array(
823 'type' => $this->switch,
824 'label' => $this->l('Strip tags'),
825 'name' => 'strip_tags',
826 'class' => 't',
827 'is_bool' => true,
828 'values' => array(
829 array(
830 'id' => 'active_on',
831 'value' => 1,
832 'label' => $this->l('Yes')
833 ),
834 array(
835 'id' => 'active_off',
836 'value' => 0,
837 'label' => $this->l('No')
838 )
839 ),
840 ),
841 array(
842 'type' => $this->switch,
843 'label' => $this->l('Export not exported'),
844 'name' => 'only_new',
845 'class' => 't',
846 'is_bool' => true,
847 'desc' =>$this->l('Export not exported yet.'),
848 'values' => array(
849 array(
850 'id' => 'active_on',
851 'value' => 1,
852 'label' => $this->l('Yes')
853 ),
854 array(
855 'id' => 'active_off',
856 'value' => 0,
857 'label' => $this->l('No')
858 )
859 ),
860 ),
861 array(
862 'type' => 'text',
863 'label' => $this->l('Begin id'),
864 'name' => 'start_id',
865 'desc' =>$this->l('You can specyify start id number.')
866 ),
867 array(
868 'type' => 'text',
869 'label' => $this->l('Finish id'),
870 'name' => 'end_id',
871 'desc' =>$this->l('You can specify end id number.')
872 ),
873 array(
874 'type' => (_PS_VERSION_ >= 1.6 ? 'datetime' : 'date'),
875 'label' => $this->l('From'),
876 'name' => 'date_from',
877 'size' => 10,
878 'maxlength' => 10,
879 'desc' =>$this->l('Format: 2011-12-31 HH-MM-SS(inclusive).')
880 ),
881 array(
882 'type' => (_PS_VERSION_ >= 1.6 ? 'datetime' : 'date'),
883 'label' => $this->l('To'),
884 'name' => 'date_to',
885 'size' => 10,
886 'maxlength' => 10,
887 'desc' =>$this->l('Format: 2012-12-31 HH-MM-SS(inclusive).')
888 )
889 ),
890 'submit' => array(
891 'title' => $this->l('Save'),
892 ),
893 'buttons' => array(
894 'cancelBlock' => array(
895 'title' => $this->l('Cancel'),
896 'href' => $this->getAdminUrl(),
897 'icon' => 'process-icon-cancel'
898 )
899 )
900 ),
901 );
902
903 if($type != "orders" && $type != "newsletters" && $type != "customers") {
904 $result[0]['form']['input'][] = array(
905 'type' => 'select',
906 'label' => $this->l('Image type'),
907 'name' => 'image_type',
908 'col' => '4',
909 'options' => array(
910 'query' => ImageType::getImagesTypes($type),
911 'id' => 'name',
912 'name' => 'name'
913 ),
914 );
915 }
916
917 $specific = $this->$functionName();
918 $result[0]['form']['input'] = array_merge( $result[0]['form']['input'], $specific);
919
920 return $result;
921 }
922
923 public function getCharsets()
924 {
925 return array(
926 array('id' => 1, 'name' => "UTF-8"),
927 array('id' => 2, 'name' => "ISO-8859-1"),
928 array('id' => 3, 'name' => "GB2312"),
929 array('id' => 4, 'name' => "Windows-1251"),
930 array('id' => 5, 'name' => "Windows-1252"),
931 array('id' => 6, 'name' => "Shift JIS"),
932 array('id' => 7, 'name' => "GBK"),
933 array('id' => 8, 'name' => "Windows-1256"),
934 array('id' => 9, 'name' => "ISO-8859-2"),
935 array('id' => 10, 'name' => "EUC-JP"),
936 array('id' => 11, 'name' => "ISO-8859-15"),
937 array('id' => 12, 'name' => "ISO-8859-9"),
938 array('id' => 13, 'name' => "Windows-1250"),
939 array('id' => 14, 'name' => "Windows-1254"),
940 array('id' => 15, 'name' => "EUC-KR"),
941 array('id' => 16, 'name' => "Big5"),
942 array('id' => 17, 'name' => "Windows-874"),
943 array('id' => 18, 'name' => "US-ASCII"),
944 array('id' => 19, 'name' => "TIS-620"),
945 array('id' => 20, 'name' => "ISO-8859-7"),
946 array('id' => 21, 'name' => "Windows-1255"),
947 );
948 }
949
950 public function getSaveTypes()
951 {
952 return array(
953 array('id' => 0, 'name' => $this->l("Save to disc"), 'short_name' => $this->l("disc")),
954 array('id' => 1, 'name' => $this->l("Save to server"), 'short_name' => $this->l("ftp")),
955 array('id' => 2, 'name' => $this->l("Sent to emal"), 'short_name' => $this->l("email"))
956 );
957 }
958
959 public function getFileTypes()
960 {
961 return array(
962 array('id' => 1, 'name' => "csv"),
963 array('id' => 2, 'name' => "xml")
964 );
965 }
966
967 public function getAdminUrl()
968 {
969 return AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules');
970 }
971
972 public function isFeatureActive()
973 {
974 return Shop::isFeatureActive();
975 }
976
977 public function isSubmit($value)
978 {
979 return Tools::isSubmit($value);
980 }
981
982 public function getExportType($id_advancedexport)
983 {
984 Configuration::updateGlobalValue("AdvancedExport_CURRENT", 0);
985
986 $ae = new AdvancedExportClass($id_advancedexport);
987 $this->createExportFile($ae);
988 }
989
990 public function showTimeAndMemoryUsage($time_start)
991 {
992 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>";
993 $time_end = microtime(true);
994 //dividing with 60 will give the execution time in minutes other wise seconds
995 $execution_time = ($time_end - $time_start)/60;
996 //execution time of the script
997 echo '</br><b>Total Execution Time: <span style="font-size:20px;"> '.$execution_time.'</span> Mins</b>';
998 }
999
1000 public function createExportFile($ae)
1001 {
1002 ini_set('memory_limit','725M');
1003 $ae->fields = Tools::jsonDecode($ae->fields, true);
1004 $sorted_fields = $this->getLabelsAndFields($ae->type, $ae->fields);
1005
1006 $functionName = $ae->type.'Query';
1007 $elements = $this->$functionName($ae, $sorted_fields);
1008 //total number for progress bar
1009 Configuration::updateGlobalValue("AdvancedExport_TOTAL", DB::getInstance()->numRows());
1010
1011 $url = $this->writeToFile($ae, $sorted_fields, $elements);
1012
1013 $this->saveLastId($ae, $this->lastElement);
1014
1015 $this->processFile($ae->save_type, $url, $ae);
1016 }
1017
1018 public function getLabelsAndFields($type, $fields)
1019 {
1020 if($fields)
1021 {
1022 set_time_limit(0);
1023
1024 foreach ($fields['fields[]'] as $key => $field)
1025 {
1026 $fields['allexportfields'][] = $this->{$type}[$field]['field'];
1027 $fields['labels'][] = $this->{$type}[$field]['name'];
1028
1029 if($this->{$type}[$field]['database'] == 'other')
1030 {
1031 $fields['otherfields'][$this->{$type}[$field]['field']] = $this->{$type}[$field]['field'];
1032 }
1033 else
1034 {
1035 //Jeśli jest podany alias w array
1036 //to użyj ten alias jako prefix w nazwie tabeli
1037 //wraz z kropkÄ….
1038 $alias = (isset($this->{$type}[$field]['alias']) ? $this->{$type}[$field]['alias'].'.' : '');
1039
1040 //jeśli wartość as jest false
1041 //to utwórz polecenie as
1042 //wymagane przy nazwach pól które siępowtarzają
1043 if (isset($this->{$type}[$field]['as']) && $this->{$type}[$field]['as']) {
1044 $fields['sqlfields'][] = $alias."`".Tools::substr(strstr($this->{$type}[$field]['field'], '_'), Tools::strlen('_'))."` as " .$this->{$type}[$field]['field']."";
1045 }else {
1046 $fields['sqlfields'][] = $alias . "`" . $this->{$type}[$field]['field'] . "`";
1047 }
1048 }
1049
1050 if(isset($this->{$type}[$field]['attribute']) && $this->{$type}[$field]['attribute'] == true)
1051 {
1052 $fields['attribute_fields'][] = $this->{$type}[$field]['field'];
1053 }
1054
1055 if($this->{$type}[$field]['database'] == "order_detail")
1056 {
1057 $fields['order_detail'] = true;
1058 $fields['sqlfields'][] = "od.`product_id`";
1059 $fields['sqlfields'][] = "od.`product_attribute_id`";
1060 $fields['sqlfields'][] = "o.`id_cart`";
1061 }
1062 }
1063 }
1064
1065 return $fields;
1066 }
1067
1068 //manufacturer name
1069
1070 public function writeToFile($ae, $sorted_fields, $elements)
1071 {
1072 $url = $this->getFileUrl($ae->filename, $ae->type);
1073
1074 $file = @fopen( $url, 'w' );
1075 //add labels for export data
1076 if($ae->add_header)
1077 $this->filewrite($ae, $sorted_fields, $file);
1078
1079 $i = 0;
1080 while ($element = $this->nextRow($elements))
1081 {
1082 if ($i == $this->rowsNumber - 1)
1083 {
1084 $this->lastElement = $element;
1085 }
1086
1087 Configuration::updateGlobalValue("AdvancedExport_CURRENT", (string)$i);
1088 //progress bar
1089 $this->getDataFromObject($element, $file, $sorted_fields, $ae);
1090 $i++; //progress bar
1091 }
1092
1093 //close file
1094 if($file)
1095 fclose($file);
1096
1097 return $url;
1098 }
1099
1100 public function nextRow ($elements)
1101 {
1102 return Db::getInstance(_PS_USE_SQL_SLAVE_)->nextRow($elements);
1103 }
1104
1105 public function hookAjaxCall()
1106 {
1107 header("Cache-Control: no-cache, must-revalidate");
1108 header("Expires: 0");
1109
1110 $action = Tools::getValue('action');
1111
1112 switch ($action) {
1113 case "getCurrentIndex":
1114 $response = array(
1115 'total' => (int)Configuration::get("AdvancedExport_TOTAL"),
1116 'current' => (int)Configuration::get("AdvancedExport_CURRENT")
1117 );
1118 echo Tools::jsonEncode($response);
1119 break;
1120 }
1121 }
1122
1123 public function getFileUrl($filename, $type)
1124 {
1125 //open file for write
1126 if($filename == null || $filename == "")
1127 $filename = $type.date('Y-m-d_His').".csv";
1128 else
1129 $filename = $filename.".csv";
1130
1131 $url = _PS_ROOT_DIR_.'/modules/advancedexport/csv/'.$type;
1132 if (!is_dir($url)) {
1133 mkdir($url);
1134 }
1135
1136 return $url.'/'.$filename;
1137 }
1138
1139 public function getDataFromObject($element, $file, $sorted_fields, $ae)
1140 {
1141 $id_object = $element[$this->getId($ae->type)];
1142 //instance od product object
1143 $obj = $this->getObject($id_object, $ae->type);
1144 //has attributes clean varible
1145 $this->hasAttr = 0;
1146 if(isset($sorted_fields['otherfields']))
1147 {
1148 foreach ($sorted_fields['otherfields'] as $key => $value) {
1149 $run = $ae->type.'_'.$value;
1150 $element[$value] = $this->$run($obj, $ae, $element);
1151 }
1152 }
1153
1154 if($ae->type == "products" && $ae->fields['attributes'] && isset($sorted_fields['attribute_fields']))
1155 $this->processWithAttributes($obj, $element, $file, $sorted_fields, $ae);
1156
1157 if($this->hasAttr == 0)
1158 $this->fputToFile($file, $sorted_fields['allexportfields'], $element, $ae);
1159 }
1160
1161 //get default category name
1162
1163 public function getObject($id_object, $type, $full = false)
1164 {
1165 switch ($type) {
1166 case 'categories':
1167 $type = new Category($id_object);
1168 break;
1169 case 'manufacturers':
1170 $type = new Manufacturer($id_object);
1171 break;
1172 case 'newsletters':
1173 $type = null;
1174 break;
1175 case 'orders':
1176 $type = new Order($id_object);
1177 break;
1178 case 'products':
1179 $type = new Product($id_object, $full);
1180 break;
1181 case 'suppliers':
1182 $type = new Supplier($id_object);
1183 break;
1184 default:
1185 $type = null;
1186 break;
1187 }
1188
1189 return $type;
1190 }
1191
1192 //get images url
1193
1194 public function processWithAttributes($obj, $element, $file, $sorted_fields, $ae)
1195 {
1196 $combArray = null;
1197 $combArray = $this->getProductCombination($obj, $ae);
1198
1199 if (isset($combArray)){
1200 $this->hasAttr = 1;
1201 foreach ($combArray AS $id_products_attribute => $products_attribute) {
1202 $list = null;
1203 $elementCopy = null;
1204 $elementCopy = $element;
1205
1206 foreach ($sorted_fields['attribute_fields'] as $key => $value) {
1207 $run = 'combination_' . $value;
1208 $elementCopy[$value] = $this->$run($obj, $products_attribute, $ae);
1209 }
1210
1211 $this->fputToFile($file, $sorted_fields['allexportfields'], $elementCopy, $ae);
1212 }
1213 }
1214 }
1215
1216 //tags
1217
1218 //$dati_csv = $readyForExport[$value];
1219 //echo " <script type=\"text/javascript\">alert('$dati_csv')</script>";
1220
1221private $count = null; // put this within class
1222
1223 public function fputToFile($file, $allexportfields, $object, $ae)
1224 {
1225 if($allexportfields && $file && $object && $ae)
1226 {
1227 //one ready for export product
1228 $readyForExport = array();
1229
1230
1231 //put in correct sort order
1232 foreach ($allexportfields as $value)
1233 {
1234 $object = $this->processDecimalSettings($object, $ae, $value);
1235 $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);
1236
1237 }
1238
1239
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}