1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: function piUsEditFormAdditionalRows($idart, $idlang, $idclient) {
25: $shortUrl = new cApiShortUrl();
26: $shortUrl->loadByMany(array(
27: 'idart' => $idart,
28: 'idlang' => $idlang,
29: 'idclient' => $idclient
30: ));
31:
32: $tr = new cHTMLTableRow();
33:
34: $td = new cHTMLTableData();
35: $td->setClass('text_medium');
36: $td->setContent(i18n('Short URL', 'url_shortener'));
37: $tr->appendContent($td);
38:
39: $td = new cHTMLTableData();
40: $td->setClass('text_medium');
41: $textbox = new cHTMLTextbox('url_shortener_shorturl', $shortUrl->get('shorturl'), 24);
42: $td->setContent($textbox.' <img class="vAlignMiddle" title="'. i18n('INFO', 'url_shortener') .'" src="images/info.gif">');
43: $tr->appendContent($td);
44:
45: return $tr->render();
46: }
47:
48: 49: 50: 51: 52: 53:
54: function piUsConSaveArtAfter($editedIdArt, $values) {
55:
56: if (!isset($_POST['url_shortener_shorturl']) || $_POST['url_shortener_shorturl'] == '' || !isset($editedIdArt)) {
57: return;
58: }
59: $shorturl = $_POST['url_shortener_shorturl'];
60: $idart = $editedIdArt;
61: $idlang = cRegistry::getLanguageId();
62: $idclient = cRegistry::getClientId();
63: $shortUrlItem = new cApiShortUrl();
64: $shortUrlItem->loadByMany(array(
65: 'idart' => $idart,
66: 'idlang' => $idlang,
67: 'idclient' => $idclient
68: ));
69:
70: $checkShortUrlItem = new cApiShortUrl();
71: $checkShortUrlItem->loadBy('shorturl', $shorturl);
72: if ($checkShortUrlItem->isLoaded()) {
73:
74: if ($shortUrlItem->get('shorturl') === $checkShortUrlItem->get('shorturl')) {
75: return;
76: }
77:
78:
79:
80:
81: $message = piUsGetErrorMessage(cApiShortUrlCollection::ERR_ALREADY_EXISTS, $shortUrlItem);
82: $notification = new cGuiNotification();
83: $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
84: return;
85: }
86:
87: $shortUrlColl = new cApiShortUrlCollection();
88: $errorCode = $shortUrlColl->isValidShortUrl($shorturl);
89: if ($errorCode !== true) {
90: $message = piUsGetErrorMessage($errorCode);
91:
92:
93:
94:
95: $notification = new cGuiNotification();
96: $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
97: return;
98: }
99: if ($_POST['url_shortener_shorturl'] === '') {
100:
101: if ($shortUrlItem->isLoaded()) {
102: $shortUrlColl->delete($shortUrlItem->get('idshorturl'));
103: }
104: } else {
105:
106: if ($shortUrlItem->isLoaded()) {
107:
108: $shortUrlItem->set('shorturl', $shorturl);
109: $shortUrlItem->store();
110: } else {
111:
112: $shortUrlColl->create($shorturl, $idart, $idlang, $idclient);
113: }
114: }
115: }
116:
117: 118: 119: 120: 121: 122:
123: function piUsGetErrorMessage($errorCode, $shortUrlItem = null) {
124: switch ($errorCode) {
125: case cApiShortUrlCollection::ERR_INVALID_CHARS:
126: return i18n('The entered short URL contains invalid characters!', 'url_shortener');
127: break;
128: case cApiShortUrlCollection::ERR_IS_ARTICLE_ALIAS:
129: return i18n('The entered short URL is already an article alias!', 'url_shortener');
130: break;
131: case cApiShortUrlCollection::ERR_IS_CATEGORY_ALIAS:
132: return i18n('The entered short URL is already a category alias!', 'url_shortener');
133: break;
134: case cApiShortUrlCollection::ERR_IS_CLIENT_FOLDER:
135: return i18n('The entered short URL is a subdirectory of the client directory!', 'url_shortener');
136: break;
137: case cApiShortUrlCollection::ERR_TOO_SHORT:
138: return i18n('The entered short URL is too short!', 'url_shortener');
139: break;
140: case cApiShortUrlCollection::ERR_ALREADY_EXISTS:
141: $message = i18n('The entered short URL already exists!', 'url_shortener');
142: $message .= '<br />';
143: if ($shortUrlItem !== null) {
144:
145: $clientColl = new cApiClientCollection();
146: $message .= i18n('Client', 'url_shortener') . ': ' . $clientColl->getClientname($shortUrlItem->get('idclient'));;
147: $message .= '<br />';
148:
149: $langColl = new cApiLanguageCollection();
150: $message .= i18n('Language', 'url_shortener') . ': ' . $langColl->getLanguageName($shortUrlItem->get('idlang'));
151: $message .= '<br />';
152:
153: $catArt = new cApiCategoryArticle();
154: $catArt->loadBy('idart', $shortUrlItem->get('idart'));
155: $catLang = new cApiCategoryLanguage();
156: $catLang->loadByCategoryIdAndLanguageId($catArt->get('idcat'), $shortUrlItem->get('idlang'));
157: $message .= i18n('Category', 'url_shortener') . ': ' . $catLang->get('name');
158: $message .= '<br />';
159:
160: $artlang = new cApiArticleLanguage();
161: $artlang->loadByArticleAndLanguageId($shortUrlItem->get('idart'), $shortUrlItem->get('idlang'));
162: $message .= i18n('Article', 'url_shortener') . ': ' . $artlang->get('title');
163: }
164: return $message;
165: break;
166: }
167: return i18n('The entered short URL is not valid!', 'url_shortener');
168: }
169:
170: 171: 172: 173: 174:
175: function piUsAfterLoadPlugins() {
176: $requestUri = $_SERVER['REQUEST_URI'];
177: $shorturl = substr($requestUri, strrpos($requestUri, '/') + 1);
178: $shortUrlItem = new cApiShortUrl();
179: $shortUrlItem->loadBy('shorturl', $shorturl);
180: if ($shortUrlItem->isLoaded()) {
181: $uriParams = array(
182: 'idart' => $shortUrlItem->get('idart'),
183: 'lang' => $shortUrlItem->get('idlang')
184: );
185: $url = cUri::getInstance()->build($uriParams, true);
186: header('Location:' . $url);
187: exit();
188: }
189: }
190: