Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cApiShortUrl
  • cApiShortUrlCollection

Functions

  • piUsAfterLoadPlugins
  • piUsConSaveArtAfter
  • piUsEditFormAdditionalRows
  • piUsGetErrorMessage
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the Plugin Manager configurations.
  4:  *
  5:  * @package Plugin
  6:  * @subpackage UrlShortener
  7:  * @author Simon Sprankel
  8:  * @copyright four for business AG <www.4fb.de>
  9:  * @license http://www.contenido.org/license/LIZENZ.txt
 10:  * @link http://www.4fb.de
 11:  * @link http://www.contenido.org
 12:  */
 13: 
 14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 15: 
 16: /**
 17:  * Constructs the HTML code containing table rows which are added to the end of
 18:  * the article edit form
 19:  *
 20:  * @return string rendered HTML code
 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:  * Function is called after an article has been saved.
 48:  * Checks whether a short URL has been given via $_POST and saves/deletes it.
 49:  *
 50:  * @param array $values the values which are saved
 51:  */
 52: function piUsConSaveArtAfter($editedIdArt, $values) {
 53:     // if not all parameters have been given, do nothing
 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:     // if given shorturl is already in use, show error message
 68:     $checkShortUrlItem = new cApiShortUrl();
 69:     $checkShortUrlItem->loadBy('shorturl', $shorturl);
 70:     if ($checkShortUrlItem->isLoaded()) {
 71:         // if shorturl has not been changed, do nothing
 72:         if ($shortUrlItem->get('shorturl') === $checkShortUrlItem->get('shorturl')) {
 73:             return;
 74:         }
 75:         // TODO add warning to session as soon as this is possible (depends
 76:         // CON-772)
 77:         // $session = cRegistry::getSession();
 78:         // $session->addWarning($message);
 79:         $message = piUsGetErrorMessage(cApiShortUrlCollection::ERR_ALREADY_EXISTS, $shortUrlItem);
 80:         $notification = new cGuiNotification();
 81:         $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
 82:         return;
 83:     }
 84:     // check if given shorturl is valid
 85:     $shortUrlColl = new cApiShortUrlCollection();
 86:     $errorCode = $shortUrlColl->isValidShortUrl($shorturl);
 87:     if ($errorCode !== true) {
 88:         $message = piUsGetErrorMessage($errorCode);
 89:         // TODO add warning to session as soon as this is possible (depends
 90:         // CON-772)
 91:         // $session = cRegistry::getSession();
 92:         // $session->addWarning($message);
 93:         $notification = new cGuiNotification();
 94:         $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
 95:         return;
 96:     }
 97:     if ($_POST['url_shortener_shorturl'] === '') {
 98:         // delete short URL if it exists
 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:         // a short URL has been given, so save it
109:         if ($shortUrlItem->isLoaded()) {
110:             // short URL already exists, update it
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:             // short URL does not exist yet, create a new one
122:             $shortUrlItem = $shortUrlColl->create($shorturl, $idart, $idlang, $idclient);
123:             cApiCecHook::executeAndReturn('ContenidoPlugin.UrlShortener.AfterCreate', $shortUrlItem);
124:         }
125:     }
126: }
127: 
128: /**
129:  * Computes an error message which describes the given error code.
130:  *
131:  * @param int $errorCode the error code
132:  * @return string the error message describing the given error code
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:                 // add the client name to the error message
156:                 $clientColl = new cApiClientCollection();
157:                 $message .= i18n('Client', 'url_shortener') . ': ' . $clientColl->getClientname($shortUrlItem->get('idclient'));;
158:                 $message .= '<br />';
159:                 // add the language name to the error message
160:                 $langColl = new cApiLanguageCollection();
161:                 $message .= i18n('Language', 'url_shortener') . ': ' . $langColl->getLanguageName($shortUrlItem->get('idlang'));
162:                 $message .= '<br />';
163:                 // add the category name to the error message
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:                 // add the article name to the error message
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:  * Function is called after the plugins have been loaded.
183:  * If the string placeholder in the example URL http://www.domain.de/placeholder
184:  * is a defined short URL, the user is redirected to the correct URL.
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: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0