Browse code

[Update] Added noAvatar to ModuleAvatar

Sebastian Zoglowek authored on26/02/2022 03:06:50
Showing2 changed files
... ...
@@ -1,30 +1,50 @@
1 1
 {
2
-  "name":"oveleon/contao-member-extension-bundle",
3
-  "type":"contao-bundle",
4
-  "description":"Member feature extension for Contao.",
5
-  "keywords":["contao","member-extension-bundle"],
6
-  "homepage":"https://oveleon.de/",
7
-  "license":"MIT",
8
-  "authors":[
2
+  "name": "oveleon/contao-member-extension-bundle",
3
+  "type": "contao-bundle",
4
+  "description": "Member feature extension for Contao.",
5
+  "keywords": ["contao","member-extension-bundle"],
6
+  "homepage": "https://oveleon.de/",
7
+  "license": "MIT",
8
+  "authors": [
9 9
     {
10
-      "name":"Oveleon",
11
-      "homepage":"https://oveleon.de/",
10
+      "name": "Oveleon",
11
+      "homepage": "https://oveleon.de/",
12
+      "role": "Developer"
13
+    },
14
+    {
15
+      "name": "Daniele Sciannimanica",
16
+      "homepage": "https://github.com/doishub",
12 17
       "role":"Developer"
18
+    },
19
+    {
20
+      "name": "Fabian Ekert",
21
+      "homepage": "https://github.com/eki89",
22
+      "role": "Developer"
23
+    },
24
+    {
25
+      "name": "Sebastian Zoglowek",
26
+      "homepage": "https://github.com/zoglo",
27
+      "role": "Developer"
13 28
     }
14 29
   ],
15
-  "require":{
16
-    "php":">=7.1",
17
-    "contao/core-bundle":"^4.4"
30
+  "require": {
31
+    "php": "^7.4 || ^8.0",
32
+    "contao/core-bundle":"^4.9"
18 33
   },
19 34
   "require-dev": {
20
-    "contao/manager-plugin": "^2.0"
35
+    "contao/manager-plugin": "^2.3.1"
21 36
   },
22 37
   "conflict": {
23 38
     "contao/core": "*",
24
-    "contao/core-bundle": "4.4.1",
25 39
     "contao/manager-plugin": "<2.0 || >=3.0"
26 40
   },
27
-  "autoload":{
41
+  "extra": {
42
+    "branch-alias": {
43
+      "dev-master": "1.2.x-dev"
44
+    },
45
+    "contao-manager-plugin": "Oveleon\\ContaoMemberExtensionBundle\\ContaoManager\\Plugin"
46
+  },
47
+  "autoload": {
28 48
     "psr-4": {
29 49
       "Oveleon\\ContaoMemberExtensionBundle\\": "src/"
30 50
     },
... ...
@@ -38,7 +58,7 @@
38 58
       "src/Resources/contao/templates/"
39 59
     ]
40 60
   },
41
-  "extra":{
42
-    "contao-manager-plugin": "Oveleon\\ContaoMemberExtensionBundle\\ContaoManager\\Plugin"
61
+  "support": {
62
+    "issues": "https://github.com/oveleon/contao-member-extension-bundle/issues"
43 63
   }
44 64
 }
45 65
\ No newline at end of file
... ...
@@ -1,105 +1,119 @@
1 1
 <?php
2 2
 
3
+declare(strict_types=1);
4
+
3 5
 /*
4 6
  * This file is part of Oveleon ContaoMemberExtension Bundle.
5 7
  *
6
- * (c) https://www.oveleon.de/
8
+ * @package     contao-member-extension-bundle
9
+ * @license     MIT
10
+ * @author      Daniele Sciannimanica   <https://github.com/doishub>
11
+ * @author      Fabian Ekert            <https://github.com/eki89>
12
+ * @author      Sebastian Zoglowek      <https://github.com/zoglo>
13
+ * @copyright   Oveleon                 <https://www.oveleon.de/>
7 14
  */
8 15
 
9 16
 namespace Oveleon\ContaoMemberExtensionBundle;
10 17
 
11
-use Patchwork\Utf8;
18
+use Contao\BackendTemplate;
19
+use Contao\FilesModel;
20
+use Contao\FrontendUser;
21
+use Contao\MemberModel;
22
+use Contao\Module;
23
+use Contao\System;
12 24
 
13 25
 /**
14 26
  * Class ModuleAvatar
15 27
  *
16 28
  * @author Fabian Ekert <fabian@oveleon.de>
29
+ * @author Sebastian Zoglowek <https://github.com/zoglo>
17 30
  */
18
-class ModuleAvatar extends \Module
31
+class ModuleAvatar extends Module
19 32
 {
33
+    /**
34
+     * Template.
35
+     *
36
+     * @var string
37
+     */
38
+    protected $strTemplate = 'member_avatar';
39
+
40
+    /**
41
+     * Default avatar file path
42
+     *
43
+     * @var string
44
+     */
45
+    private static $strDefaultPath = 'bundles/contaomemberextension/avatar.png';
46
+
47
+    /**
48
+     * Return a wildcard in the back end
49
+     *
50
+     * @return string
51
+     */
52
+    public function generate()
53
+    {
54
+        $request = System::getContainer()->get('request_stack')->getCurrentRequest();
55
+
56
+        if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
57
+        {
58
+            $objTemplate = new BackendTemplate('be_wildcard');
59
+            $objTemplate->wildcard = '### ' . mb_strtoupper($GLOBALS['TL_LANG']['FMD']['avatar'][0], 'UTF-8') . ' ###';
60
+            $objTemplate->title = $this->headline;
61
+            $objTemplate->id = $this->id;
62
+            $objTemplate->link = $this->name;
63
+            $objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;
64
+
65
+            return $objTemplate->parse();
66
+        }
20 67
 
21
-	/**
22
-	 * Template
23
-	 * @var string
24
-	 */
25
-	protected $strTemplate = 'member_avatar';
26
-
27
-	/**
28
-	 * Return a wildcard in the back end
29
-	 *
30
-	 * @return string
31
-	 */
32
-	public function generate()
33
-	{
34
-		if (TL_MODE == 'BE')
35
-		{
36
-			/** @var BackendTemplate|object $objTemplate */
37
-			$objTemplate = new \BackendTemplate('be_wildcard');
38
-
39
-			$objTemplate->wildcard = '### ' . Utf8::strtoupper($GLOBALS['TL_LANG']['FMD']['avatar'][0]) . ' ###';
40
-			$objTemplate->title = $this->headline;
41
-			$objTemplate->id = $this->id;
42
-			$objTemplate->link = $this->name;
43
-			$objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;
44
-
45
-			return $objTemplate->parse();
46
-		}
47
-
48
-		// Return if user is not logged in
49
-		if (!FE_USER_LOGGED_IN)
50
-		{
51
-			return '';
52
-		}
53
-
54
-		if ($this->memberTpl != '')
55
-		{
56
-			$this->strTemplate = $this->memberTpl;
57
-		}
58
-
59
-		return parent::generate();
60
-	}
61
-
62
-	/**
63
-	 * Generate the module
64
-	 */
65
-	protected function compile()
66
-	{
67
-        $this->size = $this->imgSize;
68
-
69
-        $this->import('FrontendUser', 'User');
68
+        // Return if user is not logged in
69
+        $tokenChecker = System::getContainer()->get('contao.security.token_checker');
70
+        $blnFeUserLoggedIn = $tokenChecker->hasFrontendUser();
70 71
 
71
-        if ($this->User->avatar == '' && \Config::get('defaultAvatar') == '')
72
+        if (!$blnFeUserLoggedIn)
72 73
         {
73 74
             return '';
74 75
         }
75 76
 
76
-        if ($this->User->avatar == '')
77
-        {
78
-            $objFile = \FilesModel::findByUuid(\Config::get('defaultAvatar'));
77
+        $this->strTemplate = $this->memberTpl ?: 'member_avatar';
79 78
 
80
-            if ($objFile === null || !is_file(TL_ROOT . '/' . $objFile->path))
81
-            {
82
-                return '';
83
-            }
79
+        return parent::generate();
80
+    }
84 81
 
85
-            $this->singleSRC = $objFile->path;
82
+    /**
83
+     * Generate the module
84
+     */
85
+    protected function compile()
86
+    {
87
+        $objTemplate = $this->Template;
86 88
 
87
-            $this->addImageToTemplate($this->Template, $this->arrData);
88
-            return;
89
-        }
89
+        $this->size = $this->imgSize;
90 90
 
91
-        $objFile = \FilesModel::findByUuid($this->User->avatar);
91
+        $objTemplate->noAvatar = true;
92 92
 
93
-        if ($objFile === null || !is_file(TL_ROOT . '/' . $objFile->path))
94
-        {
95
-            $this->singleSRC = \FilesModel::findByUuid(\Config::get('defaultAvatar'))->path;
93
+        $this->import(FrontendUser::class, 'User');
94
+        $objMember = MemberModel::findByPk($this->User->id);
95
+
96
+        $projectDir = System::getContainer()->getParameter('kernel.project_dir');
96 97
 
97
-            $this->addImageToTemplate($this->Template, $this->arrData);
98
-            return;
99
-        }
100 98
 
101
-        $this->singleSRC = $objFile->path;
99
+        if (!$this->User->avatar)
100
+        {
101
+            $objTemplate->singleSRC = self::$strDefaultPath;
102
+        }
103
+        else
104
+        {
105
+            $objFile = FilesModel::findByUuid($this->User->avatar);
102 106
 
103
-        $this->addImageToTemplate($this->Template, $this->arrData, null, null, $objFile);
104
-	}
107
+            if ($objFile === null || !is_file($projectDir . '/' . $objFile->path))
108
+            {
109
+                $objTemplate->singleSRC = self::$strDefaultPath;
110
+            }
111
+            else
112
+            {
113
+                $objTemplate->noAvatar = false;
114
+                $this->singleSRC = $objFile->path;
115
+                $this->addImageToTemplate($this->Template, $this->arrData);
116
+            }
117
+        }
118
+    }
105 119
 }