Commit 93d01f96 authored by mabolek's avatar mabolek Committed by Thomas Löffler
Browse files

[FEATURE] Example karma generation when new user is created.

Please note: Must still be tested with femanager hooks and real user creation, but serves as a good example of how to implement karma integrations.
parent 5740e51f
......@@ -12,6 +12,16 @@ plugin.tx_karma {
# uncomment the following line to enable the new Property Mapper.
# rewrittenPropertyMapper = 1
}
settings {
userProfileChangeKarmaIssuer {
newUserWasCreated {
sourceCode = usercreated
defaultSourceLabel = Created my.typo3.org User
valueEarned = 10
}
}
}
}
config.tx_extbase {
......
......@@ -36,7 +36,7 @@ defined('TYPO3_MODE') or die('Access denied.');
//Add karma to users when they are created
/** @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher */
$signalSlotDispatcher = t3lib_div::makeInstance('TYPO3\CMS\Extbase\SignalSlot\Dispatcher');
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\SignalSlot\Dispatcher');
$signalSlotDispatcher->connect(
\In2code\Femanager\Controller\AbstractController::class,
'finalCreateAfterPersist',
......
......@@ -2,6 +2,10 @@
namespace T3o\Karma\Controller;
use T3o\Karma\Utility\UserProfileChangeKarmaIssuer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/***************************************************************
* Copyright notice
*
......@@ -34,12 +38,11 @@ class ManagerController extends AbstractController
{
$frontendUser = $this->frontendUserRepository->findByUid(1);
$karmaSource = $this->karmaSourceRepository->findByUid(1);
$this->karmaService->addKarmaToUser(12, $frontendUser, $karmaSource);
$valueReturned = $this->karmaService->getImmutableKarmaForUser($frontendUser, true, true);
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var UserProfileChangeKarmaIssuer $testUserProfileChangeKarmaIssuerSlot */
$testUserProfileChangeKarmaIssuerSlot = $objectManager->get(UserProfileChangeKarmaIssuer::class);
var_dump($valueReturned);
$testUserProfileChangeKarmaIssuerSlot->newUserWasCreated($frontendUser);
}
}
......@@ -3,10 +3,13 @@
namespace T3o\Karma\Utility;
use In2code\Femanager\Controller\AbstractController as In2CodeAbstractController;
use T3o\Karma\Domain\Model\KarmaSource;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
use T3o\Karma\Domain\Repository\KarmaSourceRepository;
use T3o\Karma\Service\KarmaService;
use T3o\Karma\Domain\Repository\FrontendUserRepository;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/***************************************************************
* Copyright notice
......@@ -34,7 +37,7 @@ use T3o\Karma\Domain\Repository\FrontendUserRepository;
class UserProfileChangeKarmaIssuer
{
/**
* @var \T3o\Karma\Domain\Repository\KarmaService
* @var \T3o\Karma\Service\KarmaService
*/
protected $karmaService;
......@@ -49,7 +52,31 @@ class UserProfileChangeKarmaIssuer
protected $karmaSourceRepository;
/**
* @param \T3o\Karma\Domain\Repository\KarmaService $karmaService
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
/**
* @var array
*/
protected $settings;
/**
* UserProfileChangeKarmaIssuer constructor.
*/
public function __construct()
{
$objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
/** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager */
$configurationManager = $objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
$this->settings = $configuration = $configurationManager->getConfiguration(
\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'karma'
);
}
/**
* @param \T3o\Karma\Service\KarmaService $karmaService
*/
public function injectKarmaService(KarmaService $karmaService)
{
......@@ -72,15 +99,41 @@ class UserProfileChangeKarmaIssuer
$this->karmaSourceRepository = $karmaSourceRepository;
}
/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
*/
public function injectObjectManager(ObjectManager $objectManager) {
$this->objectManager = $objectManager;
}
/**
* @param ExtbaseFrontendUser $frontendUser
* @param string $action
* @param In2CodeAbstractController $object
*/
public function newUserWasCreated(ExtbaseFrontendUser $frontendUser, string $action, In2CodeAbstractController $object)
public function newUserWasCreated(ExtbaseFrontendUser $frontendUser)
{
$frontendUser = $this->frontendUserRepository->findByUid($frontendUser->getUid());
$karmaSourceCode = $this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['sourceCode'];
if ($karmaSourceCode == '') {
throw new \Exception('Empty karma source code supplied. Please configure it in TypoScript.', 1541508560);
}
$karmaSource = $this->karmaSourceRepository->findByCode($karmaSourceCode)->getFirst();
if ($karmaSource === null) {
/** @var KarmaSource $karmaSource */
$karmaSource = new KarmaSource();
$karmaSource->setCode($karmaSourceCode);
$karmaSource->setTitle($this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['defaultSourceLabel']);
$this->karmaSourceRepository->add($karmaSource);
}
$this->karmaService->addKarmaToUser(
$this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['valueEarned'],
$frontendUser,
$karmaSource
);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment