1: <?php
2:
3: /**
4: * This file contains the uri builder custom path class.
5: *
6: * @package Core
7: * @subpackage Frontend_URI
8: * @author Rudi Bieller
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: cInclude('includes', 'functions.pathresolver.php');
18:
19: /**
20: * Custom path uri builder class.
21: *
22: * Implementation to build URL in style index-a-1.html
23: * with category path (/category/subcategory/index-a-1.html).
24: *
25: * @package Core
26: * @subpackage Frontend_URI
27: */
28: class cUriBuilderCustomPath extends cUriBuilder {
29:
30: /**
31: * Self instance
32: *
33: * @var cUriBuilderCustomPath
34: */
35: static private $_instance;
36:
37: /**
38: * Configuration
39: *
40: * @var array
41: */
42: private $aConfig;
43:
44: /**
45: * Constructor to create an instance of this class.
46: */
47: private function __construct() {
48: $this->sHttpBasePath = '';
49: }
50:
51: /**
52: * Get instance of self.
53: *
54: * @return cUriBuilderCustomPath
55: */
56: public static function getInstance() {
57: if (self::$_instance == NULL) {
58: self::$_instance = new self();
59: }
60: return self::$_instance;
61: }
62:
63: /**
64: * Builds a URL in index-a-1.html style.
65: *
66: * Index keys of $aParams will be used as "a", corresponding values
67: * as "1" in this sample.
68: *
69: * For creating the location string $aParams needs to have keys
70: * idcat, level, lang and at least one custom key.
71: *
72: * If level is not set, level 0 will be used as default.
73: *
74: * @todo Somehow get around using prCreateURLNameLocationString()
75: * @throws cInvalidArgumentException
76: * @param array $aParams
77: * Required keys are: idcat, level, lang and at least one
78: * custom key.
79: * @param bool $bUseAbsolutePath [optional]
80: * @param array $aConfig [optional]
81: * If not set, will use UriBuilderConfig::getConfig()
82: */
83: public function buildUrl(array $aParams, $bUseAbsolutePath = false, array $aConfig = array()) {
84: if (!isset($aParams['idcat'])) {
85: throw new cInvalidArgumentException('$aParams[idcat] must be set!');
86: }
87: if (!isset($aParams['level'])) {
88: //throw new cInvalidArgumentException('$aParams[level] must be set! Setting it to 0 will create complete path.');
89: $aParams['level'] = '1';
90: }
91: if (!isset($aParams['lang'])) {
92: throw new cInvalidArgumentException('$aParams[lang] must be set!');
93: }
94: if (sizeof($aParams) <= 3) {
95: throw new cInvalidArgumentException('$aParams must have at least one custom entry!');
96: }
97: // if no config passed or not all parameters available, use default config
98: if (sizeof($aConfig) == 0 || !isset($aConfig['prefix']) || !isset($aConfig['suffix']) || !isset($aConfig['separator'])) {
99: include_once('class.uribuilder.config.php');
100: $aConfig = cUriBuilderConfig::getConfig();
101: }
102: $this->aConfig = $aConfig;
103:
104: $sCategoryString = '';
105: prCreateURLNameLocationString(intval($aParams['idcat']), "/", $sCategoryString, false, "", $aParams['level'], $aParams['lang'], true, false);
106: if (strlen($sCategoryString) > 0 && substr($sCategoryString, -1) != '/') {
107: $sCategoryString .= '/';
108: }
109: $this->sUrl = $bUseAbsolutePath === true ? $this->sHttpBasePath : '';
110: $this->sUrl .= $sCategoryString;
111: $this->sUrl .= $this->aConfig['prefix'];
112: foreach ($aParams as $sKey => $mVal) {
113: if ($sKey != 'idcat' && $sKey != 'lang' && $sKey != 'level') {
114: $sVal = $mVal; // assuming mVal is a string and thus a single value
115: if (is_array($mVal)) { // mVal has more than one value, e.g. index-b-1-2.html
116: $sVal = implode($this->aConfig['separator'], $mVal);
117: }
118: $this->sUrl .= $this->aConfig['separator'] . strval($sKey) . $this->aConfig['separator'] . strval($sVal);
119: }
120: }
121: $this->sUrl .= $this->aConfig['suffix'];
122: }
123:
124: }
125: