• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/volume/base.php
  • Класс: Bitrix\Crm\Volume\Base
  • Вызов: Base::prepareLiveFeedQuery
protected function prepareLiveFeedQuery($entityClass, $eventEntityType, array $entityGroupField = array())
{
	if (!(self::isModuleAvailable('socialnetwork') && self::isModuleAvailable('disk')))
	{
		return '';
	}

	/**
	 * @var ORM\Data\DataManager $entityClass
	 */
	$entityQuery = $entityClass::query();
	$entityEntity = $entityClass::getEntity();

	$entityQuery->addSelect('ID');

	// STAGE_SEMANTIC_ID
	if ($entityClass == Crm\QuoteTable::class)
	{
		Volume\Quote::registerStageField($entityQuery, '', 'QUOTE_STAGE_SEMANTIC_ID');
	}
	if ($entityClass == Crm\InvoiceTable::class)
	{
		Volume\Invoice::registerStageField($entityQuery, '', 'INVOICE_STAGE_SEMANTIC_ID');
	}
	// DATE
	if (
		$entityClass == Crm\CompanyTable::class ||
		$entityClass == Crm\ContactTable::class
	)
	{
		$dayField = new ORM\Fields\ExpressionField(
			'DATE_CREATE_SHORT',
			'DATE(%s)',
			'DATE_CREATE'
		);
		$entityQuery->registerRuntimeField($dayField);
	}
	if (
		$entityClass == Crm\InvoiceTable::class
	)
	{
		$dayField = new ORM\Fields\ExpressionField(
			'DATE_CREATE_SHORT',
			'DATE(%s)',
			'DATE_INSERT'
		);
		$entityQuery->registerRuntimeField($dayField);
	}

	$entityFieldsSql = '';
	$entityFieldsGroupSql = '';
	$entityFields = array();
	foreach ($entityGroupField as $alias => $field)
	{
		$entityQuery->addSelect($field, $alias);
		$entityFields[] = 'entity.'. $alias;
	}

	if ($this->prepareEntityFilter($entityQuery, $entityEntity))
	{
		$entityFilterQuerySql = $entityQuery->getQuery();

		if (count($entityFields) > 0)
		{
			$entityFieldsSql = ', '.implode(', ', $entityFields);
			$entityFieldsGroupSql = 'GROUP BY '.implode(', ', $entityFields);
		}
	}
	else
	{
		// cannot filter this Entity
		return '';
	}

	$logTable = \Bitrix\Socialnetwork\LogTable::getTableName();
	$helper = Main\Application::getConnection()->getSqlHelper();

	$attachedEntitySql = $helper->forSql(Disk\Uf\SonetLogConnector::class);
	$eventEntitySql = $helper->forSql($eventEntityType);

	$querySql = "
		SELECT 
			SUM(ver.SIZE) as FILE_SIZE,
			COUNT(ver.FILE_ID) as FILE_COUNT,
			SUM(ver.SIZE) as DISK_SIZE,
			COUNT(DISTINCT files.ID) as DISK_COUNT
			{$entityFieldsSql}
		FROM 
			b_disk_version ver 
			INNER JOIN b_disk_object files
				ON files.ID  = ver.OBJECT_ID
				AND files.TYPE = '".Disk\Internals\ObjectTable::TYPE_FILE."'
				AND files.ID = files.REAL_OBJECT_ID
			INNER JOIN b_disk_attached_object attached
				ON attached.OBJECT_ID = files.ID
				AND (attached.VERSION_ID IS NULL OR attached.VERSION_ID = ver.ID)
				AND attached.ENTITY_TYPE = '{$attachedEntitySql}'
			INNER JOIN {$logTable} live_feed_log
				ON attached.ENTITY_ID = live_feed_log.ID
				AND live_feed_log.ENTITY_TYPE = '{$eventEntitySql}'
			INNER JOIN ( {$entityFilterQuerySql} ) entity 
				ON entity.ID = live_feed_log.ENTITY_ID
		{$entityFieldsGroupSql}
	";

	return $querySql;
}