ActivityDynamic::parseTemplate

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. ActivityDynamic
  4. parseTemplate
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/widget/data/activity/activitydynamic.php
  • Класс: Bitrix\Crm\Widget\Data\Activity\ActivityDynamic
  • Вызов: ActivityDynamic::parseTemplate
static function parseTemplate(array $groupItems, array $items, array $results)
{
	if (!(is_array($groupItems) && !empty($groupItems) && is_array($items) && !empty($items)))
	{
		Loc::getMessage("CRM_ACTIVITY_DYNAMIC_CHANNELS_ARE_NOT_FOUND");
	}

	$mess = self::includeModuleFile();

	$maxCounter = array(
		\CCrmActivityDirection::Incoming => 0,
		\CCrmActivityDirection::Outgoing => 0
	);

	foreach ($results as $res)
	{
		$value = (int)$res["COUNT"];
		if (array_key_exists($res["CHANNEL_ID"], $items))
		{
			$item = &$items[$res["CHANNEL_ID"]];
			$direction = ((int)$res["DIRECTION"] == \CCrmActivityDirection::Outgoing ? \CCrmActivityDirection::Outgoing : \CCrmActivityDirection::Incoming);
			$item["COUNTERS"][$direction] += $value;
			$groupID = $item["GROUP_ID"];
			if ($groupID !== '' && isset($groupItems[$groupID]))
			{
				$groupItems[$groupID]["COUNTERS"][$direction] += $item["COUNTERS"][$direction];
				$groupItems[$groupID]["MAX_COUNTER"][$direction] = max($groupItems[$groupID]["MAX_COUNTER"][$direction], $item["COUNTERS"][$direction]);
				$maxCounter[$direction] = max($maxCounter[$direction], $groupItems[$groupID]["COUNTERS"][$direction]);
				$parentId = $groupItems[$groupID]["PARENT_ID"];
				if ($parentId && isset($groupItems[$parentId]))
				{
					$groupItems[$parentId]["COUNTERS"][$direction] += $item["COUNTERS"][$direction];
					$groupItems[$parentId]["MAX_COUNTER"][$direction] = max($groupItems[$parentId]["MAX_COUNTER"][$direction], $item["COUNTERS"][$direction]);
					$maxCounter[$direction] = max($maxCounter[$direction], $groupItems[$parentId]["COUNTERS"][$direction]);
				}
			}
		}
	}
	unset($item);

	$activities = array("active" => "", "inactive" => "");
	$inactiveItems = array();
	$inactiveItemCaptions = array();
	$activeCount = 0;
	$telephonyIsChecked = false;
	foreach($activities as $status => &$html)
	{
		if ($item = reset($items))
		{
			$groupStack = array();
			$innerHTML = array();
			$currentGroupID = '';
			do
			{
				$groupID = $item['GROUP_ID'];
				$group = isset($groupItems[$groupID]) ? $groupItems[$groupID] : null;
				if ($status === "active" && ($groupID === '' || is_null($group) ||
						isset($group['IS_IN_USE']) && !$group['IS_IN_USE'] ||
						isset($item['IS_IN_USE']) && !$item['IS_IN_USE']))
				{
					if (isset($item['IS_CONFIGURABLE']) && $item['IS_CONFIGURABLE'])
					{
						$inactiveItems[] = $item;
						if ($groupID !== \Bitrix\Crm\Integration\Channel\VoxImplantTracker::GROUP_ID)
						{
							$inactiveItemCaptions[] = $item["CAPTION"];
						}
						else if ($telephonyIsChecked === false)
						{
							$telephonyIsChecked = true;
							$inactiveItemCaptions[] = $group["CAPTION"];
						}
					}
					continue;
				}
				$activeCount++;
				if ($groupID !== $currentGroupID)
				{
					$innerHTML["#" . $groupID . "#"] = "";
					$parent = ($group === null ? null : $group["PARENT_ID"]);
					$parentGroup = isset($groupItems[$parent]) ? $groupItems[$parent] : null;

					if (is_null($group) || ($status === "active" && ($parentGroup === null || $parentGroup["ID"] != ChannelType::IMOPENLINE_NAME) && $group['ACTIVE_ITEMS_COUNT'] <= 1))
					{
						if (!is_null($group))
							$group["hidden"] = "Y";
						$html .= "#" . $groupID . "#";
					}
					else
					{
						$groups = array();
						reset($groupStack);
						while ($parentGroup !== null)
						{
							if ($parentGroup && (
								$status !== "active" ||
								$parentGroup["ID"] != ChannelType::IMOPENLINE_NAME ||
								$parentGroup["ITEMS_COUNT"] > $group["ITEMS_COUNT"])
							)
							{
								$groups[] = array(
									"id" => $parentGroup['ID'],
									"url" => $parentGroup['IS_CONFIGURABLE'] ? $parentGroup['URL'] : '',
									"caption" => $parentGroup['CAPTION'],
									"counters" => $parentGroup["COUNTERS"]);
								$groupStack[] = $parent;
								$parent = $parentGroup["PARENT_ID"];
							}
							else
							{
								$parent = null;
							}
							$parentGroup = isset($groupItems[$parent]) ? $groupItems[$parent] : null;
						}
						$groups = array_reverse($groups);
						$groups[] = array(
							"id" => $group['ID'],
							"caption" => $group['CAPTION'],
							"url" => $group['IS_CONFIGURABLE'] ? $group['URL'] : '',
							"counters" => $group["COUNTERS"]);
						$g = reset($groups);
						$lastGroupID = null;
						do
						{
							$id = "#{$g["id"]}#";
							$caption = htmlspecialcharsbx($g["caption"]);
							$caption = ($g["url"] !== '' ? '' . $caption . '' : '' . $caption . '');
							if (mb_strpos($html, $id) === false)
							{
								if ($status !== "active")
								{
									$g["counters"][\CCrmActivityDirection::Incoming] = 0;
									$g["counters"][\CCrmActivityDirection::Outgoing] = 0;
								}
								$g["percentage"][\CCrmActivityDirection::Incoming] = 0;
								if ($maxCounter[\CCrmActivityDirection::Incoming] > 0)
									$g["percentage"][\CCrmActivityDirection::Incoming] = intval($g["counters"][\CCrmActivityDirection::Incoming] * 100 / $maxCounter[\CCrmActivityDirection::Incoming]);
								$g["percentage"][\CCrmActivityDirection::Incoming] = max($g["percentage"][\CCrmActivityDirection::Incoming], 1);
								$g["percentage"][\CCrmActivityDirection::Outgoing] = 0;
								if ($maxCounter[\CCrmActivityDirection::Outgoing] > 0)
									$g["percentage"][\CCrmActivityDirection::Outgoing] = intval($g["counters"][\CCrmActivityDirection::Outgoing] * 100/ $maxCounter[\CCrmActivityDirection::Outgoing]);
								$g["percentage"][\CCrmActivityDirection::Outgoing] = max($g["percentage"][\CCrmActivityDirection::Outgoing], 1);
								$htmlG = <<
							
{$g["counters"][\CCrmActivityDirection::Incoming]}
{$g["counters"][\CCrmActivityDirection::Outgoing]}
{$caption}
{$id}
HTML; if ($lastGroupID === null) { $html .= $htmlG; } else { $html = str_replace("#{$lastGroupID}#", $htmlG . "#{$lastGroupID}#", $html); } if (!isset($innerHTML[$id])) { $innerHTML[$id] = ""; } } $lastGroupID = $g["id"]; } while ($g = next($groups)); } } $caption = htmlspecialcharsbx($item["CAPTION"]); $caption = ($item["CONFIG_URL"] !== '' && $item['IS_CONFIGURABLE'] ? '' . $caption . '' : $caption); $item["PERCENTAGE"][\CCrmActivityDirection::Incoming] = 0; $item["PERCENTAGE"][\CCrmActivityDirection::Outgoing] = 0; if (is_null($group) || ($group["hidden"] ?? '') === "Y") { if ($maxCounter[\CCrmActivityDirection::Incoming] > 0) $item["PERCENTAGE"][\CCrmActivityDirection::Incoming] = intval($item["COUNTERS"][\CCrmActivityDirection::Incoming] * 100 / $maxCounter[\CCrmActivityDirection::Incoming]); $item["PERCENTAGE"][\CCrmActivityDirection::Incoming] = max($item["PERCENTAGE"][\CCrmActivityDirection::Incoming], 1); if ($maxCounter[\CCrmActivityDirection::Outgoing] > 0) $item["PERCENTAGE"][\CCrmActivityDirection::Outgoing] = intval($item["COUNTERS"][\CCrmActivityDirection::Outgoing] * 100/ $maxCounter[\CCrmActivityDirection::Outgoing]); $item["PERCENTAGE"][\CCrmActivityDirection::Outgoing] = max($item["PERCENTAGE"][\CCrmActivityDirection::Outgoing], 1); $innerHTML["#" . $groupID . "#"] .= <<
{$item["COUNTERS"][\CCrmActivityDirection::Incoming]}
{$item["COUNTERS"][\CCrmActivityDirection::Outgoing]}
{$caption}
HTML; } else { if ($group["MAX_COUNTER"][\CCrmActivityDirection::Incoming] > 0) $item["PERCENTAGE"][\CCrmActivityDirection::Incoming] = intval($item["COUNTERS"][\CCrmActivityDirection::Incoming] * 100 / $maxCounter[\CCrmActivityDirection::Incoming]); $item["PERCENTAGE"][\CCrmActivityDirection::Incoming] = max($item["PERCENTAGE"][\CCrmActivityDirection::Incoming], 1); if ($group["MAX_COUNTER"][\CCrmActivityDirection::Outgoing] > 0) $item["PERCENTAGE"][\CCrmActivityDirection::Outgoing] = intval($item["COUNTERS"][\CCrmActivityDirection::Outgoing] * 100/ $maxCounter[\CCrmActivityDirection::Outgoing]); $item["PERCENTAGE"][\CCrmActivityDirection::Outgoing] = max($item["PERCENTAGE"][\CCrmActivityDirection::Outgoing], 1); $innerHTML["#" . $groupID . "#"] .= <<
{$caption}
{$item["COUNTERS"][\CCrmActivityDirection::Incoming]}
{$item["COUNTERS"][\CCrmActivityDirection::Outgoing]}
HTML; } $currentGroupID = $groupID; } while ($item = next($items)) ; $html = str_replace(array_keys($innerHTML), array_values($innerHTML), $html); } $items = $inactiveItems; } $innerHTML = ''; if ($activities["active"] !== "") { $innerHTML = <<
{$mess["CRM_ACTIVITY_DYNAMIC_CONNECTED_CHANNELS"]}
{$mess["CRM_ACTIVITY_DYNAMIC_CONNECTED_CHANNELS_IN"]}
{$mess["CRM_ACTIVITY_DYNAMIC_CONNECTED_CHANNELS_OUT"]}
{$activities["active"]}
HTML; } $id = randString(10); if ($activities["inactive"] !== "") { $inactiveItemCaptions = implode(", ", array_slice($inactiveItemCaptions, 0, 3))." ".(count($inactiveItemCaptions) > 3 ? $mess["CRM_ACTIVITY_DYNAMIC_UNCONNECTED_1"] : ""); $inactiveItems = count($inactiveItems); $activeCount -= $inactiveItems; $innerHTML .= <<
{$mess["CRM_ACTIVITY_DYNAMIC_UNCONNECTED_3"]} {$inactiveItemCaptions}
{$mess["CRM_ACTIVITY_DYNAMIC_CONNECTED"]}{$activeCount}
{$mess["CRM_ACTIVITY_DYNAMIC_CONNECT"]}{$inactiveItems}
HTML; } if ($innerHTML !== '') { $videoUrl = GetMessageJS("CRM_CH_TRACKER_START_VIDEO", array("#VOLUME#" => "")); $videoFnc = ""; if (!(($res = \CUserOptions::GetOption("crm.widget", "activityDynamic")) && is_array($res) && $res["firstSeen"] === "N")) { $videoFnc = "f(true);"; $videoUrl = GetMessageJS("CRM_CH_TRACKER_START_VIDEO", array("#VOLUME#" => "&volume=0&mute=1")); } $innerHTML .= << BX.ready(function() { var node = BX('custom_widget_{$id}'); if (node && (node = BX.findParent(node, {className : "crm-widget-container"})) && node) BX.addClass(node, "crm-widget-container-height-auto"); if (node && (node = BX.findParent(node, {className : "crm-widget-row"})) && node) BX.addClass(node, "crm-widget-row-height-auto"); var openChanelDeeper = function() { var block = this.parentNode, blockDeeper = block.querySelector('[data-role="inner-chanel"]'), blockDeeperHeight = blockDeeper.firstElementChild.offsetHeight, i; if (block.classList.contains('crm-widget-activity-dynamic-item-open')) { block.classList.remove('crm-widget-activity-dynamic-item-open'); blockDeeper.style.height = ''; var blockDeeperInner = block.querySelectorAll('[data-role="inner-chanel"]'); for (i = 0; i < blockDeeperInner.length; i++) { blockDeeperInner[i].parentNode.classList.remove('crm-widget-activity-dynamic-item-open'); blockDeeperInner[i].style.height = ''; } } else { block.classList.add('crm-widget-activity-dynamic-item-open'); blockDeeper.style.height = blockDeeperHeight + 'px'; setTimeout(function () { blockDeeper.style.height = 'auto'; }, 220); } }, showChanelButton = node.querySelectorAll('[data-role="open-chanel"]'), i; for (i = 0; i < showChanelButton.length; i++) BX.bind(showChanelButton[i], "click", openChanelDeeper); var visState = false; BX.bind(BX('custom_widget_inactive_control_$id'), "click", function() { BX.fx[(visState === true ? "hide" : "show")](BX('custom_widget_inactive_container_$id')); visState = !visState; }); var f = function(firstTime) { (new BX.PopupWindow('crm-start-video-window', null, { className: "crm-start-video-window", autoHide: false, zIndex: 200, overlay: {opacity: 50, backgroundColor: "#000000"}, closeByEsc: true, closeIcon : true, contentColor : "white", content : '{$videoUrl}', events : { onPopupClose : function(){ this.destroy(); BX.onCustomEvent(window, "crm.widget", ["activityDynamic", "close", BX("custom_widget_{$id}_click_node")]); BX.userOptions.save('crm.widget', 'activityDynamic', 'firstSeen', 'N'); } } })).show(); }; node = BX.findParent(BX('custom_widget_{$id}'), {className : "crm-widget-container"}); if (node && (node = BX.findChild(node, {className : "crm-widget-settings"}, true)) && node) { var vNode = node.cloneNode(true); BX.addClass(vNode, "crm-start-title-icons-item-video"); vNode.setAttribute("id", "custom_widget_{$id}_click_node"); BX.bind(vNode, "click", f); if (node.nextSibling) node.parentNode.insertBefore(vNode, node.nextSibling); else node.parentNode.appendChild(vNode); BX.onCustomEvent(window, "crm.widget", ["activityDynamic", "append", vNode]); if (BX('custom_widget_inactive_control_$id')) BX.onCustomEvent(window, "crm.widget", ["inactiveControl", "append", BX('custom_widget_inactive_control_$id')]); } {$videoFnc} }); HTML; } return '
'.$innerHTML.'
'; }