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