Поддержка серверов

CCrmActivity::RegisterLiveFeedEvent: метод Битрикс

Постоянная ссылка: http://bxapi.ru/src/?id=184628

//    CCrmActivity::RegisterLiveFeedEvent()
//    /bitrix/modules/crm/classes/general/crm_activity.php:6354

    private static function RegisterLiveFeedEvent(&$arFields)
    {
        static $contextUserId = false;

        $ID = isset($arFields['ID']) ? intval($arFields['ID']) : 0;
        if($ID <= 0)
        {
            $arFields['ERROR'] = 'Could not find activity ID.';
            return false;
        }

        $ownerTypeID = isset($arFields['OWNER_TYPE_ID']) ? intval($arFields['OWNER_TYPE_ID']) : CCrmOwnerType::Undefined;
        if(!CCrmOwnerType::IsDefined($ownerTypeID))
        {
            $arFields['ERROR'] = 'Could not find owner type ID.';
            return false;
        }

        $ownerID = isset($arFields['OWNER_ID']) ? intval($arFields['OWNER_ID']) : 0;
        if($ownerID <= 0)
        {
            $arFields['ERROR'] = 'Could not find owner ID.';
            return false;
        }

        $arOwners = array(
            array(
                "OWNER_TYPE_ID" => $ownerTypeID,
                "OWNER_ID" => $ownerID
            )
        );

        $authorID = isset($arFields['AUTHOR_ID']) ? intval($arFields['AUTHOR_ID']) : 0;
        $editorID = isset($arFields['EDITOR_ID']) ? intval($arFields['EDITOR_ID']) : 0;
        $userID = $authorID > 0 ? $authorID : $editorID;
        if($userID <= 0)
        {
            $userID = CCrmSecurityHelper::GetCurrentUserID();
        }

        // Params are not assigned - we will use current activity only.
        $liveFeedFields = array(
            'ENTITY_TYPE_ID' => CCrmOwnerType::Activity,
            'ENTITY_ID' => $ID,
            'USER_ID' => $userID,
            'MESSAGE' => '',
            'TITLE' => '',
            'LOG_RIGHTS' => (!empty($arFields["RESPONSIBLE_ID"]) && intval($arFields["RESPONSIBLE_ID"]) > 0 ? array('U'.$arFields["RESPONSIBLE_ID"]) : false)
        );

        $bindings = isset($arFields['BINDINGS']) && is_array($arFields['BINDINGS']) ? $arFields['BINDINGS'] : array();
        if(!empty($bindings))
        {
            $liveFeedFields['PARENTS'] = $bindings;
            $liveFeedFields['PARENT_OPTIONS'] = array(
                'ENTITY_TYPE_ID_KEY' => 'OWNER_TYPE_ID',
                'ENTITY_ID_KEY' => 'OWNER_ID'
            );

            $ownerInfoOptions = array(
                'ENTITY_TYPE_ID_KEY' => 'OWNER_TYPE_ID',
                'ENTITY_ID_KEY' => 'OWNER_ID',
                'ADDITIONAL_DATA' => array('LEVEL' => 2)
            );

            $additionalParents = array();
            foreach($bindings as &$binding)
            {
                $ownerTypeID = isset($binding['OWNER_TYPE_ID']) ? intval($binding['OWNER_TYPE_ID']) : CCrmOwnerType::Undefined;
                $ownerID = isset($binding['OWNER_ID']) ? intval($binding['OWNER_ID']) : 0;

                if (
                    $ownerTypeID != CCrmOwnerType::Undefined
                    && $ownerID > 0
                )
                {
                    $arOwners[] = array(
                        "OWNER_TYPE_ID" => $ownerTypeID,
                        "OWNER_ID" => $ownerID
                    );
                }

                if(
                    $ownerTypeID === CCrmOwnerType::Contact
                    && $ownerID > 0
                )
                {
                    $owners = array();
                    if(CCrmOwnerType::TryGetOwnerInfos(CCrmOwnerType::Contact, $ownerID, $owners, $ownerInfoOptions))
                    {
                        $additionalParents = array_merge($additionalParents, $owners);
                    }
                }
            }
            unset($binding);
            if(!empty($additionalParents))
            {
                $liveFeedFields['PARENTS'] = array_merge($bindings, $additionalParents);
            }

            $arOwners = array_unique($arOwners);
        }

        self::PrepareStorageElementIDs($arFields);
        $arStorageElementID = $arFields["STORAGE_ELEMENT_IDS"];
        if (!empty($arStorageElementID))
        {
            if ($arFields["STORAGE_TYPE_ID"] == StorageType::WebDav)
            {
                $liveFeedFields["UF_SONET_LOG_DOC"] = $arStorageElementID;
            }
            else if ($arFields["STORAGE_TYPE_ID"] == StorageType::Disk)
            {
                $liveFeedFields["UF_SONET_LOG_DOC"] = array();
                //We have to add prefix Bitrix\Disk\Uf\FileUserType::NEW_FILE_PREFIX to file ID
                foreach($arStorageElementID as $elementID)
                {
                    $liveFeedFields["UF_SONET_LOG_DOC"][] = "n{$elementID}";
                }
            }
            else
            {
                $liveFeedFields["UF_SONET_LOG_FILE"] = $arStorageElementID;
            }
        }

        if ($arFields["TYPE_ID"] == CCrmActivityType::Email)
        {
            if ($contextUserId === false)
            {
                $res = \Bitrix\Main\UserGroupTable::getList(array(
                    'order' => array(
                        'USER_ID' => 'ASC'
                    ),
                    'filter' => array(
                        'GROUP_ID' => 1,
                        '=USER.ACTIVE' => 'Y'
                    ),
                    'select' => array('USER_ID'),
                    'limit' => 1
                ));
                if ($userGroupFields = $res->fetch())
                {
                    // hack: for UF CheckFields(), agent call
                    $contextUserId = $liveFeedFields['CONTEXT_USER_ID'] = $userGroupFields['USER_ID'];
                }
            }
            else
            {
                $liveFeedFields['CONTEXT_USER_ID'] = $contextUserId;
            }
        }

        $eventID = 0;
        $associatedEntityId = isset($arFields['ASSOCIATED_ENTITY_ID']) ? (int)$arFields['ASSOCIATED_ENTITY_ID'] : 0;
        $provider = self::GetActivityProvider($arFields);
        if ($provider !== null)
            $eventID = $provider::createLiveFeedLog($associatedEntityId, $arFields, $liveFeedFields);

        if ($eventID === 0)
        {
            $arOptions = array();
            if (isset($arFields['PROVIDER_ID']))
            {
                $arOptions['ACTIVITY_PROVIDER_ID'] = $arFields['PROVIDER_ID'];
            }
            $eventID = CCrmLiveFeed::CreateLogEvent($liveFeedFields, CCrmLiveFeedEvent::Add, $arOptions);
        }

        if(!(is_int($eventID) && $eventID > 0) && isset($liveFeedFields['ERROR']))
        {
            $arFields['ERROR'] = $liveFeedFields['ERROR'];
        }
        else
        {
            if (
                intval($arFields["RESPONSIBLE_ID"]) > 0
                && $arFields["RESPONSIBLE_ID"] != $userID
                && CModule::IncludeModule("im")
            )
            {
                $bHasPermissions = false;
                $perms = CCrmPerms::GetUserPermissions($arFields["RESPONSIBLE_ID"]);
                foreach ($arOwners as $arOwner)
                {
                    if (CCrmActivity::CheckReadPermission($arOwner["OWNER_TYPE_ID"], $arOwner["OWNER_ID"], $perms))
                    {
                        $bHasPermissions = true;
                        break;
                    }
                }

                switch ($arFields['TYPE_ID'])
                {
                    case CCrmActivityType::Call:
                        $type = 'CALL';
                        break;
                    case CCrmActivityType::Meeting:
                        $type = 'MEETING';
                        break;
                    default:
                        $type = false;
                }

                if ($type)
                {
                    $url = "/crm/stream/?log_id=#log_id#";
                    $url = str_replace(array("#log_id#"), array($eventID), $url);
                    $serverName = (CMain::IsHTTPS() ? "https" : "http")."://".((defined("SITE_SERVER_NAME") && strlen(SITE_SERVER_NAME) > 0) ? SITE_SERVER_NAME : COption::GetOptionString("main", "server_name", ""));

                    $arMessageFields = array(
                        "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
                        "TO_USER_ID" => $arFields["RESPONSIBLE_ID"],
                        "FROM_USER_ID" => $userID,
                        "NOTIFY_TYPE" => IM_NOTIFY_FROM,
                        "NOTIFY_MODULE" => "crm",
                        "LOG_ID" => $eventID,
                        "NOTIFY_EVENT" => "activity_add",
                        "NOTIFY_TAG" => "CRM|ACTIVITY|".$ID,
                        "NOTIFY_MESSAGE" => GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => ''.htmlspecialcharsbx($arFields['SUBJECT']).'')),
                        "NOTIFY_MESSAGE_OUT" => GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($arFields['SUBJECT'])))." (".$serverName.$url.")"
                    );

                    if(!$bHasPermissions)
                    {
                        //TODO: Add message 'Need for permissions'
                        $arMessageFields["NOTIFY_MESSAGE"] = GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($arFields['SUBJECT'])));
                        $arMessageFields["NOTIFY_MESSAGE_OUT"] = GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($arFields['SUBJECT'])));
                    }

                    CIMNotify::Add($arMessageFields);
                }
            }
        }

        return $eventID;
    }

Блог разработчика

9 Августа 2016 ResizeImageGet и простой водяной знак налету

Сегодня поговорим о том как проще всего воткнуть водяной знак на картинки в Битрикс. Например, на все изображения каталога.

21 Июля 2016 Добавление вкладки в CRM коробки Битрикс24

В этом посте я покажу, как легко и просто добавить вкладку в сущность CRM. И не просто добавить, а вывести там свой контент.

21 Июля 2016 Как просто обращаться из коробки Битрикс24 на сайт на базе Битрикс

К примеру вам хочется внутренним скриптом дернуть с сайта-магазина информацию, которая там, в свою очередь, закрыта некоторыми правами. Например, админскими. Можно конечно мутить свой REST, или иной формат общения, но проще всего воспользоваться штатным инструментом:

Текст сообщения*
Загрузить изображение