CCrmComponentHelper::SynchronizeFormSettings

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmComponentHelper
  4. SynchronizeFormSettings
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_component_helper.php
  • Класс: \CCrmComponentHelper
  • Вызов: CCrmComponentHelper::SynchronizeFormSettings
static function SynchronizeFormSettings($formID, $userFieldEntityID, $options = array())
{
	$formID = strval($formID);
	$userFieldEntityID = strval($userFieldEntityID);
	$options = is_array($options) ? $options : array();

	if($formID === '')
	{
		return;
	}

	$arOptions = CUserOptions::GetOption('main.interface.form', $formID, array());
	if(isset($arOptions['settings_disabled'])
		&& $arOptions['settings_disabled'] === 'Y'
		|| !(isset($arOptions['tabs']) && is_array($arOptions['tabs'])))
	{
		return;
	}

	$changed = false;

	$normalizeTabs = isset($options['NORMALIZE_TABS']) ? $options['NORMALIZE_TABS'] : array();
	if(!empty($normalizeTabs))
	{
		if(COption::GetOptionString('crm', mb_strtolower($formID).'_normalized', 'N') !== 'Y')
		{
			foreach($arOptions['tabs'] as &$tab)
			{
				if(!in_array($tab['id'], $normalizeTabs, true))
				{
					continue;
				}

				$tabName = $tab['name'];
				// remove counter from tab name
				$tabName = preg_replace('/\s\(\d+\)$/', '', $tabName);
				if($tabName !== $tab['name'])
				{
					$tab['name'] = $tabName;
					if(!$changed)
					{
						$changed = true;
					}
				}
			}
			unset($tab);
			reset($arOptions['tabs']);
			if($changed)
			{
				CUserOptions::SetOption('main.interface.form', $formID, $arOptions);
				$changed = false;
			}
			COption::SetOptionString('crm', mb_strtolower($formID).'_normalized', 'Y');
		}
	}

	if($userFieldEntityID === '')
	{
		return;
	}

	$bRemoveFields = (isset($options['REMOVE_FIELDS']) && is_array($options['REMOVE_FIELDS']));
	$bAddFields = (isset($options['ADD_FIELDS']) && is_array($options['ADD_FIELDS']));
	if ($bRemoveFields)
	{
		foreach($arOptions['tabs'] as &$tab)
		{
			if (is_array($tab) && isset($tab['id']) && isset($tab['fields']) && is_array($tab['fields'])
				&& isset($options['REMOVE_FIELDS'][$tab['id']]) && is_array($options['REMOVE_FIELDS'][$tab['id']]))
			{
				foreach($tab['fields'] as $key => $item)
				{
					if (is_array($item) && isset($item['id']))
					{
						if (in_array($item['id'], $options['REMOVE_FIELDS'][$tab['id']]))
						{
							unset($tab['fields'][$key]);
							$changed = true;
						}
					}
				}
			}
		}
	}
	if ($bAddFields)
	{
		foreach($arOptions['tabs'] as &$tab)
		{
			if (is_array($tab) && isset($tab['id']) && isset($tab['fields']) && is_array($tab['fields'])
				&& isset($options['ADD_FIELDS'][$tab['id']]) && is_array($options['ADD_FIELDS'][$tab['id']]))
			{
				$addFieldsNames = array();
				foreach ($options['ADD_FIELDS'][$tab['id']] as $key => $item)
				{
					if (is_array($item) && isset($item['id']))
						$addFieldsNames[$item['id']] = $key;
				}
				unset($key, $item);
				$addIndex = array();
				$removeIndex = array();
				foreach($tab['fields'] as $key => $item)
				{
					if (is_array($item) && isset($item['id']))
					{
						if (isset($options['ADD_FIELDS'][$tab['id']][$item['id']]))
							$addIndex[$item['id']] = $key;
						if ($addFieldsNames[$item['id']])
							$removeIndex[] = $addFieldsNames[$item['id']];
					}
				}
				unset($key, $item);
				foreach ($removeIndex as $key)
					unset($addIndex[$key]);
				unset($key);
				if (count($addIndex) > 0)
				{
					foreach ($addIndex as $key => $index)
					{
						array_splice(
							$tab['fields'], $index + 1, 0,
							array($index + 1 => $options['ADD_FIELDS'][$tab['id']][$key])
						);
						$changed = true;
					}
					unset($key, $index);
				}
			}
		}
	}

	global $USER_FIELD_MANAGER;
	$arUserFields = $USER_FIELD_MANAGER->GetUserFields($userFieldEntityID);
	if(is_array($arUserFields) && count($arUserFields) > 0)
	{
		foreach($arOptions['tabs'] as &$tab)
		{
			if(!(isset($tab['fields']) && is_array($tab['fields'])))
			{
				continue;
			}

			$arJunkKeys = array();

			foreach($tab['fields'] as $itemKey => $item)
			{
				$itemID = isset($item['id'])? mb_strtoupper($item['id']) : '';
				if(mb_strpos($itemID, 'UF_CRM_') === 0 && !isset($arUserFields[$itemID]))
				{
					$arJunkKeys[] = $itemKey;
				}
			}

			if(count($arJunkKeys) > 0)
			{
				if(!$changed)
				{
					$changed = true;
				}

				foreach($arJunkKeys as $key)
				{
					unset($tab['fields'][$key]);
				}
			}
		}
		unset($tab);
	}

	if($changed)
	{
		CUserOptions::SetOption('main.interface.form', $formID, $arOptions);
	}
}

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