Пример #1
0
	public static function PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields, $arUF = array())
	{
		global $DB;

		$obUserFieldsSql = false;
		
		if (is_array($arUF) && array_key_exists("ENTITY_ID", $arUF))
		{
			$obUserFieldsSql = new CUserTypeSQL;
			$obUserFieldsSql->SetEntity($arUF["ENTITY_ID"], $arFields["ID"]["FIELD"]);
			$obUserFieldsSql->SetSelect($arSelectFields);
			$obUserFieldsSql->SetFilter($arFilter);
			$obUserFieldsSql->SetOrder($arOrder);
		}

		$strSqlSelect = "";
		$strSqlFrom = "";
		$strSqlWhere = "";
		$strSqlGroupBy = "";
		$strSqlOrderBy = "";

		$arGroupByFunct = array("COUNT", "AVG", "MIN", "MAX", "SUM");

		$arAlreadyJoined = array();

		// GROUP BY -->
		if (is_array($arGroupBy) && count($arGroupBy)>0)
		{
			$arSelectFields = $arGroupBy;
			foreach ($arGroupBy as $key => $val)
			{
				$val = strtoupper($val);
				$key = strtoupper($key);
				if (array_key_exists($val, $arFields) && !in_array($key, $arGroupByFunct))
				{
					if (strlen($strSqlGroupBy) > 0)
						$strSqlGroupBy .= ", ";
					$strSqlGroupBy .= $arFields[$val]["FIELD"];

					if (isset($arFields[$val]["FROM"])
						&& strlen($arFields[$val]["FROM"]) > 0
						&& !in_array($arFields[$val]["FROM"], $arAlreadyJoined))
					{
						if (strlen($strSqlFrom) > 0)
							$strSqlFrom .= " ";
						$strSqlFrom .= $arFields[$val]["FROM"];
						$arAlreadyJoined[] = $arFields[$val]["FROM"];
					}
				}
			}
		}
		// <-- GROUP BY

		// WHERE -->
		$arSqlSearch = Array();

		if (!is_array($arFilter))
			$filter_keys = Array();
		else
			$filter_keys = array_keys($arFilter);

		$tmp_count = count($filter_keys);
		for ($i = 0; $i < $tmp_count; $i++)
		{
			$vals = $arFilter[$filter_keys[$i]];
			if (!is_array($vals))
				$vals = array($vals);

			$key = $filter_keys[$i];
			$key_res = CSocNetGroup::GetFilterOperation($key);
			$key = $key_res["FIELD"];
			$strNegative = $key_res["NEGATIVE"];
			$strOperation = $key_res["OPERATION"];
			$strOrNull = $key_res["OR_NULL"];

			if (array_key_exists($key, $arFields))
			{
				$arSqlSearch_tmp = array();
				foreach ($vals as $val)
				{
					if (isset($arFields[$key]["WHERE"]))
					{
						$arSqlSearch_tmp1 = call_user_func_array(
								$arFields[$key]["WHERE"],
								array($val, $key, $strOperation, $strNegative, $arFields[$key]["FIELD"], &$arFields, &$arFilter)
							);
						if ($arSqlSearch_tmp1 !== false)
							$arSqlSearch_tmp[] = $arSqlSearch_tmp1;
					}
					else
					{
						if ($arFields[$key]["TYPE"] == "int")
						{
							if ((IntVal($val) == 0) && (strpos($strOperation, "=") !== False))
								$arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
							else
								$arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".IntVal($val)." )";
						}
						elseif ($arFields[$key]["TYPE"] == "double")
						{
							$val = str_replace(",", ".", $val);

							if ((DoubleVal($val) == 0) && (strpos($strOperation, "=") !== False))
								$arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
							else
								$arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".DoubleVal($val)." )";
						}
						elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
						{
							if ($strOperation == "QUERY")
								$arSqlSearch_tmp[] = GetFilterQuery($arFields[$key]["FIELD"], $val, (strpos($val, "%") === false ? "Y" : "N"));
							else
							{
								if ((strlen($val) == 0) && (strpos($strOperation, "=") !== False))
									$arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$DB->Length($arFields[$key]["FIELD"])." <= 0) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$arFields[$key]["FIELD"]." ".$strOperation." '".$DB->ForSql($val)."' )";
								else
									$arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." '".$DB->ForSql($val)."' )";
							}
						}
						elseif ($arFields[$key]["TYPE"] == "string_or_null" || $arFields[$key]["TYPE"] == "char_or_null")
						{
							if ($strOperation == "QUERY")
								$arSqlSearch_tmp[] = GetFilterQuery($arFields[$key]["FIELD"], $val, (strpos($val, "%") === false ? "Y" : "N"));
							else
							{
								if ((strlen($val) == 0) && (strpos($strOperation, "=") !== False))
								{
// future functionality
								}
								else
									$arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." ".$strOperation." '".$DB->ForSql($val)."' OR ".$arFields[$key]["FIELD"]." IS NULL)";
							}
						}
						elseif ($arFields[$key]["TYPE"] == "datetime")
						{
							if (strlen($val) <= 0)
								$arSqlSearch_tmp[] = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
							elseif (strtoupper($val) === "NOW")
								$arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->GetNowFunction().")";
							else
								$arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($DB->ForSql($val), "FULL").")";
						}
						elseif ($arFields[$key]["TYPE"] == "date")
						{
							if (strlen($val) <= 0)
								$arSqlSearch_tmp[] = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
							else
								$arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($DB->ForSql($val), "SHORT").")";
						}
					}
				}

				if (isset($arFields[$key]["FROM"])
					&& strlen($arFields[$key]["FROM"]) > 0
					&& !in_array($arFields[$key]["FROM"], $arAlreadyJoined))
				{
					if (strlen($strSqlFrom) > 0)
						$strSqlFrom .= " ";
					$strSqlFrom .= $arFields[$key]["FROM"];
					$arAlreadyJoined[] = $arFields[$key]["FROM"];
				}

				$strSqlSearch_tmp = "";
				$tmp_count_1 = count($arSqlSearch_tmp);
				for ($j = 0; $j < $tmp_count_1; $j++)
				{
					if ($j > 0)
						$strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
					$strSqlSearch_tmp .= "(".$arSqlSearch_tmp[$j].")";
				}
				if ($strOrNull == "Y")
				{
					if (strlen($strSqlSearch_tmp) > 0)
						$strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
					$strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." IS ".($strNegative=="Y" ? "NOT " : "")."NULL)";

					if (strlen($strSqlSearch_tmp) > 0)
						$strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
					if ($arFields[$key]["TYPE"] == "int" || $arFields[$key]["TYPE"] == "double")
						$strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." 0)";
					elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
						$strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." '')";
				}

				if ($strSqlSearch_tmp != "")
					$arSqlSearch[] = "(".$strSqlSearch_tmp.")";
			}
		}

		$tmp_count = count($arSqlSearch);
		for ($i = 0; $i < $tmp_count; $i++)
		{
			if (strlen($strSqlWhere) > 0)
				$strSqlWhere .= " AND ";
			$strSqlWhere .= "(".$arSqlSearch[$i].")";
		}
		
		if ($obUserFieldsSql)
		{
			$r = $obUserFieldsSql->GetFilter();
			if(strlen($r) > 0)
				$strSqlWhere .= (strlen($strSqlWhere) > 0 ? " AND" : "")." (".$r.") ";
		}
		// <-- WHERE

		// ORDER BY -->
		$arSqlOrder = Array();
		foreach ($arOrder as $by => $order)
		{
			$by = strtoupper($by);
			$order = strtoupper($order);

			if ($order != "ASC")
				$order = "DESC";
			else
				$order = "ASC";

			if (array_key_exists($by, $arFields))
			{
				if ($arFields[$by]["TYPE"] == "datetime" || $arFields[$by]["TYPE"] == "date")
				{
					$arSqlOrder[] = " ".$by."_X1 ".$order." ";
					if (!is_array($arSelectFields) || !in_array($by, $arSelectFields))
						$arSelectFields[] = $by;
				}
				else
					$arSqlOrder[] = " ".$arFields[$by]["FIELD"]." ".$order." ";

				if (isset($arFields[$by]["FROM"])
					&& strlen($arFields[$by]["FROM"]) > 0
					&& !in_array($arFields[$by]["FROM"], $arAlreadyJoined))
				{
					if (strlen($strSqlFrom) > 0)
						$strSqlFrom .= " ";
					$strSqlFrom .= $arFields[$by]["FROM"];
					$arAlreadyJoined[] = $arFields[$by]["FROM"];
				}
			}
			elseif($obUserFieldsSql && $s = $obUserFieldsSql->GetOrder($by))
				$arSqlOrder[$by] = " ".$s." ".$order." ";
		}
		
		$strSqlOrderBy = "";
		DelDuplicateSort($arSqlOrder); 
		$tmp_count = count($arSqlOrder);
		for ($i=0; $i < $tmp_count; $i++)
		{
			if (strlen($strSqlOrderBy) > 0)
				$strSqlOrderBy .= ", ";

			if(strtoupper($DB->type)=="ORACLE")
			{
				if(substr($arSqlOrder[$i], -3)=="ASC")
					$strSqlOrderBy .= $arSqlOrder[$i]." NULLS FIRST";
				else
					$strSqlOrderBy .= $arSqlOrder[$i]." NULLS LAST";
			}
			else
				$strSqlOrderBy .= $arSqlOrder[$i];
		}
		// <-- ORDER BY

		// SELECT -->
		$arFieldsKeys = array_keys($arFields);

		if (is_array($arGroupBy) && count($arGroupBy)==0)
			$strSqlSelect = "COUNT(%%_DISTINCT_%% ".$arFields[$arFieldsKeys[0]]["FIELD"].") as CNT ";
		else
		{
			if (isset($arSelectFields) && !is_array($arSelectFields) && is_string($arSelectFields) && strlen($arSelectFields)>0 && array_key_exists($arSelectFields, $arFields))
				$arSelectFields = array($arSelectFields);

			if (!isset($arSelectFields)
				|| !is_array($arSelectFields)
				|| count($arSelectFields) <= 0
				|| in_array("*", $arSelectFields))
			{
				$tmp_count = count($arFieldsKeys);
				for ($i = 0; $i < $tmp_count; $i++)
				{
					if (isset($arFields[$arFieldsKeys[$i]]["WHERE_ONLY"])
						&& $arFields[$arFieldsKeys[$i]]["WHERE_ONLY"] == "Y")
						continue;

					if (strlen($strSqlSelect) > 0)
						$strSqlSelect .= ", ";

					if ($arFields[$arFieldsKeys[$i]]["TYPE"] == "datetime")
					{
						if (array_key_exists($arFieldsKeys[$i], $arOrder))
							$strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"]." as ".$arFieldsKeys[$i]."_X1, ";

						$strSqlSelect .= $DB->DateToCharFunction($arFields[$arFieldsKeys[$i]]["FIELD"], "FULL")." as ".$arFieldsKeys[$i];
					}
					elseif ($arFields[$arFieldsKeys[$i]]["TYPE"] == "date")
					{
						if (array_key_exists($arFieldsKeys[$i], $arOrder))
							$strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"]." as ".$arFieldsKeys[$i]."_X1, ";

						$strSqlSelect .= $DB->DateToCharFunction($arFields[$arFieldsKeys[$i]]["FIELD"], "SHORT")." as ".$arFieldsKeys[$i];
					}
					else
						$strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"]." as ".$arFieldsKeys[$i];

					if (isset($arFields[$arFieldsKeys[$i]]["FROM"])
						&& strlen($arFields[$arFieldsKeys[$i]]["FROM"]) > 0
						&& !in_array($arFields[$arFieldsKeys[$i]]["FROM"], $arAlreadyJoined))
					{
						if (strlen($strSqlFrom) > 0)
							$strSqlFrom .= " ";
						$strSqlFrom .= $arFields[$arFieldsKeys[$i]]["FROM"];
						$arAlreadyJoined[] = $arFields[$arFieldsKeys[$i]]["FROM"];
					}
				}
			}
			else
			{
				foreach ($arSelectFields as $key => $val)
				{
					$val = strtoupper($val);
					$key = strtoupper($key);
					if (array_key_exists($val, $arFields))
					{
						if (strlen($strSqlSelect) > 0)
							$strSqlSelect .= ", ";

						if (in_array($key, $arGroupByFunct))
							$strSqlSelect .= $key."(".$arFields[$val]["FIELD"].") as ".$val;
						else
						{
							if ($arFields[$val]["TYPE"] == "datetime")
							{
								if (array_key_exists($val, $arOrder))
									$strSqlSelect .= $arFields[$val]["FIELD"]." as ".$val."_X1, ";

								$strSqlSelect .= $DB->DateToCharFunction($arFields[$val]["FIELD"], "FULL")." as ".$val;
							}
							elseif ($arFields[$val]["TYPE"] == "date")
							{
								if (array_key_exists($val, $arOrder))
									$strSqlSelect .= $arFields[$val]["FIELD"]." as ".$val."_X1, ";

								$strSqlSelect .= $DB->DateToCharFunction($arFields[$val]["FIELD"], "SHORT")." as ".$val;
							}
							else
								$strSqlSelect .= $arFields[$val]["FIELD"]." as ".$val;
						}

						if (isset($arFields[$val]["FROM"])
							&& strlen($arFields[$val]["FROM"]) > 0
							&& !in_array($arFields[$val]["FROM"], $arAlreadyJoined))
						{
							if (strlen($strSqlFrom) > 0)
								$strSqlFrom .= " ";
							$strSqlFrom .= $arFields[$val]["FROM"];
							$arAlreadyJoined[] = $arFields[$val]["FROM"];
						}
					}
				}
			}

			if ($obUserFieldsSql)
				$strSqlSelect .= (strlen($strSqlSelect) <= 0 ? $arFields["ID"]["FIELD"] : "").$obUserFieldsSql->GetSelect();
			
			if (strlen($strSqlGroupBy) > 0)
			{
				if (strlen($strSqlSelect) > 0)
					$strSqlSelect .= ", ";
				$strSqlSelect .= "COUNT(%%_DISTINCT_%% ".$arFields[$arFieldsKeys[0]]["FIELD"].") as CNT";
			}
			else
				$strSqlSelect = "%%_DISTINCT_%% ".$strSqlSelect;
		}
		// <-- SELECT

		if ($obUserFieldsSql)
			$strSqlFrom .= " ".$obUserFieldsSql->GetJoin($arFields["ID"]["FIELD"]);
		
		return array(
			"SELECT" => $strSqlSelect,
			"FROM" => $strSqlFrom,
			"WHERE" => $strSqlWhere,
			"GROUPBY" => $strSqlGroupBy,
			"ORDERBY" => $strSqlOrderBy
		);
	}
Пример #2
0
 function GetSubSelect($entityId, $entity_type = false, $entity_id = false, $event_id = false, $created_by_id = false, $arOfEntities = false, $arAdmin = false, $transport = false, $visible = "Y", $type = "L", $params = array(), $bDecrement = false, $bForAllAccess = false)
 {
     global $DB;
     if (is_array($entityId) && isset($entityId["LOG_ID"])) {
         $arFields = $entityId;
         $entityId = intval($arFields["LOG_ID"]);
         $entity_type = isset($arFields["ENTITY_TYPE"]) ? $arFields["ENTITY_TYPE"] : false;
         $entity_id = isset($arFields["ENTITY_ID"]) ? $arFields["ENTITY_ID"] : false;
         $event_id = isset($arFields["EVENT_ID"]) ? $arFields["EVENT_ID"] : false;
         $created_by_id = isset($arFields["CREATED_BY_ID"]) ? $arFields["CREATED_BY_ID"] : false;
         $arOfEntities = isset($arFields["ENTITIES"]) ? $arFields["ENTITIES"] : false;
         $transport = isset($arFields["TRANSPORT"]) ? $arFields["TRANSPORT"] : false;
         $visible = isset($arFields["VISIBLE"]) ? $arFields["VISIBLE"] : "Y";
         $type = isset($arFields["TYPE"]) ? $arFields["TYPE"] : "L";
         $code = isset($arFields["CODE"]) ? $arFields["CODE"] : false;
         $params = isset($arFields["PARAMS"]) ? $arFields["PARAMS"] : array();
         $bDecrement = isset($arFields["DECREMENT"]) ? $arFields["DECREMENT"] : false;
         $bMultiple = isset($arFields["MULTIPLE"]) && $arFields["MULTIPLE"] == "Y";
         $IsForAllAccessOnly = false;
         if (isset($arFields["FOR_ALL_ACCESS_ONLY"])) {
             $IsForAllAccessOnly = $arFields["FOR_ALL_ACCESS_ONLY"] ? "Y" : "N";
         }
         $bForAllAccess = $IsForAllAccessOnly == "Y" ? true : (isset($arFields["FOR_ALL_ACCESS"]) ? $arFields["FOR_ALL_ACCESS"] : false);
         $tagSet = isset($arFields["TAG_SET"]) ? $arFields["TAG_SET"] : false;
     }
     if (intval($entityId) <= 0) {
         return false;
     }
     $arSocNetAllowedSubscribeEntityTypesDesc = CSocNetAllowed::GetAllowedEntityTypesDesc();
     $bGroupCounters = $type === "group";
     $params = is_array($params) ? $params : array();
     $params['CODE'] = !empty($params['CODE']) ? $params['CODE'] : ($code ? $code : ($bGroupCounters ? "SLR0.GROUP_CODE" : "'**" . ($bMultiple ? $type . $entityId : "") . "'"));
     if ($type == "L" && ($arLog = CSocNetLog::GetByID($entityId))) {
         $logId = $entityId;
         $entity_type = $arLog["ENTITY_TYPE"];
         $entity_id = $arLog["ENTITY_ID"];
         $event_id = $arLog["EVENT_ID"];
         $created_by_id = $arLog["USER_ID"];
         $log_user_id = $arLog["USER_ID"];
     } elseif ($type == "LC" && ($arLogComment = CSocNetLogComments::GetByID($entityId))) {
         $entity_type = $arLogComment["ENTITY_TYPE"];
         $entity_id = $arLogComment["ENTITY_ID"];
         $event_id = $arLogComment["EVENT_ID"];
         $created_by_id = $arLogComment["USER_ID"];
         $logId = $arLogComment["LOG_ID"];
         // recalculate log_id
         $log_user_id = $arLogComment["LOG_USER_ID"];
     } else {
         $logId = $entityId;
     }
     if (!in_array($entity_type, CSocNetAllowed::GetAllowedEntityTypes()) || intval($entity_id) <= 0 || strlen($event_id) <= 0) {
         return false;
     }
     if (!$arOfEntities) {
         if (array_key_exists($entity_type, $arSocNetAllowedSubscribeEntityTypesDesc) && array_key_exists("HAS_MY", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["HAS_MY"] == "Y" && array_key_exists("CLASS_OF", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && array_key_exists("METHOD_OF", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && strlen($arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["CLASS_OF"]) > 0 && strlen($arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["METHOD_OF"]) > 0 && method_exists($arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["CLASS_OF"], $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["METHOD_OF"])) {
             $arOfEntities = call_user_func(array($arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["CLASS_OF"], $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["METHOD_OF"]), $entity_id);
         } else {
             $arOfEntities = array();
         }
     }
     if ((!defined("DisableSonetLogVisibleSubscr") || DisableSonetLogVisibleSubscr !== true) && $visible && strlen($visible) > 0) {
         $key_res = CSocNetGroup::GetFilterOperation($visible);
         $strField = $key_res["FIELD"];
         $strNegative = $key_res["NEGATIVE"];
         $strOperation = $key_res["OPERATION"];
         $visibleFilter = "AND (" . ($strNegative == "Y" ? " SLE.VISIBLE IS NULL OR NOT " : "") . "(SLE.VISIBLE " . $strOperation . " '" . $DB->ForSql($strField) . "'))";
         $transportFilter = "";
     } else {
         $visibleFilter = "";
         if ($transport && strlen($transport) > 0) {
             $key_res = CSocNetGroup::GetFilterOperation($transport);
             $strField = $key_res["FIELD"];
             $strNegative = $key_res["NEGATIVE"];
             $strOperation = $key_res["OPERATION"];
             $transportFilter = "AND (" . ($strNegative == "Y" ? " SLE.TRANSPORT IS NULL OR NOT " : "") . "(SLE.TRANSPORT " . $strOperation . " '" . $DB->ForSql($strField) . "'))";
         } else {
             $transportFilter = "";
         }
     }
     if ($type == "LC" && (!defined("DisableSonetLogFollow") || DisableSonetLogFollow !== true)) {
         $default_follow = COption::GetOptionString("socialnetwork", "follow_default_type", "Y");
         if ($default_follow == "Y") {
             $followJoin = " LEFT JOIN b_sonet_log_follow LFW ON LFW.USER_ID = U.ID AND (LFW.CODE = 'L" . $logId . "' OR LFW.CODE = '**') ";
             $followWhere = "AND (LFW.USER_ID IS NULL OR LFW.TYPE = 'Y')";
         } else {
             $followJoin = " \n\t\t\t\t\tINNER JOIN b_sonet_log_follow LFW ON LFW.USER_ID = U.ID AND (LFW.CODE = 'L" . $logId . "' OR LFW.CODE = '**')\n\t\t\t\t\tLEFT JOIN b_sonet_log_follow LFW2 ON LFW2.USER_ID = U.ID AND (LFW2.CODE = 'L" . $logId . "' AND LFW2.TYPE = 'N')\n\t\t\t\t";
             $followWhere = "\n\t\t\t\t\tAND (LFW.USER_ID IS NOT NULL AND LFW.TYPE = 'Y')\n\t\t\t\t\tAND LFW2.USER_ID IS NULL\n\t\t\t\t";
         }
     }
     $strOfEntities = is_array($arOfEntities) && count($arOfEntities) > 0 ? "U.ID IN (" . implode(",", $arOfEntities) . ")" : "";
     $strSQL = "\n\t\tSELECT DISTINCT\n\t\t\tU.ID as ID\n\t\t\t," . ($bDecrement ? "-1" : "1") . " as CNT\n\t\t\t," . $DB->IsNull("SLS.SITE_ID", "'**'") . " as SITE_ID\n\t\t\t," . $params['CODE'] . " as CODE,\n\t\t\t0 as SENT\n\t\t\t" . ($tagSet ? ", '" . $DB->ForSQL($tagSet) . "' as TAG" : "") . "\n\t\tFROM\n\t\t\tb_user U \n\t\t\tINNER JOIN b_sonet_log_right SLR ON SLR.LOG_ID = " . $logId . "\n\t\t\t" . ($bGroupCounters ? "INNER JOIN b_sonet_log_right SLR0 ON SLR0.LOG_ID = SLR.LOG_ID " : "") . "\n\t\t\t" . (!$bForAllAccess ? "INNER JOIN b_user_access UA ON UA.USER_ID = U.ID" : "") . "\n\t\t\tLEFT JOIN b_sonet_log_site SLS ON SLS.LOG_ID = SLR.LOG_ID\n\t\t\t" . (strlen($followJoin) > 0 ? $followJoin : "") . "\n\t\t\t" . (!$bGroupCounters && !IsModuleInstalled("intranet") ? "LEFT JOIN b_sonet_log_smartfilter SLSF ON SLSF.USER_ID = U.ID " : "") . "\n\t\t\t\n\t\tWHERE\n\t\t\tU.ACTIVE = 'Y'\n\t\t\tAND U.LAST_ACTIVITY_DATE IS NOT NULL\n\t\t\tAND\tU.LAST_ACTIVITY_DATE > " . CSocNetLogCounter::dbWeeksAgo(2) . "\n\t\t\t" . (($type == "LC" || array_key_exists("USE_CB_FILTER", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["USE_CB_FILTER"] == "Y") && intval($created_by_id) > 0 ? "AND U.ID <> " . $created_by_id : "") . "\n\t\t\t" . ($bGroupCounters ? "AND (SLR0.GROUP_CODE like 'SG%' AND SLR0.GROUP_CODE NOT LIKE 'SG%\\_%')" : "") . (!$bGroupCounters && !IsModuleInstalled("intranet") ? COption::GetOptionString("socialnetwork", "sonet_log_smart_filter", "N") == "Y" ? "\n\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\t0=1 \n\t\t\t\t\t\t\t\t\tOR (\n\t\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\t\tSLSF.USER_ID IS NULL \n\t\t\t\t\t\t\t\t\t\t\tOR SLSF.TYPE = 'Y'\n\t\t\t\t\t\t\t\t\t\t) \n\t\t\t\t\t\t\t\t\t\t" . (!$bForAllAccess ? "AND (UA.ACCESS_CODE = SLR.GROUP_CODE)" : "") . "\n\t\t\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\t\t\tSLR.GROUP_CODE LIKE 'SG%'\n\t\t\t\t\t\t\t\t\t\t\tOR SLR.GROUP_CODE = 'U" . $log_user_id . "' \n\t\t\t\t\t\t\t\t\t\t\tOR SLR.GROUP_CODE = " . $DB->Concat("'U'", $DB->type == "MSSQL" ? "CAST(U.ID as varchar(17))" : "U.ID") . " \n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tOR (\n\t\t\t\t\t\t\t\t\t\tSLSF.TYPE <> 'Y'\n\t\t\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\t\t\tSLR.GROUP_CODE IN ('AU', 'G2')\n\t\t\t\t\t\t\t\t\t\t\t" . (!$bForAllAccess ? "OR (UA.ACCESS_CODE = SLR.GROUP_CODE)" : "") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t" : "\n\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\t0=1 \n\t\t\t\t\t\t\t\t\tOR (\n\t\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\t\tSLSF.USER_ID IS NULL \n\t\t\t\t\t\t\t\t\t\t\tOR SLSF.TYPE <> 'Y'\n\t\t\t\t\t\t\t\t\t\t) \n\t\t\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\t\t\tSLR.GROUP_CODE IN ('AU', 'G2')\n\t\t\t\t\t\t\t\t\t\t\t" . ($bForAllAccess ? "" : " OR (UA.ACCESS_CODE = SLR.GROUP_CODE)") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tOR (\n\t\t\t\t\t\t\t\t\t\tSLSF.TYPE = 'Y' \n\t\t\t\t\t\t\t\t\t\t" . ($bForAllAccess ? "" : "AND (UA.ACCESS_CODE = SLR.GROUP_CODE)") . "\n\t\t\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\t\t\tSLR.GROUP_CODE LIKE 'SG%'\n\t\t\t\t\t\t\t\t\t\t\tOR SLR.GROUP_CODE = 'U" . $log_user_id . "'\n\t\t\t\t\t\t\t\t\t\t\tOR SLR.GROUP_CODE = " . $DB->Concat("'U'", $DB->type == "MSSQL" ? "CAST(U.ID as varchar(17))" : "U.ID") . " \n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t" : "\n\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t0=1\n\t\t\t\t\t\t\t" . ($IsForAllAccessOnly != "N" || $bForAllAccess ? "OR (SLR.GROUP_CODE IN ('AU', 'G2'))" : "") . "\n\t\t\t\t\t\t\t" . (!$bForAllAccess && $IsForAllAccessOnly != "Y" ? " OR (UA.ACCESS_CODE = SLR.GROUP_CODE) " : "") . "\n\t\t\t\t\t\t)\n\t\t\t\t\t") . " " . (strlen($followWhere) > 0 ? $followWhere : "") . "\n\t\t";
     if ($bGroupCounters) {
         return $strSQL;
     }
     if (strlen($visibleFilter) > 0 || strlen($transportFilter) > 0) {
         $strSQL .= "\n\t\t\t\tAND\t\n\t\t\t\t(\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\tOR\n\t\t\t\t(\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t)\n\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\tOR\n\t\t\t(\n\t\t\t\t(\n\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t)\n\t\t\t\t\tOR\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t)\n\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\tAND\n\t\t\t\t(\n\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t)\n\t\t\t\t\tOR\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t)\n\n\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\t\tAND\n\t\t\t\t(\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\t\tOR\n\t\t\t\t\t(\n\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t)\n\t\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\t\t\tOR\n\t\t\t\t\t(\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t\tAND ";
         if (array_key_exists("USE_CB_FILTER", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t\tAND\n\t\t\t\t\t\t(\n\t\t\t\t\t\t";
         }
         if (strlen($strOfEntities) > 0) {
             $strSQL .= "\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\t" . $strOfEntities . "\n\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t\tOR\n\t\t\t\t\t";
         }
         $strSQL .= "\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]) && $arSocNetAllowedSubscribeEntityTypesDesc[$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t)\n\n\t\t\t)";
     }
     return $strSQL;
 }
Пример #3
0
 function GetSQL($user_id, $arMyEntities, $transport, $visible, $table_alias = "L")
 {
     global $DB;
     if (intval($user_id) <= 0) {
         return false;
     }
     if ((!defined("DisableSonetLogVisibleSubscr") || DisableSonetLogVisibleSubscr !== true) && $visible && strlen($visible) > 0) {
         $key_res = CSocNetGroup::GetFilterOperation($visible);
         $strField = $key_res["FIELD"];
         $strNegative = $key_res["NEGATIVE"];
         $strOperation = $key_res["OPERATION"];
         $visibleFilter = "AND (" . ($strNegative == "Y" ? " SLE.VISIBLE IS NULL OR NOT " : "") . "(SLE.VISIBLE " . $strOperation . " '" . $DB->ForSql($strField) . "'))";
         $transportFilter = "";
     } else {
         $visibleFilter = "";
         if ($transport && strlen($transport) > 0) {
             $key_res = CSocNetGroup::GetFilterOperation($transport);
             $strField = $key_res["FIELD"];
             $strNegative = $key_res["NEGATIVE"];
             $strOperation = $key_res["OPERATION"];
             $transportFilter = "AND (" . ($strNegative == "Y" ? " SLE.TRANSPORT IS NULL OR NOT " : "") . "(SLE.TRANSPORT " . $strOperation . " '" . $DB->ForSql($strField) . "'))";
         } else {
             $transportFilter = "";
         }
     }
     $strMyEntities = array();
     foreach ($arMyEntities as $entity_type_tmp => $arMyEntity) {
         if (is_array($arMyEntity) && count($arMyEntity) > 0) {
             $strMyEntities[$entity_type_tmp] = $table_alias . ".ENTITY_ID IN (" . implode(",", $arMyEntity) . ")";
             $strNotMyEntities[$entity_type_tmp] = "(" . $table_alias . ".ENTITY_ID NOT IN (" . implode(",", $arMyEntity) . ") AND " . $table_alias . ".ENTITY_TYPE = '" . $entity_type_tmp . "')";
         }
     }
     $arCBFilterEntityType = array();
     $arSocNetAllowedSubscribeEntityTypesDesc = CSocNetAllowed::GetAllowedEntityTypesDesc();
     foreach ($arSocNetAllowedSubscribeEntityTypesDesc as $entity_type_tmp => $arEntityTypeTmp) {
         if (array_key_exists("USE_CB_FILTER", $arEntityTypeTmp) && $arEntityTypeTmp["USE_CB_FILTER"] == "Y") {
             $arCBFilterEntityType[] = "'" . $entity_type_tmp . "'";
         }
     }
     if (is_array($arCBFilterEntityType) && count($arCBFilterEntityType) > 0) {
         $strCBFilterEntityType = $table_alias . ".ENTITY_TYPE IN (" . implode(",", $arCBFilterEntityType) . ") AND ";
         $strNotCBFilterEntityType = $table_alias . ".ENTITY_TYPE NOT IN (" . implode(",", $arCBFilterEntityType) . ") OR ";
     } else {
         $strCBFilterEntityType = "";
         $strNotCBFilterEntityType = "";
     }
     $strSQL = "\n\t\tEXISTS(\n\t\t\tSELECT ID \n\t\t\tFROM b_sonet_log_events SLE \n\t\t\tWHERE \n\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE\n\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".ENTITY_ID\n\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t" . $transportFilter . "\n\t\t\t\t" . $visibleFilter . "\n\t\t)\n\t\tOR \n\t\t(\n\t\t\t" . $strCBFilterEntityType . "\n\t\t\tEXISTS(\n\t\t\t\tSELECT ID \n\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\tWHERE \n\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".USER_ID\n\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t" . $visibleFilter . "\n\t\t\t)\t\t\n\t\t)\n\t\tOR \n\t\t(\n\t\t\t(\n\t\t\t\tNOT EXISTS(\n\t\t\t\t\tSELECT ID \n\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\tWHERE \n\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE\n\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".ENTITY_ID\n\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t)\n\t\t\t\tOR\n\t\t\t\tEXISTS(\n\t\t\t\t\tSELECT ID \n\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\tWHERE \n\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE\n\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".ENTITY_ID\n\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t)\n\t\t\t)\n\t\t\tAND \n\t\t\t(\n\t\t\t\t" . $strNotCBFilterEntityType . "\t\t\t\n\t\t\t\tNOT EXISTS(\n\t\t\t\t\tSELECT ID \n\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\tWHERE \n\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".USER_ID\n\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t)\n\t\t\t\tOR \n\t\t\t\tEXISTS(\n\t\t\t\t\tSELECT ID \n\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\tWHERE \n\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".USER_ID\n\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t)\n\t\t\t\t\n\t\t\t)\n\t\t\tAND\n\t\t\t(\n\t\t\t\tEXISTS(\n\t\t\t\t\tSELECT ID \n\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\tWHERE \n\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE\n\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".ENTITY_ID\n\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t)\n\t\t\t\tOR\n\t\t\t\t(\n\t\t\t\t\t" . $strCBFilterEntityType . "\t\t\t\t\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".USER_ID\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t\tOR\n\t\t\t\t(\n\t\t\t\t\t(\n\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".ENTITY_ID\n\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t)\n\t\t\t\t\t\tOR \n\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".ENTITY_ID\n\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t\tAND \n\t\t\t\t\t(\n\t\t\t\t\t\t" . $strNotCBFilterEntityType . "\t\t\t\t\t\n\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".USER_ID\n\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t)\n\t\t\t\t\t\tOR\n\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $table_alias . ".USER_ID\n\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t)\n\t\t\t\t\t)\t\t\t\t\t\n\t\t\t\t\tAND\n\t\t\t\t\t(\n\t\t\t\t\t";
     if (count($strMyEntities) > 0) {
         foreach ($strMyEntities as $entity_type_tmp => $strMyEntity) {
             $strSQL .= strlen($strMyEntity) > 0 ? "\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\t" . $strMyEntity . "\n\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type_tmp . "'\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type_tmp . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type_tmp . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . " \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type_tmp . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t\tOR\n\t\t\t\t" : "";
         }
     }
     $strSQL .= "\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . "\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . "\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = " . $table_alias . ".EVENT_ID\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tAND \n\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . "\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . "\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\tSELECT ID \n\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE \n\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = " . $user_id . "\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = " . $table_alias . ".ENTITY_TYPE \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0 \n\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t)\t\n\t\t\t\t)\n\t\t\t)\n\t\t)";
     return $strSQL;
 }
Пример #4
0
 function GetSubSelect($log_id, $entity_type = false, $entity_id = false, $event_id = false, $created_by_id = false, $arOfEntities = false, $arAdmin = false, $transport = false, $visible = "Y", $type = "L")
 {
     global $DB;
     if (intval($log_id) <= 0) {
         return false;
     }
     $bGroupCounters = $type === "group";
     if (!$entity_type || !$entity_id || !$created_by_id) {
         if ($type == "L" && ($arLog = CSocNetLog::GetByID($log_id))) {
             $entity_type = $arLog["ENTITY_TYPE"];
             $entity_id = $arLog["ENTITY_ID"];
             $event_id = $arLog["EVENT_ID"];
             $created_by_id = $arLog["USER_ID"];
         } elseif ($type == "LC" && ($arLogComment = CSocNetLogComments::GetByID($log_id))) {
             $entity_type = $arLogComment["ENTITY_TYPE"];
             $entity_id = $arLogComment["ENTITY_ID"];
             $event_id = $arLogComment["EVENT_ID"];
             $created_by_id = $arLogComment["USER_ID"];
             $log_id = $arLogComment["LOG_ID"];
         }
     }
     if (!in_array($entity_type, $GLOBALS["arSocNetAllowedSubscribeEntityTypes"])) {
         return false;
     }
     if (intval($entity_id) <= 0) {
         return false;
     }
     if (strlen($event_id) <= 0) {
         return false;
     }
     if (!$arOfEntities) {
         if (array_key_exists($entity_type, $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"]) && array_key_exists("HAS_MY", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["HAS_MY"] == "Y" && array_key_exists("CLASS_OF", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && array_key_exists("METHOD_OF", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && strlen($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["CLASS_OF"]) > 0 && strlen($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["METHOD_OF"]) > 0 && method_exists($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["CLASS_OF"], $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["METHOD_OF"])) {
             $arOfEntities = call_user_func(array($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["CLASS_OF"], $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["METHOD_OF"]), $entity_id);
         } else {
             $arOfEntities = array();
         }
     }
     if ((!defined("DisableSonetLogVisibleSubscr") || DisableSonetLogVisibleSubscr !== true) && $visible && strlen($visible) > 0) {
         $key_res = CSocNetGroup::GetFilterOperation($visible);
         $strField = $key_res["FIELD"];
         $strNegative = $key_res["NEGATIVE"];
         $strOperation = $key_res["OPERATION"];
         $visibleFilter = "AND (" . ($strNegative == "Y" ? " SLE.VISIBLE IS NULL OR NOT " : "") . "(SLE.VISIBLE " . $strOperation . " '" . $DB->ForSql($strField) . "'))";
         $transportFilter = "";
     } else {
         $visibleFilter = "";
         if ($transport && strlen($transport) > 0) {
             $key_res = CSocNetGroup::GetFilterOperation($transport);
             $strField = $key_res["FIELD"];
             $strNegative = $key_res["NEGATIVE"];
             $strOperation = $key_res["OPERATION"];
             $transportFilter = "AND (" . ($strNegative == "Y" ? " SLE.TRANSPORT IS NULL OR NOT " : "") . "(SLE.TRANSPORT " . $strOperation . " '" . $DB->ForSql($strField) . "'))";
         } else {
             $transportFilter = "";
         }
     }
     if ($type == "LC" && (!defined("DisableSonetLogFollow") || DisableSonetLogFollow !== true)) {
         $default_follow = COption::GetOptionString("socialnetwork", "follow_default_type", "Y");
         if ($default_follow == "Y") {
             $followJoin = " LEFT JOIN b_sonet_log_follow LFW ON LFW.USER_ID = U.ID AND (LFW.CODE = 'L" . $log_id . "' OR LFW.CODE = '**') ";
             $followWhere = "AND (LFW.USER_ID IS NULL OR LFW.TYPE = 'Y')";
         } else {
             $followJoin = " INNER JOIN b_sonet_log_follow LFW ON LFW.USER_ID = U.ID AND (LFW.CODE = 'L" . $log_id . "' OR LFW.CODE = '**') ";
             $followWhere = "AND (LFW.USER_ID IS NOT NULL AND LFW.TYPE = 'Y')";
         }
     }
     if (is_array($arOfEntities) && count($arOfEntities) > 0) {
         $strOfEntities = "U.ID IN (" . implode(",", $arOfEntities) . ")";
     } else {
         $strOfEntities = "";
     }
     $strSQL = "\n\t\tSELECT DISTINCT\n\t\t\tU.ID as ID\n\t\t\t,1\n\t\t\t," . $DB->IsNull("SLS.SITE_ID", "'**'") . " as SITE_ID\n\t\t\t," . ($bGroupCounters ? "SLR0.GROUP_CODE" : "'**'") . " as CODE\n\t\tFROM\n\t\t\tb_user U \n\t\t\tINNER JOIN b_sonet_log_right SLR ON SLR.LOG_ID = " . $log_id . "\n\t\t\t" . ($bGroupCounters ? "INNER JOIN b_sonet_log_right SLR0 ON SLR0.LOG_ID = SLR.LOG_ID " : "") . "\n\t\t\tINNER JOIN b_user_access UA ON UA.USER_ID = U.ID\n\t\t\tLEFT JOIN b_sonet_log_site SLS ON SLS.LOG_ID = SLR.LOG_ID\n\t\t\t" . (strlen($followJoin) > 0 ? $followJoin : "") . "\n\t\tWHERE\n\t\t\tU.ACTIVE = 'Y'\n\t\t\t" . (array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0 ? "AND U.ID <> " . $created_by_id : "") . "\n\t\t\t" . ($bGroupCounters ? "AND (SLR0.GROUP_CODE like 'SG%' AND SLR0.GROUP_CODE NOT LIKE 'SG%\\_%')" : "") . "\n\t\t\tAND (\n\t\t\t\t0=1 \n\t\t\t\tOR (SLR.GROUP_CODE IN ('AU', 'G2'))\n\t\t\t\tOR (UA.ACCESS_CODE = SLR.GROUP_CODE)\n\t\t\t)\n\t\t\t" . (strlen($followWhere) > 0 ? $followWhere : "") . "\n\t\t";
     if ($bGroupCounters) {
         return $strSQL;
     }
     if (strlen($visibleFilter) > 0 || strlen($transportFilter) > 0) {
         $strSQL .= "\n\t\t\t\tAND\t\n\t\t\t\t(\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\tOR\n\t\t\t\t(\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t)\n\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\tOR\n\t\t\t(\n\t\t\t\t(\n\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t)\n\t\t\t\t\tOR\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t)\n\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\tAND\n\t\t\t\t(\n\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t)\n\t\t\t\t\tOR\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t)\n\n\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\t\tAND\n\t\t\t\t(\n\t\t\t\t\tEXISTS(\n\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\t\tOR\n\t\t\t\t\t(\n\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t)\n\t\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\t\t\tOR\n\t\t\t\t\t(\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'N'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $entity_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t\tAND ";
         if (array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_CB = 'Y'\n\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = " . $created_by_id . "\n\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t\tAND\n\t\t\t\t\t\t(\n\t\t\t\t\t\t";
         }
         if (strlen($strOfEntities) > 0) {
             $strSQL .= "\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\t" . $strOfEntities . "\n\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'Y'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t\tOR\n\t\t\t\t\t";
         }
         $strSQL .= "\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = '" . $event_id . "'\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t" . $transportFilter . "\n\t\t\t\t\t\t\t\t\t\t" . $visibleFilter . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tEXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t\t\tAND " . ($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'") . "\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\t\t\t\t\tSELECT ID\n\t\t\t\t\t\t\t\t\t\t\tFROM b_sonet_log_events SLE\n\t\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t\tSLE.USER_ID = U.ID\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_TYPE = '" . $entity_type . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_ID = 0\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.ENTITY_MY = 'N'\n\t\t\t\t\t\t\t\t\t\t\t\tAND SLE.EVENT_ID = 'all'\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)";
         if (array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) && $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y" && intval($created_by_id) > 0) {
             $strSQL .= "\n\t\t\t\t\t\t)";
         }
         $strSQL .= "\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t)\n\n\t\t\t)";
     }
     return $strSQL;
 }
Пример #5
0
	public static function GetSubSelect($log_id, $entity_type = false, $entity_id = false, $event_id = false, $created_by_id = false, $arOfEntities = false, $arAdmin = false, $transport = false, $visible = "Y", $type = "L", $params = array())
	{
		global $DB;

		if (intval($log_id) <= 0)
			return false;

		$bGroupCounters = ($type === "group");
		$params = (is_array($params) ? $params : array());
		$params['CODE'] = (!empty($params['CODE']) ? $params['CODE'] : ($bGroupCounters ? "SLR0.GROUP_CODE" : "'**'"));

		if ($type == "L" && ($arLog = CSocNetLog::GetByID($log_id)))
		{
			$entity_type = $arLog["ENTITY_TYPE"];
			$entity_id = $arLog["ENTITY_ID"];
			$event_id = $arLog["EVENT_ID"];
			$created_by_id = $arLog["USER_ID"];
			$log_user_id = $arLog["USER_ID"];
		}
		elseif ($type == "LC" && ($arLogComment = CSocNetLogComments::GetByID($log_id)))
		{
			$entity_type = $arLogComment["ENTITY_TYPE"];
			$entity_id = $arLogComment["ENTITY_ID"];
			$event_id = $arLogComment["EVENT_ID"];
			$created_by_id = $arLogComment["USER_ID"];
			$log_id = $arLogComment["LOG_ID"]; // recalculate log_id
			$log_user_id = $arLogComment["LOG_USER_ID"];
		}

		if (!in_array($entity_type, $GLOBALS["arSocNetAllowedSubscribeEntityTypes"]))
			return false;

		if (intval($entity_id) <= 0)
			return false;

		if (strlen($event_id) <= 0)
			return false;

		if (!$arOfEntities)
		{
			if (
				array_key_exists($entity_type, $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"])
				&& array_key_exists("HAS_MY", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["HAS_MY"] == "Y"
				&& array_key_exists("CLASS_OF", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& array_key_exists("METHOD_OF", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& strlen($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["CLASS_OF"]) > 0
				&& strlen($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["METHOD_OF"]) > 0
				&& method_exists($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["CLASS_OF"], $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["METHOD_OF"])
			)
				$arOfEntities = call_user_func(array($GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["CLASS_OF"], $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["METHOD_OF"]), $entity_id);
			else
				$arOfEntities = array();
		}

		if (
			(!defined("DisableSonetLogVisibleSubscr") || DisableSonetLogVisibleSubscr !== true) 
			&& $visible 
			&& strlen($visible) > 0
		)
		{
			$key_res = CSocNetGroup::GetFilterOperation($visible);
			$strField = $key_res["FIELD"];
			$strNegative = $key_res["NEGATIVE"];
			$strOperation = $key_res["OPERATION"];
			$visibleFilter = "AND (".($strNegative == "Y" ? " SLE.VISIBLE IS NULL OR NOT " : "")."(SLE.VISIBLE ".$strOperation." '".$DB->ForSql($strField)."'))";

			$transportFilter = "";
		}
		else
		{
			$visibleFilter = "";

			if ($transport && strlen($transport) > 0)
			{
				$key_res = CSocNetGroup::GetFilterOperation($transport);
				$strField = $key_res["FIELD"];
				$strNegative = $key_res["NEGATIVE"];
				$strOperation = $key_res["OPERATION"];
				$transportFilter = "AND (".($strNegative == "Y" ? " SLE.TRANSPORT IS NULL OR NOT " : "")."(SLE.TRANSPORT ".$strOperation." '".$DB->ForSql($strField)."'))";
			}
			else
				$transportFilter = "";
		}

		if (
			$type == "LC" 
			&& (
				!defined("DisableSonetLogFollow") 
				|| DisableSonetLogFollow !== true)
			)
		{
			$default_follow = COption::GetOptionString("socialnetwork", "follow_default_type", "Y");

			if ($default_follow == "Y")
			{
				$followJoin = " LEFT JOIN b_sonet_log_follow LFW ON LFW.USER_ID = U.ID AND (LFW.CODE = 'L".$log_id."' OR LFW.CODE = '**') ";
				$followWhere = "AND (LFW.USER_ID IS NULL OR LFW.TYPE = 'Y')";
			}
			else
			{
				$followJoin = " 
					INNER JOIN b_sonet_log_follow LFW ON LFW.USER_ID = U.ID AND (LFW.CODE = 'L".$log_id."' OR LFW.CODE = '**') 
					LEFT JOIN b_sonet_log_follow LFW2 ON LFW2.USER_ID = U.ID AND (LFW2.CODE = 'L".$log_id."' AND LFW2.TYPE = 'N')
				";
				$followWhere = "
					AND (LFW.USER_ID IS NOT NULL AND LFW.TYPE = 'Y')
					AND LFW2.USER_ID IS NULL
				";
			}
		}

		if (
			is_array($arOfEntities)
			&& count($arOfEntities) > 0
		)
			$strOfEntities = "U.ID IN (".implode(",", $arOfEntities).")";
		else
			$strOfEntities = "";

		$strSQL = "
		SELECT DISTINCT
			U.ID as ID
			,1 as CNT
			,".$DB->IsNull("SLS.SITE_ID", "'**'")." as SITE_ID
			,".$params['CODE']." as CODE,
			0 as SENT
		FROM
			b_user U 
			INNER JOIN b_sonet_log_right SLR ON SLR.LOG_ID = ".$log_id."
			".($bGroupCounters ? "INNER JOIN b_sonet_log_right SLR0 ON SLR0.LOG_ID = SLR.LOG_ID ": "")."
			INNER JOIN b_user_access UA ON UA.USER_ID = U.ID
			LEFT JOIN b_sonet_log_site SLS ON SLS.LOG_ID = SLR.LOG_ID
			".(strlen($followJoin) > 0 ? $followJoin : "")."
			".(!$bGroupCounters && !IsModuleInstalled("intranet") ? "LEFT JOIN b_sonet_log_smartfilter SLSF ON SLSF.USER_ID = U.ID " : "")."
			
		WHERE
			U.ACTIVE = 'Y'
			".(
				array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]) 
				&& $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y" 
				&& intval($created_by_id) > 0 
					? "AND U.ID <> ".$created_by_id 
					: ""
			)."
			".($bGroupCounters ? "AND (SLR0.GROUP_CODE like 'SG%' AND SLR0.GROUP_CODE NOT LIKE 'SG%\_%')": "").
			(
				!$bGroupCounters && !IsModuleInstalled("intranet")
					? (
						COption::GetOptionString("socialnetwork", "sonet_log_smart_filter", "N") == "Y"
							? "
								AND (
									0=1 
									OR (
										(
											SLSF.USER_ID IS NULL 
											OR SLSF.TYPE = 'Y'
										) 
										AND UA.ACCESS_CODE = SLR.GROUP_CODE 
										AND (
											SLR.GROUP_CODE LIKE 'SG%'
											OR SLR.GROUP_CODE = 'U".$log_user_id."' 
											OR SLR.GROUP_CODE = ".$DB->Concat("'U'", ($DB->type == "MSSQL" ? "CAST(U.ID as varchar(17))" : "U.ID"))." 
										)
									)
									OR (
										SLSF.TYPE <> 'Y'
										AND (
											SLR.GROUP_CODE IN ('AU', 'G2') 
											OR UA.ACCESS_CODE = SLR.GROUP_CODE
										)
									)
								)
							"
							: "
								AND (
									0=1 
									OR (
										(
											SLSF.USER_ID IS NULL 
											OR SLSF.TYPE <> 'Y'
										) 
										AND (
											SLR.GROUP_CODE IN ('AU', 'G2') 
											OR UA.ACCESS_CODE = SLR.GROUP_CODE
										)
									)
									OR (
										SLSF.TYPE = 'Y' 
										AND UA.ACCESS_CODE = SLR.GROUP_CODE 
										AND (
											SLR.GROUP_CODE LIKE 'SG%'
											OR SLR.GROUP_CODE = 'U".$log_user_id."'
											OR SLR.GROUP_CODE = ".$DB->Concat("'U'", ($DB->type == "MSSQL" ? "CAST(U.ID as varchar(17))" : "U.ID"))." 
										)
									)
								)
							"
					)
					: "
						AND (
							0=1 
							OR (SLR.GROUP_CODE IN ('AU', 'G2'))
							OR (UA.ACCESS_CODE = SLR.GROUP_CODE)
						)
					"
			)." ".
			(strlen($followWhere) > 0 ? $followWhere : "")."
		";

		if($bGroupCounters)
			return $strSQL;

		if (
			strlen($visibleFilter) > 0 
			|| strlen($transportFilter) > 0
		)
		{
			$strSQL .= "
				AND	
				(
					EXISTS(
						SELECT ID
						FROM b_sonet_log_events SLE
						WHERE
							SLE.USER_ID = U.ID
							AND SLE.ENTITY_TYPE = '".$entity_type."'
							AND SLE.ENTITY_CB = 'N'
							AND SLE.ENTITY_ID = ".$entity_id."
							AND SLE.EVENT_ID = '".$event_id."'
							".$transportFilter."
							".$visibleFilter."
					)";

			if (
				array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y"
				&& intval($created_by_id) > 0
			)
			{
				$strSQL .= "
				OR
				(
					EXISTS(
						SELECT ID
						FROM b_sonet_log_events SLE
						WHERE
							SLE.USER_ID = U.ID
							AND SLE.ENTITY_CB = 'Y'
							AND SLE.ENTITY_ID = ".$created_by_id."
							AND SLE.EVENT_ID = '".$event_id."'
							".$transportFilter."
							".$visibleFilter."
					)
				)";
			}

			$strSQL .= "
			OR
			(
				(
					NOT EXISTS(
						SELECT ID
						FROM b_sonet_log_events SLE
						WHERE
							SLE.USER_ID = U.ID
							AND SLE.ENTITY_TYPE = '".$entity_type."'
							AND SLE.ENTITY_CB = 'N'
							AND SLE.ENTITY_ID = ".$entity_id."
							AND SLE.EVENT_ID = '".$event_id."'
					)
					OR
					EXISTS(
						SELECT ID
						FROM b_sonet_log_events SLE
						WHERE
							SLE.USER_ID = U.ID
							AND SLE.ENTITY_TYPE = '".$entity_type."'
							AND SLE.ENTITY_CB = 'N'
							AND SLE.ENTITY_ID = ".$entity_id."
							AND SLE.EVENT_ID = '".$event_id."'
							AND ".($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'")."
					)
				)";

			if (
				array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y"
				&& intval($created_by_id) > 0
			)
			{
				$strSQL .= "
				AND
				(
					NOT EXISTS(
						SELECT ID
						FROM b_sonet_log_events SLE
						WHERE
							SLE.USER_ID = U.ID
							AND SLE.ENTITY_CB = 'Y'
							AND SLE.ENTITY_ID = ".$created_by_id."
							AND SLE.EVENT_ID = '".$event_id."'
					)
					OR
					EXISTS(
						SELECT ID
						FROM b_sonet_log_events SLE
						WHERE
							SLE.USER_ID = U.ID
							AND SLE.ENTITY_CB = 'Y'
							AND SLE.ENTITY_ID = ".$created_by_id."
							AND SLE.EVENT_ID = '".$event_id."'
							AND ".($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'")."
					)

				)";
			}

			$strSQL .= "
				AND
				(
					EXISTS(
						SELECT ID
						FROM b_sonet_log_events SLE
						WHERE
							SLE.USER_ID = U.ID
							AND SLE.ENTITY_TYPE = '".$entity_type."'
							AND SLE.ENTITY_CB = 'N'
							AND SLE.ENTITY_ID = ".$entity_id."
							AND SLE.EVENT_ID = 'all'
							".$transportFilter."
							".$visibleFilter."
					)";

			if (
				array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y"
				&& intval($created_by_id) > 0
			)
			{
				$strSQL .= "
					OR
					(
						EXISTS(
							SELECT ID
							FROM b_sonet_log_events SLE
							WHERE
								SLE.USER_ID = U.ID
								AND SLE.ENTITY_CB = 'Y'
								AND SLE.ENTITY_ID = ".$created_by_id."
								AND SLE.EVENT_ID = 'all'
								".$transportFilter."
								".$visibleFilter."
						)
					)";
			}

			$strSQL .= "
					OR
					(
						(
							NOT EXISTS(
								SELECT ID
								FROM b_sonet_log_events SLE
								WHERE
									SLE.USER_ID = U.ID
									AND SLE.ENTITY_TYPE = '".$entity_type."'
									AND SLE.ENTITY_CB = 'N'
									AND SLE.ENTITY_ID = ".$entity_id."
									AND SLE.EVENT_ID = 'all'
							)
							OR
							EXISTS(
								SELECT ID
								FROM b_sonet_log_events SLE
								WHERE
									SLE.USER_ID = U.ID
									AND SLE.ENTITY_TYPE = '".$entity_type."'
									AND SLE.ENTITY_CB = 'N'
									AND SLE.ENTITY_ID = ".$entity_id."
									AND SLE.EVENT_ID = 'all'
									AND ".($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'")."
							)
						)
						AND ";

			if (
				array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y"
				&& intval($created_by_id) > 0
			)
			{
				$strSQL .= "
						(
							NOT EXISTS(
								SELECT ID
								FROM b_sonet_log_events SLE
								WHERE
									SLE.USER_ID = U.ID
									AND SLE.ENTITY_CB = 'Y'
									AND SLE.ENTITY_ID = ".$created_by_id."
									AND SLE.EVENT_ID = 'all'
							)
							OR
							EXISTS(
								SELECT ID
								FROM b_sonet_log_events SLE
								WHERE
									SLE.USER_ID = U.ID
									AND SLE.ENTITY_CB = 'Y'
									AND SLE.ENTITY_ID = ".$created_by_id."
									AND SLE.EVENT_ID = 'all'
									AND ".($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'")."
							)
						)
						AND
						(
						";
			}

			if (strlen($strOfEntities) > 0)
			{
					$strSQL .= "
						(
							".$strOfEntities."
							AND
							(
								EXISTS(
									SELECT ID
									FROM b_sonet_log_events SLE
									WHERE
										SLE.USER_ID = U.ID
										AND SLE.ENTITY_TYPE = '".$entity_type."'
										AND SLE.ENTITY_ID = 0
										AND SLE.ENTITY_MY = 'Y'
										AND SLE.EVENT_ID = '".$event_id."'
										".$transportFilter."
										".$visibleFilter."
								)
								OR
								(
									(
										EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'Y'
												AND SLE.EVENT_ID = '".$event_id."'
												AND ".($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'")."
										)
										OR
										NOT EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'Y'
												AND SLE.EVENT_ID = '".$event_id."'
										)
									)
									AND
									(
										EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'Y'
												AND SLE.EVENT_ID = 'all'
												".$transportFilter."
												".$visibleFilter."
										)
									)
								)
							)
						)
						OR
					";
			}

			$strSQL .=	"
							(
								EXISTS(
									SELECT ID
									FROM b_sonet_log_events SLE
									WHERE
										SLE.USER_ID = U.ID
										AND SLE.ENTITY_TYPE = '".$entity_type."'
										AND SLE.ENTITY_ID = 0
										AND SLE.ENTITY_MY = 'N'
										AND SLE.EVENT_ID = '".$event_id."'
										".$transportFilter."
										".$visibleFilter."
								)
								OR
								(
									(
										EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'N'
												AND SLE.EVENT_ID = '".$event_id."'
												AND ".($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'")."
											)
										OR
										NOT EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'N'
												AND SLE.EVENT_ID = '".$event_id."'
										)
									)
									AND
									(
										EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'N'
												AND SLE.EVENT_ID = 'all'
										".$transportFilter."
										".$visibleFilter."
										)
										OR
										EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'N'
												AND SLE.EVENT_ID = 'all'
												AND ".($visibleFilter ? "SLE.VISIBLE = 'I'" : "SLE.TRANSPORT = 'I'")."
										)
										OR
										NOT EXISTS(
											SELECT ID
											FROM b_sonet_log_events SLE
											WHERE
												SLE.USER_ID = U.ID
												AND SLE.ENTITY_TYPE = '".$entity_type."'
												AND SLE.ENTITY_ID = 0
												AND SLE.ENTITY_MY = 'N'
												AND SLE.EVENT_ID = 'all'
										)
									)
								)
							)";

			if (
				array_key_exists("USE_CB_FILTER", $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type])
				&& $GLOBALS["arSocNetAllowedSubscribeEntityTypesDesc"][$entity_type]["USE_CB_FILTER"] == "Y"
				&& intval($created_by_id) > 0
			)
				$strSQL .="
						)";

			$strSQL .="
					)
				)
			)

			)";
		}

		return $strSQL;
	}