| 1 |
<?php |
| 2 |
|
| 3 |
trait UserInsertion |
| 4 |
{ |
| 5 |
private $customFields; |
| 6 |
private $usergroups; |
| 7 |
|
| 8 |
//Keep the sender information to not load them every time |
| 9 |
private $sendervalues = []; |
| 10 |
|
| 11 |
public function dynamicText(?int $mailId): ?object |
| 12 |
{ |
| 13 |
return $this->pluginDescription; |
| 14 |
} |
| 15 |
|
| 16 |
public function textPopup(): void |
| 17 |
{ |
| 18 |
?> |
| 19 |
<script type="text/javascript"> |
| 20 |
let selectedUserDText; |
| 21 |
|
| 22 |
function changeUserTag(tagname) { |
| 23 |
if (!tagname) return; |
| 24 |
|
| 25 |
selectedUserDText = tagname; |
| 26 |
|
| 27 |
let dText; |
| 28 |
const iscf = tagname.toLowerCase().indexOf('custom'); |
| 29 |
|
| 30 |
if (iscf >= 0) { |
| 31 |
dText = '{usertag:' + tagname.substring(0, iscf) + '|type:custom'; |
| 32 |
} else { |
| 33 |
dText = '{usertag:' + tagname; |
| 34 |
} |
| 35 |
|
| 36 |
if (tagname.toLowerCase().indexOf('date') >= 0) { |
| 37 |
dText += '|type:date'; |
| 38 |
} |
| 39 |
dText += '|info:' + jQuery('input[name="typeInfoUser"]:checked').val() + '}'; |
| 40 |
|
| 41 |
setTag(dText, jQuery('#' + tagname + 'option')); |
| 42 |
} |
| 43 |
</script> |
| 44 |
|
| 45 |
<?php |
| 46 |
$isAutomation = acym_getVar('string', 'automation'); |
| 47 |
echo '<div class="acym__popup__listing text-center grid-x">'; |
| 48 |
|
| 49 |
$typeinfo = []; |
| 50 |
$typeinfo[] = acym_selectOption('receiver', 'ACYM_RECEIVER_INFORMATION'); |
| 51 |
$typeinfo[] = acym_selectOption('sender', 'ACYM_SENDER_INFORMATION'); |
| 52 |
if (!empty($isAutomation)) { |
| 53 |
$typeinfo[] = acym_selectOption('current', 'ACYM_USER_TRIGGERING_AUTOMATION'); |
| 54 |
} |
| 55 |
|
| 56 |
acym_radio( |
| 57 |
$typeinfo, |
| 58 |
'typeInfoUser', |
| 59 |
'receiver', |
| 60 |
['onclick' => 'changeUserTag(selectedUserDText)'], |
| 61 |
['containerClass' => 'margin-bottom-1'] |
| 62 |
); |
| 63 |
|
| 64 |
global $acymCmsUserVars; |
| 65 |
$fields = [ |
| 66 |
$acymCmsUserVars->username => 'ACYM_LOGIN_NAME', |
| 67 |
$acymCmsUserVars->name => 'ACYM_USER_NAME', |
| 68 |
$acymCmsUserVars->registered => 'ACYM_REGISTRATION_DATE', |
| 69 |
'groups' => 'ACYM_USER_GROUPS', |
| 70 |
]; |
| 71 |
|
| 72 |
foreach ($fields as $fieldname => $description) { |
| 73 |
echo '<div class="grid-x medium-12 cell acym__row__no-listing acym__listing__row__popup text-left" id="'.esc_attr( |
| 74 |
$fieldname |
| 75 |
).'option" onclick="changeUserTag(\''.esc_attr($fieldname).'\');" > |
| 76 |
<div class="cell medium-6 small-12 acym__listing__title acym__listing__title__dynamics">'.esc_html($fieldname).'</div> |
| 77 |
<div class="cell medium-6 small-12 acym__listing__title acym__listing__title__dynamics">'.esc_html(acym_translation($description)).'</div> |
| 78 |
</div>'; |
| 79 |
} |
| 80 |
|
| 81 |
// Handle joomla custom fields |
| 82 |
if (ACYM_CMS == 'joomla' && ACYM_J37) { |
| 83 |
// Load field groups |
| 84 |
$groups = acym_loadObjectList('SELECT id, title FROM #__fields_groups WHERE context = "com_users.user" AND state = 1 ORDER BY title ASC'); |
| 85 |
$defaultGroup = new stdClass(); |
| 86 |
$defaultGroup->id = 0; |
| 87 |
$defaultGroup->title = acym_translation('ACYM_NO_GROUP'); |
| 88 |
array_unshift($groups, $defaultGroup); |
| 89 |
|
| 90 |
// Load custom fields |
| 91 |
$customFields = acym_loadObjectList('SELECT id, title, group_id FROM #__fields WHERE context = "com_users.user" AND state = 1 ORDER BY title ASC'); |
| 92 |
if (!empty($customFields)) { |
| 93 |
echo '<h1 class="acym__title acym__title__secondary text-center cell" style="margin-top: 20px;">'.esc_html(acym_translation('ACYM_CUSTOM_FIELDS')).'</h1>'; |
| 94 |
|
| 95 |
foreach ($groups as $oneGroup) { |
| 96 |
foreach ($customFields as $oneCF) { |
| 97 |
if ($oneCF->group_id != $oneGroup->id) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
echo '<div class="grid-x medium-12 cell acym__row__no-listing acym__listing__row__popup text-left" id="'.esc_attr( |
| 101 |
$oneCF->id |
| 102 |
).'customoption" onclick="changeUserTag(\''.esc_attr($oneCF->id).'custom\');" > |
| 103 |
<div class="cell medium-6 small-12 acym__listing__title acym__listing__title__dynamics">'.esc_html($oneCF->title).'</div> |
| 104 |
</div>'; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
echo '</div>'; |
| 111 |
} |
| 112 |
|
| 113 |
public function replaceUserInformation(object &$email, ?object &$user, bool $send = true): void |
| 114 |
{ |
| 115 |
$extractedTags = $this->pluginHelper->extractTags($email, 'usertag'); |
| 116 |
if (empty($extractedTags)) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
if (empty($this->customFields) && ACYM_CMS == 'joomla' && ACYM_J37) { |
| 121 |
$this->customFields = acym_loadObjectList('SELECT * FROM #__fields WHERE context = "com_users.user"', 'id'); |
| 122 |
foreach ($this->customFields as &$oneCF) { |
| 123 |
if (!empty($oneCF->fieldparams)) { |
| 124 |
$oneCF->fieldparams = json_decode($oneCF->fieldparams, true); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$tags = []; |
| 130 |
$receivervalues = []; |
| 131 |
foreach ($extractedTags as $i => $mytag) { |
| 132 |
if (isset($tags[$i])) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
$mytag->default = ''; |
| 136 |
|
| 137 |
$values = new stdClass(); |
| 138 |
$idused = 0; |
| 139 |
//Should we keep |
| 140 |
$save = false; |
| 141 |
|
| 142 |
//Sender information |
| 143 |
if (!empty($mytag->info) && $mytag->info == 'sender' && !empty($email->creator_id)) { |
| 144 |
$idused = $email->creator_id; |
| 145 |
$save = true; |
| 146 |
} |
| 147 |
|
| 148 |
//Current user information |
| 149 |
if (!empty($mytag->info) && $mytag->info == 'current') { |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
//Receiver information |
| 154 |
if ((empty($mytag->info) || $mytag->info == 'receiver') && !empty($user->cms_id)) { |
| 155 |
$idused = $user->cms_id; |
| 156 |
} |
| 157 |
|
| 158 |
if (!empty($idused) && empty($this->sendervalues[$idused]) && empty($receivervalues[$idused])) { |
| 159 |
global $acymCmsUserVars; |
| 160 |
$receivervalues[$idused] = acym_loadObject('SELECT * FROM '.$acymCmsUserVars->table.' WHERE '.$acymCmsUserVars->id.' = '.intval($idused)); |
| 161 |
|
| 162 |
//If we save the value in the object as we may reuse it... |
| 163 |
if ($save) { |
| 164 |
$this->sendervalues[$idused] = $receivervalues[$idused]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if (!empty($this->sendervalues[$idused])) { |
| 169 |
$values = $this->sendervalues[$idused]; |
| 170 |
} elseif (!empty($receivervalues[$idused])) { |
| 171 |
$values = $receivervalues[$idused]; |
| 172 |
} |
| 173 |
|
| 174 |
if ($mytag->id == 'groups') { |
| 175 |
$groups = acym_getGroupsByUser($idused, true, true); |
| 176 |
$values->groups = implode(', ', $groups); |
| 177 |
} |
| 178 |
|
| 179 |
if (empty($mytag->type)) { |
| 180 |
$mytag->type = ''; |
| 181 |
} |
| 182 |
|
| 183 |
if ($mytag->type == 'custom' && ACYM_CMS == 'joomla') { |
| 184 |
$mytag->id = intval($mytag->id); |
| 185 |
if (empty($mytag->id)) { |
| 186 |
$replaceme = ''; |
| 187 |
} else { |
| 188 |
$userFieldVals = acym_loadResultArray('SELECT value FROM #__fields_values WHERE item_id = '.intval($idused).' AND field_id = '.intval($mytag->id)); |
| 189 |
|
| 190 |
$fieldValues = trim(implode(', ', $userFieldVals), ', '); |
| 191 |
if (empty($fieldValues)) { |
| 192 |
$defaultValue = acym_loadObject('SELECT default_value, type FROM #__fields WHERE id = '.intval($mytag->id)); |
| 193 |
if ( |
| 194 |
($defaultValue->type === 'user' && !empty($defaultValue->default_value)) |
| 195 |
|| ($defaultValue->type !== 'user' && strlen($defaultValue->default_value) > 0) |
| 196 |
) { |
| 197 |
$userFieldVals = [$defaultValue->default_value]; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
foreach ($userFieldVals as &$oneFieldVal) { |
| 202 |
switch ($this->customFields[$mytag->id]->type) { |
| 203 |
case 'radio': |
| 204 |
case 'list': |
| 205 |
case 'checkboxes': |
| 206 |
foreach ($this->customFields[$mytag->id]->fieldparams['options'] as $oneOPT) { |
| 207 |
if ($oneOPT['value'] == $oneFieldVal) { |
| 208 |
$oneFieldVal = $oneOPT['name']; |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
break; |
| 213 |
|
| 214 |
case 'usergrouplist': |
| 215 |
if (empty($this->usergroups)) { |
| 216 |
$this->usergroups = acym_loadObjectList('SELECT id, title FROM #__usergroups', 'id'); |
| 217 |
} |
| 218 |
|
| 219 |
$oneFieldVal = $this->usergroups[$oneFieldVal]->title; |
| 220 |
break; |
| 221 |
|
| 222 |
case 'imagelist': |
| 223 |
if (strlen($this->customFields[$mytag->id]->fieldparams['directory']) > 1) { |
| 224 |
$oneFieldVal = '/'.$oneFieldVal; |
| 225 |
} else { |
| 226 |
$this->customFields[$mytag->id]->fieldparams['directory'] = ''; |
| 227 |
} |
| 228 |
$oneFieldVal = '<img src="images/'.$this->customFields[$mytag->id]->fieldparams['directory'].$oneFieldVal.'" />'; |
| 229 |
break; |
| 230 |
|
| 231 |
case 'url': |
| 232 |
$oneFieldVal = '<a target="_blank" href="'.$oneFieldVal.'">'.$oneFieldVal.'</a>'; |
| 233 |
break; |
| 234 |
|
| 235 |
case 'sql': |
| 236 |
if (empty($this->customFields[$mytag->id]->options)) { |
| 237 |
$this->customFields[$mytag->id]->options = acym_loadObjectList($this->customFields[$mytag->id]->fieldparams['query'], 'value'); |
| 238 |
} |
| 239 |
|
| 240 |
$oneFieldVal = $this->customFields[$mytag->id]->options[$oneFieldVal]->text; |
| 241 |
break; |
| 242 |
|
| 243 |
case 'user': |
| 244 |
$oneFieldVal = acym_currentUserName($oneFieldVal); |
| 245 |
break; |
| 246 |
|
| 247 |
case 'media': |
| 248 |
$oneFieldVal = '<img src="'.$oneFieldVal.'" />'; |
| 249 |
break; |
| 250 |
|
| 251 |
case 'calendar': |
| 252 |
$format = $this->customFields[$mytag->id]->fieldparams['showtime'] == '1' ? 'Y-m-d H:i' : 'Y-m-d'; |
| 253 |
$oneFieldVal = acym_date(strtotime($oneFieldVal), $format); |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
$replaceme = implode(', ', $userFieldVals); |
| 259 |
} |
| 260 |
} else { |
| 261 |
$replaceme = isset($values->{$mytag->id}) ? $values->{$mytag->id} : $mytag->default; |
| 262 |
} |
| 263 |
|
| 264 |
$tags[$i] = $replaceme; |
| 265 |
$this->pluginHelper->formatString($tags[$i], $mytag); |
| 266 |
} |
| 267 |
|
| 268 |
$this->pluginHelper->replaceTags($email, $tags); |
| 269 |
} |
| 270 |
} |
| 271 |
|