View source
<?php
namespace Drupal\salesforce_mapping\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\salesforce\Exception;
use Drupal\salesforce\SelectQuery;
use Drupal\salesforce_mapping\MappingConstants;
class SalesforceMapping extends ConfigEntityBase implements SalesforceMappingInterface {
use StringTranslationTrait;
protected $type = 'salesforce_mapping';
protected $id;
protected $label;
protected $uuid;
protected $weight = 0;
protected $async = FALSE;
protected $push_standalone = FALSE;
protected $pull_standalone = FALSE;
protected $pull_trigger_date = 'LastModifiedDate';
protected $pull_where_clause = '';
protected $drupal_entity_type;
protected $drupal_bundle;
protected $salesforce_object_type;
protected $key;
protected $always_upsert;
protected $field_mappings = [];
protected $sync_triggers = [];
protected $push_info;
protected $pull_info;
protected $push_frequency = 0;
protected $push_limit = 0;
protected $push_retries = 3;
protected $pull_frequency = 0;
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$push_info = $this
->state()
->get('salesforce.mapping_push_info', []);
if (empty($push_info[$this
->id()])) {
$push_info[$this
->id()] = [
'last_timestamp' => 0,
];
}
$this->push_info = $push_info[$this
->id()];
$pull_info = $this
->state()
->get('salesforce.mapping_pull_info', []);
if (empty($pull_info[$this
->id()])) {
$pull_info[$this
->id()] = [
'last_pull_timestamp' => 0,
'last_delete_timestamp' => 0,
];
}
$this->pull_info = $pull_info[$this
->id()];
foreach ($this->field_mappings as $i => &$field_mapping) {
$field_mapping['id'] = $i;
$field_mapping['mapping'] = $this;
}
}
public function __get($key) {
return $this->{$key};
}
public function getPluginCollections() {
if (empty($this->field_mappings)) {
return [];
}
return [
'field_mappings' => new DefaultLazyPluginCollection($this
->fieldManager(), $this->field_mappings),
];
}
public function toArray() {
$entity_array = parent::toArray();
foreach ($entity_array['field_mappings'] as $i => &$value) {
unset($value['mapping']);
}
return $entity_array;
}
public function save() {
$this->updated = $this
->getRequestTime();
if (isset($this->is_new) && $this->is_new) {
$this->created = $this
->getRequestTime();
}
return parent::save();
}
protected function getRequestTime() {
return \Drupal::time()
->getRequestTime();
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
$pull_mappings = $storage
->loadByProperties([
'salesforce_object_type' => $this->salesforce_object_type,
]);
unset($pull_mappings[$this
->id()]);
foreach ($pull_mappings as $mapping) {
if ($this->pull_frequency != $mapping->pull_frequency) {
$mapping->pull_frequency = $this->pull_frequency;
$mapping
->save();
}
}
}
public function calculateDependencies() {
$this->dependencies = array_intersect_key($this->dependencies, [
'enforced' => '',
]);
foreach ($this
->getFieldMappings() as $field) {
$this
->calculatePluginDependencies($field);
}
if ($entity_type = $this
->entityTypeManager()
->getDefinition($this
->getDrupalEntityType())) {
$dependency = $entity_type
->getBundleConfigDependency($this
->getDrupalBundle());
$this
->addDependency($dependency['type'], $dependency['name']);
}
if ($this
->doesPull()) {
$this
->addDependency('module', 'salesforce_pull');
}
if ($this
->doesPush()) {
$this
->addDependency('module', 'salesforce_push');
}
return $this;
}
public function onDependencyRemoval(array $dependencies) {
parent::onDependencyRemoval($dependencies);
$entity_type = $this
->entityTypeManager()
->getDefinition($this
->getDrupalEntityType());
$dependency = $entity_type
->getBundleConfigDependency($this
->getDrupalBundle());
if (!empty($dependencies[$dependency['type']][$dependency['name']])) {
return FALSE;
}
return $this
->removePluginDependencies($dependencies);
}
public function removePluginDependencies(array $dependencies) {
$changed = FALSE;
foreach ($this
->getFieldMappings() as $i => $field) {
if ($field
->checkFieldMappingDependency($dependencies)) {
$changed = TRUE;
unset($this->field_mappings[$i]);
}
}
return $changed;
}
public function getPullFields() {
$fields = [];
foreach ($this
->getFieldMappings() as $i => $field_plugin) {
if (!$field_plugin
->pull()) {
continue;
}
$fields[$i] = $field_plugin;
}
return $fields;
}
public function getPullFieldsArray() {
return array_column($this->field_mappings, 'salesforce_field', 'salesforce_field');
}
public function getKeyField() {
return $this->key ? $this->key : FALSE;
}
public function hasKey() {
return $this->key ? TRUE : FALSE;
}
public function getKeyValue(EntityInterface $entity) {
if (!$this
->hasKey()) {
throw new \Exception('No key defined for this mapping.');
}
foreach ($this
->getFieldMappings() as $i => $field_plugin) {
if ($field_plugin
->get('salesforce_field') == $this
->getKeyField()) {
return $field_plugin
->value($entity, $this);
}
}
throw new \Exception($this
->t('Key %key not found for this mapping.', [
'%key' => $this
->getKeyField(),
]));
}
public function getSalesforceObjectType() {
return $this->salesforce_object_type;
}
public function getDrupalEntityType() {
return $this->drupal_entity_type;
}
public function getDrupalBundle() {
return $this->drupal_bundle;
}
public function getFieldMappings() {
$fields = [];
foreach ($this->field_mappings as $i => $field) {
$fields[$i] = $this
->fieldManager()
->createInstance($field['drupal_field_type'], $field + [
'mapping' => $this,
]);
}
return $fields;
}
public function getFieldMapping(array $field) {
return $this
->fieldManager()
->createInstance($field['drupal_field_type'], $field['config'] + [
'mapping' => $this,
]);
}
public function getPullTriggerDate() {
return $this->pull_trigger_date;
}
public function doesPushStandalone() {
return $this->push_standalone;
}
public function doesPullStandalone() {
return $this->pull_standalone;
}
public function doesPush() {
return $this
->checkTriggers([
MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_CREATE,
MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_UPDATE,
MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE,
]);
}
public function doesPull() {
return $this
->checkTriggers([
MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE,
MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE,
MappingConstants::SALESFORCE_MAPPING_SYNC_SF_DELETE,
]);
}
public function checkTriggers(array $triggers) {
foreach ($triggers as $trigger) {
if (!empty($this->sync_triggers[$trigger])) {
return TRUE;
}
}
return FALSE;
}
public function getName() {
return $this->name;
}
public function getLastDeleteTime() {
return $this->pull_info['last_delete_timestamp'] ?? NULL;
}
public function setLastDeleteTime($time) {
return $this
->setPullInfo('last_delete_timestamp', $time);
}
public function getLastPullTime() {
return $this->pull_info['last_pull_timestamp'] ?? NULL;
}
public function setLastPullTime($time) {
return $this
->setPullInfo('last_pull_timestamp', $time);
}
protected function setPullInfo($key, $value) {
$this->pull_info[$key] = $value;
$pull_info = $this
->state()
->get('salesforce.mapping_pull_info');
$pull_info[$this
->id()] = $this->pull_info;
$this
->state()
->set('salesforce.mapping_pull_info', $pull_info);
return $this;
}
public function getNextPullTime() {
return $this->pull_info['last_pull_timestamp'] + $this->pull_frequency;
}
public function getLastPushTime() {
return $this->push_info['last_timestamp'];
}
public function setLastPushTime($time) {
return $this
->setPushInfo('last_timestamp', $time);
}
protected function setPushInfo($key, $value) {
$this->push_info[$key] = $value;
$push_info = $this
->state()
->get('salesforce.mapping_push_info');
$push_info[$this
->id()] = $this->push_info;
$this
->state()
->set('salesforce.mapping_push_info', $push_info);
return $this;
}
public function getNextPushTime() {
return $this->push_info['last_timestamp'] + $this->push_frequency;
}
public function getPullQuery(array $mapped_fields = [], $start = 0, $stop = 0) {
if (!$this
->doesPull()) {
throw new Exception('Mapping does not pull.');
}
$object_type = $this
->getSalesforceObjectType();
$soql = new SelectQuery($object_type);
if (empty($mapped_fields)) {
$mapped_fields = $this
->getPullFieldsArray();
}
$soql->fields = $mapped_fields;
$soql->fields[] = 'Id';
$soql->fields[] = $this
->getPullTriggerDate();
$start = $start > 0 ? $start : $this
->getLastPullTime();
if ($start) {
$start = gmdate('Y-m-d\\TH:i:s\\Z', $start);
$soql
->addCondition($this
->getPullTriggerDate(), $start, '>');
}
if ($stop) {
$stop = gmdate('Y-m-d\\TH:i:s\\Z', $stop);
$soql
->addCondition($this
->getPullTriggerDate(), $stop, '<');
}
if (!empty($this->pull_where_clause)) {
$soql->conditions[] = [
$this->pull_where_clause,
];
}
$soql->order[$this
->getPullTriggerDate()] = 'ASC';
return $soql;
}
public function alwaysUpsert() {
return $this
->hasKey() && !empty($this->always_upsert);
}
protected function fieldManager() {
return \Drupal::service('plugin.manager.salesforce_mapping_field');
}
protected function client() {
return \Drupal::service('salesforce.client');
}
protected function state() {
return \Drupal::state();
}
}