Spaces:
No application file
No application file
File size: 10,121 Bytes
d2897cd |
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
<?php
namespace Mautic\CoreBundle\Event;
use Mautic\CoreBundle\Helper\BuilderTokenHelper;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Contracts\EventDispatcher\Event;
class BuilderEvent extends Event
{
protected $slotTypes = [];
protected $sections = [];
protected $tokens = [];
protected $abTestWinnerCriteria = [];
/**
* @var string|string[]
*/
protected string|array $tokenFilterText;
protected string $tokenFilterTarget;
public function __construct(
protected $translator,
protected $entity = null,
protected $requested = 'all',
protected string $tokenFilter = ''
) {
$this->tokenFilterTarget = (str_starts_with($tokenFilter, '{@')) ? 'label' : 'token';
$this->tokenFilterText = str_replace(['{@', '{', '}'], '', $tokenFilter);
$this->tokenFilter = ('label' == $this->tokenFilterTarget) ? $this->tokenFilterText : str_replace('{@', '{', $tokenFilter);
}
/**
* @param int $priority
*/
public function addSlotType($key, $header, $icon, $content, $form, $priority = 0, array $params = []): void
{
$this->slotTypes[$key] = [
'header' => $this->translator->trans($header),
'icon' => $icon,
'content' => $content,
'params' => $params,
'form' => $form,
'priority' => $priority,
];
}
/**
* Get slot types.
*
* @return array
*/
public function getSlotTypes()
{
$sort = ['priority' => [], 'header' => []];
foreach ($this->slotTypes as $k => $v) {
$sort['priority'][$k] = $v['priority'];
$sort['header'][$k] = $v['header'];
}
array_multisort($sort['priority'], SORT_DESC, $sort['header'], SORT_ASC, $this->slotTypes);
foreach ($this->slotTypes as $i => $slot) {
$slot['header'] = str_replace(' ', '<br />', $slot['header']);
$this->slotTypes[$i] = $slot;
}
return $this->slotTypes;
}
public function addSection($key, $header, $icon, $content, $form, $priority = 0): void
{
$this->sections[$key] = [
'header' => $this->translator->trans($header),
'icon' => $icon,
'content' => $content,
'form' => $form,
'priority' => $priority,
];
}
/**
* Get slot types.
*
* @return array
*/
public function getSections()
{
$sort = ['priority' => [], 'header' => []];
foreach ($this->sections as $k => $v) {
$sort['priority'][$k] = $v['priority'];
$sort['header'][$k] = $v['header'];
}
array_multisort($sort['priority'], SORT_DESC, $sort['header'], SORT_ASC, $this->sections);
return $this->sections;
}
/**
* Get list of AB Test winner criteria.
*/
public function getAbTestWinnerCriteria(): array
{
uasort(
$this->abTestWinnerCriteria,
fn ($a, $b): int => strnatcasecmp(
$a['group'],
$b['group']
)
);
$array = ['criteria' => $this->abTestWinnerCriteria];
$choices = [];
foreach ($this->abTestWinnerCriteria as $k => $c) {
$choices[$c['group']][$c['label']] = $k;
}
$array['choices'] = $choices;
return $array;
}
/**
* Adds an A/B test winner criteria option.
*
* @param string $key - a unique identifier; it is recommended that it be namespaced i.e. lead.points
* @param array{
* group: string,
* label: string,
* event: string,
* formType?: string,
* formTypeOptions?: string
* } $criteria Can contain the following keys:
* - group - (required) translation string to group criteria by in the dropdown select list
* - label - (required) what to display in the list
* - event - (required) event class constant that will receieve the DetermineWinnerEvent for further handling. E.g. `HelloWorldEvents::ON_DETERMINE_PLANET_VISIT_WINNER`
* - formType - (optional) name of the form type SERVICE for the criteria
* - formTypeOptions - (optional) array of options to pass to the formType service
*/
public function addAbTestWinnerCriteria($key, array $criteria): void
{
if (array_key_exists($key, $this->abTestWinnerCriteria)) {
throw new InvalidArgumentException("The key, '$key' is already used by another criteria. Please use a different key.");
}
// check for required keys
$this->verifyCriteria(
['group', 'label', 'event'],
$criteria
);
// translate the group
$criteria['group'] = $this->translator->trans($criteria['group']);
$this->abTestWinnerCriteria[$key] = $criteria;
}
private function verifyCriteria(array $keys, array $criteria): void
{
foreach ($keys as $k) {
if (!array_key_exists($k, $criteria)) {
throw new InvalidArgumentException("The key, '$k' is missing.");
}
}
}
/**
* @param bool $convertToLinks
*/
public function addTokens(array $tokens, $convertToLinks = false): void
{
if ($convertToLinks) {
array_walk($tokens, function (&$val, $key): void {
$val = 'a:'.$val;
});
}
$this->tokens = array_merge($this->tokens, $tokens);
}
public function addToken($key, $value): void
{
$this->tokens[$key] = $value;
}
/**
* Get token array.
*
* @return array
*/
public function getTokens($withBC = true)
{
if (false === $withBC) {
$tokens = [];
foreach ($this->tokens as $key => $value) {
if (!str_starts_with($key, '{leadfield')) {
$tokens[$key] = $value;
}
}
return $tokens;
}
return $this->tokens;
}
/**
* Check if tokens have been requested.
* Pass in string or array of tokens to filter against if filterType == token.
*
* @param string|array|null $tokenKeys
*/
public function tokensRequested($tokenKeys = null): bool
{
if ($requested = $this->getRequested('tokens')) {
if (!empty($this->tokenFilter) && 'token' == $this->tokenFilterTarget) {
if (!is_array($tokenKeys)) {
$tokenKeys = [$tokenKeys];
}
$found = false;
foreach ($tokenKeys as $token) {
if (0 === stripos($token, $this->tokenFilter)) {
$found = true;
break;
}
}
if (!$found) {
$requested = false;
}
}
}
return $requested;
}
/**
* Get text of the search filter.
*/
public function getTokenFilter(): array
{
return [
'target' => $this->tokenFilterTarget,
'filter' => $this->tokenFilterText,
];
}
/**
* Simple token filtering.
*
* @param array $tokens array('token' => 'label')
*
* @return array
*/
public function filterTokens($tokens)
{
$filter = $this->tokenFilter;
if (empty($filter)) {
return $tokens;
}
if ('label' == $this->tokenFilterTarget) {
// Do a search against the label
$tokens = array_filter(
$tokens,
fn ($v): bool => 0 === stripos($v, $filter)
);
} else {
// Do a search against the token
$found = array_filter(
array_keys($tokens),
fn ($k): bool => 0 === stripos($k, $filter)
);
$tokens = array_intersect_key($tokens, array_flip($found));
}
return $tokens;
}
/**
* Add tokens from a BuilderTokenHelper.
*
* @param string $labelColumn
* @param string $valueColumn
* @param bool $convertToLinks If true, the tokens will be converted to links
*/
public function addTokensFromHelper(
BuilderTokenHelper $tokenHelper,
$tokens,
$labelColumn = 'name',
$valueColumn = 'id',
$convertToLinks = false
): void {
$tokens = $this->getTokensFromHelper($tokenHelper, $tokens, $labelColumn, $valueColumn);
if (null == $tokens) {
$tokens = [];
}
$this->addTokens(
$tokens,
$convertToLinks
);
}
/**
* Get tokens from a BuilderTokenHelper.
*
* @return array|void
*/
public function getTokensFromHelper(BuilderTokenHelper $tokenHelper, $tokens, $labelColumn = 'name', $valueColumn = 'id')
{
return $tokenHelper->getTokens(
$tokens,
'label' == $this->tokenFilterTarget ? $this->tokenFilterText : '',
$labelColumn,
$valueColumn
);
}
/**
* Check if AB Test Winner Criteria has been requested.
*/
public function abTestWinnerCriteriaRequested(): bool
{
return $this->getRequested('abTestWinnerCriteria');
}
/**
* Check if Slot types has been requested.
*/
public function slotTypesRequested(): bool
{
return $this->getRequested('slotTypes');
}
/**
* Check if Sections has been requested.
*/
public function sectionsRequested(): bool
{
return $this->getRequested('sections');
}
protected function getRequested($type): bool
{
if (is_array($this->requested)) {
return in_array($type, $this->requested);
}
return $this->requested == $type || 'all' == $this->requested;
}
}
|