Spaces:
No application file
No application file
File size: 4,718 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 |
<?php
namespace Mautic\CoreBundle\Helper;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Mautic\CoreBundle\Factory\ModelFactory;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
class BuilderTokenHelper
{
private bool $isConfigured = false;
protected $permissionSet;
protected $modelName;
protected $viewPermissionBase;
protected $langVar;
protected $bundleName;
/**
* @param ModelFactory<object> $modelFactory
*/
public function __construct(
private CorePermissions $security,
private ModelFactory $modelFactory,
private Connection $connection,
private UserHelper $userHelper
) {
}
/**
* This method must be called before the BuilderTokenHelper can be used.
*/
public function configure(
string $modelName,
?string $viewPermissionBase = null,
?string $bundleName = null,
?string $langVar = null
): void {
$this->modelName = $modelName;
$this->viewPermissionBase = (!empty($viewPermissionBase)) ? $viewPermissionBase : "$modelName:{$modelName}s";
$this->bundleName = (!empty($bundleName)) ? $bundleName : 'Mautic'.ucfirst($modelName).'Bundle';
$this->langVar = (!empty($langVar)) ? $langVar : $modelName;
$this->permissionSet = [
$this->viewPermissionBase.':viewown',
$this->viewPermissionBase.':viewother',
];
$this->isConfigured = true;
}
/**
* @param string $tokenRegex Token regex without wrapping regex escape characters. Use (value) or (.*?) where the ID of the
* entity should go. i.e. {pagelink=(value)}
* @param string $filter String to filter results by
* @param string $labelColumn The column that houses the label
* @param string $valueColumn The column that houses the value
* @param CompositeExpression $expr Use $factory->getDatabase()->getExpressionBuilder()->andX()
*
* @return array|void
*
* @throws \BadMethodCallException
*/
public function getTokens(
$tokenRegex,
$filter = '',
$labelColumn = 'name',
$valueColumn = 'id',
CompositeExpression $expr = null
) {
if (!$this->isConfigured) {
throw new \BadMethodCallException('You must call the "'.static::class.'::configure()" method first.');
}
// set some permissions
$permissions = $this->security->isGranted(
$this->permissionSet,
'RETURN_ARRAY'
);
if (1 == count(array_unique($permissions)) && false == end($permissions)) {
return;
}
$repo = $this->modelFactory->getModel($this->modelName)->getRepository();
$prefix = $repo->getTableAlias();
if (!empty($prefix)) {
$prefix .= '.';
}
$exprBuilder = $this->connection->createExpressionBuilder();
if (isset($permissions[$this->viewPermissionBase.':viewother']) && !$permissions[$this->viewPermissionBase.':viewother']) {
$expr = $expr->with(
$exprBuilder->eq($prefix.'created_by', $this->userHelper->getUser()->getId())
);
}
if (!empty($filter)) {
$expr = $expr->with(
$exprBuilder->like('LOWER('.$labelColumn.')', ':label')
);
$parameters = [
'label' => strtolower($filter).'%',
];
} else {
$parameters = [];
}
$items = $repo->getSimpleList($expr, $parameters, $labelColumn, $valueColumn);
$tokens = [];
foreach ($items as $item) {
$token = str_replace(['(value)', '(.*?)'], $item['value'], $tokenRegex);
$tokens[$token] = $item['label'];
}
return $tokens;
}
/**
* Override default permission set of viewown and viewother.
*/
public function setPermissionSet(array $permissions): void
{
$this->permissionSet = $permissions;
}
/**
* @deprecated 2.6.0 to be removed in 3.0
*/
public static function getVisualTokenHtml($token, $description, $forPregReplace = false): string
{
if ($forPregReplace) {
return preg_quote('<strong contenteditable="false" data-token="', '/').'(.*?)'.preg_quote('">**', '/')
.'(.*?)'.preg_quote('**</strong>', '/');
}
return '<strong contenteditable="false" data-token="'.$token.'">**'.$description.'**</strong>';
}
}
|