/
ExtendedMemberInserttags.php
195 lines (179 loc) · 6.17 KB
/
ExtendedMemberInserttags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Contao Open Source CMS
* Copyright (C) 2005-2015 Leo Feyer
*
* Formerly known as TYPOlight Open Source CMS.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at <http://www.gnu.org/licenses/>.
*
* PHP version 5
* @copyright Cliff Parnitzky 2012-2019
* @author Cliff Parnitzky
* @package ExtendedMemberInserttags
* @license LGPL
*/
/**
* Run in a custom namespace, so the class can be replaced
*/
namespace ExtendedMemberInserttags;
/**
* Class ExtendedMemberInserttags
*
* InsertTag hook class.
* @copyright Cliff Parnitzky 2012-2019
* @author Cliff Parnitzky
* @package Controller
*/
class ExtendedMemberInserttags extends \Controller
{
public function __construct()
{
parent::__construct();
}
/**
* Replaces the additional member inserttags
*/
public function replaceExtendedMemberInserttags($strTag)
{
$strTag = explode('::', $strTag);
switch ($strTag[0])
{
case 'member':
$member = null;
$attributeIndex = 0;
if (is_numeric($strTag[1]) && intval($strTag[1]) > 0)
{
$objMember = \MemberModel::findById($strTag[1]);
if ($objMember !== null)
{
$member = $objMember;
}
$attributeIndex = 2;
}
elseif (FE_USER_LOGGED_IN)
{
$this->import('FrontendUser', 'User');
$member = $this->User;
$attributeIndex = 1;
}
if ($member != null)
{
$value = $member->{$strTag[$attributeIndex]};
$this->loadDataContainer('tl_member');
if ($GLOBALS['TL_DCA']['tl_member']['fields'][$strTag[$attributeIndex]]['inputType'] == 'password')
{
// do not allow extracting the password
return "";
}
$value = deserialize($value);
$rgxp = $GLOBALS['TL_DCA']['tl_member']['fields'][$strTag[$attributeIndex]]['eval']['rgxp'];
$opts = $GLOBALS['TL_DCA']['tl_member']['fields'][$strTag[$attributeIndex]]['options'];
$rfrc = $GLOBALS['TL_DCA']['tl_member']['fields'][$strTag[$attributeIndex]]['reference'];
$fkey = $GLOBALS['TL_DCA']['tl_member']['fields'][$strTag[$attributeIndex]]['foreignKey'];
$returnValue = '';
if ($rgxp == 'date' || $rgxp == 'time' || $rgxp == 'datim')
{
$dateFormat = $GLOBALS['TL_CONFIG'][$rgxp . 'Format'];
// check if custom format was set
if (count($strTag) == $attributeIndex + 2 && strlen($strTag[$attributeIndex + 1]) > 0)
{
$dateFormat = $strTag[$attributeIndex + 1];
}
$returnValue = $this->parseDate($dateFormat, $value);
}
elseif (is_array($value))
{
$returnValue = implode(', ', $value);
if (strlen($fkey) > 0)
{
$returnValue = $this->getArrayValueAsList($fkey, $returnValue, $strTag, $attributeIndex);
}
}
elseif (is_array($opts) && array_is_assoc($opts))
{
$returnValue = isset($opts[$value]) ? $opts[$value] : $value;
}
elseif (is_array($rfrc))
{
$returnValue = isset($rfrc[$value]) ? ((is_array($rfrc[$value])) ? $rfrc[$value][0] : $rfrc[$value]) : $value;
}
elseif ($strTag[$attributeIndex] == 'age')
{
$returnValue = floor((date("Ymd") - date("Ymd", $member->dateOfBirth)) / 10000);
}
elseif ($strTag[$attributeIndex] == 'name')
{
$returnValue = $member->firstname . " " . $member->lastname;
}
elseif ($strTag[$attributeIndex] == 'salutation')
{
$returnValue = $GLOBALS['TL_LANG']['MSC'][$strTag[$attributeIndex] . '_' . $member->gender];
if (strlen($returnValue) == 0)
{
return false;
}
}
elseif ($strTag[$attributeIndex] == 'welcoming')
{
$key = $strTag[$attributeIndex] . '_formally';
if (count($strTag) == $attributeIndex + 2 && strlen($strTag[$attributeIndex + 1]) > 0 && $strTag[$attributeIndex + 1] == 'personally')
{
$key = $strTag[$attributeIndex] . '_personally';
}
$returnValue = $GLOBALS['TL_LANG']['MSC'][$key . '_' . $member->gender];
if (strlen($returnValue) == 0)
{
$returnValue = $GLOBALS['TL_LANG']['MSC'][$key];
}
}
else
{
$returnValue = $value;
}
// Convert special characters (see #1890)
return specialchars($returnValue);
}
}
return false;
}
/**
* Get all values of the given array
*/
private function getArrayValueAsList($foreignKey, $valueIds, $strTag, $attributeIndex)
{
$foreignKey = explode('.', $foreignKey);
$table = $foreignKey[0];
$fieldname = $foreignKey[1];
if (strlen($table) > 0 && strlen($valueIds) > 0)
{
$values = \Database::getInstance()->prepare("SELECT " . $fieldname . " FROM " . $table . " WHERE id IN (" . $valueIds . ") ORDER BY name ASC")
->execute();
$list = array();
while ($values->next())
{
$list[] = $values->$fieldname;
}
$separator = ", ";
if (count($strTag) == $attributeIndex + 2 && strlen($strTag[$attributeIndex + 1]) > 0)
{
$separator = $strTag[$attributeIndex + 1];
}
return implode($separator, $list);
}
return "";
}
}
?>