Skip to content

Commit

Permalink
Initial commit: Frontend module member avatar added
Browse files Browse the repository at this point in the history
  • Loading branch information
eki89 committed Mar 8, 2019
1 parent 0320bd3 commit 9b9291b
Show file tree
Hide file tree
Showing 14 changed files with 571 additions and 0 deletions.
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name":"oveleon/contao-member-extension-bundle",
"type":"contao-bundle",
"description":"Member feature extension for Contao.",
"keywords":["contao","member-extension-bundle"],
"homepage":"https://oveleon.de/",
"license":"MIT",
"authors":[
{
"name":"Oveleon",
"homepage":"https://oveleon.de/",
"role":"Developer"
}
],
"require":{
"php":">=7.1",
"contao/core-bundle":"^4.4"
},
"require-dev": {
"contao/manager-plugin": "^2.0"
},
"conflict": {
"contao/core": "*",
"contao/core-bundle": "4.4.1",
"contao/manager-plugin": "<2.0 || >=3.0"
},
"autoload":{
"psr-4": {
"Oveleon\\ContaoMemberExtensionBundle\\": "src/"
},
"classmap": [
"src/Resources/contao/"
],
"exclude-from-classmap": [
"src/Resources/contao/config/",
"src/Resources/contao/dca/",
"src/Resources/contao/languages/",
"src/Resources/contao/templates/"
]
},
"extra":{
"contao-manager-plugin": "Oveleon\\ContaoMemberExtensionBundle\\ContaoManager\\Plugin"
}
}
33 changes: 33 additions & 0 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of Oveleon ContaoMemberExtension Bundle.
*
* (c) https://www.oveleon.de/
*/

namespace Oveleon\ContaoMemberExtensionBundle\ContaoManager;

use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use Oveleon\ContaoMemberExtensionBundle\ContaoMemberExtensionBundle;
use Symfony\Component\HttpKernel\KernelInterface;

class Plugin implements BundlePluginInterface
{
/**
* {@inheritdoc}
*/
public function getBundles(ParserInterface $parser): array
{
return [
BundleConfig::create(ContaoMemberExtensionBundle::class)
->setLoadAfter([ContaoCoreBundle::class])
->setReplace(['contao-member-extension-bundle']),
];
}
}
17 changes: 17 additions & 0 deletions src/ContaoMemberExtensionBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file is part of Oveleon ContaoMemberExtension Bundle.
*
* (c) https://www.oveleon.de/
*/

namespace Oveleon\ContaoMemberExtensionBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ContaoMemberExtensionBundle extends Bundle
{
}
202 changes: 202 additions & 0 deletions src/Resources/contao/classes/Member.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Oveleon\ContaoMemberExtensionBundle;

/**
* Class Member
*
* @author Fabian Ekert <[email protected]>
*/
class Member extends \Frontend
{
/**
* Update avatar of member
*
* @param \FrontendUser $objUser
* @param array $arrData
*/
public function updateAvatar($objUser, $arrData)
{
$objMember = \MemberModel::findByPk($objUser->id);

if ($objMember === null)
{
return;
}

$file = $_SESSION['FILES']['avatar'];
$maxlength_kb = $this->getMaximumUploadSize();

// Sanitize the filename
try
{
$file['name'] = \StringUtil::sanitizeFileName($file['name']);
}
catch (\InvalidArgumentException $e)
{
// ToDo: Fehler: Dateiname beinhaltet unzulässige Zeichen

return;
}

// Invalid file name
if (!\Validator::isValidFileName($file['name']))
{
// ToDo: Fehler: Dateiname beinhaltet unzulässige Zeichen

return;
}

// File was not uploaded
// ToDo

// File is too big
if ($file['size'] > $maxlength_kb)
{
// ToDo: Fehler: Datei zu groß
unset($_SESSION['FILES']['avatar']);

return;
}

$objFile = new \File($file['name']);
$uploadTypes = \StringUtil::trimsplit(',', \Config::get('validImageTypes'));

// File type is not allowed
if (!\in_array($objFile->extension, $uploadTypes))
{
// ToDo: Fehler: Dateityp nicht erlaubt
unset($_SESSION['FILES']['avatar']);

return;
}

if ($arrImageSize = @getimagesize($file['tmp_name']))
{
$intImageWidth = \Config::get('imageWidth');

// Image exceeds maximum image width
if ($intImageWidth > 0 && $arrImageSize[0] > $intImageWidth)
{
// ToDo: Fehler: Bild ist zu groß in der breite
unset($_SESSION['FILES']['avatar']);

return;
}

$intImageHeight = \Config::get('imageHeight');

// Image exceeds maximum image height
if ($intImageHeight > 0 && $arrImageSize[1] > $intImageHeight)
{
// ToDo: Fehler: Bild ist zu groß in der höhe
unset($_SESSION['FILES']['avatar']);

return;
}

$_SESSION['FILES']['avatar'] = $_SESSION['FILES']['avatar'];

// Overwrite the upload folder with user's home directory
if ($objMember->assignDir && $objMember->homeDir)
{
$intUploadFolder = $objMember->homeDir;
}

$objUploadFolder = \FilesModel::findByUuid($intUploadFolder);

// The upload folder could not be found
if ($objUploadFolder === null)
{
throw new \Exception("Invalid upload folder ID $intUploadFolder");
}

$strUploadFolder = $objUploadFolder->path;

// Store the file if the upload folder exists
if ($strUploadFolder != '' && is_dir(TL_ROOT . '/' . $strUploadFolder))
{
$this->import('Files');

// Move the file to its destination
$this->Files->move_uploaded_file($file['tmp_name'], $strUploadFolder . '/' . $file['name']);
$this->Files->chmod($strUploadFolder . '/' . $file['name'], \Config::get('defaultFileChmod'));

$strUuid = null;
$strFile = $strUploadFolder . '/' . $file['name'];

// Generate the DB entries
if (\Dbafs::shouldBeSynchronized($strFile))
{
$objModel = \FilesModel::findByPath($strFile);

if ($objModel === null)
{
$objModel = \Dbafs::addResource($strFile);
}

$strUuid = \StringUtil::binToUuid($objModel->uuid);

// Update the hash of the target folder
\Dbafs::updateFolderHashes($strUploadFolder);

// Update member avatar
$objMember->avatar = $objModel->uuid;
$objMember->save();
}

// Add the session entry (see #6986)
$_SESSION['FILES']['avatar'] = array
(
'name' => $file['name'],
'type' => $file['type'],
'tmp_name' => TL_ROOT . '/' . $strFile,
'error' => $file['error'],
'size' => $file['size'],
'uploaded' => true,
'uuid' => $strUuid
);

// Add a log entry
$this->log('File "' . $strUploadFolder . '/' . $file['name'] . '" has been uploaded', __METHOD__, TL_FILES);
}
}

unset($_SESSION['FILES']['avatar']);
}

/**
* Return the maximum upload file size in bytes
*
* @return string
*/
protected function getMaximumUploadSize()
{
// Get the upload_max_filesize from the php.ini
$upload_max_filesize = ini_get('upload_max_filesize');

// Convert the value to bytes
if (stripos($upload_max_filesize, 'K') !== false)
{
$upload_max_filesize = round($upload_max_filesize * 1024);
}
elseif (stripos($upload_max_filesize, 'M') !== false)
{
$upload_max_filesize = round($upload_max_filesize * 1024 * 1024);
}
elseif (stripos($upload_max_filesize, 'G') !== false)
{
$upload_max_filesize = round($upload_max_filesize * 1024 * 1024 * 1024);
}

return min($upload_max_filesize, \Config::get('maxFileSize'));
}
}
23 changes: 23 additions & 0 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Oveleon ContaoOnofficeShopTv Bundle.
*
* (c) https://www.oveleon.de/
*/

// Back end modules
$GLOBALS['BE_MOD']['system']['member_settings'] = array
(
'tables' => array('tl_member_settings'),
'hideInNavigation' => true,
);

// Front end modules
array_insert($GLOBALS['FE_MOD']['user'], -1, array
(
'avatar' => '\\Oveleon\\ContaoMemberExtensionBundle\\ModuleAvatar'
));

// Register hooks
$GLOBALS['TL_HOOKS']['updatePersonalData'][] = array('\\Oveleon\\ContaoMemberExtensionBundle\\Member', 'updateAvatar');
35 changes: 35 additions & 0 deletions src/Resources/contao/dca/tl_member.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of Oveleon ContaoMemberExtension Bundle.
*
* (c) https://www.oveleon.de/
*/

// Extend the default palette
Contao\CoreBundle\DataContainer\PaletteManipulator::create()
->addField(array('avatar'), 'personal_legend', Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->applyToPalette('default', 'tl_member')
;

// Add global operations
array_insert($GLOBALS['TL_DCA']['tl_member']['list']['global_operations'], 0, array
(
'settings' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_member']['settings'],
'href' => 'do=member_settings',
'icon' => 'edit.svg',
'attributes' => 'onclick="Backend.getScrollOffset()" accesskey="e"'
)
));

// Add fields to tl_user
$GLOBALS['TL_DCA']['tl_member']['fields']['avatar'] = array
(
'label' => &$GLOBALS['TL_LANG']['tl_member']['avatar'],
'exclude' => true,
'inputType' => 'fileTree',
'eval' => array('feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'personal', 'fieldType'=>'radio', 'filesOnly'=>true, 'isGallery'=>true, 'extensions'=>Config::get('validImageTypes'), 'tl_class'=>'clr'),
'sql' => "binary(16) NULL"
);
35 changes: 35 additions & 0 deletions src/Resources/contao/dca/tl_member_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of Oveleon ContaoMemberExtension Bundle.
*
* (c) https://www.oveleon.de/
*/

$GLOBALS['TL_DCA']['tl_member_settings'] = array
(

// Config
'config' => array
(
'dataContainer' => 'File',
'closed' => true
),

// Palettes
'palettes' => array
(
'default' => '{avatar_legend},defaultAvatar;'
),

// Fields
'fields' => array
(
'defaultAvatar' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_member_settings']['defaultAvatar'],
'inputType' => 'fileTree',
'eval' => array('fieldType'=>'radio', 'filesOnly'=>true, 'isGallery'=>true, 'extensions'=>Config::get('validImageTypes'), 'tl_class'=>'clr')
)
)
);
Loading

0 comments on commit 9b9291b

Please sign in to comment.