From 1949f2e3b0b2791b3ad20b124eb8ac7e2bb9479a Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 3 Feb 2019 18:04:39 +0100 Subject: [PATCH] Add support for (location-specific) variables in ads Close #2843 --- wcfsetup/install/files/acp/js/WCF.ACP.js | 15 ++++++++- .../install/files/acp/templates/adAdd.tpl | 4 ++- .../files/lib/acp/form/AdAddForm.class.php | 14 +++++++- .../install/files/lib/data/ad/Ad.class.php | 30 +++++++++++++++++ .../files/lib/system/ad/AdHandler.class.php | 2 +- .../ad/location/AbstractAdLocation.class.php | 14 ++++++++ .../system/ad/location/IAdLocation.class.php | 32 +++++++++++++++++++ wcfsetup/install/lang/de.xml | 5 ++- wcfsetup/install/lang/en.xml | 5 ++- 9 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/ad/location/AbstractAdLocation.class.php create mode 100644 wcfsetup/install/files/lib/system/ad/location/IAdLocation.class.php diff --git a/wcfsetup/install/files/acp/js/WCF.ACP.js b/wcfsetup/install/files/acp/js/WCF.ACP.js index d48e7c392b..cd09681e2e 100644 --- a/wcfsetup/install/files/acp/js/WCF.ACP.js +++ b/wcfsetup/install/files/acp/js/WCF.ACP.js @@ -2480,11 +2480,17 @@ WCF.ACP.Ad.LocationHandler = Class.extend({ /** * 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 @@ -2551,6 +2557,13 @@ WCF.ACP.Ad.LocationHandler = Class.extend({ 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]; + } }, /** diff --git a/wcfsetup/install/files/acp/templates/adAdd.tpl b/wcfsetup/install/files/acp/templates/adAdd.tpl index 3f438f8770..d583b75811 100644 --- a/wcfsetup/install/files/acp/templates/adAdd.tpl +++ b/wcfsetup/install/files/acp/templates/adAdd.tpl @@ -2,7 +2,9 @@ diff --git a/wcfsetup/install/files/lib/acp/form/AdAddForm.class.php b/wcfsetup/install/files/lib/acp/form/AdAddForm.class.php index a13764c221..c2c66524ee 100644 --- a/wcfsetup/install/files/lib/acp/form/AdAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/AdAddForm.class.php @@ -5,6 +5,7 @@ use wcf\data\object\type\ObjectTypeCache; 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; @@ -88,6 +89,16 @@ class AdAddForm extends AbstractForm { 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, @@ -97,7 +108,8 @@ class AdAddForm extends AbstractForm { 'isDisabled' => $this->isDisabled, 'groupedConditionObjectTypes' => $this->groupedConditionObjectTypes, 'objectTypeID' => $this->objectTypeID, - 'showOrder' => $this->showOrder + 'showOrder' => $this->showOrder, + 'variablesDescriptions' => $variablesDescriptions ]); } diff --git a/wcfsetup/install/files/lib/data/ad/Ad.class.php b/wcfsetup/install/files/lib/data/ad/Ad.class.php index 321130a57a..7f4a9ad07d 100644 --- a/wcfsetup/install/files/lib/data/ad/Ad.class.php +++ b/wcfsetup/install/files/lib/data/ad/Ad.class.php @@ -3,9 +3,11 @@ namespace wcf\data\ad; 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. @@ -56,4 +58,32 @@ class Ad extends DatabaseObject implements IRouteController { 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; + } } diff --git a/wcfsetup/install/files/lib/system/ad/AdHandler.class.php b/wcfsetup/install/files/lib/system/ad/AdHandler.class.php index 79703f05b8..c671e9d45b 100644 --- a/wcfsetup/install/files/lib/system/ad/AdHandler.class.php +++ b/wcfsetup/install/files/lib/system/ad/AdHandler.class.php @@ -62,7 +62,7 @@ class AdHandler extends SingletonFactory { } } - $output .= '
' . $ad->ad . '
'; + $output .= '
' . $ad->getHtmlCode() . '
'; if (ENABLE_AD_ROTATION) break; } diff --git a/wcfsetup/install/files/lib/system/ad/location/AbstractAdLocation.class.php b/wcfsetup/install/files/lib/system/ad/location/AbstractAdLocation.class.php new file mode 100644 index 0000000000..dac08bafda --- /dev/null +++ b/wcfsetup/install/files/lib/system/ad/location/AbstractAdLocation.class.php @@ -0,0 +1,14 @@ + + * @package WoltLabSuite\Core\System\Ad\Location + * @since 5.2 + */ +abstract class AbstractAdLocation implements IAdLocation {} diff --git a/wcfsetup/install/files/lib/system/ad/location/IAdLocation.class.php b/wcfsetup/install/files/lib/system/ad/location/IAdLocation.class.php new file mode 100644 index 0000000000..dd8afabdd4 --- /dev/null +++ b/wcfsetup/install/files/lib/system/ad/location/IAdLocation.class.php @@ -0,0 +1,32 @@ + + * @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 (`
  • `) 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); +} diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 0d6341fe3b..22c7dd9480 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -20,7 +20,10 @@ - + +
  • {literal}{$username}{/literal} durch den Namen des aktiven Benutzers oder durch „Gast“
  • +]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 0b63e1611d..f68a58e968 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -20,7 +20,10 @@ - + +
  • {literal}{$username}{/literal} with the name of the active user or “Guest”
  • +]]>
    -- 2.20.1