• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/category/dealcategory.php
  • Класс: Bitrix\Crm\Category\DealCategory
  • Вызов: DealCategory::add
static function add(array $fields)
{
	$data = array();
	$data['NAME'] = isset($fields['NAME']) ? trim($fields['NAME']) : '';
	if($data['NAME'] === '')
	{
		throw new AddException(\CCrmOwnerType::DealCategory, array("Field 'NAME' is required."));
	}

	$data['SORT'] = isset($fields['SORT']) ? max((int)$fields['SORT'], 0) : 0;
	$data['CREATED_DATE'] = new Date();

	if (isset($fields['ORIGIN_ID']))
	{
		$data['ORIGIN_ID'] = $fields['ORIGIN_ID'];
	}

	if (isset($fields['ORIGINATOR_ID']))
	{
		$data['ORIGINATOR_ID'] = $fields['ORIGINATOR_ID'];
	}

	/** @var Main\Entity\AddResult $result */
	$result = null;
	try
	{
		$result = DealCategoryTable::add($data);
	}
	catch(\Exception $ex)
	{
		throw new AddException(\CCrmOwnerType::DealCategory, array($ex->getMessage()), 0, '', 0, $ex);
	}

	if(!$result->isSuccess())
	{
		throw new AddException(\CCrmOwnerType::DealCategory, $result->getErrorMessages());
	}

	if(self::$all !== null)
	{
		self::$all = null;
	}

	$ID = $result->getId();
	self::createDefaultStages($ID);

	//region Setup default rights
	$permissionEntity = DealCategory::convertToPermissionEntityType($ID);

	$systemRolesIds = \Bitrix\Crm\Security\Role\RolePermission::getSystemRolesIds();
	$role = new \CCrmRole();
	$roleDbResult = $role->GetList();
	while($roleFields = $roleDbResult->Fetch())
	{
		$roleID = (int)$roleFields['ID'];
		if (in_array($roleID, $systemRolesIds, false)) // do not affect system roles
		{
			continue;
		}
		$roleRelation = \CCrmRole::GetRolePerms($roleID);
		if(isset($roleRelation[$permissionEntity]))
		{
			continue;
		}

		if(!isset($roleRelation[$permissionEntity]))
		{
			$roleRelation[$permissionEntity] = \CCrmRole::GetDefaultPermissionSet();
		}
		$fields = array('RELATION' => $roleRelation);
		$role->Update($roleID, $fields);
	}
	//endregion

	return $ID;
}