CrmCoreUserSyncRelation.php in CRM Core 8
File
modules/crm_core_user_sync/src/CrmCoreUserSyncRelation.php
View source
<?php
namespace Drupal\crm_core_user_sync;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\crm_core_user_sync\Entity\Relation;
use Drupal\user\UserInterface;
class CrmCoreUserSyncRelation implements CrmCoreUserSyncRelationInterface {
protected $storage;
protected $rules;
protected $logger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, CrmCoreUserSyncRelationRules $rules, LoggerChannelInterface $logger) {
$this->storage = $entity_type_manager
->getStorage('crm_core_user_sync_relation');
$this->rules = $rules;
$this->logger = $logger;
}
public function getUserIndividualId($user_id) {
$individual_id = NULL;
$rids = $this->storage
->getQuery()
->condition('user_id', $user_id)
->range(0, 1)
->execute();
if (!empty($rids)) {
$relation_id = reset($rids);
$relation = $this->storage
->load($relation_id);
$individual_id = $relation
->getIndividualId();
}
return $individual_id;
}
public function getIndividualUserId($individual_id) {
$user_id = NULL;
$rids = $this->storage
->getQuery()
->condition('individual_id', $individual_id)
->range(0, 1)
->execute();
if (!empty($rids)) {
$relation_id = reset($rids);
$relation = $this->storage
->load($relation_id);
$user_id = $relation
->getIndividualId();
}
return $user_id;
}
public function getUserRelationId($user_id) {
$rids = $this->storage
->getQuery()
->condition('user_id', $user_id)
->range(0, 1)
->execute();
if (!empty($rids)) {
return reset($rids);
}
return NULL;
}
public function getIndividualRelationId($individual_id) {
$rids = $this->storage
->getQuery()
->condition('individual_id', $individual_id)
->range(0, 1)
->execute();
if (!empty($rids)) {
return reset($rids);
}
return NULL;
}
public function relate(UserInterface $account, IndividualInterface $contact = NULL) {
if (empty($contact) && !empty($account->crm_core_no_auto_sync)) {
return NULL;
}
if (empty($contact)) {
if ($this
->getUserIndividualId($account
->id())) {
return NULL;
}
$contact_type = $this->rules
->getContactType($account);
if (!$contact_type) {
return NULL;
}
$contact = Individual::create([
'type' => $contact_type,
]);
$contact
->setOwner($account);
$contact->name->given = $account
->getAccountName();
$contact
->save();
}
if (!$this->rules
->valid($account, $contact)) {
return NULL;
}
if ($this
->getUserIndividualId($account
->id()) || $this
->getIndividualUserId($contact
->id())) {
return NULL;
}
$relation = Relation::create();
$relation
->setUser($account);
$relation
->setIndividual($contact);
$relation
->save();
$this->logger
->notice('User @user @uid has been synchronized to the contact @contact_id, relation @rid has been created.', [
'@user' => $account
->getDisplayName(),
'@uid' => $account
->id(),
'@contact_id' => $contact
->id(),
'@rid' => $relation
->id(),
]);
return $contact;
}
}