private static function GetSubSelect($arLogFields, $bDecrement = false) { global $DB; $author_id = CCrmSecurityHelper::GetCurrentUserID(); if ($author_id <= 0 && isset($arLogFields["USER_ID"])) { $author_id = intval($arLogFields["USER_ID"]); } if ($author_id <= 0) { return ""; } $entityTypeID = CCrmLiveFeedEntity::ResolveEntityTypeID($arLogFields["ENTITY_TYPE"]); $entityID = $arLogFields["ENTITY_ID"]; $arEntities = array(); if ($entityTypeID == CCrmOwnerType::Activity) { if ($arActivity = CCrmActivity::GetByID($entityID)) { $entityTypeID = $arActivity["OWNER_TYPE_ID"]; $entityID = $arActivity["OWNER_ID"]; $entityName = CCrmOwnerType::ResolveName($entityTypeID); $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false); $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false); if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0) { if (!array_key_exists($entityName, $arEntities)) { $arEntities[$entityName] = array(); } $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id); } $arCommunications = CCrmActivity::GetCommunications($arActivity["ID"]); foreach ($arCommunications as $arActivityCommunication) { $entityTypeID = $arActivityCommunication["ENTITY_TYPE_ID"]; $entityID = $arActivityCommunication["ENTITY_ID"]; $entityName = CCrmOwnerType::ResolveName($entityTypeID); $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false); $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false); if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0) { if (!array_key_exists($entityName, $arEntities)) { $arEntities[$entityName] = array(); } $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id); } } } } elseif ($entityTypeID == CCrmOwnerType::Invoice) { if ($arInvoice = CCrmInvoice::GetByID($entityID)) { $arBindings = array(CCrmOwnerType::Contact => $arInvoice["UF_CONTACT_ID"], CCrmOwnerType::Company => $arInvoice["UF_COMPANY_ID"], CCrmOwnerType::Deal => $arInvoice["UF_DEAL_ID"]); foreach ($arBindings as $entityTypeID => $entityID) { if (intval($entityID) > 0) { $entityName = CCrmOwnerType::ResolveName($entityTypeID); $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false); $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false); if ($entityName && intval($responsible_id) > 0) { if (!array_key_exists($entityName, $arEntities)) { $arEntities[$entityName] = array(); } $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id); } } } } } else { $entityName = CCrmOwnerType::ResolveName($entityTypeID); $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false); $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false); if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0) { if (!array_key_exists($entityName, $arEntities)) { $arEntities[$entityName] = array(); } $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id); } } if (intval($arLogFields["LOG_ID"]) > 0 && in_array($arLogFields["EVENT_ID"], array("crm_lead_message", "crm_deal_message", "crm_contact_message", "crm_company_message"))) { $dbRight = CSocNetLogRights::GetList(array(), array("LOG_ID" => $arLogFields["LOG_ID"])); while ($arRight = $dbRight->Fetch()) { if (preg_match('/^(' . CCrmLiveFeedEntity::Contact . '|' . CCrmLiveFeedEntity::Lead . '|' . CCrmLiveFeedEntity::Company . '|' . CCrmLiveFeedEntity::Deal . ')(\\d+)$/', $arRight["GROUP_CODE"], $matches)) { $entityTypeID = CCrmLiveFeedEntity::ResolveEntityTypeID($matches[1]); $entityID = $matches[2]; $entityName = CCrmOwnerType::ResolveName($entityTypeID); $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false); if (!array_key_exists($entityName, $arEntities)) { $arEntities[$entityName] = array(); } if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0 && !array_key_exists($entityTypeID . "_" . $entityID, $arEntities[$entityName])) { $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => CCrmOwnerType::isOpened($entityTypeID, $entityID, false), "RESPONSIBLE_ID" => $responsible_id); } } } } $arUserID = array(); foreach ($arEntities as $entityName => $arTmp) { $sSql = "SELECT RL.RELATION, RP.ATTR \n\t\t\t\tFROM b_crm_role_relation RL \n\t\t\t\tINNER JOIN b_crm_role_perms RP ON RL.ROLE_ID = RP.ROLE_ID AND RP.ENTITY = '" . $entityName . "' AND RP.PERM_TYPE = 'READ'\n\t\t\t"; $res = $DB->Query($sSql, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__); while ($row = $res->Fetch()) { $user_id = false; switch ($row["ATTR"]) { case BX_CRM_PERM_SELF: foreach ($arTmp as $arEntity) { $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tUA.USER_ID = " . intval($arEntity["RESPONSIBLE_ID"]) . "\n\t\t\t\t\t\t\t\tAND UA.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'"; $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__); if (($arUser = $rsUser->Fetch()) && !in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) { $arUserID[] = $arUser["USER_ID"]; } } break; case BX_CRM_PERM_ALL: case BX_CRM_PERM_CONFIG: $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tUA.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'"; $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__); while ($arUser = $rsUser->Fetch()) { if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) { $arUserID[] = $arUser["USER_ID"]; } } break; case BX_CRM_PERM_OPEN: foreach ($arTmp as $arEntity) { if ($arEntity["IS_OPENED"]) { $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tUA.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'"; $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__); while ($arUser = $rsUser->Fetch()) { if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) { $arUserID[] = $arUser["USER_ID"]; } } } } break; case BX_CRM_PERM_DEPARTMENT: foreach ($arTmp as $arEntity) { $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\tINNER JOIN b_user_access UA1 ON \n\t\t\t\t\t\t\t\tUA1.USER_ID = " . intval($arEntity["RESPONSIBLE_ID"]) . "\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE LIKE 'D%'\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE NOT LIKE 'DR%'\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE = UA.ACCESS_CODE\n\t\t\t\t\t\t\tINNER JOIN b_user_access UA2 ON \n\t\t\t\t\t\t\t\tUA2.USER_ID = UA.USER_ID\n\t\t\t\t\t\t\t\tAND UA2.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'"; $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__); while ($arUser = $rsUser->Fetch()) { if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) { $arUserID[] = $arUser["USER_ID"]; } } } break; case BX_CRM_PERM_SUBDEPARTMENT: foreach ($arTmp as $arEntity) { $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\tINNER JOIN b_user_access UA1 ON \n\t\t\t\t\t\t\t\tUA1.USER_ID = " . intval($arEntity["RESPONSIBLE_ID"]) . "\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE LIKE 'DR%'\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE = UA.ACCESS_CODE\n\t\t\t\t\t\t\tINNER JOIN b_user_access UA2 ON \n\t\t\t\t\t\t\t\tUA2.USER_ID = UA.USER_ID\n\t\t\t\t\t\t\t\tAND UA2.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'"; $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__); while ($arUser = $rsUser->Fetch()) { if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) { $arUserID[] = $arUser["USER_ID"]; } } } break; } } } $strSubscription = ""; $cnt = 0; foreach ($arEntities as $entityName => $arTmp) { foreach ($arTmp as $arEntity) { if ($cnt > 0) { $strSubscription .= " OR "; } $strSubscription .= "\n\t\t\t\t\tEXISTS (\n\t\t\t\t\t\t\tSELECT S.USER_ID \n\t\t\t\t\t\t\tFROM " . CCrmSonetSubscription::TABLE_NAME . " S \n\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\tS.SL_ENTITY_TYPE = '" . CCrmLiveFeedEntity::GetByEntityTypeID($arEntity["ENTITY_TYPE_ID"]) . "'\n\t\t\t\t\t\t\t\tAND S.ENTITY_ID = " . intval($arEntity["ENTITY_ID"]) . "\n\t\t\t\t\t\t\t\tAND U.ID = S.USER_ID\n\t\t\t\t\t\t) "; $cnt++; } } $strReturn = "SELECT \n\t\t\tU.ID as ID\n\t\t\t," . ($bDecrement ? "-1" : "1") . " as CNT\n\t\t\t,'**' as SITE_ID\n\t\t\t,'CRM_**' as CODE,\n\t\t\t0 as SENT\n\t\tFROM b_user U \n\t\tWHERE\n\t\t\t(\n\t\t\t\tU.ID IN (SELECT USER_ID FROM b_user_access WHERE ACCESS_CODE = 'G1' AND USER_ID <> " . $author_id . ")\n\t\t\t\t" . (!empty($arUserID) ? " OR U.ID IN (" . implode(",", $arUserID) . ") " : "") . "\n\t\t\t)" . (strlen($strSubscription) > 0 || intval($arLogFields["LOG_ID"]) > 0 ? "\n\t\t\t\t\tAND\n\t\t\t\t\t(\n\t\t\t\t\t\t" . $strSubscription . (intval($arLogFields["LOG_ID"]) > 0 ? (strlen($strSubscription) > 0 ? " OR " : "") . " \n\t\t\t\t\t\t\t\tEXISTS (\n\t\t\t\t\t\t\t\t\tSELECT GROUP_CODE \n\t\t\t\t\t\t\t\t\tFROM b_sonet_log_right LR\n\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\tLR.LOG_ID = " . intval($arLogFields["LOG_ID"]) . " \n\t\t\t\t\t\t\t\t\t\tAND LR.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) " : "") . "\n\t\t\t\t\t)\n\t\t\t\t\t" : ""); return $strReturn; }