meraproject/vendor/amocrm/amocrm-api-library/examples/custom_fields_actions.php
keboss-m 5c21d25d45 Initial commit: Merakomis portal, Docker stack and user-reader API.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 11:04:05 +03:00

133 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use AmoCRM\Collections\CustomFields\CustomFieldEnumsCollection;
use AmoCRM\Helpers\EntityTypesInterface;
use AmoCRM\Collections\CustomFields\CustomFieldsCollection;
use AmoCRM\Exceptions\AmoCRMApiException;
use AmoCRM\Models\CustomFields\CheckboxCustomFieldModel;
use AmoCRM\Models\CustomFields\CustomFieldModel;
use AmoCRM\Models\CustomFields\EnumModel;
use AmoCRM\Models\CustomFields\SelectCustomFieldModel;
use AmoCRM\Models\CustomFields\TextCustomFieldModel;
use League\OAuth2\Client\Token\AccessTokenInterface;
include_once __DIR__ . '/bootstrap.php';
$accessToken = getToken();
$apiClient->setAccessToken($accessToken)
->setAccountBaseDomain($accessToken->getValues()['baseDomain'])
->onAccessTokenRefresh(
function (AccessTokenInterface $accessToken, string $baseDomain) {
saveToken(
[
'accessToken' => $accessToken->getToken(),
'refreshToken' => $accessToken->getRefreshToken(),
'expires' => $accessToken->getExpires(),
'baseDomain' => $baseDomain,
]
);
}
);
//Сервис кастом полей для сделок
$customFieldsService = $apiClient->customFields(EntityTypesInterface::LEADS);
//Сервис кастом полей для сегментов
//$customFieldsService = $apiClient->customFields(EntityTypesInterface::CUSTOMERS_SEGMENTS);
//Сервис кастом полей для каталога (id каталога указывается через :)
//$customFieldsService = $apiClient->customFields(EntityTypesInterface::CATALOGS . ':' . 4255);
//Получим поля
try {
$result = $customFieldsService->get();
} catch (AmoCRMApiException $e) {
printError($e);
die;
}
//Создадим модель поля и освежим его
$customFieldModel = new CustomFieldModel();
$customFieldModel->setId(269303);
try {
$customFieldModel = $customFieldsService->syncOne($customFieldModel);
} catch (AmoCRMApiException $e) {
printError($e);
die;
}
//Создадим несколько полей
$customFieldsCollection = new CustomFieldsCollection();
$cf = new TextCustomFieldModel();
$cf
->setName('Поле Текст')
->setSort(10);
$customFieldsCollection->add($cf);
$cf = new CheckboxCustomFieldModel();
$cf
->setName('Поле Чекбокс')
->setSort(20)
->setCode('MYSUPERCHECKBOX100');
$customFieldsCollection->add($cf);
$cf = new SelectCustomFieldModel();
$cf
->setName('Поле Список')
->setSort(30)
->setCode('MYSUPERLISTCF100')
->setEnums(
(new CustomFieldEnumsCollection())
->add(
(new EnumModel())
->setValue('Значение 1')
->setSort(10)
)
->add(
(new EnumModel())
->setValue('Значение 2')
->setSort(20)
)
->add(
(new EnumModel())
->setValue('Значение 3')
->setSort(30)
)
);
$customFieldsCollection->add($cf);
try {
//Добавим поля в аккаунт
$customFieldsCollection = $customFieldsService->add($customFieldsCollection);
//Получим объект поля и удалим его
$fieldToDelete = $customFieldsCollection->getBy('code', 'MYSUPERCHECKBOX100');
if ($fieldToDelete) {
$customFieldsService->deleteOne($fieldToDelete);
}
//Получим объект группы и обновим, добавим enum и сделаем поле доступным для редактирования только через API
$fieldToUpdate = $customFieldsCollection->getBy('code', 'MYSUPERLISTCF100');
$enums = $fieldToUpdate->getEnums();
if ($enums) {
$enums->add(
(new EnumModel())
->setValue('Значение 4')
->setSort(40)
);
}
$fieldToUpdate->setEnums($enums);
$fieldToUpdate->setIsApiOnly(true);
$fieldToUpdate = $customFieldsService->updateOne($fieldToUpdate);
var_dump($fieldToUpdate->toArray());
} catch (AmoCRMApiException $e) {
printError($e);
die;
}