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; }
$errorMsg = GetMessage('CRM_CONTACT_UPDATE_PERMISSION_DENIED', array('#TITLE#' => $entityTitle)); } elseif ($ownerTypeID === CCrmOwnerType::Company) { $errorMsg = GetMessage('CRM_COMPANY_UPDATE_PERMISSION_DENIED', array('#TITLE#' => $entityTitle)); } elseif ($ownerTypeID === CCrmOwnerType::Lead) { $errorMsg = GetMessage('CRM_LEAD_UPDATE_PERMISSION_DENIED', array('#TITLE#' => $entityTitle)); } elseif ($ownerTypeID === CCrmOwnerType::Deal) { $errorMsg = GetMessage('CRM_DEAL_UPDATE_PERMISSION_DENIED', array('#TITLE#' => $entityTitle)); } else { $errorMsg = GetMessage('CRM_PERMISSION_DENIED'); } echo CUtil::PhpToJSObject(array('ERROR' => $errorMsg)); die; } $userID = $curUser->GetID(); if ($userID <= 0) { $userID = CCrmOwnerType::GetResponsibleID($ownerTypeID, $ownerID, false); if ($userID <= 0) { echo CUtil::PhpToJSObject(array('ERROR' => GetMessage('CRM_ACTIVITY_RESPONSIBLE_NOT_FOUND'))); die; } } $arErrors = array(); $crmEmail = CCrmMailHelper::ExtractEmail(COption::GetOptionString('crm', 'mail', '')); $from = isset($data['from']) ? trim(strval($data['from'])) : ''; if ($from === '') { if ($crmEmail !== '') { $from = $crmEmail; } else { $arErrors[] = GetMessage('CRM_ACTIVITY_EMAIL_EMPTY_FROM_FIELD'); } } else {
public static function GetDocumentAuthorID($documentId) { if (!is_array($documentId) || count($documentId) < 3) { return 0; } $documentInfo = self::GetDocumentInfo($documentId[2]); $entityTypeName = isset($documentInfo['TYPE']) ? $documentInfo['TYPE'] : ''; $entityId = isset($documentInfo['ID']) ? intval($documentInfo['ID']) : 0; return CCrmOwnerType::GetResponsibleID(CCrmOwnerType::ResolveID($entityTypeName), $entityId, false); }
protected function innerAdd(&$fields, &$errors, array $params = null) { $ownerTypeID = isset($fields['OWNER_TYPE_ID']) ? intval($fields['OWNER_TYPE_ID']) : 0; $ownerID = isset($fields['OWNER_ID']) ? intval($fields['OWNER_ID']) : 0; $bindings = array(); if ($ownerTypeID > 0 && $ownerID > 0) { $bindings["{$ownerTypeID}_{$ownerID}"] = array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID); } $responsibleID = isset($fields['RESPONSIBLE_ID']) ? intval($fields['RESPONSIBLE_ID']) : 0; if ($responsibleID <= 0 && $ownerTypeID > 0 && $ownerID > 0) { $fields['RESPONSIBLE_ID'] = $responsibleID = CCrmOwnerType::GetResponsibleID($ownerTypeID, $ownerID); } if ($responsibleID <= 0) { $responsibleID = CCrmSecurityHelper::GetCurrentUserID(); } if ($responsibleID <= 0) { $errors[] = 'The field RESPONSIBLE_ID is not defined or invalid.'; return false; } $typeID = isset($fields['TYPE_ID']) ? intval($fields['TYPE_ID']) : CCrmActivityType::Undefined; if (!CCrmActivityType::IsDefined($typeID)) { $errors[] = 'The field TYPE_ID is not defined or invalid.'; return false; } if (!in_array($typeID, array(CCrmActivityType::Call, CCrmActivityType::Meeting, CCrmActivityType::Email), true)) { $errors[] = 'The activity type "' . CCrmActivityType::ResolveDescription($typeID) . ' is not supported in current context".'; return false; } $description = isset($fields['DESCRIPTION']) ? $fields['DESCRIPTION'] : ''; $descriptionType = isset($fields['DESCRIPTION_TYPE']) ? intval($fields['DESCRIPTION_TYPE']) : CCrmContentType::PlainText; if ($description !== '' && CCrmActivity::AddEmailSignature($description, $descriptionType)) { $fields['DESCRIPTION'] = $description; } $direction = isset($fields['DIRECTION']) ? intval($fields['DIRECTION']) : CCrmActivityDirection::Undefined; $completed = isset($fields['COMPLETED']) && strtoupper($fields['COMPLETED']) === 'Y'; $communications = isset($fields['COMMUNICATIONS']) && is_array($fields['COMMUNICATIONS']) ? $fields['COMMUNICATIONS'] : array(); $this->prepareCommunications($ownerTypeID, $ownerID, $typeID, $communications, $bindings); if (empty($communications)) { $errors[] = 'The field COMMUNICATIONS is not defined or invalid.'; return false; } if (($typeID === CCrmActivityType::Call || $typeID === CCrmActivityType::Meeting) && count($communications) > 1) { $errors[] = 'The only one communication is allowed for activity of specified type.'; return false; } if (empty($bindings)) { $errors[] = 'Could not build binding. Please ensure that owner info and communications are defined correctly.'; return false; } foreach ($bindings as &$binding) { if (!CCrmActivity::CheckUpdatePermission($binding['OWNER_TYPE_ID'], $binding['OWNER_ID'])) { $errors[] = 'Access denied.'; return false; } } unset($binding); $fields['BINDINGS'] = array_values($bindings); $fields['COMMUNICATIONS'] = $communications; $storageTypeID = $fields['STORAGE_TYPE_ID'] = CCrmActivity::GetDefaultStorageTypeID(); $fields['STORAGE_ELEMENT_IDS'] = array(); if ($storageTypeID === StorageType::WebDav) { $webdavElements = isset($fields['WEBDAV_ELEMENTS']) && is_array($fields['WEBDAV_ELEMENTS']) ? $fields['WEBDAV_ELEMENTS'] : array(); foreach ($webdavElements as &$element) { $elementID = isset($element['ELEMENT_ID']) ? intval($element['ELEMENT_ID']) : 0; if ($elementID > 0) { $fields['STORAGE_ELEMENT_IDS'][] = $elementID; } } unset($element); } elseif ($storageTypeID === StorageType::Disk) { $diskFiles = isset($fields['FILES']) && is_array($fields['FILES']) ? $fields['FILES'] : array(); if (empty($diskFiles)) { //For backward compatibility only $diskFiles = isset($fields['WEBDAV_ELEMENTS']) && is_array($fields['WEBDAV_ELEMENTS']) ? $fields['WEBDAV_ELEMENTS'] : array(); } foreach ($diskFiles as &$fileInfo) { $fileID = isset($fileInfo['FILE_ID']) ? (int) $fileInfo['FILE_ID'] : 0; if ($fileID > 0) { $fields['STORAGE_ELEMENT_IDS'][] = $fileID; } } unset($fileInfo); } if (!($ID = CCrmActivity::Add($fields))) { $errors[] = CCrmActivity::GetLastErrorMessage(); return false; } CCrmActivity::SaveCommunications($ID, $communications, $fields, false, false); if ($completed && $typeID === CCrmActivityType::Email && $direction === CCrmActivityDirection::Outgoing) { $sendErrors = array(); if (!CCrmActivityEmailSender::TrySendEmail($ID, $fields, $sendErrors)) { foreach ($sendErrors as &$error) { $code = $error['CODE']; if ($code === CCrmActivityEmailSender::ERR_CANT_LOAD_SUBSCRIBE) { $errors[] = 'Email send error. Failed to load module "subscribe".'; } elseif ($code === CCrmActivityEmailSender::ERR_INVALID_DATA) { $errors[] = 'Email send error. Invalid data.'; } elseif ($code === CCrmActivityEmailSender::ERR_INVALID_EMAIL) { $errors[] = 'Email send error. Invalid email is specified.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_FIND_EMAIL_FROM) { $errors[] = 'Email send error. "From" is not found.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_FIND_EMAIL_TO) { $errors[] = 'Email send error. "To" is not found.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_ADD_POSTING) { $errors[] = 'Email send error. Failed to add posting. Please see details below.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_SAVE_POSTING_FILE) { $errors[] = 'Email send error. Failed to save posting file. Please see details below.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_UPDATE_ACTIVITY) { $errors[] = 'Email send error. Failed to update activity.'; } else { $errors[] = 'Email send error. General error.'; } $msg = isset($error['MESSAGE']) ? $error['MESSAGE'] : ''; if ($msg !== '') { $errors[] = $msg; } } unset($error); return false; } } return $ID; }