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: 23: 24: 25: 26: 27: 28:
29: function piUsEditFormAdditionalRows($idart, $idlang, $idclient, $disabled) {
30: $shortUrl = new cApiShortUrl();
31: $shortUrl->loadByMany(array(
32: 'idart' => $idart,
33: 'idlang' => $idlang,
34: 'idclient' => $idclient
35: ));
36:
37: $tr = new cHTMLTableRow();
38:
39: $td = new cHTMLTableData();
40: $td->setClass('text_medium');
41: $td->setContent(i18n('Short URL', 'url_shortener'));
42: $tr->appendContent($td);
43:
44: $td = new cHTMLTableData();
45: $td->setClass('text_medium');
46: $textbox = new cHTMLTextbox('url_shortener_shorturl', $shortUrl->get('shorturl'), 24, '', '', $disabled);
47: $td->setContent($textbox.' <img class="vAlignMiddle" title="'. i18n('INFO', 'url_shortener') .'" src="images/info.gif" alt="">');
48: $tr->appendContent($td);
49:
50: return $tr->render();
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
64: function piUsConSaveArtAfter($editedIdArt, $values) {
65:
66: if (!isset($_POST['url_shortener_shorturl']) || !isset($editedIdArt)) {
67: return;
68: }
69: $shorturl = $_POST['url_shortener_shorturl'];
70: $idart = $editedIdArt;
71: $idlang = cRegistry::getLanguageId();
72: $idclient = cRegistry::getClientId();
73: $shortUrlItem = new cApiShortUrl();
74: $shortUrlItem->loadByMany(array(
75: 'idart' => $idart,
76: 'idlang' => $idlang,
77: 'idclient' => $idclient
78: ));
79:
80: $checkShortUrlItem = new cApiShortUrl();
81: $checkShortUrlItem->loadBy('shorturl', $shorturl);
82: if ($checkShortUrlItem->isLoaded()) {
83:
84: if ($shortUrlItem->get('shorturl') === $checkShortUrlItem->get('shorturl')) {
85: return;
86: }
87:
88:
89:
90:
91: $message = piUsGetErrorMessage(cApiShortUrlCollection::ERR_ALREADY_EXISTS, $shortUrlItem);
92: $notification = new cGuiNotification();
93: $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
94: return;
95: }
96:
97: $shortUrlColl = new cApiShortUrlCollection();
98: $errorCode = $shortUrlColl->isValidShortUrl($shorturl);
99: if ($errorCode !== true) {
100: $message = piUsGetErrorMessage($errorCode);
101:
102:
103:
104:
105: $notification = new cGuiNotification();
106: $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
107: return;
108: }
109: if ($_POST['url_shortener_shorturl'] === '') {
110:
111: if ($shortUrlItem->isLoaded()) {
112: $item = cApiCecHook::executeAndReturn('ContenidoPlugin.UrlShortener.BeforeRemove', $shortUrlItem);
113: if ($item instanceof cApiShortUrl) {
114: $shortUrlItem = $item;
115: }
116:
117: $shortUrlColl->delete($shortUrlItem->get('idshorturl'));
118: }
119: } else {
120:
121: if ($shortUrlItem->isLoaded()) {
122:
123: $oldShortUrlItem = clone $shortUrlItem;
124: $shortUrlItem->set('shorturl', $shorturl);
125:
126: $item = cApiCecHook::executeAndReturn('ContenidoPlugin.UrlShortener.BeforeEdit', $shortUrlItem, $oldShortUrlItem);
127: if ($item instanceof cApiShortUrl) {
128: $shortUrlItem = $item;
129: }
130:
131: $shortUrlItem->store();
132: } else {
133:
134: $shortUrlItem = $shortUrlColl->create($shorturl, $idart, $idlang, $idclient);
135: cApiCecHook::executeAndReturn('ContenidoPlugin.UrlShortener.AfterCreate', $shortUrlItem);
136: }
137: }
138: }
139:
140: 141: 142: 143: 144: 145: 146: 147: 148: 149:
150: function piUsGetErrorMessage($errorCode, $shortUrlItem = NULL) {
151: switch ($errorCode) {
152: case cApiShortUrlCollection::ERR_INVALID_CHARS:
153: return i18n('The entered short URL contains invalid characters!', 'url_shortener');
154: break;
155: case cApiShortUrlCollection::ERR_IS_ARTICLE_ALIAS:
156: return i18n('The entered short URL is already an article alias!', 'url_shortener');
157: break;
158: case cApiShortUrlCollection::ERR_IS_CATEGORY_ALIAS:
159: return i18n('The entered short URL is already a category alias!', 'url_shortener');
160: break;
161: case cApiShortUrlCollection::ERR_IS_CLIENT_FOLDER:
162: return i18n('The entered short URL is a subdirectory of the client directory!', 'url_shortener');
163: break;
164: case cApiShortUrlCollection::ERR_TOO_SHORT:
165: return i18n('The entered short URL is too short!', 'url_shortener');
166: break;
167: case cApiShortUrlCollection::ERR_ALREADY_EXISTS:
168: $message = i18n('The entered short URL already exists!', 'url_shortener');
169: $message .= '<br />';
170: if ($shortUrlItem !== NULL) {
171:
172: $clientColl = new cApiClientCollection();
173: $message .= i18n('Client', 'url_shortener') . ': ' . $clientColl->getClientname($shortUrlItem->get('idclient'));;
174: $message .= '<br />';
175:
176: $langColl = new cApiLanguageCollection();
177: $message .= i18n('Language', 'url_shortener') . ': ' . $langColl->getLanguageName($shortUrlItem->get('idlang'));
178: $message .= '<br />';
179:
180: $catArt = new cApiCategoryArticle();
181: $catArt->loadBy('idart', $shortUrlItem->get('idart'));
182: $catLang = new cApiCategoryLanguage();
183: $catLang->loadByCategoryIdAndLanguageId($catArt->get('idcat'), $shortUrlItem->get('idlang'));
184: $message .= i18n('Category', 'url_shortener') . ': ' . $catLang->get('name');
185: $message .= '<br />';
186:
187: $artlang = new cApiArticleLanguage();
188: $artlang->loadByArticleAndLanguageId($shortUrlItem->get('idart'), $shortUrlItem->get('idlang'));
189: $message .= i18n('Article', 'url_shortener') . ': ' . $artlang->get('title');
190: }
191: return $message;
192: break;
193: }
194: return i18n('The entered short URL is not valid!', 'url_shortener');
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204: 205:
206: function piUsAfterLoadPlugins() {
207: $requestUri = $_SERVER['REQUEST_URI'];
208: $shorturl = cString::getPartOfString($requestUri, cString::findLastPos($requestUri, '/') + 1);
209: $shortUrlItem = new cApiShortUrl();
210: $shortUrlItem->loadBy('shorturl', $shorturl);
211: if ($shortUrlItem->isLoaded()) {
212: $uriParams = array(
213: 'idart' => $shortUrlItem->get('idart'),
214: 'lang' => $shortUrlItem->get('idlang')
215: );
216: $url = cUri::getInstance()->build($uriParams, true);
217: header('Location:' . $url);
218: exit();
219: }
220: }
221:
222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233:
234: function piUseConDeleteArtAfter($idart)
235: {
236: $count = 0;
237: if (cRegistry::getPerm()->have_perm_area_action('url_shortener', 'url_shortener_delete')) {
238: $idart = cSecurity::toInteger($idart);
239: $shortUrlColl = new cApiShortUrlCollection();
240: $count = $shortUrlColl->deleteBy('idart', $idart);
241: }
242:
243: return $count;
244: }
245: