S
S
serik2021-02-04 13:41:06
Bitrix24
serik, 2021-02-04 13:41:06

How to make a file substitution on the OnBeforeCrmDealUpdate event?

Please tell me, I want to replace the file in the transaction on the OnBeforeCrmDealUpdate event.

the code:

public static function createDealBasedOnFiles(&$fields)
    {
        $deal = \CCrmDeal::GetList([], array('ID' => $fields['ID']))->Fetch();

        if ($deal['UF_CONTRACT_REGISTER_DOCUMENT'][0] !== $fields['UF_CONTRACT_REGISTER_DOCUMENT'][0]) {
            $resPdf = DealSubscriber::createPdf($fields['UF_CONTRACT_REGISTER_DOCUMENT'][0]);

            $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === false ? 'http' : 'https';

            $fields['UF_CONTRACT_REGISTER_DOCUMENT'][] = [  
                'fileData' => [
                    preg_replace('#.*/#', '', $resPdf['path']),
                    base64_encode(file_get_contents($protocol . '://' . $_SERVER["SERVER_NAME"] . $resPdf['path']))
                ]
            ];
        }
    }


Tell me,
1. Is it correct to use file_get_contents($protocol . '://' . $_SERVER["SERVER_NAME"] . $resPdf['path']), as I do it?

2. As a result, at the moment of changing and saving the transaction, the eternal loading is on (Screen 1.)

please tell me what I'm doing wrong!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2021-02-10
@black_xe

You have already answered correctly in your question: How to attach a file to a transaction in a multiple field of the File type?
1) When working with a boxed API, using the cloud API is not allowed.
Instead of construct:

$fields['UF_CONTRACT_REGISTER_DOCUMENT'][] = [  
                'fileData' => [
                    preg_replace('#.*/#', '', $resPdf['path']),
                    base64_encode(file_get_contents($protocol . '://' . $_SERVER["SERVER_NAME"] . $resPdf['path']))
                ]
            ];

You need to use \CFile::MakeFileArray() to which you will pass the absolute path to the desired file.
2) It is not necessary to get all the transaction fields at once. To begin with, it makes sense to check whether the UF_CONTRACT_REGISTER_DOCUMENT field has arrived in your $fields. The fact is that in the new card you can only receive NAME (for example, change the title) or a set of other fields. It makes no sense to get these fields every time and compare with a possible null value, because a null value will always not be equal to an existing element.
You can use the array_key_exists('UF_CONTRACT_REGISTER_DOCUMENT', $fields) construct to check
3) Generating a pdf file can take a long time, and the save request has a timeout of 30 seconds. If you do not have time, then for the user it will cause an endless download and a non-working card. It makes sense to do delayed generation. those. register an agent for further generation or use other mechanisms to fill in this field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question