Advertisement
miriamdepaula

OpenCart - SEO URLs

Aug 23rd, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.91 KB | None | 0 0
  1. <?php
  2. //opencart SEO Urls - SQL INSERT: http://pastebin.com/vA7n5KKK
  3.  
  4. class ControllerCommonSeoUrl extends Controller {
  5.    public function index() {
  6.       // Add rewrite to url class
  7.       if ($this->config->get('config_seo_url')) {
  8.          $this->url->addRewrite($this);
  9.       }
  10.      
  11.       // Decode URL
  12.       if (isset($this->request->get['_route_'])) {
  13.          $parts = explode('/', $this->request->get['_route_']);
  14.  
  15.          $route = "";
  16.          
  17.          foreach ($parts as $part) {
  18.             $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
  19.            
  20.             if ($query->num_rows) {
  21.                $url = explode('=', $query->row['query']);
  22.  
  23.                if(count($url) > 1){
  24.                
  25.                if ($url[0] == 'product_id') {
  26.                   $this->request->get['product_id'] = $url[1];
  27.                }
  28.                
  29.                if ($url[0] == 'category_id') {
  30.                   if (!isset($this->request->get['path'])) {
  31.                      $this->request->get['path'] = $url[1];
  32.                   } else {
  33.                      $this->request->get['path'] .= '_' . $url[1];
  34.                   }
  35.                }  
  36.                
  37.                if ($url[0] == 'manufacturer_id') {
  38.                   $this->request->get['manufacturer_id'] = $url[1];
  39.                }
  40.                
  41.                if ($url[0] == 'information_id') {
  42.                   $this->request->get['information_id'] = $url[1];
  43.                }
  44.                }else{
  45.                   $route = $url[0];
  46.                }
  47.             } else {
  48.                $this->request->get['route'] = 'error/not_found';  
  49.             }
  50.          }
  51.          
  52.          if (isset($this->request->get['product_id'])) {
  53.             $this->request->get['route'] = 'product/product';
  54.          } elseif (isset($this->request->get['path'])) {
  55.             $this->request->get['route'] = 'product/category';
  56.          } elseif (isset($this->request->get['manufacturer_id'])) {
  57.             $this->request->get['route'] = 'product/manufacturer/product';
  58.          } elseif (isset($this->request->get['information_id'])) {
  59.             $this->request->get['route'] = 'information/information';
  60.          }else {
  61.             $this->request->get['route'] = $route;
  62.          }
  63.  
  64.          
  65.          if (isset($this->request->get['route'])) {
  66.             return $this->forward($this->request->get['route']);
  67.          }
  68.       }
  69.    }
  70.    
  71.    public function rewrite($link) {
  72.       if ($this->config->get('config_seo_url')) {
  73.          $url_data = parse_url(str_replace('&amp;', '&', $link));
  74.      
  75.          $url = '';
  76.          
  77.          $data = array();
  78.      
  79.          parse_str($url_data['query'], $data);
  80.          
  81.          foreach ($data as $key => $value) {
  82.             if (($key == 'product_id') || ($key == 'manufacturer_id') || ($key == 'information_id')) {
  83.                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
  84.            
  85.                if ($query->num_rows) {
  86.                   $url .= '/' . $query->row['keyword'];
  87.                  
  88.                   unset($data[$key]);
  89.                }              
  90.             } elseif ($key == 'path') {
  91.                $categories = explode('_', $value);
  92.                
  93.                foreach ($categories as $category) {
  94.                   $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
  95.            
  96.                   if ($query->num_rows) {
  97.                      $url .= '/' . $query->row['keyword'];
  98.                   }                    
  99.                }
  100.                
  101.                unset($data[$key]);
  102.             }elseif ($key == 'route') {
  103.                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($value) . "'");
  104.            
  105.                if ($query->num_rows) {
  106.                   $url .= '/' . $query->row['keyword'];
  107.                  
  108.                   unset($data[$key]);
  109.                }              
  110.             }
  111.          }
  112.      
  113.          if ($url) {
  114.             unset($data['route']);
  115.          
  116.             $query = '';
  117.          
  118.             if ($data) {
  119.                foreach ($data as $key => $value) {
  120.                   $query .= '&' . $key . '=' . $value;
  121.                }
  122.                
  123.                if ($query) {
  124.                   $query = '?' . trim($query, '&');
  125.                }
  126.             }
  127.  
  128.             return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query;
  129.          } else {
  130.             return $link;
  131.          }
  132.       } else {
  133.          return $link;
  134.       }      
  135.    }  
  136. }
  137. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement