/**
* Initializes a new WCF.ACP.Ad.LocationHandler object.
+ *
+ * @param {object} variablesDescriptions
*/
- init: function() {
+ init: function(variablesDescriptions) {
+ this._variablesDescriptions = variablesDescriptions;
+
this._pageConditions = $('#pageConditions');
this._pageInputs = $('input[name="pageIDs[]"]');
+ this._variablesDescriptionsList = $('#ad').next('small').children('ul');
+
var dl = $(this._pageInputs[0]).parents('dl:eq(0)');
// hide the page controller elements
if (triggerEvent) Core.triggerEvent(this._pageInputs[i], 'change');
}
}.bind(this));
+
+ this._variablesDescriptionsList.children(':not(.jsDefaultItem)').remove();
+
+ var objectTypeId = $('#objectTypeID').val();
+ if (objectTypeId in this._variablesDescriptions) {
+ this._variablesDescriptionsList[0].innerHTML += this._variablesDescriptions[objectTypeId];
+ }
},
/**
<script data-relocate="true">
$(function() {
- new WCF.ACP.Ad.LocationHandler();
+ new WCF.ACP.Ad.LocationHandler({
+ {implode from=$variablesDescriptions key=objectType item=description}'{$objectType}': '{@$description|encodeJS}'{/implode}
+ });
});
</script>
use wcf\data\ad\AdAction;
use wcf\form\AbstractForm;
use wcf\system\ad\AdHandler;
+use wcf\system\ad\location\IAdLocation;
use wcf\system\condition\ConditionHandler;
use wcf\system\exception\UserInputException;
use wcf\system\WCF;
public function assignVariables() {
parent::assignVariables();
+ $variablesDescriptions = [];
+ foreach ($this->locationObjectTypes as $objectType) {
+ if ($objectType->className && is_subclass_of($objectType->className, IAdLocation::class)) {
+ /** @var IAdLocation $adLocation */
+ $adLocation = $objectType->getProcessor();
+
+ $variablesDescriptions[$objectType->objectTypeID] = $adLocation->getVariablesDescription();
+ }
+ }
+
WCF::getTPL()->assign([
'action' => 'add',
'ad' => $this->ad,
'isDisabled' => $this->isDisabled,
'groupedConditionObjectTypes' => $this->groupedConditionObjectTypes,
'objectTypeID' => $this->objectTypeID,
- 'showOrder' => $this->showOrder
+ 'showOrder' => $this->showOrder,
+ 'variablesDescriptions' => $variablesDescriptions
]);
}
use wcf\data\condition\Condition;
use wcf\data\object\type\ObjectTypeCache;
use wcf\data\DatabaseObject;
+use wcf\system\ad\location\IAdLocation;
use wcf\system\condition\ConditionHandler;
use wcf\system\request\IRouteController;
use wcf\system\WCF;
+use wcf\util\StringUtil;
/**
* Represents an ad.
public function getTitle() {
return $this->adName;
}
+
+ /**
+ * Returns the HTML code used to display the ad.
+ *
+ * @return string
+ * @since 5.2
+ */
+ public function getHtmlCode() {
+ $output = $this->ad;
+
+ $objectType = ObjectTypeCache::getInstance()->getObjectType($this->objectTypeID);
+
+ if (WCF::getUser()->userID) {
+ $output = strtr($output, ['{$username}' => StringUtil::encodeHTML(WCF::getUser()->username)]);
+ }
+ else {
+ $output = strtr($output, ['{$username}' => StringUtil::encodeHTML(WCF::getLanguage()->get('wcf.user.guest'))]);
+ }
+
+ if ($objectType->className && is_subclass_of($objectType->className, IAdLocation::class)) {
+ /** @var IAdLocation $adLocation */
+ $adLocation = $objectType->getProcessor();
+
+ $output = $adLocation->replaceVariables($output);
+ }
+
+ return $output;
+ }
}
}
}
- $output .= '<div>' . $ad->ad . '</div>';
+ $output .= '<div>' . $ad->getHtmlCode() . '</div>';
if (ENABLE_AD_ROTATION) break;
}
--- /dev/null
+<?php
+namespace wcf\system\ad\location;
+
+/**
+ * Abstract implementation of the `IAdLocation` that has to be extended instead of implementing the
+ * interface directly.
+ *
+ * @author Matthias Schmidt
+ * @copyright 2001-2019 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Ad\Location
+ * @since 5.2
+ */
+abstract class AbstractAdLocation implements IAdLocation {}
--- /dev/null
+<?php
+namespace wcf\system\ad\location;
+
+/**
+ * Every ad location that provides custom variables has to provide a PHP class implementing this
+ * interface.
+ *
+ * @author Matthias Schmidt
+ * @copyright 2001-2019 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Ad\Location
+ * @since 5.2
+ */
+interface IAdLocation {
+ /**
+ * Returns the description of the additional variables that can be used in ads in the active
+ * user's language.
+ *
+ * The returned description will be inserted into a list, thus each variable should be in a
+ * list item (`<li>`) element.
+ *
+ * @return string
+ */
+ public function getVariablesDescription();
+
+ /**
+ * Replaces all relevant variables in the given ad and returns the processed ad.
+ *
+ * @return string
+ */
+ public function replaceVariables($ad);
+}
</category>
<category name="wcf.acp.ad">
<item name="wcf.acp.ad.ad"><![CDATA[Werbung]]></item>
- <item name="wcf.acp.ad.ad.description"><![CDATA[HTML-Code der Werbung]]></item>
+ <item name="wcf.acp.ad.ad.description"><![CDATA[HTML-Code der Werbung. Die folgende Ausdrücke werden automatisch im HTML-Code ersetzt:\r
+<ul class="nativeList">\r
+ <li class="jsDefaultItem"><kbd>{literal}{$username}{/literal}</kbd> durch den Namen des aktiven Benutzers oder durch „Gast“</li>\r
+</ul>]]></item>
<item name="wcf.acp.ad.add"><![CDATA[Werbung hinzufügen]]></item>
<item name="wcf.acp.ad.conditions"><![CDATA[Bedingungen]]></item>
<item name="wcf.acp.ad.conditions.description"><![CDATA[Werden keine Bedingungen ausgewählt, wird die Werbung für jeden Benutzer angezeigt.]]></item>
</category>
<category name="wcf.acp.ad">
<item name="wcf.acp.ad.ad"><![CDATA[Ads]]></item>
- <item name="wcf.acp.ad.ad.description"><![CDATA[Enter the ad’s HTML code here.]]></item>
+ <item name="wcf.acp.ad.ad.description"><![CDATA[Enter the ad’s HTML code here. The following expressions will be automatically replace within the HTML code:\r
+<ul class="nativeList">\r
+ <li class="jsDefaultItem"><kbd>{literal}{$username}{/literal}</kbd> with the name of the active user or “Guest”</li>\r
+</ul>]]></item>
<item name="wcf.acp.ad.add"><![CDATA[Add Ad]]></item>
<item name="wcf.acp.ad.conditions"><![CDATA[Conditions]]></item>
<item name="wcf.acp.ad.conditions.description"><![CDATA[If no conditions are specified, the ad will display for every user.]]></item>