· 8 years ago · Dec 08, 2017, 04:26 PM
1<?php
2/**
3* 2007-2016 Daniel
4*
5* NOTICE OF LICENSE
6*
7* This source file is subject to the Academic Free License (AFL 3.0)
8* that is bundled with this package in the file LICENSE.txt.
9* It is also available through the world-wide-web at this URL:
10* http://opensource.org/licenses/afl-3.0.php
11* If you did not receive a copy of the license and are unable to
12* obtain it through the world-wide-web, please send an email
13* to license@prestashop.com so we can send you a copy immediately.
14*
15* DISCLAIMER
16*
17* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18* versions in the future. If you wish to customize PrestaShop for your
19* needs please refer to http://www.prestashop.com for more information.
20*
21* @author weblir - weblir.com
22* @copyright 2007-2017 weblir
23* @license contact@weblir.com
24* International Registered Trademark & Property of PrestaShop
25*/
26
27class OlxPremium extends Module
28{
29 public $available_fields;
30 public $html = '';
31
32 public function __construct()
33 {
34 $this->name = 'olxpremium';
35 $this->tab = 'administration';
36 $this->version = '1.0.3';
37 $this->displayName = 'OLX Premium - XML Feeds';
38 $this->author = 'weblir';
39 $this->description = $this->l('Generate XML Product feeds for OLX');
40 $this->module_key = '89f1858c7125dc4c57d8bdb9aaec8d37';
41 $this->table_name = $this->name;
42
43 $this->bootstrap = true;
44
45 $this->available_fields = array(
46 'reference' => array('label' => 'ID'),
47 'name' => array('label' => 'Product Title'),
48 'description' => array('label' => 'Description'),
49 'categories' => array('label' => 'Categories'),
50 'url' => array('label' => 'Product URL'),
51 'image' => array('label' => 'Image URLs'),
52 'price_tin' => array('label' => 'Selling Price'),
53 'condition' => array('label' => 'Condition'),
54 'quantity' => array('label' => 'Quantity'),
55 'manufacturer_name' => array('label' => 'Manufacturer'),
56 );
57
58 $this->olx_categories = array(
59 array(
60 'id_option' => 987,
61 'name' => $this->l('Sport, timp liber, arta - Biciclete – Fitness - Suplimente - Biciclete')
62 ),
63 array(
64 'id_option' => 989,
65 'name' => $this->l('Sport, timp liber, arta - Biciclete – Fitness - Suplimente - Fitness')
66 ),
67 array(
68 'id_option' => 1263,
69 'name' => $this->l('Sport, timp liber, arta - Biciclete – Fitness - Suplimente - Suplimente alimentare')
70 ),
71 array(
72 'id_option' => 1005,
73 'name' => $this->l('Sport, timp liber, arta - Echipamente sportive si de turism - Echipamente pentru sport')
74 ),
75 array(
76 'id_option' => 1003,
77 'name' => $this->l('Sport, timp liber, arta - Echipamente sportive si de turism - Echipamente pentru turism')
78 ),
79 array(
80 'id_option' => 431,
81 'name' => $this->l('Sport, timp liber, arta - Arta - Obiecte de colectie')
82 ),
83 array(
84 'id_option' => 989,
85 'name' => $this->l('Sport, timp liber, arta - Carti - Muzica - Filme - Carti, reviste')
86 ),
87 array(
88 'id_option' => 995,
89 'name' => $this->l('Sport, timp liber, arta - Carti - Muzica - Filme - Filme')
90 ),
91 array(
92 'id_option' => 997,
93 'name' => $this->l('Sport, timp liber, arta - Carti - Muzica - Filme - Instrumente muzicale')
94 ),
95 array(
96 'id_option' => 993,
97 'name' => $this->l('Sport, timp liber, arta - Carti - Muzica - Filme - Muzica')
98 ),
99 array(
100 'id_option' => 460,
101 'name' => $this->l('Sport, timp liber, arta - Evenimente - Divertisment')
102 ),
103 );
104
105 $this->olx_platforms = array(
106 array(
107 'id_option' => 'www.olx.ro',
108 'name' => 'Romania'
109 ),
110 );
111
112 parent::__construct();
113 }
114
115 public function install()
116 {
117 if (!parent::install() or
118 !Configuration::updateValue('OLX_PLATFORM', 1) or
119 !Configuration::updateValue('OLX_SECRET_FEED', $this->randomString()) or
120 !Configuration::updateValue('OLX_INTERVAL', '2') or
121 !$this->installMyTables() or
122 !$this->registerHook('displayBackOfficeHeader')
123 ) {
124 return false;
125 }
126 return true;
127 }
128
129 public function uninstall()
130 {
131 if (!parent::uninstall() or
132 !$this->removeTable()
133 ) {
134 return false;
135 }
136 return true;
137 }
138
139 private function removeTable()
140 {
141 if (!Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.$this->table_name.'_feed`') ||
142 !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.$this->table_name.'_product`') ||
143 !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.$this->table_name.'_region`') ||
144 !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.$this->table_name.'_city`') ||
145 !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.$this->table_name.'_district`')
146 ) {
147 return false;
148 }
149 return true;
150 }
151
152 public function getOlxRegions($region)
153 {
154 $xml_categories_file = Tools::file_get_contents($region);
155 $xml_categories_load = simplexml_load_string($xml_categories_file, 'SimpleXMLElement', LIBXML_NOCDATA);
156 $xml_categories = Tools::jsonDecode(Tools::jsonEncode((array)$xml_categories_load), true);
157 //$key = array_search((int)$input, array_column($xml_categories, 'CATEGORY_ID'));
158 //d($region);
159 return $xml_categories['region'];
160 }
161
162 public function getOlxCities($city)
163 {
164 $xml_categories_file = Tools::file_get_contents($city);
165 $xml_categories_load = simplexml_load_string($xml_categories_file, 'SimpleXMLElement', LIBXML_NOCDATA);
166 $xml_categories = Tools::jsonDecode(Tools::jsonEncode((array)$xml_categories_load), true);
167 //$key = array_search((int)$input, array_column($xml_categories, 'CATEGORY_ID'));
168 return $xml_categories['city'];
169 }
170
171 public function getOlxDistricts($district)
172 {
173 $xml_categories_file = Tools::file_get_contents($district);
174 $xml_categories_load = simplexml_load_string($xml_categories_file, 'SimpleXMLElement', LIBXML_NOCDATA);
175 $xml_categories = Tools::jsonDecode(Tools::jsonEncode((array)$xml_categories_load), true);
176 //$key = array_search((int)$input, array_column($xml_categories, 'CATEGORY_ID'));
177 return $xml_categories['district'];
178 }
179
180 private function installMyTables()
181 {
182
183 $product = '
184 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.pSQL($this->table_name) .'_product` (
185 `id_product` INT(12) NOT NULL,
186 `included` INT(12) NOT NULL,
187 `name` VARCHAR(255) NOT NULL,
188 `custom_price` VARCHAR(255) NOT NULL,
189 `quantity` INT(12) NOT NULL,
190 PRIMARY KEY ( `id_product` )
191 ) ENGINE = ' ._MYSQL_ENGINE_;
192
193 $feed = '
194 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.pSQL($this->table_name) .'_feed` (
195 `id` INT(12) NOT NULL AUTO_INCREMENT,
196 `id_category` INT(12) NOT NULL,
197 `olx_category` INT(12) NOT NULL,
198 `secret` VARCHAR(255) NOT NULL,
199 PRIMARY KEY ( `id` )
200 ) ENGINE = ' ._MYSQL_ENGINE_;
201
202 $region = '
203 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.pSQL($this->table_name) .'_region` (
204 `id` INT(12) NOT NULL AUTO_INCREMENT,
205 `name` VARCHAR(255) NOT NULL,
206 PRIMARY KEY ( `id` )
207 ) ENGINE = ' ._MYSQL_ENGINE_;
208
209 $city = '
210 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.pSQL($this->table_name) .'_city` (
211 `id` INT(12) NOT NULL,
212 `text` VARCHAR(255) NOT NULL,
213 `lon` VARCHAR(255) NOT NULL,
214 `lat` VARCHAR(255) NOT NULL,
215 `radius` VARCHAR(255) NOT NULL,
216 `zoom` INT(12) NOT NULL,
217 `name` VARCHAR(255) NOT NULL,
218 `name_full` VARCHAR(255) NOT NULL,
219 `url` VARCHAR(255) NOT NULL,
220 `districts_city_id` INT(12) NOT NULL,
221 `region_id` INT(12) NOT NULL,
222 PRIMARY KEY ( `id` )
223 ) ENGINE = ' ._MYSQL_ENGINE_;
224
225 $district = '
226 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.pSQL($this->table_name) .'_district` (
227 `id` INT(12) NOT NULL AUTO_INCREMENT,
228 `lon` VARCHAR(255) NOT NULL,
229 `lat` VARCHAR(255) NOT NULL,
230 `city_id` INT(12) NOT NULL,
231 `zoom` INT(12) NOT NULL,
232 `name` VARCHAR(255) NOT NULL,
233 PRIMARY KEY ( `id` )
234 ) ENGINE = ' ._MYSQL_ENGINE_;
235
236 if (!Db::getInstance()->Execute($region) or
237 !Db::getInstance()->Execute($city) or
238 !Db::getInstance()->Execute($district) or
239 !Db::getInstance()->Execute($product) or
240 !Db::getInstance()->Execute($feed)
241 ) {
242 return false;
243 }
244 return true;
245 }
246
247
248
249 public function randomString($length = 7)
250 {
251 $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
252 $charactersLength = Tools::strlen($characters);
253 $randomString = '';
254 for ($i = 0; $i < $length; $i++) {
255 $randomString .= $characters[rand(0, $charactersLength - 1)];
256 }
257 return $randomString;
258 }
259
260
261 public function getContent()
262 {
263 $this->postProcess();
264 $this->displayForm();
265 return $this->html;
266 }
267
268 public function getOlxCategoryById($id)
269 {
270 foreach ($this->olx_categories as $value) {
271 if ($value['id_option'] == $id) {
272 $key = $value['name'];
273 }
274 }
275
276 return $key;
277 }
278
279 private function displayCustom()
280 {
281 // With Template
282 $this->context->smarty->assign(array(
283 'path' => $this->_path,
284 'secret' => Configuration::get('OLX_SECRET_FEED')
285 ));
286
287 if (Tools::getIsset('add_product')) {
288 $this->context->smarty->assign(array(
289 'path' => $this->_path,
290 'secret' => Configuration::get('OLX_SECRET_FEED'),
291 'go' => 'go'
292 ));
293 }
294 $this->html .= $this->display(__FILE__, 'backoffice.tpl');
295 }
296
297 private function displayCustomTop()
298 {
299 $this->context->smarty->assign(array(
300 'path'=> $this->_path
301 ));
302 $this->html .= $this->display(__FILE__, 'top.tpl');
303 }
304
305 private function displayCustomBottom()
306 {
307 $this->context->smarty->assign(array(
308 'path'=> $this->_path
309 ));
310 $this->html .= $this->display(__FILE__, 'bottom.tpl');
311 }
312
313 public function displayForm()
314 {
315 $this->html .= $this->displayCustom();
316
317 if (Tools::isSubmit('add_feed')) {
318 $this->html .= $this->newFeedForm();
319 } elseif (Tools::isSubmit('add_product')) {
320 $this->html .= $this->newProductForm();
321 } else {
322 if (count($this->getAllRegions())<1 ||
323 count($this->getAllCities())<1 ||
324 count($this->getAllDistricts())<1
325 ) {
326 $this->html .= $this->displayConfirmation(
327 $this->l('To go further, you have to fetch the location data from OLX. Click the button below!')
328 );
329 $this->html .= $this->updateLocations();
330 } else {
331 $this->html .= $this->displayCustomTop();
332 $this->html .= $this->displayCustomBottom();
333 $this->html .= $this->renderConfigurationForm();
334 $this->html .= $this->generateFeedList();
335 $this->html .= $this->generateProductList();
336 }
337 }
338 }
339
340 public function renderConfigurationForm()
341 {
342 $lang = new Language((int)Configuration::get('OLX_SECRET_FEED'));
343 $langs = Language::getLanguages();
344 $currencies = Currency::getCurrencies();
345 $id_shop = (int)$this->context->shop->id;
346 $options = array();
347 foreach ($langs as $language) {
348 $options[] = array('id_option' => $language['id_lang'], 'name' => $language['name']);
349 }
350
351 $cats = $this->getCategories(
352 $lang->id,
353 true,
354 $id_shop
355 );
356
357 $categories = array();
358 $categories[] = array('id_option' => 99999, 'name' => 'All');
359
360 foreach ($cats as $cat) {
361 $categories[] = array('id_option' => $cat['id_category'], 'name' => $cat['name']);
362 }
363
364 $inputs = array(
365 array(
366 'type' => 'select',
367 'label' => $this->l('OLX Platform'),
368 'desc' => $this->l('Choose the OLX Country Platform'),
369 'name' => 'OLX_PLATFORM',
370 'class' => 't',
371 'required' => 'true',
372 'options' => array(
373 'query' => $this->olx_platforms,
374 'id' => 'id_option',
375 'name' => 'name',
376 ),
377 ),
378 array(
379 'type' => 'select',
380 'label' => $this->l('OLX Region'),
381 'desc' => $this->l('Choose the OLX Region'),
382 'name' => 'OLX_REGION',
383 'class' => 'olx-region chosen',
384 'required' => 'true',
385 'options' => array(
386 'query' => $this->getAllRegions(),
387 'id' => 'id',
388 'name' => 'name',
389 ),
390 ),
391 array(
392 'type' => 'select',
393 'label' => $this->l('OLX City'),
394 'desc' => $this->l('Choose the OLX City'),
395 'name' => 'OLX_CITY',
396 'class' => 'olx-city chosen',
397 'required' => 'true',
398 'options' => array(
399 'query' => $this->getAllCities(Configuration::get('OLX_REGION')),
400 'id' => 'id',
401 'name' => 'name',
402 ),
403 ),
404 array(
405 'type' => 'select',
406 'label' => $this->l('OLX District'),
407 'desc' => $this->l('Choose the OLX District'),
408 'name' => 'OLX_DISTRICT',
409 'class' => 'olx-district chosen',
410 'options' => array(
411 'query' => $this->getAllDistricts(Configuration::get('OLX_CITY')),
412 'id' => 'id',
413 'name' => 'name',
414 ),
415 ),
416
417 array(
418 'type' => 'select',
419 'label' => $this->l('Currency'),
420 'desc' => $this->l('Choose your currency'),
421 'name' => 'OLX_CURRENCY',
422 'class' => 'olx-currency',
423 'required' => 'true',
424 'options' => array(
425 'query' => $currencies,
426 'id' => 'iso_code',
427 'name' => 'name',
428 ),
429 ),
430
431 array(
432 'type' => 'text',
433 'label' => $this->l('Contact Email'),
434 'desc' => $this->l('Set the contact email'),
435 'name' => 'OLX_EMAIL',
436 'class' => 'email',
437 'required' => 'true'
438 ),
439
440 array(
441 'type' => 'text',
442 'label' => $this->l('Contact Phone'),
443 'desc' => $this->l('Set the contact phone number'),
444 'name' => 'OLX_PHONE',
445 'class' => 'phone',
446 'required' => 'true'
447 ),
448
449 );
450
451 $inputs = array_merge(
452 $inputs
453 );
454
455 $fields_form = array(
456 'form' => array(
457 'legend' => array(
458 'title' => $this->l('Export Options'),
459 'icon' => 'icon-cogs'
460 ),
461 'input' => $inputs,
462 'submit' => array(
463 'title' => $this->l('Save'),
464 'name' => 'saveData'
465 ),
466 ),
467 );
468
469 $helper = new HelperForm();
470 $helper->show_toolbar = false;
471
472 $helper->default_form_language = $lang->id;
473 $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get(
474 'PS_BO_ALLOW_EMPLOYEE_FORM_LANG'
475 ) : 0;
476 $this->fields_form = array();
477 $helper->identifier = $this->identifier;
478 $helper->submit_action = 'submitExport';
479 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).
480 '&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
481 $helper->token = Tools::getAdminTokenLite('AdminModules');
482 $helper->tpl_vars = array(
483 'fields_value' => $this->getConfigFieldsValues(),
484 'languages' => $this->context->controller->getLanguages(),
485 'id_language' => $this->context->language->id
486 );
487
488 return $helper->generateForm(array($fields_form));
489 }
490
491 private function newFeedForm()
492 {
493 $categories = Category::getSimpleCategories($this->context->language->id);
494
495 $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
496 $langs = Language::getLanguages();
497 $id_shop = (int)$this->context->shop->id;
498
499 array_unshift($categories, array('id_category' => '0', 'name' => $this->l('All categories')));
500
501 $inputs = array();
502
503 $cats = $this->getCategories(
504 $lang->id,
505 true,
506 $id_shop
507 );
508
509 $inputs = array();
510
511 foreach ($cats as $cat) {
512 $categories[] = array('id_option' => $cat['id_category'], 'name' => $cat['name']);
513 }
514
515 $inputs[] = array(
516 'type' => 'categories',
517 'label' => $this->l('Categories'),
518 'required' => true,
519 'name' => 'categories',
520 'tree' => array(
521 'root_category' => $this->context->shop->getCategory(),
522 'id' => 'id_category',
523 'use_checkbox' => false,
524 'use_search' => true,
525 'selected_categories' => array()
526 )
527 );
528
529 $inputs[] = array(
530 'type' => 'select',
531 'label' => $this->l('OLX category'),
532 'name' => 'olx_category',
533 'hint' => 'In lista sunt adaugate doar sub-categoriile din categoria Sport',
534 'options' => array(
535 'query' => $this->olx_categories,
536 'id' => 'id_option',
537 'name' => 'name'
538 )
539 );
540
541 $fields_form = array(
542 'form' => array(
543 'legend' => array(
544 'title' => $this->l('Create new feed'),
545 'icon' => 'icon-add'
546 ),
547 'input' => $inputs,
548 'submit' => array(
549 'title' => $this->l('Add'),
550 'class' => 'btn btn-default pull-right'
551 )
552 )
553 );
554 $vals = array();
555 $vals['category'] = '';
556 $vals['category_type'] = '';
557 $vals['olx_category'] = '';
558
559 $helper = new HelperForm();
560 $helper->submit_action = 'submitNewFeed';
561 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) .
562 '&configure=' . $this->name .'&module_name=' . $this->name;
563 $helper->token = Tools::getAdminTokenLite('AdminModules');
564 $helper->tpl_vars = array(
565 'fields_value' => $vals
566 );
567
568 return $helper->generateForm(array($fields_form));
569 }
570
571 private function newProductForm()
572 {
573 $values = Product::getProducts($this->context->language->id, 0, 1000000, 'name', 'asc');
574
575 $inputs = array();
576 $inputs[] = array(
577 'type' => 'select',
578 'label' => $this->l('Product'),
579 'name' => 'product',
580 'options' => array(
581 'query' => $values,
582 'id' => 'id_product',
583 'name' => 'name'
584 )
585 );
586
587 $inputs[] = array(
588 'type' => 'switch',
589 'label' => $this->l('Included in the feed'),
590 'name' => 'included',
591 'values' => array(
592 array(
593 'id' => 'active_on',
594 'value' => 1,
595 'label' => $this->l('Yes')
596 ),
597 array(
598 'id' => 'active_ff',
599 'value' => 0,
600 'label' => $this->l('No')
601 )
602 )
603 );
604
605 $inputs[] = array(
606 'type' => 'text',
607 'label' => $this->l('Name on Olx'),
608 'name' => 'name'
609 );
610
611 $inputs[] = array(
612 'type' => 'text',
613 'label' => $this->l('Custom price'),
614 'name' => 'custom_price'
615 );
616
617 $inputs[] = array(
618 'type' => 'text',
619 'label' => $this->l('Custom quantity'),
620 'name' => 'quantity'
621 );
622
623 $fields_form = array(
624 'form' => array(
625 'legend' => array(
626 'title' => $this->l('Add new product rule'),
627 'icon' => 'icon-add'
628 ),
629 'input' => $inputs,
630 'submit' => array(
631 'title' => $this->l('Add'),
632 'class' => 'btn btn-default pull-right'
633 )
634 )
635 );
636 $vals = array();
637 $vals['product'] = '';
638 $vals['name'] = '';
639 $vals['included'] = '1';
640 $vals['custom_price'] = '';
641 $vals['quantity'] = '';
642
643 $helper = new HelperForm();
644 $helper->submit_action = 'submitNewProduct';
645 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) .
646 '&configure=' . $this->name .'&module_name=' . $this->name;
647 $helper->token = Tools::getAdminTokenLite('AdminModules');
648 $helper->tpl_vars = array(
649 'fields_value' => $vals
650 );
651
652 return $helper->generateForm(array($fields_form));
653 }
654
655
656 private function updateLocations()
657 {
658 $inputs = array();
659
660 $inputs[] = array(
661 'type' => 'text',
662 'label' => $this->l('OLX Regions URL'),
663 'name' => 'OLX_REGIONS_URL',
664 'desc' => $this->l('Do not change this field! For experts only!'),
665 'class' => 'disabled-input'
666
667 );
668
669 $inputs[] = array(
670 'type' => 'text',
671 'label' => $this->l('OLX Cities URL'),
672 'name' => 'OLX_CITIES_URL',
673 'desc' => $this->l('Do not change this field! For experts only!'),
674 'class' => 'disabled-input'
675 );
676
677 $inputs[] = array(
678 'type' => 'text',
679 'label' => $this->l('OLX Districts URL'),
680 'name' => 'OLX_DISTRICTS_URL',
681 'desc' => $this->l('Do not change this field! For experts only!'),
682 'class' => 'disabled-input'
683 );
684
685 $fields_form = array(
686 'form' => array(
687 'legend' => array(
688 'title' => $this->l('Fetch regions, cities and districts from OLX'),
689 'icon' => 'icon-add'
690 ),
691 'input' => $inputs,
692 'submit' => array(
693 'title' => $this->l('Fetch'),
694 'class' => 'btn btn-default pull-right'
695 )
696 )
697 );
698 $vals = array();
699 $vals['product'] = '';
700 $vals['name'] = '';
701 $vals['included'] = '1';
702 $vals['custom_price'] = '';
703
704 $helper = new HelperForm();
705 $helper->submit_action = 'submitFetchLocations';
706 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) .
707 '&configure=' . $this->name .'&module_name=' . $this->name;
708 $helper->token = Tools::getAdminTokenLite('AdminModules');
709 $helper->tpl_vars = array(
710 'fields_value' => $this->getConfigFieldsValues(),
711 );
712
713 return $helper->generateForm(array($fields_form));
714 }
715
716 private function generateFeedList()
717 {
718 $content = $this->getAllFeeds();
719 $fields_list = array(
720 'id' => array(
721 'title' => 'ID',
722 'align' => 'center',
723 'search' => false,
724 'class' => 'fixed-width-xs'
725 ),
726 'olx_category' => array(
727 'title' => $this->l('OLX Category'),
728 'search' => false,
729 ),
730 'id_category' => array(
731 'title' => $this->l('Category ID'),
732 'search' => false,
733 ),
734 'link' => array(
735 'title' => $this->l('Feed link'),
736 'search' => false,
737 'type' => 'editable',
738
739 )
740 );
741
742 $helper = new HelperList();
743 $helper->shopLinkType = '';
744 $helper->actions = array('delete');
745 $helper->module = $this;
746 $helper->listTotal = count($content);
747 $helper->identifier = 'id';
748 $helper->title = $this->l('Active feeds');
749 $helper->table = $this->name.'_feed';
750 $helper->token = Tools::getAdminTokenLite('AdminModules');
751 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) .
752 '&configure=' . $this->name .'&module_name=' . $this->name;
753 $helper->toolbar_btn = array(
754 'new' => array(
755 'desc' => $this->l('Create new feed'),
756 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&add_feed'.
757 '&token='.Tools::getAdminTokenLite('AdminModules'),
758 )
759 );
760
761 return $helper->generateList($content, $fields_list);
762 }
763
764 private function generateProductList()
765 {
766 $content = $this->getAllProducts();
767 $fields_list = array(
768 'id_product' => array(
769 'title' => $this->l('Product ID'),
770 'search' => false,
771 ),
772 'name' => array(
773 'title' => $this->l('Name on Olx'),
774 'search' => false,
775 ),
776 'included' => array(
777 'title' => $this->l('Included in the feed(1=Yes)'),
778 'search' => false,
779 ),
780 'custom_price' => array(
781 'title' => $this->l('Custom price'),
782 'search' => false,
783 ),
784 'quantity' => array(
785 'title' => $this->l('Custom quantity'),
786 'search' => false,
787 ),
788 );
789
790 $helper = new HelperList();
791 $helper->shopLinkType = '';
792 $helper->actions = array('delete');
793 $helper->module = $this;
794 $helper->listTotal = count($content);
795 $helper->identifier = 'id_product';
796 $helper->title = $this->l('Product custom data');
797 $helper->table = $this->name.'_products';
798 $helper->token = Tools::getAdminTokenLite('AdminModules');
799 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) .
800 '&configure=' . $this->name .'&module_name=' . $this->name;
801 $helper->toolbar_btn = array(
802 'new' => array(
803 'desc' => $this->l('Add new entry'),
804 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&add_product'.
805 '&token='.Tools::getAdminTokenLite('AdminModules'),
806 )
807 );
808
809 return $helper->generateList($content, $fields_list);
810 }
811
812 public function getAllFeeds()
813 {
814 $feeds = Db::getInstance()->ExecuteS('
815 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_feed');
816
817 $new = array();
818 foreach ($feeds as $feed) {
819 $feed['link'] = Tools::getHttpHost(true).__PS_BASE_URI__.'modules/'.
820 $this->name.'/classes/feed.olx.php?token='.$feed['secret'];
821
822 $feed['olx_category'] = $this->getOlxCategoryById($feed['olx_category']);
823 if ($feed['id_category']==0) {
824 $feed['id_category'] = $this->l('All categories');
825 } else {
826 $cat = new Category($feed['id_category'], $this->context->language->id);
827 $feed['id_category'] = $cat->name;
828 }
829 array_push($new, $feed);
830 }
831
832 return $new;
833 }
834
835 public function getFeedBySecret($secret)
836 {
837 $feeds = Db::getInstance()->ExecuteS('
838 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_feed WHERE secret = "'.pSQL($secret).'"');
839
840 return $feeds;
841 }
842
843 public function getAllRegions()
844 {
845 return Db::getInstance()->ExecuteS('
846 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_region ORDER BY name ASC');
847 }
848
849 public function getRegionInfo($id_region)
850 {
851 return Db::getInstance()->getRow('
852 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).
853 '_region WHERE id = '.(int)$id_region.' ORDER BY name ASC');
854 }
855
856 public function getAllCities($id_region = '')
857 {
858 if (Tools::strlen($id_region)>0) {
859 $where = ' WHERE region_id = '.pSQL($id_region);
860 } else {
861 $where = '';
862 }
863 return Db::getInstance()->ExecuteS('
864 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_city'.$where. ' ORDER BY name ASC');
865 }
866
867 public function getCityInfo($id_city)
868 {
869 return Db::getInstance()->getRow('
870 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_city WHERE id = '.(int)$id_city.' ORDER BY name ASC');
871 }
872
873 public function getAllDistricts($id_city = '')
874 {
875 if (Tools::strlen($id_city)>0) {
876 $sql = 'SELECT districts_city_id FROM '._DB_PREFIX_.
877 pSQL($this->table_name).'_city WHERE id = '.(int)$id_city;
878 $districts_city_id = Db::getInstance()->getValue($sql);
879 $where = ' WHERE city_id = '.(int)$districts_city_id;
880 } else {
881 $where = '';
882 }
883 return Db::getInstance()->ExecuteS('
884 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_district'.$where. ' ORDER BY name ASC');
885 }
886
887 public function getDistrictInfo($id_district)
888 {
889 if (Tools::strlen($id_district)>0) {
890 return Db::getInstance()->getRow('
891 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).
892 '_district WHERE id = '.(int)$id_district.' ORDER BY name ASC');
893 } else {
894 return true;
895 }
896 }
897
898 public function getAllProducts()
899 {
900 return Db::getInstance()->ExecuteS('
901 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_product');
902 }
903
904 public function getCustomProduct($id_product, $check = false)
905 {
906 $data = Db::getInstance()->ExecuteS('
907 SELECT * FROM '._DB_PREFIX_.pSQL($this->table_name).'_product WHERE id_product = '.(int)$id_product);
908 if (count($data)>0) {
909 if ($check == false) {
910 return $data;
911 } else {
912 return true;
913 }
914 } else {
915 if ($check == false) {
916 return $data;
917 } else {
918 return false;
919 }
920 }
921 }
922
923 public function getConfigFieldsValues()
924 {
925 return array(
926 'OLX_PLATFORM' => (int)Configuration::get('OLX_PLATFORM'),
927 'OLX_REGIONS_URL' => 'http://olx.ro/xmlads/xmlregions/',
928 'OLX_CITIES_URL' => 'http://olx.ro/xmlads/xmlcities/',
929 'OLX_DISTRICTS_URL' => 'http://olx.ro/xmlads/xmldistricts/',
930 'OLX_REGION' => Configuration::get('OLX_REGION'),
931 'OLX_CITY' => Configuration::get('OLX_CITY'),
932 'OLX_DISTRICT' => Configuration::get('OLX_DISTRICT'),
933 'OLX_CURRENCY' => Configuration::get('OLX_CURRENCY'),
934 'OLX_EMAIL' => Configuration::get('OLX_EMAIL'),
935 'OLX_PHONE' => Configuration::get('OLX_PHONE'),
936 );
937 }
938
939 public function postProcess()
940 {
941 if (Tools::getIsset('submitFetchLocations')) {
942 $err = 0;
943
944 if (!Validate::isUrl(Tools::getValue('OLX_REGIONS_URL')) ||
945 Tools::strlen(Tools::getValue('OLX_REGIONS_URL')) < 1
946 ) {
947 $this->html .= $this->displayError('OLX Regions URL Must contain a valid URL');
948 $err++;
949 } elseif (!Validate::isUrl(Tools::getValue('OLX_CITIES_URL')) ||
950 Tools::strlen(Tools::getValue('OLX_CITIES_URL')) < 1
951 ) {
952 $this->html .= $this->displayError('OLX Cities URL Must contain a valid URL');
953 $err++;
954 } elseif (!Validate::isUrl(Tools::getValue('OLX_DISTRICTS_URL')) ||
955 Tools::strlen(Tools::getValue('OLX_DISTRICTS_URL')) < 1
956 ) {
957 $this->html .= $this->displayError('OLX Districts URL Must contain a valid URL');
958 }
959
960
961
962 if ($err < 1) {
963 $regions = $this->getOlxRegions(Context::getContext()->shop->getBaseURL(true).'modules/'.$this->name.'/data/regions.xml');
964 foreach ($regions as $region) {
965 Db::getInstance()->insert(
966 pSQL($this->table_name).'_region',
967 array(
968 'id' => (int)$region['id'],
969 'name' => $region['name']['ro'],
970 )
971 );
972 }
973
974 $cities = $this->getOlxCities(Context::getContext()->shop->getBaseURL(true).'modules/'.$this->name.'/data/cities.xml');
975 foreach ($cities as $city) {
976 Db::getInstance()->insert(
977 pSQL($this->table_name).'_city',
978 array(
979 'id' => (int)$city['id'],
980 'text' => pSQL($city['text']),
981 'lon' => pSQL($city['lon']),
982 'lat' => pSQL($city['lat']),
983 'radius' => pSQL($city['radius']),
984 'zoom' => pSQL($city['zoom']),
985 'name' => pSQL($city['name']),
986 'name_full' => pSQL($city['name_full']),
987 'url' => pSQL($city['url']),
988 'districts_city_id' => (int)$city['districts_city_id'],
989 'region_id' => (int)$city['region_id'],
990
991 )
992 );
993 }
994
995 $districts = $this->getOlxDistricts(Context::getContext()->shop->getBaseURL(true).'modules/'.$this->name.'/data/districts.xml');
996 foreach ($districts as $district) {
997 Db::getInstance()->insert(
998 pSQL($this->table_name).'_district',
999 array(
1000 'id' => pSQL($district['id']),
1001 'lon' => pSQL($district['lon']),
1002 'lat' => pSQL($district['lat']),
1003 'city_id' => (int)$district['city_id'],
1004 'zoom' => pSQL($district['zoom']),
1005 'name' => pSQL($district['name']),
1006 )
1007 );
1008 }
1009
1010 $this->html .= $this->displayConfirmation(
1011 $this->l('OLX Location Data has been downloaded successfully.')
1012 );
1013 }
1014 }
1015
1016 if (Tools::getIsset('submitNewFeed')) {
1017 Db::getInstance()->insert(
1018 pSQL($this->table_name).'_feed',
1019 array(
1020 'id_category' => pSQL(Tools::getValue('category')),
1021 'olx_category' => pSQL(Tools::getValue('olx_category')),
1022 'secret' => $this->randomString()
1023 )
1024 );
1025 $this->html .= $this->displayConfirmation($this->l('The new entry has been added.'));
1026 }
1027
1028 if (Tools::getIsset('submitNewProduct')) {
1029 $err = 0;
1030
1031 if (Tools::strlen(Tools::getValue('name'))<1 or
1032 !Validate::isGenericName(Tools::getValue('name'))
1033 ) {
1034 $this->html .= $this->displayError('Please a valid product name!');
1035 $err++;
1036 }
1037
1038 if (!Validate::isFloat(Tools::getValue('custom_price')) or
1039 Tools::strlen(Tools::getValue('custom_price'))<1
1040 ) {
1041 $this->html .= $this->displayError('Please enter a valid float product price!');
1042 $err++;
1043 }
1044
1045 if (!Validate::isInt(Tools::getValue('quantity')) or
1046 Tools::strlen(Tools::getValue('quantity'))<1
1047 ) {
1048 $this->html .= $this->displayError('Please a valid int product quantity!');
1049 $err++;
1050 }
1051
1052
1053 if ($err<1) {
1054 Db::getInstance()->insert(
1055 pSQL($this->table_name).'_product',
1056 array(
1057 'id_product' => pSQL(Tools::getValue('product')),
1058 'name' => pSQL(Tools::getValue('name')),
1059 'included' => pSQL(Tools::getValue('included')),
1060 'custom_price' => pSQL(Tools::getValue('custom_price')),
1061 'quantity' => pSQL(Tools::getValue('quantity')),
1062 )
1063 );
1064 $this->html .= $this->displayConfirmation($this->l('The new entry has been added.'));
1065 }
1066 }
1067
1068 if (Tools::getIsset('deleteolxpremium_feed')) {
1069 $id = Tools::getValue('id');
1070 Db::getInstance()->delete(pSQL($this->table_name).'_feed', 'id = ' . (int)$id);
1071 $this->html .= $this->displayConfirmation($this->l('The entry has been deleted.'));
1072 }
1073
1074 if (Tools::getIsset('deleteolxpremium_products')) {
1075 $id = Tools::getValue('id_product');
1076 Db::getInstance()->delete(pSQL($this->table_name).'_product', 'id_product = ' . (int)$id);
1077 $this->html .= $this->displayConfirmation($this->l('The entry has been deleted.'));
1078 }
1079
1080 if (Tools::isSubmit('saveData')) {
1081 if (Tools::strlen(Tools::getValue('OLX_EMAIL'))<1 ||
1082 Tools::strlen(Tools::getValue('OLX_PHONE'))<1 ||
1083 !Validate::isEmail(Tools::getValue('OLX_EMAIL')) ||
1084 !Validate::isPhoneNumber(Tools::getValue('OLX_PHONE'))) {
1085 $this->html .= $this->displayError('Please fill the required fields with correct data!');
1086 } else {
1087 Configuration::updateValue('OLX_PLATFORM', Tools::getValue('OLX_PLATFORM'));
1088 Configuration::updateValue('OLX_REGION', Tools::getValue('OLX_REGION'));
1089 Configuration::updateValue('OLX_CITY', Tools::getValue('OLX_CITY'));
1090 Configuration::updateValue('OLX_DISTRICT', Tools::getValue('OLX_DISTRICT'));
1091 Configuration::updateValue('OLX_CURRENCY', Tools::getValue('OLX_CURRENCY'));
1092 Configuration::updateValue('OLX_EMAIL', Tools::getValue('OLX_EMAIL'));
1093 Configuration::updateValue('OLX_PHONE', Tools::getValue('OLX_PHONE'));
1094
1095 $this->html .= $this->displayConfirmation($this->l('Settings Updated'));
1096 }
1097 }
1098 }
1099
1100 public function getWarehouses($id_warehouses)
1101 {
1102 return $id_warehouses['id_warehouse'];
1103 }
1104
1105 public function getCategories(
1106 $id_lang,
1107 $active,
1108 $id_shop
1109 ) {
1110 $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
1111 '
1112 SELECT *
1113 FROM `' . _DB_PREFIX_ . 'category` c
1114 LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON c.`id_category` = cl.`id_category`
1115 WHERE ' . ($id_shop ? 'cl.`id_shop` = ' . (int)$id_shop : '') . ' ' .
1116 ($id_lang ? 'AND `id_lang` = ' . (int)$id_lang : '') . '
1117 ' . ($active ? 'AND `active` = 1' : '') . '
1118 ' . (!$id_lang ? 'GROUP BY c.id_category' : '') . '
1119 ORDER BY c.`level_depth` ASC, c.`position` ASC'
1120 );
1121
1122 return $result;
1123 }
1124
1125 public function hookDisplayBackOfficeHeader()
1126 {
1127 if (Tools::getValue('controller') == 'AdminModules' && Tools::getValue('configure') == 'olxpremium') {
1128 $this->context->controller->addJS($this->_path.'views/js/admin.js');
1129 } else {
1130 return;
1131 }
1132 }
1133}