Commit 8d820c29 authored by Stefan Busemann's avatar Stefan Busemann
Browse files

[TASK] remove userprofile Extension

parent 180dd809
Pipeline #3134 failed with stages
in 3 minutes and 31 seconds
<?php
declare(strict_types=1);
namespace T3o\Userprofile\Controller;
use T3o\Userprofile\Domain\Model\UserProfile;
use T3o\Userprofile\Domain\Repository\UserProfileRepository;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
/**
* Class UserController
*/
class UserProfileController extends ActionController
{
/**
* @var UserProfileRepository
* */
public $userProfileRepository;
/**
* @var persistenceManager
* */
public $persistenceManager;
/**
* @param UserProfileRepository $userProfileRepository
*/
public function injectUserProfileRepository(UserProfileRepository $userProfileRepository)
{
$this->userProfileRepository = $userProfileRepository;
}
/**
* @param PersistenceManager $persistenceManager
*/
public function injectPersistenceManager(PersistenceManager $persistenceManager)
{
$this->persistenceManager = $persistenceManager;
}
/**
* @param \T3o\Userprofile\Domain\Model\Userprofile $userProfile
*
* @return void
*
* @ignoevalidation $userProfile
*/
public function showAction(UserProfile $userProfile = null)
{
if (!$userProfile) {
if ($GLOBALS['TSFE']->fe_user->user['uid'] > 0) {
$userProfile = $this->userProfileRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
if (!$userProfile) {
$this->addFlashMessage('Something went wrong. We were not able to find your profile found!',
'Profile can not be shown', AbstractMessage::ERROR);
}
$this->view->assign('showEditButton', true);
} else {
$this->addFlashMessage('You need to be logged in!', 'Profile can not be shown', AbstractMessage::ERROR);
}
}
// display a profile of a user
$this->view->assign('userProfile', $userProfile);
$this->view->assign('privacySettings', $userProfile->getCompiledPrivacySettings($this->settings['privacy']));
// render an edit button, if the current user is logged in
}
/**
* @param \T3o\Userprofile\Domain\Model\Userprofile $userProfile
*
* @return void
*
*/
public function changeProfileVisibilityAction(UserProfile $userProfile)
{
if ($userProfile->isPublicProfile()) {
$userProfile->setPublicProfile(false);
$this->addFlashMessage('Your profile is now hidden.', 'Profile visibility', AbstractMessage::INFO);
} else {
$userProfile->setPublicProfile(true);
$this->addFlashMessage('Your profile is now visible to the public.', 'Profile visibility',
AbstractMessage::OK);
}
$this->userProfileRepository->update($userProfile);
}
/**
* @param \T3o\Userprofile\Domain\Model\Userprofile $userProfile
*
* @return void
*
*/
public function privacyEditAction(UserProfile $userProfile)
{
$this->view->assign('userProfile', $userProfile);
$this->view->assign('privacySettings', $userProfile->getCompiledPrivacySettings($this->settings['privacy']));
}
/**
* @param \T3o\Userprofile\Domain\Model\UserProfile $userProfile
*
* @return void
*
*/
public function privacyUpdateAction(UserProfile $userProfile)
{
// process privacy settings
$userProfile->compilePrivacySettings($this->request->getArgument('privacy'),$this->settings['privacy']);
$this->userProfileRepository->update($userProfile);
$this->addFlashMessage('Your privacy settings were updated', 'Success', AbstractMessage::OK);
$this->redirect('show');
}
/**
*
* @param \T3o\Userprofile\Domain\Model\UserProfile $userProfile
*
* @return void
*
*/
public function editAction(UserProfile $userProfile)
{
$this->view->assign('userProfile', $userProfile);
}
/**
* @param \T3o\Userprofile\Domain\Model\UserProfile $userProfile
*
* @return void
*
* @ignoevalidation $userProfile
*/
public function updateAction(UserProfile $userProfile)
{
// store changes in database
$this->userProfileRepository->update($userProfile);
$this->persistenceManager->persistAll();
$this->addFlashMessage('Your profile was updated.');
$this->redirect('show');
// @todo clear cache entry for this entry
}
}
<?php
declare(strict_types=1);
namespace T3o\Userprofile\Controller;
use T3o\Userprofile\Domain\Model\UserProfile;
use T3o\Userprofile\Domain\Repository\UserProfileRepository;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* Class ListUserProfileController
*/
class UserProfileListController extends ActionController
{
/**
* @var UserProfileRepository
* */
public $userProfileRepository;
/**
* @param UserProfileRepository $userProfileRepository
*/
public function injectUserProfileRepository(UserProfileRepository $userProfileRepository)
{
$this->userProfileRepository = $userProfileRepository;
}
/**
*
*/
public function listAction()
{
$allUsers = $this->userProfileRepository->findAll();
$this->view->assign('frontendUsers', $allUsers);
}
}
<?php
declare(strict_types=1);
namespace T3o\Userprofile\Domain\Model;
use In2code\Femanager\Domain\Model\User;
/**
* Class UserProfile
*/
class UserProfile extends User
{
/**
* $privacySettings
*
* @var string
*/
public $privacySettings;
/**
* $realName
*
* @var string
*/
public $aboutMe;
/**
* $publicProfile
*
* @var boolean
*/
public $publicProfile = false;
/**
* @return array
*/
public function getPrivacySettings()
{
return json_decode($this->privacySettings, true);
}
/**
* @param array $defaultPrivacySettings
* @return array
*/
public function getCompiledPrivacySettings($defaultPrivacySettings = array())
{
// example data
// public => '0' (1 chars)
// authenticated => '0' (1 chars)
// groups => '0' (1 chars)
$defaultSettingsTemplate = $defaultPrivacySettings['_default'];
$currentPrivacySettings = $this->getPrivacySettings();
foreach ($defaultPrivacySettings as $defaultPrivacySetting => $defaultPrivacySettingValue) {
if ($defaultPrivacySetting <> '_default' AND $defaultPrivacySettingValue == 1) {
foreach ($defaultSettingsTemplate as $defaultSettingTemplateKey => $defaultSettingTemplateValue) {
// set the default value of the TS setting
$compiledPrivacySettings[$defaultPrivacySetting][$defaultSettingTemplateKey] = $defaultSettingTemplateValue;
// check if there is an existing value
if ($currentPrivacySettings[$defaultPrivacySetting][$defaultSettingTemplateKey]) {
$compiledPrivacySettings[$defaultPrivacySetting][$defaultSettingTemplateKey] = (int)$currentPrivacySettings[$defaultPrivacySetting][$defaultSettingTemplateKey];
}
}
}
}
return $compiledPrivacySettings;
}
/**
* @param array $newPrivacySettings
* @param array $defaultPrivacySettings
* @return boolean
*/
public function compilePrivacySettings($newPrivacySettings = array(), $defaultPrivacySettings = array())
{
// example data
// public => '0' (1 chars)
// authenticated => '0' (1 chars)
// groups => '0' (1 chars)
$defaultSettingsTemplate = $defaultPrivacySettings['_default'];
foreach ($defaultPrivacySettings as $defaultPrivacySetting => $defaultPrivacySettingValue) {
if ($defaultPrivacySetting <> '_default' AND $defaultPrivacySettingValue == 1) {
foreach ($defaultSettingsTemplate as $defaultSettingTemplateKey => $defaultSettingTemplateValue) {
// set the default value of the TS setting
$compiledPrivacySettings[$defaultPrivacySetting][$defaultSettingTemplateKey] = 0;
// check if there is an existing value
if ($newPrivacySettings[$defaultPrivacySetting . '.' . $defaultSettingTemplateKey]) {
$compiledPrivacySettings[$defaultPrivacySetting][$defaultSettingTemplateKey] = 1;
}
}
}
}
$this->setPrivacySettings($compiledPrivacySettings);
return true;
}
/**
* @param array $privacySettings
*/
public function setPrivacySettings($privacySettings)
{
$this->privacySettings = json_encode($privacySettings);
}
/**
* @return string
*/
public function getAboutMe()
{
return $this->aboutMe;
}
/**
* @param string $aboutMe
*/
public function setAboutme($aboutMe)
{
$this->aboutMe = $aboutMe;
}
/**
* @return bool
*/
public function isPublicProfile(): bool
{
return $this->publicProfile;
}
/**
* @param bool $publicProfile
*/
public function setPublicProfile(bool $publicProfile = false)
{
$this->publicProfile = $publicProfile;
}
/**
* @param string $propertyName
*/
public function showProperty($propertyName = '')
{
// get privacy settings for this property
$privacySettingsArray = $this->getPrivacySettings();
if (is_array($privacySettingsArray[$propertyName])) {
$public = (int)$privacySettingsArray[$propertyName]['public'];
$authenticated = (int)$privacySettingsArray[$propertyName]['authenticated'];
$groups = (int)$privacySettingsArray[$propertyName]['groups'];
// get context
// User is logged in?
if ($GLOBALS['TSFE']->fe_user->user['uid'] > 0) {
// groups context
if ($groups) {
// check if usergroups match
$useroroups = $this->getUsergroup();
// @todo exclude groups from comparision, if they are added for all users
foreach ($useroroups as $userqroup) {
$userGroupArray[] = $userqroup->uid;
}
$feUserGroups = explode(',',$GLOBALS['TSFE']->fe_user->user['usergroup']);
foreach ($feUserGroups as $feUserGroup) {
if (in_array($feUserGroup,$userGroupArray)) {
return true;
}
}
}
// authenticated context
if ($authenticated) {
return true;
}
} else {
// we are in public context
if ($public) {
return true;
} else {
return false;
}
}
#if ($user == true) {
# } else {
#}
}
return false;
}
}
<?php
declare(strict_types=1);
namespace T3o\Userprofile\Domain\Repository;
use T3o\userprofile\Domain\Model\UserProfile;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* Class UserProfileRepository
*/
class UserProfileRepository extends Repository
{
/**
* Overload Find by UID to also get hidden records
*
* @param int $uid fe_users UID
* @return UserProfile
*/
public function findByUid($uid)
{
$query = $this->createQuery();
$and = [$query->equals('uid', $uid)];
/** @var UserProfile $user */
$user = $query->matching($query->logicalAnd($and))->execute()->getFirst();
return $user;
}
/**
* Find users by commaseparated usergroup list
*
* @param string $userGroupList commaseparated list of usergroup uids
* @param array $settings Flexform and TypoScript Settings
* @param array $filter Filter Array
* @return QueryResultInterface|array
*/
public function findByUsergroups($userGroupList, $settings, $filter)
{
$query = $this->createQuery();
// where
$and = [
$query->greaterThan('uid', 0)
];
if (!empty($userGroupList)) {
$selectedUsergroups = GeneralUtility::trimExplode(',', $userGroupList, true);
$logicalOr = [];
foreach ($selectedUsergroups as $group) {
$logicalOr[] = $query->contains('usergroup', $group);
}
$and[] = $query->logicalOr($logicalOr);
}
if (!empty($filter['searchword'])) {
$searchwords = GeneralUtility::trimExplode(' ', $filter['searchword'], true);
$fieldsToSearch = GeneralUtility::trimExplode(
',',
$settings['list']['filter']['searchword']['fieldsToSearch'],
true
);
foreach ($searchwords as $searchword) {
$logicalOr = [];
foreach ($fieldsToSearch as $searchfield) {
$logicalOr[] = $query->like($searchfield, '%' . $searchword . '%');
}
$and[] = $query->logicalOr($logicalOr);
}
}
$query->matching($query->logicalAnd($and));
// sorting
$sorting = QueryInterface::ORDER_ASCENDING;
if ($settings['list']['sorting'] === 'desc') {
$sorting = QueryInterface::ORDER_DESCENDING;
}
$field = preg_replace('/[^a-zA-Z0-9_-]/', '', $settings['list']['orderby']);
$query->setOrderings([$field => $sorting]);
// set limit
if ((int)$settings['list']['limit'] > 0) {
$query->setLimit((int)$settings['list']['limit']);
}
$users = $query->execute();
return $users;
}
}
<?php
declare(strict_types=1);
namespace T3o\Userprofile\ViewHelpers;
use T3o\Userprofile\Domain\Model\UserProfile;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* Class IsPropertyVisibleViewHelper
*/
class IsPropertyVisibleViewHelper extends AbstractViewHelper
{
/**
* Check if a property of the userprofile is visible in the current context
*
* @param string $propertyName
* @param UserProfile $userProfile
* @return bool
*/
public function render(string $propertyName, UserProfile $userProfile)
{
// return always all fields for the current user
if ($GLOBALS['TSFE']->fe_user->user['uid']==$userProfile->getUid()) {
return true;
}
return $userProfile->showProperty($propertyName);
}
}
<?php
/**
* Table configuration fe_users
*/
$feUsersColumns = [
'privacy_settings' => [
'exclude' => 0,
'label' => 'Privacy settings',
'config' => [
'type' => 'text',
]
],
'about_me' => [
'exclude' => 0,
'label' => 'about me',
'config' => [
'type' => 'text',
]
],
'public_profile' => [
'exclude' => 0,
'label' => 'show user profile',
'config' => [
'type' => 'check',
]
],
];
$fields = 'privacy_settings,about_me,';
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'fe_users',
'privacy_settings,about_me,public_profile',
'',
'after:name'
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('fe_users', $feUsersColumns);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'fe_users',
'--div--;LLL:EXT:femanager/Resources/Private/Language/locallang_db.xlf:fe_users.tab, ' . $fields
);
plugin.tx_userprofile {
view {
# cat=plugin.tx_userprofile/file; type=string; label=Path to template root (FE)
templateRootPath = EXT:tx_userprofile/Resources/Private/Templates/
# cat=plugin.tx_userprofile/file; type=string; label=Path to template partials (FE)
partialRootPath = EXT:tx_userprofile/Resources/Private/Partials/
# cat=plugin.tx_userprofile/file; type=string; label=Path to template layouts (FE)
layoutRootPath = EXT:tx_userprofile/Resources/Private/Layouts/
}
persistence {
# cat=plugin.tx_userprofile//a; type=string; label=Default storage PID
storagePid =
}
}
plugin.tx_userprofile {
view {
templateRootPath = {$plugin.tx_userprofile.view.templateRootPath}