ResultEntity::addActivity

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. ResultEntity
  4. addActivity
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/webform/resultentity.php
  • Класс: Bitrix\Crm\WebForm\ResultEntity
  • Вызов: ResultEntity::addActivity
protected function addActivity()
{
	// prepare bindings
	$bindings = BindingSelector::findBindings($this->selector);

	$mainEntityTypeId = Entity::getSchemes($this->scheme)['MAIN_ENTITY'];

	foreach($this->resultEntityPack as $entity)
	{
		$entityTypeId = \CCrmOwnerType::ResolveID($entity['ENTITY_NAME']);
		$bindings[] = [
			'OWNER_ID' => $entity['ITEM_ID'],
			'OWNER_TYPE_ID' => $entityTypeId,
		];

		if ($mainEntityTypeId === $entityTypeId)
		{
			$mainEntityName = \CCrmOwnerType::GetDescription($entityTypeId);
			$mainEntityLink = \Bitrix\Crm\Service\Container::getInstance()
				->getRouter()
				->getItemDetailUrl($entityTypeId, $entity['ITEM_ID'])
				->getLocator();
		}
	}
	if (
		!isset($mainEntityName)
		&& isset($this->resultEntityPack[0])
	)
	{
		$entityTypeId = \CCrmOwnerType::ResolveID($this->resultEntityPack[0]['ENTITY_NAME']);
		$mainEntityName = \CCrmOwnerType::GetDescription($entityTypeId);
		$mainEntityLink = \Bitrix\Crm\Service\Container::getInstance()
			->getRouter()
			->getItemDetailUrl($entityTypeId, $this->resultEntityPack[0]['ITEM_ID'])
			->getLocator();
	}


	$bindings = BindingSelector::sortBindings($bindings);
	$bindings = $this->removeIgnoredActivityBindings($bindings);
	if (empty($bindings))
	{
		return;
	}

	// add activity
	$activityFields = array(
		'TYPE_ID' =>  \CCrmActivityType::Provider,
		'PROVIDER_ID' => Provider\WebForm::PROVIDER_ID,
		'PROVIDER_TYPE_ID' => $this->formId,
		'DIRECTION' => \CCrmActivityDirection::Incoming,
		'ASSOCIATED_ENTITY_ID' => $this->resultId,
		'START_TIME' => new Main\Type\DateTime(),
		'COMPLETED' => LayoutSettings::getCurrent()->isSliderEnabled() ? 'Y' : 'N',
		'PRIORITY' => \CCrmActivityPriority::Medium,
		'DESCRIPTION' => '',
		'DESCRIPTION_TYPE' => \CCrmContentType::PlainText,
		'LOCATION' => '',
		'NOTIFY_TYPE' => \CCrmActivityNotifyType::None,
		'SETTINGS' => array(),
		'AUTHOR_ID' => $this->assignedById,
		'RESPONSIBLE_ID' => $this->assignedById,
		'ORIGIN_ID' => '',
		'BINDINGS' => $bindings,
		'PROVIDER_PARAMS' => array(
			'FIELDS' => $this->activityFields,
			'FORM' => array(
				'IS_USED_USER_CONSENT' => $this->formData['USE_LICENCE'] == 'Y',
				'AGREEMENTS' => $this->agreements,
				'IP' => Context::getCurrent()->getRequest()->getRemoteAddress(),
				'LINK' => Script::getUrlContext($this->formData)
			),
			'VISITED_PAGES' => array_map(
				function ($page)
				{
					return [
						'HREF' => $page['URL'],
						'DATE' => ($page['DATE_INSERT'] instanceof Main\Type\DateTime)
							? $page['DATE_INSERT']->getTimestamp()
							: null,
						'TITLE' => $page['TITLE']
					];
				},
				$this->trace->getPages()
			)
		)
	);

	if ($this->isCallback)
	{
		$activityFields['SUBJECT'] = Loc::getMessage(
			'CRM_WEBFORM_RESULT_ENTITY_NOTIFY_SUBJECT_CALL',
			[
				"%phone%" => htmlspecialcharsbx($this->callbackPhone ? $this->callbackPhone : $this->formData['NAME']),
			]
		);
	}
	else
	{
		$activityFields['SUBJECT'] = Loc::getMessage(
			'CRM_WEBFORM_RESULT_ENTITY_NOTIFY_SUBJECT',
			[
				"%title%" => htmlspecialcharsbx($this->formData['NAME']),
			]
		);
	}

	$activityFields = $this->fillFieldsByPresetFields(\CCrmOwnerType::ActivityName, $activityFields);

	$productRowsSum = $this->getProductRowsSum();
	if($productRowsSum > 0)
	{
		$activityFields['RESULT_SUM'] = $productRowsSum;
		$activityFields['RESULT_CURRENCY_ID'] = $this->currencyId;
	}

	$communications = array();
	if($this->contactId)
	{
		$communicationFields = $this->getEntityFields(\CCrmOwnerType::ContactName);
		$communications[] = array(
			'TYPE' => '',
			'VALUE' => $communicationFields['NAME'],
			'ENTITY_ID' => $this->contactId,
			'ENTITY_TYPE_ID' => \CCrmOwnerType::Contact
		);
	}
	if($this->companyId)
	{
		$communicationFields = $this->getEntityFields(\CCrmOwnerType::CompanyName);
		$communications[] = array(
			'TYPE' => '',
			'VALUE' => $communicationFields['TITLE'],
			'ENTITY_ID' => $this->companyId,
			'ENTITY_TYPE_ID' => \CCrmOwnerType::Company
		);
	}
	if($this->leadId)
	{
		$communicationFields = $this->getEntityFields(\CCrmOwnerType::LeadName);
		$communications[] = array(
			'TYPE' => '',
			'VALUE' => $communicationFields['TITLE'],
			'ENTITY_ID' => $this->leadId,
			'ENTITY_TYPE_ID' => \CCrmOwnerType::Lead
		);
	}

	$id = \CCrmActivity::Add(
		$activityFields, false, true,
		array('REGISTER_SONET_EVENT' => true)
	);
	if($id > 0)
	{
		$this->activityId = $id;
		if(count($communications) > 0)
		{
			\CCrmActivity::SaveCommunications($this->activityId, $communications, $activityFields, true, false);
		}

/*			if ($this->isCallback)
		{
			CallBackWebFormTracker::getInstance()->registerActivity($this->activityId);
		}
		else
*/			{
			WebFormTracker::getInstance()->registerActivity($this->activityId, array('ORIGIN_ID' => $this->formId));
		}

		if(Loader::includeModule('im'))
		{
			$url = "/crm/activity/?open_view=#log_id#";
			$url = str_replace(array("#log_id#"), array($id), $url);
			$serverName = (Context::getCurrent()->getRequest()->isHttps() ? "https" : "http") . "://";
			if(defined("SITE_SERVER_NAME") && SITE_SERVER_NAME <> '')
			{
				$serverName .= SITE_SERVER_NAME;
			}
			else
			{
				$serverName .= Option::get("main", "server_name", "");
			}

			if ($this->isCallback)
			{
				$notifyTag = "CRM|CALLBACK|" . $id;
				$imNotifyEvent = \CCrmNotifierSchemeType::CallbackName;
				$imNotifyMessage = Loc::getMessage(
					'CRM_WEBFORM_RESULT_ENTITY_NOTIFY_SUBJECT_CALL',
					Array(
						"%phone%" => '' . htmlspecialcharsbx(
								$this->callbackPhone ? $this->callbackPhone : $this->formData['NAME']
							) . '',
					)
				);
			}
			else
			{
				$notifyTag = "CRM|WEBFORM|" . $id;
				$imNotifyEvent = \CCrmNotifierSchemeType::WebFormName;
				$imNotifyMessage = Loc::getMessage(
					'CRM_WEBFORM_RESULT_ENTITY_NOTIFY_SUBJECT',
					Array(
						"%title%" => '' . htmlspecialcharsbx($this->formData['NAME']) . '',
					)
				);
			}

			if (
				isset($mainEntityLink)
				&& isset($mainEntityName)
				&& is_string($mainEntityName)
			)
			{
				$additionalEntityInfo = $this->getAdditionalEntityInfo($mainEntityLink, $mainEntityName);
				if ($additionalEntityInfo !== null)
				{
					$imNotifyMessage .= "\n";
					$imNotifyMessage .= $additionalEntityInfo;
				}
			}

			$imNotifyMessageOut = $imNotifyMessage . " (". $serverName . $url . ")";
			$imNotifyMessageOut .= "\n\n";
			$imNotifyMessageOut .= Result::formatFieldsByTemplate($this->activityFields);

			$imNotifyFields = array(
				"TO_USER_ID" => $activityFields['RESPONSIBLE_ID'],
				"FROM_USER_ID" => $activityFields['AUTHOR_ID'],
				"NOTIFY_TYPE" => IM_NOTIFY_FROM,
				"NOTIFY_MODULE" => "crm",
				"NOTIFY_EVENT" => $imNotifyEvent,
				"NOTIFY_TAG" => $notifyTag,
				"NOTIFY_MESSAGE" => $imNotifyMessage,
				"NOTIFY_MESSAGE_OUT" => $imNotifyMessageOut
			);
			\CIMNotify::Add($imNotifyFields);
		}
	}
}

Добавить комментарий