seo_package.php 47.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
<?php
class ModelToolSeoPackage extends Model {
  
  private $store_names = array();
  private $store_countries = array();

  public function __construct($registry) {
    parent::__construct($registry);
    
    if (version_compare(VERSION, '3', '>=')) {
      $this->url_alias = 'seo_url';
    } else {
      $this->url_alias = 'url_alias';
    }
  }
    
  public function getFriendlyUrls($lang) {

  }
  
  public function getProductOptions($product_id, $lang) {
		$product_option_data = array();

		$product_option_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_option` po LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) LEFT JOIN `" . DB_PREFIX . "option_description` od ON (o.option_id = od.option_id) WHERE po.product_id = '" . (int)$product_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		foreach ($product_option_query->rows as $product_option) {
      $options_values = array();

			$product_option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON(pov.option_value_id = ov.option_value_id) WHERE pov.product_option_id = '" . (int)$product_option['product_option_id'] . "' ORDER BY ov.sort_order ASC");
      
			foreach ($product_option_value_query->rows as $product_option_value) {
        $opt_value = $this->db->query("SELECT pov.option_value_id, ovd.name, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, pov.weight, pov.weight_prefix FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_id = '" . (int)$product_id . "' AND pov.product_option_value_id = '" . (int)$product_option_value['product_option_value_id'] . "' AND ovd.language_id = '" . (int)$lang . "'")->row;

        if (isset($opt_value['name'])) {
          $options_values[] = $opt_value['name'];
        }
			}

			$product_option_data[strtolower($product_option['name'])] = implode(' ', $options_values);
		}

		return $product_option_data;
	}
  
  public function getAttributes($product_id, $lang, $asArray = false) {
    
		$product_attribute_query = $this->db->query("SELECT pa.text, ad.name FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute_description ad ON ad.attribute_id = pa.attribute_id WHERE pa.product_id = '" . (int)$product_id . "' AND pa.language_id = " . (int) $lang . " AND ad.language_id = " . (int) $lang . " GROUP BY pa.attribute_id");

    if ($asArray) {
      $output = array();
      
      foreach ($product_attribute_query->rows as $product_attribute) {
        $output[strtolower($product_attribute['name'])] = $product_attribute['text'];
      }
    } else {
      $output = '';
      
 		  foreach ($product_attribute_query->rows as $product_attribute) {
        $output .= ($output ? ', ': '') . $product_attribute['name'] . ': ' . $product_attribute['text'];
 		  }
		}
    
    return $output;
	}
  
	public function getProductCategoryName($product_id, $lang, $parent = false) {
    if ($parent) {
      $query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "product_to_category pc LEFT JOIN " . DB_PREFIX . "category c ON c.category_id = pc.category_id LEFT JOIN " . DB_PREFIX . "category_path cp ON (cp.category_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd ON cd.category_id = c.parent_id WHERE pc.product_id = '" . (int)$product_id . "' AND cd.language_id = " . $lang . " ORDER BY cp.level DESC LIMIT 1")->row;
    } else {
      $query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category_description cd ON (cd.category_id = p2c.category_id) LEFT JOIN " . DB_PREFIX . "category_path cp ON (p2c.category_id = cp.category_id) WHERE p2c.product_id = '" . (int)$product_id . "' AND cd.language_id = " . $lang . " ORDER BY cp.level DESC LIMIT 1")->row;
    }
		
		if (isset($query['name']))
			return $query['name'];
		return '';
	}
  
  public function getCategoryName($category_id, $lang, $parent = false) {
    if (is_array($category_id)) {
      $cat_ids = implode(',', array_map('intval', $category_id));
      
      if ($parent) {
        $query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_path cp ON (cp.category_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd ON (cd.category_id = c.parent_id) WHERE c.category_id IN (" . $cat_ids . ") AND cd.language_id = " . $lang . " ORDER BY cp.level DESC")->row;
      } else {
        $query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category_description cd ON (cd.category_id = cp.category_id) WHERE cp.category_id IN (" . $cat_ids . ") AND cd.language_id = " . $lang . " ORDER BY cp.level DESC")->row;
      }
    } else if ($parent) {
      $query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_path cp ON (cp.category_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd ON cd.category_id = c.parent_id WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = " . $lang . " ORDER BY cp.level DESC LIMIT 1")->row;
    } else {
      $query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category_description cd LEFT JOIN " . DB_PREFIX . "category_path cp ON (cp.category_id = cd.category_id) WHERE cd.category_id = '" . (int)$category_id . "' AND cd.language_id = " . $lang . " ORDER BY cp.level DESC LIMIT 1")->row;
    }
		
		if (isset($query['name']))
			return $query['name'];
		return '';
	}
  
  public function getCategoryPrice($category_id, $type = 'lowest') {
    if(!isset($this->tax)) {
      if (version_compare(VERSION, '2.2', '>=')) {
        $this->registry->set('tax', new \Cart\Tax($this->registry));
      } else {
        if(!class_exists('Tax')) {
          require_once(DIR_SYSTEM . 'library/tax.php');
        }
        if(!class_exists('Customer')) {
          require_once(DIR_SYSTEM . 'library/customer.php');
          $this->registry->set('customer', new Customer($this->registry));
        }
        $this->tax = new Tax($this->registry);
      }
      
      // set shipping address of store for tax calculation
      $this->tax->setShippingAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id'));
      $this->tax->setPaymentAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id'));
      $this->tax->setStoreAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id'));
    }
    
    $order = ($type == 'lowest') ? 'ASC' : 'DESC';
    
    $query = $this->db->query("SELECT p.price, p.tax_class_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id) WHERE p2c.category_id = '" . (int)$category_id . "' ORDER BY p.price " . $order . " LIMIT 1")->row;
    
    if (!empty($query['price'])) {
			$price = $this->currency->format($this->tax->calculate($query['price'], $query['tax_class_id'], $this->config->get('config_tax')), $this->config->get('config_currency'));
      
      if (function_exists('mb_substr')) {
        if (mb_substr($price, -3) == '.00' || mb_substr($price, -3) == ',00') {
          return mb_substr($price, 0, -3);
        }
      } else {
        if (substr($price, -3) == '.00' || substr($price, -3) == ',00') {
          return substr($price, 0, -3);
        }
      }
      
      return $price;
    }
    
		return '';
  }
  
  public function getGrandParentCategoryName($category_id, $lang) {
		$query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category ch ON (c.parent_id = ch.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd ON (cd.category_id = ch.parent_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = " . $lang . " LIMIT 1")->row;
		
		if (isset($query['name']))
			return $query['name'];
		return '';
	}
  
  public function getParentCategoryName($category_id, $lang) {
		//$query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category_description cd LEFT JOIN " . DB_PREFIX . "category c ON c.category_id = cd.category_id WHERE cd.category_id = '" . (int)$category_id . "' AND language_id = " . $lang . " LIMIT 1")->row;
		$query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON c.parent_id = cd.category_id WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = " . $lang . " LIMIT 1")->row;
		
		if (isset($query['name']))
			return $query['name'];
		return '';
	}
  
  public function getChildCategoryName($category_id, $lang) {
		//$query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category_description cd LEFT JOIN " . DB_PREFIX . "category c ON c.category_id = cd.category_id WHERE cd.category_id = '" . (int)$category_id . "' AND language_id = " . $lang . " LIMIT 1")->row;
		$query = $this->db->query("SELECT cd.name FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON c.category_id = cd.category_id WHERE c.parent_id = '" . (int)$category_id . "' AND cd.language_id = " . $lang . " LIMIT 1")->row;
		
		if (isset($query['name']))
			return $query['name'];
		return '';
	}
	
	public function getProductCategoryId($product_id) {
		$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' LIMIT 1")->row;
		
		if (isset($query['category_id']))
			return $query['category_id'];
		return '';
	}
	
  public function getManufacturerData($manufacturer_id, $lang, $store_id) {
		$query = $this->db->query("SELECT m.name as orig_name, d.name, d.description as 'desc', d.meta_description as meta_desc, d.meta_title as title, d.meta_keyword as keywords, d.seo_h1 as h1, d.seo_h2 as h2, d.seo_h3 as h3 FROM " . DB_PREFIX . "manufacturer m LEFT JOIN " . DB_PREFIX . "seo_manufacturer_description d ON (m.manufacturer_id = d.manufacturer_id AND d.language_id=" . (int) $lang . " AND d.store_id = '" . (int) $store_id . "') WHERE m.manufacturer_id = '" . (int)$manufacturer_id . "' LIMIT 1")->row;
		
    if (empty($query['name']) && isset($query['orig_name'])) {
      $query['name'] = $query['orig_name'];
    }
    
		return $query;
	}
	
	public function applyFunctions($value) {
    // apply functions
    $fn = 'upcase|lowcase|ucfirst';
    $value = preg_replace_callback('#\{('.$fn.')\}(.+?)\{\/(?:'.$fn.')\}#', array($this, 'pregFunctions'), $value);
    
    // truncate without breaking word
    if (preg_match('#{limit:(\d+)}#', $value, $limit)) {
      $value = str_replace($limit[0], '', $value);
      //$value = trim(mb_substr($value, 0, (int) $limit[1]));
      
      if (strlen($value) > (int) $limit[1]) {
        $newstr = substr($value, 0, (int) $limit[1]);
        if (substr($newstr,-1,1) != ' ') {
          $newstr = substr($newstr, 0, strrpos($newstr, ' '));
        }
        $value = $newstr;
      }  

    }
    
    // basic double comma removing
    $value = trim(preg_replace('/(?:,\s*,+)+/', ', ', $value), ', ');
    
    return $value;
  }
  
	public function pregFunctions($values) {
    if ($values[1] == 'upcase') {
      return mb_convert_case($values[2], MB_CASE_UPPER, 'UTF-8');
    } else if ($values[1] == 'lowcase') {
      return mb_convert_case($values[2], MB_CASE_LOWER, 'UTF-8');
    } else if ($values[1] == 'ucfirst') {
      return mb_convert_case($values[2], MB_CASE_TITLE, 'UTF-8');
    }
  }
	
	public function commonTransform($pattern, $lang, $row, $store_id = 0, $get_value = false) {
    $replace  = array();
    
    if ($this->config->get('mlseo_current_lang')) {
      $lang_code = $this->config->get('mlseo_current_lang');
    } else {
      $lang_code = $this->getLangCode($lang);
    }
    
    $replace['{'.$lang_code.'}'] = '';
    $replace['{/'.$lang_code.'}'] = '';
    
    if (function_exists('mb_substr')) {
      $replace['{'.mb_substr($lang_code, 0, 2).'}'] = '';
      $replace['{/'.mb_substr($lang_code, 0, 2).'}'] = '';
    } else {
      $replace['{'.substr($lang_code, 0, 2).'}'] = '';
      $replace['{/'.substr($lang_code, 0, 2).'}'] = '';
    }
    
    if (strpos($pattern, '[store]') !== false) {
      if (isset($this->store_names[$store_id])) {
        $replace['[store]'] = $this->store_names[$store_id];
      } else {
        if (!$store_id) {
          $this->store_names[$store_id] = $replace['[store]'] = $this->config->get('config_name');
        } else {
          $this->load->model('setting/store');
          $store_data = $this->model_setting_store->getStore($store_id);
          $this->store_names[$store_id] = $replace['[store]'] = $store_data['name'];
        }
      }
    }
    
    /*
    if (strpos($pattern, '[store_country]') !== false) {
      if (isset($this->store_countries[$store_id])) {
        $replace['[store_country]'] = $this->store_countries[$store_id];
      } else {
        $this->load->model('localisation/country');
        
        if (!$store_id) {
          $country = $this->model_localisation_country->getCountry($this->config->get('config_country_id'));
          $this->store_countries[$store_id] = $replace['[store_country]'] = isset($country['name']) ? $country['name'] : '';
        } else {
          $this->load->model('setting/store');
          $store_data = $this->model_setting_store->getStore($store_id);
          $country = $this->model_localisation_country->getCountry($store_data['config_country_id']);
          $this->store_countries[$store_id] = $replace['[store_country]'] = isset($country['name']) ? $country['name'] : '';
        }
      }
    }
    */
    
    if (strpos($pattern, '[lang_id]') !== false)
			$replace['[lang_id]'] = $lang;
		if (strpos($pattern, '[lang]') !== false)
			$replace['[lang]'] = $lang_code;
    
    $pattern = preg_replace('#\[(.+?)\|(uppercase|lowercase)\]#', '{$2}[$1]{/$2}', $pattern);
    
    $pattern = str_replace(array_keys($replace), array_values($replace), $pattern);

    if ($this->config->get('mlseo_lang_codes')) {
      $lgCodes = array();
      foreach ($this->config->get('mlseo_lang_codes') as $lgCode) {
        $lgCodes[] = $lgCode;
        if (function_exists('mb_substr')) {
          $lgCodes[] = mb_substr($lgCode, 0, 2);
        } else {
          $lgCodes[] = substr($lgCode, 0, 2);
        }
      }
      $lgCodes = implode('|', $lgCodes);
      
      $pattern = preg_replace('/\{('.$lgCodes.')\}(.*)\{\/('.$lgCodes.')\}/isU', '', $pattern);
    }
    
    while (preg_match_all('#\{([^\{].+\|.+)\}#U', $pattern, $results, PREG_SET_ORDER)) {
      if ($results) {
        foreach ($results as $res) {
          $rand = explode('|', $res[1]);
          
          if (is_array($rand)) {
            if ($get_value) {
              if (isset($this->session->data['gkd_seorand'][abs(crc32($pattern))]) && $this->session->data['gkd_seorand'][abs(crc32($pattern))]+1 <= count($rand)-1) {
                $this->session->data['gkd_seorand'][abs(crc32($pattern))]++;
              } else {
                $this->session->data['gkd_seorand'][abs(crc32($pattern))] = 0;
              }
            
              $pattern = str_replace($res[0], $rand[$this->session->data['gkd_seorand'][abs(crc32($pattern))]], $pattern);
            } else {
              $pattern = str_replace($res[0], $rand[array_rand($rand)], $pattern);
            }
          }
        }
      }
    }
    
    return $pattern;
  }
  
	public function transformProduct($pattern, $lang, $row, $store_id = 0, $get_value = false) {
    $pattern = $this->commonTransform($pattern, $lang, $row, $store_id, $get_value);
    
		if (!isset($row['name'])) {
      if (isset($row['orig_name'])) {
        $row['name'] = $row['orig_name'];
      } else {
   			$row['name'] = isset($row['product_description'][$lang]['name']) ? $row['product_description'][$lang]['name'] : '';
      }
		}
    
    if (!isset($row['description'])) {
      if (isset($row['orig_description'])) {
        $row['description'] = $row['orig_description'];
      } else {
   			$row['description'] = isset($row['product_description'][$lang]['description']) ? $row['product_description'][$lang]['description'] : '';
      }
		}
    
    //$this->load->model('catalog/product');
    //$product_attributes = $this->model_catalog_product->getProductAttributes($row['product_id']);
    
    // $product_attributes = $this->db->query("SELECT ad.name, pa.text FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.product_id = '" . (int)$row['product_id'] . "' AND ad.language_id = '" . (int)$lang . "' AND pa.language_id = '" . (int)$lang . "' ORDER BY a.sort_order, ad.name")->rows;
		// var_dump($product_attributes);die;
    // preg_match('#\[attribute_name:(\d)\]#', $pattern, $res);
    
		$replace  = array();
		if (strpos($pattern, '[name]') !== false)
			$replace['[name]'] = trim($row['name']);
		if (strpos($pattern, '[model]') !== false)
			$replace['[model]'] = trim($row['model']);
		if (strpos($pattern, '[upc]') !== false)
			$replace['[upc]'] = trim($row['upc']);
		if (strpos($pattern, '[sku]') !== false)
			$replace['[sku]'] = trim($row['sku']);
		if (strpos($pattern, '[ean]') !== false)
			$replace['[ean]'] = trim($row['ean']);
		if (strpos($pattern, '[jan]') !== false)
			$replace['[jan]'] = trim($row['jan']);
		if (strpos($pattern, '[isbn]') !== false)
			$replace['[isbn]'] = trim($row['isbn']);
		if (strpos($pattern, '[mpn]') !== false)
			$replace['[mpn]'] = trim($row['mpn']);
		if (strpos($pattern, '[location]') !== false)
			$replace['[location]'] = trim($row['location']);
    if (strpos($pattern, '[price_notax]') !== false) 
      $replace['[price_notax]'] = (float)$row['price'] ? $this->currency->format($row['price'], $this->config->get('config_currency')) : '';
    if (strpos($pattern, '[name_') !== false) {
      $langDescriptions = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE product_id = ".(int) $row['product_id'])->rows;
      
      foreach ($langDescriptions as $langDesc) {
        $replace['[name_'.$langDesc['language_id'].']'] = $langDesc['name'];
      }
    }
    if (strpos($pattern, '[price]') !== false) {
      if (version_compare(VERSION, '2.2', '>=')) {
        $this->registry->set('tax', new \Cart\Tax($this->registry));
      } else {
        if(!class_exists('Tax')) {
          require_once(DIR_SYSTEM . 'library/tax.php');
        }
        if(!class_exists('Customer')) {
          require_once(DIR_SYSTEM . 'library/customer.php');
          $this->registry->set('customer', new Customer($this->registry));
        }
        $this->tax = new Tax($this->registry);
      }
      
      // set shipping address of store for tax calculation
      $this->tax->setShippingAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id'));
      $this->tax->setPaymentAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id'));
      $this->tax->setStoreAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id'));
      
      
      if (!empty($row['product_special'])) {
        foreach ($row['product_special'] as $prod_special) {
          if ($prod_special['customer_group_id'] == $this->config->get('mlseo_special_group')) {
            if ((float)$prod_special['price']) {
              $replace['[price]'] = $this->currency->format($this->tax->calculate($prod_special['price'], $row['tax_class_id'], $this->config->get('config_tax')), $this->config->get('config_currency'));
            } else {
              $replace['[price]'] = (float)$row['price'] ? $this->currency->format($this->tax->calculate($row['price'], $row['tax_class_id'], $this->config->get('config_tax')), $this->config->get('config_currency')) : '';
            }
          } else {
            $replace['[price]'] = (float)$row['price'] ? $this->currency->format($this->tax->calculate($row['price'], $row['tax_class_id'], $this->config->get('config_tax')), $this->config->get('config_currency')) : '';
          }
        }
      } else if (isset($row['special']) && (float)$row['special']) {
        $replace['[price]'] = (float)$row['special'] ? $this->currency->format($this->tax->calculate($row['special'], $row['tax_class_id'], $this->config->get('config_tax')), $this->config->get('config_currency')) : '';
      } else {
        $replace['[price]'] = (float)$row['price'] ? $this->currency->format($this->tax->calculate($row['price'], $row['tax_class_id'], $this->config->get('config_tax')), $this->config->get('config_currency')) : '';
      }
    }
		if (strpos($pattern, '[prod_id]') !== false)
			$replace['[prod_id]'] = isset($row['product_id']) ? trim($row['product_id']) : '';
		if (strpos($pattern, '[cat_id]') !== false)
			$replace['[cat_id]'] = isset($row['product_id']) ? trim($this->getProductCategoryId($row['product_id'], $lang)) : '';
		if (strpos($pattern, '[category]') !== false) {
      if (!empty($this->request->post['product_category'])) {
        $replace['[category]'] = trim($this->getCategoryName($this->request->post['product_category'], $lang));
      } else if (!empty($row['product_category'])) {
        $replace['[category]'] = trim($this->getCategoryName($row['product_category'], $lang));
      } else if (isset($row['product_id'])) {
        $replace['[category]'] = trim($this->getProductCategoryName($row['product_id'], $lang));
      } else {
        $replace['[category]'] = '';
      }
    }
		if (strpos($pattern, '[parent_cat]') !== false) {
      if (!empty($this->request->post['product_category'])) {
        $replace['[parent_cat]'] = $replace['[parent_category]'] = trim($this->getCategoryName($this->request->post['product_category'], $lang, true));
      } else if (!empty($row['product_category'])) {
        $replace['[parent_cat]'] = $replace['[parent_category]'] = trim($this->getCategoryName($row['product_category'], $lang, true));
      } else if (isset($row['product_id'])) {
        $replace['[parent_cat]'] = $replace['[parent_category]'] = trim($this->getProductCategoryName($row['product_id'], $lang, true));
      } else {
        $replace['[parent_cat]'] = $replace['[parent_category]'] = '';
      }
    }
    
    if (preg_match_all('/\[brand(?:_(\w+))?\]/', $pattern, $brandMatches)) {
      $brandData = !empty($row['manufacturer_id']) ? $this->getManufacturerData($row['manufacturer_id'], $lang, $store_id) : array();
      
      if (!empty($brandMatches)) {
        foreach ($brandMatches[1] as $brandTag) {
          if (!$brandTag) {
            $replace['[brand]'] = isset($brandData['name']) ? $brandData['name'] : '';
          } else {
            $replace['[brand_'.$brandTag.']'] = isset($brandData[$brandTag]) ? $brandData[$brandTag] : '';
          }
        }
      }
    }
		
    if (strpos($pattern, '[desc]') !== false) {
      if (function_exists('mb_substr')) {
        $replace['[desc]'] = trim(preg_replace('/\s\s+/', ' ', mb_substr(strip_tags(str_replace(array('</p>', '</li>'), array('</p> ', '</li> '), html_entity_decode((isset($row['description']) ? $row['description'] : ''), ENT_QUOTES, 'UTF-8'))), 0, 220)));
      } else {
        $replace['[desc]'] = trim(preg_replace('/\s\s+/', ' ', substr(strip_tags(str_replace(array('</p>', '</li>'), array('</p> ', '</li> '), html_entity_decode((isset($row['description']) ? $row['description'] : ''), ENT_QUOTES, 'UTF-8'))), 0, 220)));
      }
    }
		if (strpos($pattern, '[desc_sentence]') !== false) {
       preg_match('#(.*?(?:<\/p>|\.|\!|\?))#is', strip_tags(html_entity_decode($row['description'], ENT_QUOTES, 'UTF-8')), $desc_extract);

       if (isset($desc_extract[1])) {
         $replace['[desc_sentence]'] = strip_tags($desc_extract[1]);
       } else {
         $replace['[desc_sentence]'] = '';
       }
    }
    if (strpos($pattern, '[desc_sentence2]') !== false) {
       preg_match('#.*?(?:<\/p>|\.|\!|\?)(.*?(?:<\/p>|\.|\!|\?))#is', strip_tags(html_entity_decode($row['description'], ENT_QUOTES, 'UTF-8')), $desc_extract);
       
       if (isset($desc_extract[1])) {
         $replace['[desc_sentence2]'] = strip_tags($desc_extract[1]);
       } else {
         $replace['[desc_sentence2]'] = '';
       }
    }
    if (strpos($pattern, '[desc_sentence3]') !== false) {
       preg_match('#.*?(?:<\/p>|\.|\!|\?).*?(?:<\/p>|\.|\!|\?)(.*?(?:<\/p>|\.|\!|\?))#is', strip_tags(html_entity_decode($row['description'], ENT_QUOTES, 'UTF-8')), $desc_extract);
       
       if (isset($desc_extract[1])) {
         $replace['[desc_sentence3]'] = strip_tags($desc_extract[1]);
       } else {
         $replace['[desc_sentence3]'] = '';
       }
    }
    if (strpos($pattern, '[desc_sentence4]') !== false) {
       preg_match('#.*?(?:<\/p>|\.|\!|\?).*?(?:<\/p>|\.|\!|\?).*?(?:<\/p>|\.|\!|\?)(.*?(?:<\/p>|\.|\!|\?))#is', strip_tags(html_entity_decode($row['description'], ENT_QUOTES, 'UTF-8')), $desc_extract);
       
       if (isset($desc_extract[1])) {
         $replace['[desc_sentence4]'] = strip_tags($desc_extract[1]);
       } else {
         $replace['[desc_sentence4]'] = '';
       }
    }
    
    if (strpos($pattern, '[attr]') !== false || strpos($pattern, '[attributes]') !== false) {
      $replace['[attr]'] = $replace['[attributes]'] = !empty($row['product_id']) ? $this->getAttributes($row['product_id'], $lang) : '';
    }
    
    if (preg_match_all('#\[cat_(\d)\]#', $pattern, $catLevels)) {

      if (!empty($this->request->post['product_category'])) {
        $categories = $this->getFullProductPathNames($this->request->post['product_category'], $lang, true);
      } else if (!empty($row['product_category'])) {
        $categories = $this->getFullProductPathNames($row['product_category'], $lang, true);
      } else if (isset($row['product_id'])) {
        $categories = $this->getFullProductPathNames($row['product_id'], $lang);
      } else {
        $categories = array();
      }
      
      foreach ($catLevels[1] as $catLevel) {
        $replace['[cat_'.$catLevel.']'] = isset($categories[$catLevel-1]) ? $categories[$catLevel-1] : '';
      }
    }
    
    if (preg_match_all('#\[attr_(.+?)\]#', $pattern, $attrNames)) {
      $attrValues = !empty($row['product_id']) ? $this->getAttributes($row['product_id'], $lang, true) : array();
      
      foreach ($attrNames[1] as $attrName) {
        $replace['[attr_'.$attrName.']'] = isset($attrValues[strtolower($attrName)]) ? $attrValues[strtolower($attrName)] : '';
      }
    }
    
    if (preg_match_all('#\[opt_(.+?)\]#', $pattern, $optNames)) {
      $optValues = !empty($row['product_id']) ? $this->getProductOptions($row['product_id'], $lang) : array();
      
      foreach ($optNames[1] as $optName) {
        $replace['[opt_'.$optName.']'] = isset($optValues[strtolower($optName)]) ? $optValues[strtolower($optName)] : '';
      }
    }
    
		$value = str_replace(array_keys($replace), array_values($replace), $pattern);
    
		$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
		
    $value = $this->applyFunctions($value);
    
		return trim($value);
	}
	
	public function transformCategory($pattern, $lang, $row, $store_id = 0, $get_value = false) {
    $pattern = $this->commonTransform($pattern, $lang, $row, $store_id, $get_value);
    
		if (!isset($row['name'])) {
      if (isset($row['orig_name'])) {
        $row['name'] = $row['orig_name'];
      } else {
   			$row['name'] = isset($row['category_description'][$lang]['name']) ? $row['category_description'][$lang]['name'] : '';
      }
		}
    
    if (!isset($row['description'])) {
      if (isset($row['orig_description'])) {
        $row['description'] = $row['orig_description'];
      } else {
   			$row['description'] = isset($row['category_description'][$lang]['description']) ? $row['category_description'][$lang]['description'] : '';
      }
		}
		
		$replace  = array();
    
		if (strpos($pattern, '[name]') !== false)
			$replace['[name]'] = trim($row['name']);
    if (strpos($pattern, '[name_') !== false) {
      $langDescriptions = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_description WHERE category_id = ".(int) $row['category_id'])->rows;
      
      foreach ($langDescriptions as $langDesc) {
        $replace['[name_'.$langDesc['language_id'].']'] = $langDesc['name'];
      }
    }
		if (strpos($pattern, '[desc]') !== false) {
      if (function_exists('mb_substr')) {
        $replace['[desc]'] = trim(preg_replace('/\s\s+/', ' ', mb_substr(strip_tags(str_replace(array('</p>', '</li>'), array('</p> ', '</li> '), html_entity_decode((isset($row['description']) ? $row['description'] : ''), ENT_QUOTES, 'UTF-8'))), 0, 220)));
      } else {
        $replace['[desc]'] = trim(preg_replace('/\s\s+/', ' ', substr(strip_tags(str_replace(array('</p>', '</li>'), array('</p> ', '</li> '), html_entity_decode((isset($row['description']) ? $row['description'] : ''), ENT_QUOTES, 'UTF-8'))), 0, 220)));
      }
    }
    if (strpos($pattern, '[desc_sentence]') !== false) {
       preg_match('#<p>(.*?)(?:</p>|\.)#is', html_entity_decode($row['description'], ENT_QUOTES, 'UTF-8'), $desc_extract);
       
       if (isset($desc_extract[1])) {
         $replace['[desc_sentence]'] = strip_tags($desc_extract[1]);
       } else {
         $replace['[desc_sentence]'] = '';
       }
    }
    
		if (strpos($pattern, '[cat_id]') !== false)
			$replace['[cat_id]'] = trim($row['category_id']);
    if (strpos($pattern, '[grandparent]') !== false) {
      if (!empty($row['category_id'])) {
        $replace['[grandparent]'] = trim($this->getGrandParentCategoryName($row['category_id'], $lang));
      } else if (!empty($row['parent_id'])) {
        $replace['[grandparent]'] = trim($this->getParentCategoryName($row['parent_id'], $lang));
      } else {
        $replace['[grandparent]'] = '';
      }
    }
    if (strpos($pattern, '[parent]') !== false) {
      if (!empty($row['category_id'])) {
        $replace['[parent]'] = trim($this->getParentCategoryName($row['category_id'], $lang));
      } else if (!empty($row['parent_id'])) {
        $replace['[parent]'] = trim($this->getCategoryName($row['parent_id'], $lang));
      } else {
        $replace['[parent]'] = '';
      }
    }
    if (strpos($pattern, '[child]') !== false)
			$replace['[child]'] = !empty($row['category_id']) ? trim($this->getChildCategoryName($row['category_id'], $lang)) : '';
    if (strpos($pattern, '[lowest_price]') !== false)
			$replace['[lowest_price]'] = $this->getCategoryPrice($row['category_id'], 'lowest');
    if (strpos($pattern, '[highest_price]') !== false)
			$replace['[highest_price]'] = $this->getCategoryPrice($row['category_id'], 'highest');

			/*
		if (strpos($pattern, '[parent]') !== false)
			$replace['[parent]'] = $this->model_tool_seo_package->getParentCategoryName($row['category_id'], $lang);
			*/
			
		$value = str_replace(array_keys($replace), array_values($replace), $pattern);
		$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
		
    $value = $this->applyFunctions($value);
    
		return trim($value);
	}
	
	public function transformInformation($pattern, $lang, $row, $store_id = 0, $get_value = false) {
    $pattern = $this->commonTransform($pattern, $lang, $row, $store_id, $get_value);
    
		if (!isset($row['title'])) {
      if (isset($row['orig_title'])) {
        $row['title'] = $row['orig_title'];
      } else {
   			$row['title'] = isset($row['information_description'][$lang]['title']) ? $row['information_description'][$lang]['title'] : '';
      }
		}
    
    $replace  = array();
    
    if (strpos($pattern, '[name_') !== false) {
      $langDescriptions = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_description WHERE information_id = ".(int) $row['information_id'])->rows;
      
      foreach ($langDescriptions as $langDesc) {
        $replace['[name_'.$langDesc['language_id'].']'] = $langDesc['title'];
      }
    }
    
    if (!isset($row['description'])) {
      if (isset($row['orig_description'])) {
        $row['description'] = $row['orig_description'];
      } else {
   			$row['description'] = isset($row['information_description'][$lang]['description']) ? $row['information_description'][$lang]['description'] : '';
      }
		}
		
		if (strpos($pattern, '[name]') !== false)
			$replace['[name]'] = trim($row['title']);
		if (strpos($pattern, '[title]') !== false)
			$replace['[title]'] = trim($row['title']);
		if (strpos($pattern, '[desc]') !== false) {
      if (function_exists('mb_substr')) {
        $replace['[desc]'] = trim(preg_replace('/\s\s+/', ' ', mb_substr(strip_tags(str_replace(array('</p>', '</li>'), array('</p> ', '</li> '), html_entity_decode((isset($row['description']) ? $row['description'] : ''), ENT_QUOTES, 'UTF-8'))), 0, 220)));
      } else {
        $replace['[desc]'] = trim(preg_replace('/\s\s+/', ' ', substr(strip_tags(str_replace(array('</p>', '</li>'), array('</p> ', '</li> '), html_entity_decode((isset($row['description']) ? $row['description'] : ''), ENT_QUOTES, 'UTF-8'))), 0, 220)));
      }
    }
    
    if (strpos($pattern, '[desc_sentence]') !== false) {
       preg_match('#<p>(.*?)(?:</p>|\.)#is', html_entity_decode($row['description'], ENT_QUOTES, 'UTF-8'), $desc_extract);
       
       if (isset($desc_extract[1])) {
         $replace['[desc_sentence]'] = strip_tags($desc_extract[1]);
       } else {
         $replace['[desc_sentence]'] = '';
       }
    }
    
		$value = str_replace(array_keys($replace), array_values($replace), $pattern);
		$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
		
    $value = $this->applyFunctions($value);
    
		return trim($value);
	}
	
	public function transformManufacturer($pattern, $lang, $row, $store_id = 0, $get_value = false) {
    $pattern = $this->commonTransform($pattern, $lang, $row, $store_id, $get_value);
    
		$replace  = array();
		if (strpos($pattern, '[name]') !== false) {
      if (!empty($row['seo_manufacturer_description'][$store_id][$lang]['name'])) {
        $replace['[name]'] = trim($row['seo_manufacturer_description'][$store_id][$lang]['name']);
      } else {
			  $replace['[name]'] = trim($row['name']);
      }
    }
				
		$value = str_replace(array_keys($replace), array_values($replace), $pattern);
		$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
		
    $value = $this->applyFunctions($value);
    
		return trim($value);
	}
	
	public function filter_seo($seo_kw, $type, $id, $lang = '', $simulate = false) {
		$whitespace = $this->config->get('mlseo_whitespace');
    
    if ($lang) {
      $translit = $this->config->get('mlseo_ascii_'.$lang);
      $remove = $this->config->get('mlseo_remove_'.$lang);
      $replace = $this->config->get('mlseo_replace_'.$lang);
    } else {
      $translit = $this->config->get('mlseo_ascii_'.$this->config->get('config_language_id'));
      $remove = $this->config->get('mlseo_remove_'.$this->config->get('config_language_id'));
      $replace = $this->config->get('mlseo_replace_'.$this->config->get('config_language_id'));
    }
    
    if (!empty($replace)) {
      foreach (explode('|', $replace) as $rep) {
        $r = explode(':', $rep);
        
        $seo_kw = str_replace(html_entity_decode($r[0], ENT_QUOTES, 'UTF-8'), isset($r[1]) ? $r[1] : '', $seo_kw);
      }
    }
    
		if (!empty($remove)) {
      $beforeWord = "(\\b|\\s|\\.|\\,|\\!|\\?|\\(|\\)|\\'|\\\"|^)";
      $afterWord =  "(\\b|\\s|\\.|\\,|\\!|\\?|\\(|\\)|\\'|\\\"|$)";
      $removeArray = array();
      
      foreach (explode(',', $remove) as $rem) {
        $removeArray[] = '`'.$beforeWord.preg_quote(trim($rem)).$afterWord.'`i';
      }
      
      if ($removeArray) {
        $seo_kw = preg_replace($removeArray, '$1$2', $seo_kw);
      }
    }
    
		if ($translit) {
      if ($this->config->get('mlseo_current_lang')) {
        $lang_code = $this->config->get('mlseo_current_lang');
      } else {
        $lang_code = $this->getLangCode($lang);
      }
      
      include_once(DIR_SYSTEM . 'library/gkd_urlify.php');
      
      if (function_exists('mb_substr')) {
        $seo_kw = URLify::downcode($seo_kw, mb_substr($lang_code, 0, 2));
      } else {
        $seo_kw = URLify::downcode($seo_kw, substr($lang_code, 0, 2));
      }
      
			// language specific tr
      /* now with urlify
			$seo_kw = $this->mb_strtr($seo_kw, array('À','Á','Â','Ã','Ä','Å','Ă','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ő','Ø','Œ','Ș','Ț','Ù','Ú','Û','Ü','Ű','Ý','Þ','ß','à','á','â','ã','ä','å','ă','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','ő','ø','œ','ș','ț','ù','ú','û','ü','ű','ý','þ','ÿ'),
                                         array('A','A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','O','OE','S','T','U','U','U','U','U','Y','TH','ss','a','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','o','o','oe','s','t','u','u','u','u','u','y','th','y')); // latin
			$seo_kw = $this->mb_strtr($seo_kw, 'αβγδεζηθικλμνξοπρστυφχψωάέίόύήώςϊΰϋΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩΆΈΊΌΎΉΏΪΫ', 'abgdezh8iklmn3oprstyfxpswaeioyhwsiyyiABGDEZH8IKLMN3OPRSTYFXPSWAEIOYHWIY'); // greek
			$seo_kw = $this->mb_strtr($seo_kw, 'ÁáČčĎďĚěŇňŘřŠšŤťÚúŮůÝýŽžĐđ', 'AaCcDdEeNnRrSsTtUuUuYyZzDd'); // czech, croatian, slovenian
			$seo_kw = $this->mb_strtr($seo_kw, 'ĄČĘĖĮYŠŲŪŽąčęėįyšųūž', 'ACEEIYSUUZaceeiysuuz'); // lithunanian
			$seo_kw = $this->mb_strtr($seo_kw, 'ĀČĒĢĪĶĻŅŠŪŽāčēģīķļņšūž', 'ACEGIKLNSUZacegiklnsuz'); // latvian
			$seo_kw = $this->mb_strtr($seo_kw, 'ÇçĞğİıÖöŞşÜü', 'CcGgIiOoSsUu'); // turkish
			$seo_kw = $this->mb_strtr($seo_kw, 'ăâîșțŢţ', 'aaistTt'); // romanian
			$seo_kw = $this->mb_strtr($seo_kw, 'ąćęłńóśźżĄĆĘŁŃÓŚŹŻ', 'acelnoszzACeLNOSZZ'); // polish
			$seo_kw = $this->mb_strtr($seo_kw, array('Г','Д','Е','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х','Ь','Ъ','щ','ш','ч','ц','ю','я','ж','а','б','в','г','д','е','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ь','ъ'),
                                         array('Sht','Sh','Ch','C','Yu','Ya','J','A','B','V','G','D','E','Z','I','Y','K','L','M','N','O','P','R','S','T','U','F','H','','A','sht','sh','ch','c','yu','ya','j','a','b','v','g','d','e','z','i','y','k','l','m','n','o','p','r','s','t','u','f','h','','a')); // bulgarian
			$seo_kw = $this->mb_strtr($seo_kw, array('أ','ب','ت','ث','ج','ح','خ','د','ذ','ر','ز','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ك','ل','م','ن','ه','و','ي'), array('a','b','t','th','g','h','kh','d','th','r','z','s','sh','s','d','t','th','aa','gh','f','k','k','l','m','n','h','o','y')); // arabic
			$seo_kw = $this->mb_strtr($seo_kw, array('Ä','Ö','Ü','ä','ö','ü','ß','ẞ'), array('Ae','Oe','Ue','ae','oe','ue','ss','SS')); // german
			$seo_kw = $this->mb_strtr($seo_kw, array('ђ','ј','љ','њ','ћ','џ','đ','Ђ','Ј','Љ','Њ','Ћ','Џ','Đ'), array('dj','j','lj','nj','c','dz','dj','Dj','j','Lj','Nj','C','Dz','Dj',)); // serbian
			$seo_kw = $this->mb_strtr($seo_kw, array('а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я','А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ы','Ь','Э','Ю','Я','№'),
                                         array('a','b','v','g','d','e','yo','zh','z','i','j','k','l','m','n','o','p','r','s','t','u','f','h','c','ch','sh','sh','','y','','e','yu','ya','A','B','V','G','D','E','Yo','Zh','Z','I','J','K','L','M','N','O','P','R','S','T','U','F','H','C','Ch','Sh','Sh','','Y','','E','Yu','Ya','')); // russian
			$seo_kw = $this->mb_strtr($seo_kw, array('І', 'і', 'Ї', 'ї', 'Є', 'є', 'Ґ', 'ґ'), array('I','i','Yi','yi', 'Ye', 'ye', 'G', 'g')); // ukrainian
			*/
			
			// entities autodetect method (works for western europe languages)
			// $seo_kw = preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);~i', '$1', htmlentities($seo_kw, ENT_QUOTES, 'UTF-8'));
			// $seo_kw = html_entity_decode($seo_kw, ENT_QUOTES);
			// $seo_kw = preg_replace(array('~[^0-9a-z]~i', '~[ -]+~'), $whitespace, $seo_kw);
			$seo_kw = trim($seo_kw, '_'.$whitespace);
		}
			
    if ($type != 'image' && $this->config->get('mlseo_lowercase')) {
      if (function_exists('mb_strtolower')) {
        $seo_kw = mb_strtolower($seo_kw, 'UTF-8');
      } else {
        $seo_kw = strtolower($seo_kw);
      }
		}
    
		$seo_kw = str_replace(' ', $whitespace, $seo_kw);
    $seo_kw = preg_replace('~\x{00a0}~siu', $whitespace, $seo_kw); // unicode non-breaking space
    
		//$seo_kw = str_replace(array('"',"'",'&','(',')','.','+',',','*',':',';','=','?','@','$','/','%','#'), '', $seo_kw);
    if (!$this->config->get('mlseo_safe_url')) {
      $seo_kw = preg_replace('/([^\w\-\._~:\[\]@!\$\'()*\+,;=%]+)/u', $whitespace, $seo_kw); 
    } else if ($this->config->get('mlseo_safe_url') == 'ext') {
		$seo_kw = str_replace(array('\'','`','‘','’','|','%7C', "\r\n", "\n", "\r",'/','\\',',',':','~','(', ')','.',','), $whitespace, $seo_kw);
		$seo_kw = str_replace(array('"','“','”','&','&amp;','+','?','!','/','%','#','&gt;','&lt;',';','<','>','™','®','©','&copy;','&reg;','&trade;'), '', $seo_kw);
    } else if ($this->config->get('mlseo_safe_url') == 'max') {
      $seo_kw = preg_replace('/(\W+)/u', $whitespace, $seo_kw);
    }
    
    if ($whitespace) {
      $seo_kw = preg_replace('/'.$whitespace.$whitespace.'+/', $whitespace, $seo_kw);
      $seo_kw = trim($seo_kw, $whitespace);
    }
		
		$exists = array(1);
    $number = '';
    $search_kw = $seo_kw;
    
    if ($type != 'image') {
      while(count($exists)) {
        /*
        if ($simulate && isset($this->session->data['kwCountArray']) && in_array($search_kw, (array) $this->session->data['kwCountArray'])) {
           $seo_kw .= '-[x]';
           break;
        }
        */
        
        if ($this->config->get('mlseo_absolute') && $this->config->get('mlseo_duplicate') && ($type == 'category' || $type == 'manufacturer')) break;
        elseif ($this->config->get('mlseo_absolute') && $type == 'category')
             $sql = "SELECT * FROM " . DB_PREFIX . $this->url_alias . " WHERE query = '".$type."_id=" . (int)$id . "' AND keyword='".$this->db->escape($search_kw)."'";
        elseif ($this->config->get('mlseo_duplicate'))
             $sql = "SELECT * FROM " . DB_PREFIX . $this->url_alias . " WHERE query != '".$type."_id=" . (int)$id . "' AND keyword='".$this->db->escape($search_kw)."'";
        else $sql = "SELECT * FROM " . DB_PREFIX . $this->url_alias . " WHERE keyword='".$this->db->escape($search_kw)."'";
        
        $exists = $this->db->query($sql)->row;
        
        if (count($exists)) {
          if ($simulate) {
            if (!isset($this->session->data['kwCountArray'][$seo_kw])) {
              $this->session->data['kwCountArray'][$seo_kw] = $number;
            }
            
            $this->session->data['kwCountArray'][$seo_kw]++;
          }

          if ($number == '') {
            $number = (int)$number;
          } 

          $search_kw = $seo_kw .'-'. ++$number;
        }
        /*
        if ($this->config->get('mlseo_absolute')) break; // disable duplicate detection while absolute is enabled, todo: advanced detection of possible cases
        if ($this->config->get('mlseo_absolute') && $this->config->get('mlseo_duplicate') && $type == 'category') break;
        elseif ($this->config->get('mlseo_absolute') && $type == 'category')
             $sql = "SELECT * FROM " . DB_PREFIX . $this->url_alias . " WHERE query = '".$type."_id=" . (int)$id . "' AND keyword='".$this->db->escape($seo_kw)."'";
        elseif ($this->config->get('mlseo_duplicate'))
             $sql = "SELECT * FROM " . DB_PREFIX . $this->url_alias . " WHERE query != '".$type."_id=" . (int)$id . "' AND keyword='".$this->db->escape($seo_kw)."'";
        else $sql = "SELECT * FROM " . DB_PREFIX . $this->url_alias . " WHERE keyword='".$this->db->escape($seo_kw)."'";
        $exists = $this->db->query($sql)->row;
        if (count($exists)) $seo_kw .= '_';
        */
      }
      
      if ($number) {
        if ($simulate && isset($this->session->data['kwCountArray'][$seo_kw])) {
          //$seo_kw .= '-[x]';
          $seo_kw .= '-'.$this->session->data['kwCountArray'][$seo_kw];
        } else {
          $seo_kw .= '-'.$number;
        }
      }
    }
		
	  // config here the max chars (will cut after the current word) ; 0 = unlimited
		$this->config->set('multilingual_seo_wordlimit', 0);
	    
    if ($this->config->get('mlseo_wordlimit')) {
      $pos = strpos($seo_kw, $whitespace, $this->config->get('mlseo_wordlimit'));
      if ($pos > $this->config->get('mlseo_wordlimit')) {
        if (function_exists('mb_substr')) {
          $seo_kw = mb_substr($seo_kw, 0, $pos);
        } else {
          $seo_kw = substr($seo_kw, 0, $pos);
        }
      }
    }
    
		return $seo_kw;
	}
	
  public function getFullProductPathNames($product_id, $lang = false, $from_category = false) {
		$categories = array();

    if ($from_category) {
      if (!empty($product_id[0])) {
        $category_id = $product_id[0];
        $category = $this->db->query("SELECT cd.name, c.category_id, c.parent_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$lang . "'")->row;
      }
    } else {
      $category = $this->db->query("SELECT cd.name, c.category_id, c.parent_id FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category c ON (p2c.category_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd ON (p2c.category_id = cd.category_id) WHERE product_id = '" . (int)$product_id . "' AND cd.language_id = '" . (int)$lang . "'")->row;
    }
		
    while (!empty($category['name'])) {
      $categories[] = $category['name'];
      
      $category = $this->db->query("SELECT cd.name, c.category_id, c.parent_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . $category['parent_id']. "' AND cd.language_id = '" . (int)$lang . "'")->row;
    }
		
		return array_reverse($categories);
	}
  
	public function getFullProductPaths($product_id, $lang = false) {
		$path = array();
    
		$categories = $this->db->query("SELECT c.category_id, c.parent_id FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category c ON (p2c.category_id = c.category_id) WHERE product_id = '" . (int)$product_id . "'")->rows;
		
		foreach ($categories as $key => $category) {
			$path[$key] = '';
			if (!$category) continue;
      
			$path[$key] = $category['category_id'];
			
			while ($category['parent_id']) {
				$path[$key] = $category['parent_id'] . '_' . $path[$key];
				$category = $this->db->query("SELECT category_id, parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $category['parent_id']. "'")->row;
			}
			
			$path[$key] = $path[$key];
		}
		
		if (!count($path)) return array();
		
		return $path;
	}
	
	public function getLangCode($lang_id) {
    $this->load->model('localisation/language');
    $languages = $this->model_localisation_language->getLanguages();
    
    foreach($languages as $language) {
      if ($language['language_id'] == $lang_id) {
        return  $language['code'];
      }
    }
    
    return '';
  }
  
	private function mb_strtr($str, $from, $to = null) {
		if (is_array($from) && is_array($to))
			return str_replace($from, $to, $str);
		elseif (is_array($from))
			return str_replace(array_keys($from), array_values($from), $str);
		return str_replace(preg_split('~~u', $from, null, PREG_SPLIT_NO_EMPTY), preg_split('~~u', $to, null, PREG_SPLIT_NO_EMPTY), $str);
	}
	
}