public function addItem($ownerEntityID, $fields)
{
$ownerEntityID = (int)$ownerEntityID;
if($ownerEntityID <= 0)
{
throw new RestException("The parameter 'ownerEntityID' is invalid or not defined.");
}
if(!is_array($fields))
{
throw new RestException("The parameter 'fields' must be array.");
}
$fieldInfos = $this->getFieldsInfo();
$this->internalizeFields($fields, $fieldInfos, array());
$userPermissions = CCrmPerms::GetCurrentUserPermissions();
if(
$this->ownerEntityTypeID === CCrmOwnerType::Deal
&& $this->entityTypeID === CCrmOwnerType::Contact
)
{
//DEAL -> CONTACT
$categoryID = CCrmDeal::GetCategoryID($ownerEntityID);
if($categoryID < 0)
{
throw new RestException(
!CCrmDeal::CheckUpdatePermission(0, $userPermissions) ? 'Access denied.' : 'Not found.'
);
}
elseif(!CCrmDeal::CheckUpdatePermission($ownerEntityID, $userPermissions, $categoryID))
{
throw new AccessException();
}
if(!CCrmDeal::Exists($ownerEntityID))
{
throw new RestException('Not found.');
}
if(!EntityBinding::verifyEntityBinding(CCrmOwnerType::Contact, $fields))
{
throw new RestException("The parameter 'fields' is not valid.");
}
$entityID = EntityBinding::resolveEntityID(CCrmOwnerType::Contact, $fields);
if($entityID <= 0)
{
throw new RestException("The parameter 'fields' is not valid.");
}
$items = DealContactTable::getDealBindings($ownerEntityID);
if(is_array(EntityBinding::findBindingByEntityID(CCrmOwnerType::Contact, $entityID, $items)))
{
return false;
}
$effectiveItems = array_merge($items, array($fields));
if(EntityBinding::isPrimary($fields) || EntityBinding::findPrimaryBinding($effectiveItems) === null)
{
EntityBinding::markAsPrimary($effectiveItems, CCrmOwnerType::Contact, $entityID);
}
$removedItems = array();
$addedItems = array();
EntityBinding::prepareBindingChanges(
CCrmOwnerType::Contact,
$items,
$effectiveItems,
$addedItems,
$removedItems
);
if(!empty($addedItems))
{
DealContactTable::bindContacts($ownerEntityID, $addedItems);
}
return true;
}
elseif(
$this->ownerEntityTypeID === CCrmOwnerType::Lead
&& $this->entityTypeID === CCrmOwnerType::Contact
)
{
// LEAD -> CONTACT
if(!CCrmLead::CheckUpdatePermission($ownerEntityID, $userPermissions))
{
throw new AccessException();
}
if(!CCrmLead::Exists($ownerEntityID))
{
throw new RestException('Not found.');
}
if(!EntityBinding::verifyEntityBinding(CCrmOwnerType::Contact, $fields))
{
throw new RestException("The parameter 'fields' is not valid.");
}
$entityID = EntityBinding::resolveEntityID(CCrmOwnerType::Contact, $fields);
if($entityID <= 0)
{
throw new RestException("The parameter 'fields' is not valid.");
}
$items = LeadContactTable::getLeadBindings($ownerEntityID);
if(is_array(EntityBinding::findBindingByEntityID(CCrmOwnerType::Contact, $entityID, $items)))
{
return false;
}
$effectiveItems = array_merge($items, [$fields]);
if(EntityBinding::isPrimary($fields) || EntityBinding::findPrimaryBinding($effectiveItems) === null)
{
EntityBinding::markAsPrimary($effectiveItems, CCrmOwnerType::Contact, $entityID);
}
$removedItems = [];
$addedItems = [];
EntityBinding::prepareBindingChanges(
CCrmOwnerType::Contact,
$items,
$effectiveItems,
$addedItems,
$removedItems
);
if(!empty($addedItems))
{
LeadContactTable::bindContacts($ownerEntityID, $addedItems);
}
return true;
}
elseif(
$this->ownerEntityTypeID === CCrmOwnerType::Quote
&& $this->entityTypeID === CCrmOwnerType::Contact
)
{
//QUOTE -> CONTACT
if(!CCrmQuote::CheckUpdatePermission($ownerEntityID, $userPermissions))
{
throw new AccessException();
}
if(!CCrmQuote::Exists($ownerEntityID))
{
throw new RestException('Not found.');
}
if(!EntityBinding::verifyEntityBinding(CCrmOwnerType::Contact, $fields))
{
throw new RestException("The parameter 'fields' is not valid.");
}
$entityID = EntityBinding::resolveEntityID(CCrmOwnerType::Contact, $fields);
if($entityID <= 0)
{
throw new RestException("The parameter 'fields' is not valid.");
}
$items = QuoteContactTable::getQuoteBindings($ownerEntityID);
if(is_array(EntityBinding::findBindingByEntityID(CCrmOwnerType::Contact, $entityID, $items)))
{
return false;
}
$effectiveItems = array_merge($items, array($fields));
if(EntityBinding::isPrimary($fields) || EntityBinding::findPrimaryBinding($effectiveItems) === null)
{
EntityBinding::markAsPrimary($effectiveItems, CCrmOwnerType::Contact, $entityID);
}
$removedItems = array();
$addedItems = array();
EntityBinding::prepareBindingChanges(
CCrmOwnerType::Contact,
$items,
$effectiveItems,
$addedItems,
$removedItems
);
if(!empty($addedItems))
{
QuoteContactTable::bindContacts($ownerEntityID, $addedItems);
}
return true;
}
elseif(
$this->ownerEntityTypeID === CCrmOwnerType::Contact
&& $this->entityTypeID === CCrmOwnerType::Company
)
{
//CONTACT -> COMPANY
if(!CCrmContact::CheckUpdatePermission($ownerEntityID, $userPermissions))
{
throw new AccessException();
}
if(!CCrmContact::Exists($ownerEntityID))
{
throw new RestException('Not found.');
}
if(!EntityBinding::verifyEntityBinding(CCrmOwnerType::Company, $fields))
{
throw new RestException("The parameter 'fields' is not valid.");
}
$entityID = EntityBinding::resolveEntityID(CCrmOwnerType::Company, $fields);
if($entityID <= 0)
{
throw new RestException("The parameter 'fields' is not valid.");
}
$items = ContactCompanyTable::getContactBindings($ownerEntityID);
if(is_array(EntityBinding::findBindingByEntityID(CCrmOwnerType::Company, $entityID, $items)))
{
return false;
}
$effectiveItems = array_merge($items, array($fields));
if(EntityBinding::isPrimary($fields) || EntityBinding::findPrimaryBinding($effectiveItems) === null)
{
EntityBinding::markAsPrimary($effectiveItems, CCrmOwnerType::Company, $entityID);
}
$removedItems = array();
$addedItems = array();
EntityBinding::prepareBindingChanges(
CCrmOwnerType::Company,
$items,
$effectiveItems,
$addedItems,
$removedItems
);
if(!empty($addedItems))
{
ContactCompanyTable::bindCompanies($ownerEntityID, $addedItems);
}
return true;
}
elseif(
$this->ownerEntityTypeID === CCrmOwnerType::Company
&& $this->entityTypeID === CCrmOwnerType::Contact
)
{
//COMPANY -> CONTACT
if(!CCrmCompany::CheckUpdatePermission($ownerEntityID, $userPermissions))
{
throw new AccessException();
}
if(!CCrmCompany::Exists($ownerEntityID))
{
throw new RestException('Not found.');
}
if(!EntityBinding::verifyEntityBinding(CCrmOwnerType::Contact, $fields))
{
throw new RestException("The parameter 'fields' is not valid.");
}
$entityID = EntityBinding::resolveEntityID(CCrmOwnerType::Contact, $fields);
if($entityID <= 0)
{
throw new RestException("The parameter 'fields' is not valid.");
}
$items = ContactCompanyTable::getCompanyBindings($ownerEntityID);
if(is_array(EntityBinding::findBindingByEntityID(CCrmOwnerType::Contact, $entityID, $items)))
{
return false;
}
$effectiveItems = array_merge($items, array($fields));
if(EntityBinding::isPrimary($fields) || EntityBinding::findPrimaryBinding($effectiveItems) === null)
{
EntityBinding::markAsPrimary($effectiveItems, CCrmOwnerType::Contact, $entityID);
}
$removedItems = array();
$addedItems = array();
EntityBinding::prepareBindingChanges(
CCrmOwnerType::Contact,
$items,
$effectiveItems,
$addedItems,
$removedItems
);
if(!empty($addedItems))
{
ContactCompanyTable::bindContacts($ownerEntityID, $addedItems);
}
return true;
}
$ownerEntityTypeName = CCrmOwnerType::ResolveName($this->ownerEntityTypeID);
$entityTypeName = CCrmOwnerType::ResolveName($this->entityTypeID);
throw new RestException("The binding type '{$ownerEntityTypeName} - {$entityTypeName}' is not supported in current context.");
}