Projekt

Allgemein

Profil

GX-Feature #54468 » UrlProvider.inc.php

Moritz Bunjes, 01.12.2017 12:56

 
1
<?php
2
/* --------------------------------------------------------------
3
   UrlProvider.inc.php 2017-12-01
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2017 Gambio GmbH
7
   Released under the GNU General Public License (Version 2)
8
   [http://www.gnu.org/licenses/gpl-2.0.html]
9
   --------------------------------------------------------------
10
*/
11

    
12
/**
13
 * Class UrlProvider
14
 */
15
class UrlProvider
16
{
17
	/**
18
	 * @var \GMSEOBoost
19
	 */
20
	protected $seoBoost;
21
	
22
	/**
23
	 * @var \LanguageProviderInterface
24
	 */
25
	protected $languageProvider;
26
	
27
	/**
28
	 * @var array
29
	 */
30
	protected $languageIds = [];
31
	
32
	
33
	/**
34
	 * UrlProvider constructor.
35
	 *
36
	 * @param \GMSEOBoost                $seoBoost
37
	 * @param \LanguageProviderInterface $languageProvider
38
	 */
39
	public function __construct(\GMSEOBoost $seoBoost, \LanguageProviderInterface $languageProvider)
40
	{
41
		$this->seoBoost         = $seoBoost;
42
		$this->languageProvider = $languageProvider;
43
	}
44
	
45
	
46
	/**
47
	 * Returns the url of a product identified by its ID and language code.
48
	 * 
49
	 * @param \IdType       $productId
50
	 * @param \LanguageCode $languageCode
51
	 *
52
	 * @return string Product Url
53
	 */
54
	public function getProductUrl(\IdType $productId, \LanguageCode $languageCode)
55
	{
56
		$name       = '';
57
		$languageId = $this->getLanguageIdByCode($languageCode);
58
		
59
		$query  = 'SELECT `products_name`
60
					FROM `products_description`
61
					WHERE
62
						`products_id` = ' . $productId->asInt() . ' AND 
63
						`language_id` = ' . $languageId;
64
		$result = xtc_db_query($query);
65
		
66
		if(xtc_db_num_rows($result))
67
		{
68
			$row  = xtc_db_fetch_array($result);
69
			$name = $row['products_name'];
70
		}
71
		
72
		if($this->seoBoost->boost_products)
73
		{
74
			$url = xtc_href_link($this->seoBoost->get_boosted_product_url($productId->asInt(), $name, $languageId), '',
75
			                     'NONSSL', false, true, false, false);
76
		}
77
		else
78
		{
79
			$url = xtc_href_link(FILENAME_PRODUCT_INFO, xtc_product_link($productId->asInt(), $name), 'NONSSL', false,
80
			                     true, false, false);
81
		}
82
		
83
		return $url;
84
	}
85
	
86
	
87
	/**
88
	 * Returns the ID of a language identified by its code.
89
	 * 
90
	 * @param \LanguageCode $languageCode
91
	 *
92
	 * @return int
93
	 */
94
	protected function getLanguageIdByCode(\LanguageCode $languageCode)
95
	{
96
		if(isset($this->languageIds[$languageCode->asString()]))
97
		{
98
			return $this->languageIds[$languageCode->asString()];
99
		}
100
		
101
		return $this->languageIds[$languageCode->asString()] = $this->languageProvider->getIdByCode($languageCode);
102
	}
103
}
    (1-1/1)