Skip to content

Commit

Permalink
Improve support for mautic-extended-field.
Browse files Browse the repository at this point in the history
  • Loading branch information
heathdutton committed Feb 26, 2018
1 parent ec277ff commit a2b64be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
23 changes: 9 additions & 14 deletions Helper/TokenHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@
*/
class TokenHelper
{
/**
* To reduce overhead, fields will be searched for this before attempting token replacement.
*/
/** @var string To reduce overhead, fields will be searched for this before attempting token replacement. */
const TOKEN_KEY = '{{';

/**
* @var Engine
*/
/** @var Engine */
private $engine;

/**
* @var array context of tokens for replacement
*/
/** @var array context of tokens for replacement */
private $context = [];

/** @var DateFormatHelper */
private $dateFormatHelper;

/**
Expand All @@ -53,14 +48,14 @@ public function __construct()
/**
* Recursively replaces tokens using an array for context.
*
* @param array $array
* @param array|object $context
*
* @return array
*/
public function renderArray($array = [])
public function renderArray($context = [])
{
$result = [];
foreach ($array as $key => $value) {
foreach ($context as $key => $value) {
if (false !== strpos($key, self::TOKEN_KEY)) {
$key = $this->engine->render($key, $this->context);
}
Expand All @@ -69,7 +64,7 @@ public function renderArray($array = [])
$value = $this->engine->render($value, $this->context);
}
} elseif (is_array($value) || is_object($value)) {
$value = $this->tokenizeArray($value, $this->context);
$value = $this->renderArray($value);
}
$result[$key] = $value;
}
Expand Down Expand Up @@ -174,7 +169,7 @@ public function addContextContact(Contact $contact)
}

// Add DNC status.
/** @var DoNotContact $record */
/** @var \Mautic\LeadBundle\Model\DoNotContact $record */
foreach ($contact->getDoNotContact() as $record) {
if (!isset($context['doNotContact'])) {
$context['doNotContact'] = [];
Expand Down
4 changes: 2 additions & 2 deletions Integration/ClientIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ClientIntegration extends AbstractIntegration
protected $contact;

/** @var array */
protected $event;
protected $event = [];

/** @var bool $test */
protected $test = false;
Expand Down Expand Up @@ -368,7 +368,7 @@ private function updateContact()
if ($updatedFields || $updatedAttribution) {
/** @var \Mautic\LeadBundle\Model\LeadModel $model */
$contactModel = $this->dispatcher->getContainer()->get('mautic.lead.model.lead');
$contactModel->getRepository()->saveEntity($this->contact);
$contactModel->saveEntity($this->contact);
$this->setLogs('Operation successful. The contact was updated.', 'updated');
} else {
$this->setLogs('Operation successful, but no fields on the contact needed updating.', 'info');
Expand Down
8 changes: 6 additions & 2 deletions Model/ApiPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,14 @@ private function getTokenHelper()
$this->tokenHelper->setTimezones($tza, $tzb);

// Add the Contact as context for field replacement.
$this->tokenHelper->addContextContact($this->contact);
if ($this->contact) {
$this->tokenHelper->addContextContact($this->contact);
}

// Include the payload as additional context.
$this->tokenHelper->addContext(['payload' => $this->payload]);
if ($this->payload) {
$this->tokenHelper->addContext(['payload' => $this->payload]);
}

return $this->tokenHelper;
}
Expand Down

0 comments on commit a2b64be

Please sign in to comment.