formula functions changes

This commit is contained in:
Yuri Kuznetsov
2020-07-21 14:51:02 +03:00
parent f2da244a3c
commit 3d05606ad6
8 changed files with 109 additions and 63 deletions
@@ -159,6 +159,6 @@ abstract class BaseFunction
protected function log(string $msg, string $level = 'notice')
{
if (!$this->log) return;
$this->log->log($level, 'function: ' . $this->name . ', ' . $msg);
$this->log->log($level, 'Formula function: ' . $this->name . ', ' . $msg);
}
}
@@ -29,25 +29,28 @@
namespace Espo\Core\Formula\Functions\EnvGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
use Espo\Core\Di;
class UserAttributeType extends \Espo\Core\Formula\Functions\AttributeType implements
class UserAttributeType extends BaseFunction implements
Di\UserAware
{
use Di\UserSetter;
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
if (count($item->value) < 1) {
throw new Error("userAttribute: too few arguments.");
if (count($args) < 1) {
$this->throwTooFewArguments();
}
$attribute = $this->evaluate($item->value[0]);
$attribute = $this->evaluate($args[0]);
if (!is_string($attribute)) {
throw new Error();
$this->throwBadArgumentType(1, 'string');
}
return $this->user->get($attribute);
@@ -29,39 +29,48 @@
namespace Espo\Core\Formula\Functions\ExtGroup\EmailGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
use Espo\Core\Di;
class ApplyTemplateType extends \Espo\Core\Formula\Functions\Base implements
class ApplyTemplateType extends BaseFunction implements
Di\EntityManagerAware,
Di\ServiceFactoryAware
{
use Di\EntityManagerSetter;
use Di\ServiceFactorySetter;
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->fetchArguments($item);
if (count($args) < 2) {
$this->throwTooFewArguments(2);
}
if (count($args) < 2) throw new Error("Formula ext\email\applyTemplate: Too few arguments.");
$args = $this->evaluate($args);
$id = $args[0];
$templateId = $args[1];
$parentType = $args[2] ?? null;
$parentId = $args[3] ?? null;
if (!$id || !is_string($id))
throw new Error("Formula ext\\email\applyTemplate: 1st argument should be string and not be empty.");
if (!$id || !is_string($id)) {
$this->throwBadArgumentType(1, 'string');
}
if (!$templateId || !is_string($templateId))
throw new Error("Formula ext\\email\applyTemplate: 2nd argument should be string and not be empty.");
if (!$templateId || !is_string($templateId)) {
$this->throwBadArgumentType(2, 'string');
}
if ($parentType && !is_string($parentType))
throw new Error("Formula ext\\email\applyTemplate: 3st argument should be string.");
if ($parentType && !is_string($parentType)) {
$this->throwBadArgumentType(3, 'string');
}
if ($parentId && !is_string($parentId))
throw new Error("Formula ext\\email\applyTemplate: 4th argument should be string.");
if ($parentId && !is_string($parentId)) {
$this->throwBadArgumentType(4, 'string');
}
$em = $this->entityManager;
@@ -69,18 +78,18 @@ class ApplyTemplateType extends \Espo\Core\Formula\Functions\Base implements
$emailTemplate = $em->getEntity('EmailTemplate', $templateId);
if (!$email) {
$GLOBALS['log']->warning("Formula ext\\email\applyTemplate: Email {$id} does not exist.");
$this->log("Email {$id} does not exist.");
return false;
}
if (!$emailTemplate) {
$GLOBALS['log']->warning("Formula ext\\email\applyTemplate: EmailTemplate {$templateId} does not exist.");
$this->log("EmailTemplate {$templateId} does not exist.");
return false;
}
$status = $email->get('status');
if ($status && in_array($status, ['Sent'])) {
$GLOBALS['log']->warning("Formula ext\\email\applyTemplate: Can't apply template to email with 'Sent' status.");
$this->log("Can't apply template to email with 'Sent' status.");
return false;
}
@@ -29,42 +29,49 @@
namespace Espo\Core\Formula\Functions\ExtGroup\EmailGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
use Espo\Core\Di;
class SendType extends \Espo\Core\Formula\Functions\Base implements
class SendType extends BaseFunction implements
Di\EntityManagerAware,
Di\ServiceFactoryAware,
Di\ConfigAware
{
use Di\EntityManagerSetter;
use Di\ServiceFactorySetter;
use Di\ConfigSettr;
use Di\ConfigSetter;
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->fetchArguments($item);
if (count($args) < 1) {
$this->throwTooFewArguments(1);
}
$args = $this->evaluate($args);
if (count($args) < 1) throw new Error("Formula ext\email\send: Too few arguments.");
$id = $args[0];
if (!$id) throw new Error("Formula ext\\email\send: First argument should not be empty.");
if (!is_string($id)) throw new Error("Formula ext\\email\send: First argument should be a string.");
if (!$id || !is_string($id)) {
$this->throwBadArgumentType(1, 'string');
}
$em = $this->entityManager;
$email = $em->getEntity('Email', $id);
if (!$email) {
$GLOBALS['log']->warning("Formula ext\\email\send: Email {$id} does not exist.");
$this->log("Email '{$id}' does not exist.");
return false;
}
$status = $email->get('status');
if ($status && in_array($status, ['Sent'])) {
$GLOBALS['log']->warning("Formula ext\\email\send: Can't send email that has 'Sent' status.");
$this->log("Can't send email that has 'Sent' status.");
return false;
}
@@ -96,7 +103,8 @@ class SendType extends \Espo\Core\Formula\Functions\Base implements
try {
$service->sendEntity($email);
} catch (\Exception $e) {
$GLOBALS['log']->error("Formula ext\\email\send: Error while sending. Message: " . $e->getMessage());
$message = $e->getMessage();
$this->log("Error while sending. Message: {$message}." , 'error');
return false;
}
@@ -29,57 +29,66 @@
namespace Espo\Core\Formula\Functions\ExtGroup\PdfGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
use Espo\Core\Utils\Util;
use Espo\Core\Di;
class GenerateType extends \Espo\Core\Formula\Functions\Base implements
class GenerateType extends BaseFunction implements
Di\EntityManagerAware,
Di\ServiceFactoryAware
{
use Di\EntityManagerSetter;
use Di\ServiceFactorySetter;
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->fetchArguments($item);
if (count($args) < 3) {
$this->throwTooFewArguments(3);
}
if (count($args) < 3) throw new Error("Formula ext\\pdf\\generate: Too few arguments.");
$args = $this->evaluate($args);
$entityType = $args[0];
$id = $args[1];
$templateId = $args[2];
$fileName = $args[3];
if (!$entityType || !is_string($entityType))
throw new Error("Formula ext\\pdf\\generate: 1st argument should be string and not be empty.");
if (!$id || !is_string($id))
throw new Error("Formula ext\\pdf\\generate: 2nd argument should be string and not be empty.");
if (!$templateId || !is_string($templateId))
throw new Error("Formula ext\\pdf\\generate: 3rd argument should be string and not be empty.");
if ($fileName && !is_string($fileName))
throw new Error("Formula ext\\pdf\\generate: 4rd argument should be string.");
if (!$entityType || !is_string($entityType)) {
$this->throwBadArgumentType(1, 'string');
}
if (!$id || !is_string($id)) {
$this->throwBadArgumentType(2, 'string');
}
if (!$templateId || !is_string($templateId)) {
$this->throwBadArgumentType(3, 'string');
}
if ($fileName && !is_string($fileName)) {
$this->throwBadArgumentType(4, 'string');
}
$em = $this->entityManager;
try {
$entity = $em->getEntity($entityType, $id);
} catch (\Exception $e) {
$GLOBALS['log']->error("Formula ext\\pdf\\generate: Message: " . $e->getMessage());
$this->log("Message: " . $e->getMessage() . ".");
return null;
}
if (!$entity) {
$GLOBALS['log']->warning("Formula ext\\pdf\\generate: Record {$entityType} {$id} does not exist.");
$this->log("Record {$entityType} {$id} does not exist.");
return null;
}
$template = $em->getEntity('Template', $templateId);
if (!$template) {
$GLOBALS['log']->warning("Formula ext\\pdf\\generate: Template {$templateId} does not exist.");
$this->log("Template {$templateId} does not exist.");
return null;
}
@@ -94,7 +103,7 @@ class GenerateType extends \Espo\Core\Formula\Functions\Base implements
try {
$contents = $this->serviceFactory->create('Pdf')->buildFromTemplate($entity, $template);
} catch (\Exception $e) {
$GLOBALS['log']->error("Formula ext\\pdf\\generate: Error while generating. Message: " . $e->getMessage());
$this->log("Error while generating. Message: " . $e->getMessage() . ".", 'error');
return null;
}
@@ -29,14 +29,21 @@
namespace Espo\Core\Formula\Functions\PasswordGroup;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
use Espo\Core\Utils\Util;
use Espo\Core\Di;
class GenerateType extends \Espo\Core\Formula\Functions\Base implements
class GenerateType extends BaseFunction implements
Di\ConfigAware
{
use Di\ConfigSetter;
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$config = $this->config;
@@ -44,7 +51,7 @@ class GenerateType extends \Espo\Core\Formula\Functions\Base implements
$letterCount = $config->get('passwordGenerateLetterCount', 4);
$numberCount = $config->get('passwordGenerateNumberCount', 2);
$password = \Espo\Core\Utils\Util::generatePassword($length, $letterCount, $numberCount, true);
$password = Util::generatePassword($length, $letterCount, $numberCount, true);
return $password;
}
@@ -29,29 +29,30 @@
namespace Espo\Core\Formula\Functions\PasswordGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
use Espo\Core\Utils\PasswordHash;
use Espo\Core\Di;
class HashType extends \Espo\Core\Formula\Functions\Base implements
class HashType extends BaseFunction implements
Di\ConfigAware
{
use Di\ConfigSetter;
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $item->value ?? [];
if (count($args) < 1) {
throw new Error("Formula: password\\hash: no argument.");
$this->throwTooFewArguments();
}
$password = $this->evaluate($args[0]);
if (!is_string($password)) {
throw new Error("Formula: password\\hash: bad argument.");
$this->throwBadArgumentType(1, 'string');
}
$passwordHash = new PasswordHash($this->config);
@@ -676,7 +676,6 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase
$this->assertEquals(false, $email->get('isHtml'));
$this->assertEquals(2, count($attachmentsIds));
$case = $em->createEntity('Case', [
'name' => 'Case 1',
]);
@@ -744,4 +743,14 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase
$this->assertEquals('1.pdf', $attachment->get('name'));
}
public function testEnvUserAttribute()
{
$fm = $this->getContainer()->get('formulaManager');
$user = $this->getContainer()->get('user');
$script = "env\\userAttribute('id')";
$id = $fm->run($script);
$this->assertEquals($id, $user->id);
}
}