• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/livechatclient.php
  • Класс: BitrixImOpenLinesLiveChatClient
  • Вызов: LiveChatClient::saveForm
public function saveForm($type, $fields)
{
	if (!in_array($type, Array(self::FORM_OFFLINE, self::FORM_WELCOME, self::FORM_HISTORY)))
	{
		$this->error = new Error(__METHOD__, 'FORM_ID', Loc::getMessage('IMOL_LCC_ERROR_FORM_ID'));
		return false;
	}
	
	$access = $this->checkAccess();
	if (!$access['RESULT'])
	{
		return false;
	}
	
	Log::write(Array(
		'FORM' => Array($type, $fields)
	), 'CLIENT FORM CRM');
	
	$chat = new Chat($this->chatId);
	list($configId) = explode('|', $chat->getData('ENTITY_ID'));

	$result = $chat->load(Array(
		'USER_CODE' => 'livechat|'.$configId.'|'.$this->chatId.'|'.$this->userId,
	));
	if (!$result)
	{
		$this->error = new Error(__METHOD__, 'FORM_ID', Loc::getMessage('IMOL_LCC_ERROR_ACCESS_DENIED'));
		return false;
	}
	
	$configManager = new BitrixImOpenLinesConfig();
	$config = $configManager->get($configId);
	
	if (isset($fields['EMAIL']) && !preg_match("/^(.*)@(.*).[a-zA-Z]{2,}$/", $fields['EMAIL']))
	{
		unset($fields['EMAIL']);
	}
	if (isset($fields['PHONE']) && empty($fields['PHONE']))
	{
		unset($fields['PHONE']);
	}
	if (isset($fields['NAME']) && empty($fields['NAME']))
	{
		$fields['NAME'] = Loc::getMessage('IMOL_LCC_GUEST_NAME');
	}
	
	$user = BitrixImUser::getInstance($this->userId);
	
	$userUpdate = Array();
	$chatUpdate = Array();
	$messageParams = Array();
	
	$formSend = false;
	
	if ($type == self::FORM_WELCOME || $type == self::FORM_OFFLINE)
	{
		if (isset($fields['NAME']))
		{
			list($userName, $userLastName) = explode(" ", $fields['NAME'], 2);
			if ($userName && $userLastName)
			{
				if ($userName != $user->getName() || $userLastName != $user->getLastName())
				{
					$userUpdate['NAME'] = $userName;
					$userUpdate['LAST_NAME'] = $userLastName;
				}
			}
			else if ($user->getLastName() != $userName)
			{
				$userUpdate['LAST_NAME'] = $userName;
			}
		}
		if (isset($fields['EMAIL']) && $user->getEmail() != $fields['EMAIL'])
		{
			$userUpdate['EMAIL'] = $fields['EMAIL'];
		}
		if (isset($fields['PHONE']) && $user->getPhone(BitrixImUser::PHONE_MOBILE) != $fields['PHONE'])
		{
			$userUpdate['PERSONAL_MOBILE'] = $fields['PHONE'];
		}
		
		$attach = new CIMMessageParamAttach(null, CIMMessageParamAttach::CHAT);
		$attach->AddGrid(Array(
			Array(
				"NAME" => Loc::getMessage('IMOL_LCC_FORM_NAME'),
				"VALUE" => isset($fields['NAME'])? $fields['NAME']: Loc::getMessage('IMOL_LCC_FORM_NONE'),
				"DISPLAY" => "COLUMN"
			),
			Array(
				"NAME" => Loc::getMessage('IMOL_LCC_FORM_EMAIL'),
				"VALUE" => isset($fields['EMAIL'])? $fields['EMAIL']: Loc::getMessage('IMOL_LCC_FORM_NONE'),
				"DISPLAY" => "COLUMN"
			),
			Array(
				"NAME" => Loc::getMessage('IMOL_LCC_FORM_PHONE'),
				"VALUE" => isset($fields['PHONE'])? $fields['PHONE']: Loc::getMessage('IMOL_LCC_FORM_NONE'),
				"DISPLAY" => "COLUMN"
			),
		));
		
		if (!empty($userUpdate))
		{
			$messageParams = Array(
				"FROM_USER_ID" => $this->userId,
				"MESSAGE" => '[B]'.Loc::getMessage('IMOL_LCC_FORM_SUBMIT').'[/B]',
				"ATTACH" => $attach,
				"SKIP_CONNECTOR" => 'Y',
				"PARAMS" => Array(
					"CLASS" => "bx-messenger-content-item-system"
				),
			);
		}
		
		$formSend = true;
	}
	else if ($type == self::FORM_HISTORY)
	{
		$userUpdate = Array();
		if (isset($fields['EMAIL']) && !$user->getEmail())
		{
			$userUpdate['EMAIL'] = $fields['EMAIL'];
		}
		
		$liveChat = new Chat($this->chatId);
		$chatFieldSession = $liveChat->getFieldData(Chat::FIELD_LIVECHAT);
		
		if (isset($fields['EMAIL']) && $chatFieldSession['SESSION_ID'])
		{
			$attach = new CIMMessageParamAttach(null, CIMMessageParamAttach::CHAT);
			$attach->AddGrid(Array(
				Array(
					"NAME" => Loc::getMessage('IMOL_LCC_FORM_EMAIL'),
					"VALUE" => $fields['EMAIL'],
				),
			));
			$messageParams = Array(
				"FROM_USER_ID" => $this->userId,
				"MESSAGE" => '[B]'.Loc::getMessage('IMOL_LCC_FORM_HISTORY_2', Array("#LINK#" => "[URL=/online/?IM_HISTORY=imol|".$chatFieldSession['SESSION_ID']."]".$chatFieldSession['SESSION_ID']."[/URL]")).'[/B]',
				"ATTACH" => $attach,
				"SKIP_CONNECTOR" => 'Y',
				"SYSTEM" => 'Y',
			);
			
			BitrixImOpenLinesMail::sendSessionHistory($chatFieldSession['SESSION_ID'], $fields['EMAIL']);
		}
		$liveChat->updateFieldData(Chat::FIELD_LIVECHAT, Array( 
			'SESSION_ID' => 0,
			'SHOW_FORM' => 'N'
		));
	}
	
	// update user entity
	if (!empty($userUpdate))
	{
		$userClass = new CUser();
		$userClass->Update($this->userId, $userUpdate);
		BitrixImUser::clearStaticCache();
	}
	
	if (isset($userUpdate['NAME']) || isset($userUpdate['LAST_NAME']))
	{
		$titleParams = $chat->getTitle($config['LINE_NAME'], trim($userUpdate['NAME'].' '.$userUpdate['LAST_NAME']));
		$chatUpdate['TITLE'] = $titleParams['TITLE'];
	}
	
	// update chat entity
	if (!empty($chatUpdate))
	{
		$chat->update($chatUpdate);
	}
	
	// publish info message to chat
	$session = false;
	$sessionStart = false;
	
	if (!empty($messageParams))
	{
		if (!$session)
		{
			$session = new Session();
			$sessionStart = $session->load(Array(
				'USER_CODE' => $chat->getData('ENTITY_ID'),
			));
		}
		$messageParams['TO_CHAT_ID'] = $chat->getData('ID');
		
		$userViewChat = CIMContactList::InRecent($session->getData('OPERATOR_ID'), IM_MESSAGE_CHAT, $session->getData('CHAT_ID'));
		$messageParams['RECENT_ADD'] = $userViewChat? 'Y': 'N';
		
		CIMChat::AddMessage($messageParams);
	}

	// update crm entity
	
	if (
		$type == self::FORM_HISTORY 
		&& empty($userUpdate) 
		&& isset($fields['EMAIL']) && $fields['EMAIL'] != $user->getEmail()
	)
	{
		if (!$session)
		{
			$session = new Session();
			$sessionStart = $session->load(Array(
				'USER_CODE' => $chat->getData('ENTITY_ID'),
			));
		}
		if ($sessionStart)
		{
			$tracker = new BitrixImOpenLinesTracker();
			$tracker->message(Array(
				'SESSION' => $session,
				'MESSAGE' => Array(
					'TEXT' => $fields['EMAIL']
				),
			));
		}
	}
	else if (!empty($userUpdate))
	{
		if ($config['CRM'] == 'Y' && IsModuleInstalled('crm'))
		{
			if (!$session)
			{
				$session = new Session();
				$sessionStart = $session->load(Array(
					'USER_CODE' => $chat->getData('ENTITY_ID'),
				));
			}
			if ($sessionStart)
			{
				$params = $session->getData();
				$crmData = $session->updateCrm(array(
					'CONFIG_ID' => $params['CONFIG_ID'],
					'SESSION_ID' => $params['ID'],
					'MODE' => $params['MODE'],
					'USER_CODE' => $params['USER_CODE'],
					'USER_ID' => $params['USER_ID'],
					'CRM_TITLE' => $chat->getData('TITLE'),
					'OPERATOR_ID' => $params['OPERATOR_ID'],
					'CHAT_ID' => $params['CHAT_ID']
				));
				if ($crmData['CRM'] == 'Y')
				{
					$session->update(Array(
						'CRM' => 'Y',
						'CRM_CREATE' => 'Y',
						'CRM_ENTITY_TYPE' => $crmData['CRM_ENTITY_TYPE'],
						'CRM_ENTITY_ID' => $crmData['CRM_ENTITY_ID'],
						'CRM_ACTIVITY_ID' => $crmData['CRM_ACTIVITY_ID'],
					));
			
					$chat->updateFieldData(Chat::FIELD_SESSION, Array(
						'CRM' => 'Y',
						'CRM_ENTITY_TYPE' => $crmData['CRM_ENTITY_TYPE'],
						'CRM_ENTITY_ID' => $crmData['CRM_ENTITY_ID'],
					));
				}
			}
		}
	}
	
	if ($formSend)
	{
		if (!$session)
		{
			$session = new Session();
			$sessionStart = $session->load(Array(
				'USER_CODE' => $chat->getData('ENTITY_ID'),
			));
		}
		if ($sessionStart)
		{
			$session->update(Array(
				'SEND_FORM' => strtolower($type)
			));
		}
	}
	
	return true;
}