Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing methods and test for CatalogElement model
- Loading branch information
Showing
7 changed files
with
358 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
try { | ||
$amo = new \AmoCRM\Client(getenv('DOMAIN'), getenv('LOGIN'), getenv('HASH')); | ||
|
||
// Список элементов каталога | ||
// Метод для получения элементов каталога аккаунта. | ||
|
||
print_r($amo->catalog_element->apiList([ | ||
'catalog_id' => 4179, | ||
'term' => 'Product' | ||
])); | ||
|
||
// Добавление элементов каталога | ||
// Метод позволяет добавлять элементы каталога по одному или пакетно | ||
|
||
$element = $amo->catalog_element; | ||
$element->debug(true); // Режим отладки | ||
$element['catalog_id'] = 4179; | ||
$element['name'] = 'Product'; | ||
$element->addCustomField(212937, 1); | ||
|
||
$id = $element->apiAdd(); | ||
print_r($id); | ||
|
||
// Или массовое добавление: | ||
$element1 = clone $element; | ||
$element1['name'] = 'Product 1'; | ||
$element2 = clone $element; | ||
$element2['name'] = 'Product 2'; | ||
|
||
$ids = $amo->catalog_element->apiAdd([$element1, $element2]); | ||
print_r($ids); | ||
|
||
// Обновление элементов каталога | ||
$element = $amo->catalog_element; | ||
$element->debug(true); // Режим отладки | ||
$element['name'] = 'New product'; | ||
$element['catalog_id'] = 4179; // без catalog_id amocrm не обновит | ||
$element->addCustomField(212937, 5000); | ||
|
||
$element->apiUpdate((int)$id); | ||
|
||
// Удаление каталогов | ||
$amo->catalog_element->apiDelete((int)$id); | ||
|
||
} catch (\AmoCRM\Exception $e) { | ||
printf('Error (%d): %s' . PHP_EOL, $e->getCode(), $e->getMessage()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace AmoCRM\Models; | ||
|
||
/** | ||
* Class CatalogElement | ||
* | ||
* Класс модель для работы с Элементами каталога | ||
* | ||
* @package AmoCRM\Models | ||
* @author mb@baso-it.ru | ||
* @author dotzero <mail@dotzero.ru> | ||
* @link https://github.com/dotzero/amocrm-php | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
class CatalogElement extends AbstractModel | ||
{ | ||
/** | ||
* @var array Список доступный полей для модели (исключая кастомные поля) | ||
*/ | ||
protected $fields = [ | ||
'catalog_id', | ||
'name', | ||
'request_id', | ||
]; | ||
|
||
/** | ||
* Список элементов каталога | ||
* | ||
* Метод для получения элементов каталога аккаунта. | ||
* | ||
* @link https://developers.amocrm.ru/rest_api/catalog_elements/list.php | ||
* @param array $parameters Массив параметров к amoCRM API | ||
* @return array Ответ amoCRM API | ||
* @throws \AmoCRM\Exception | ||
*/ | ||
public function apiList($parameters = []) | ||
{ | ||
$response = $this->getRequest('/private/api/v2/json/catalog_elements/list', $parameters); | ||
|
||
return isset($response['catalog_elements']) ? $response['catalog_elements'] : []; | ||
} | ||
|
||
/** | ||
* Добавление элементов каталога | ||
* | ||
* Метод позволяет добавлять элементы каталога по одному или пакетно | ||
* | ||
* @link https://developers.amocrm.ru/rest_api/catalog_elements/set.php | ||
* @param array $elements Массив каталогов для пакетного добавления | ||
* @return int|array Уникальный идентификатор каталога или массив при пакетном добавлении | ||
*/ | ||
public function apiAdd($elements = []) | ||
{ | ||
if (empty($elements)) { | ||
$elements = [$this]; | ||
} | ||
|
||
$parameters = [ | ||
'catalog_elements' => [ | ||
'add' => [], | ||
], | ||
]; | ||
|
||
foreach ($elements AS $element) { | ||
$parameters['catalog_elements']['add'][] = $element->getValues(); | ||
} | ||
|
||
$response = $this->postRequest('/private/api/v2/json/catalog_elements/set', $parameters); | ||
|
||
if (isset($response['catalog_elements']['add']['catalog_elements'])) { | ||
$result = array_map(function ($item) { | ||
return $item['id']; | ||
}, $response['catalog_elements']['add']['catalog_elements']); | ||
} else { | ||
return []; | ||
} | ||
|
||
return count($elements) == 1 ? array_shift($result) : $result; | ||
} | ||
|
||
/** | ||
* Обновление элементов каталога | ||
* | ||
* Метод позволяет обновлять данные по уже существующим элементам каталога | ||
* | ||
* @link https://developers.amocrm.ru/rest_api/catalog_elements/set.php | ||
* @param int $id Уникальный идентификатор элемента элемента каталога | ||
* @return bool Флаг успешности выполнения запроса | ||
* @throws \AmoCRM\Exception | ||
*/ | ||
public function apiUpdate($id) | ||
{ | ||
$this->checkId($id); | ||
|
||
$parameters = [ | ||
'catalog_elements' => [ | ||
'update' => [], | ||
], | ||
]; | ||
|
||
$catalog = $this->getValues(); | ||
$catalog['id'] = $id; | ||
|
||
$parameters['catalog_elements']['update'][] = $catalog; | ||
|
||
$response = $this->postRequest('/private/api/v2/json/catalog_elements/set', $parameters); | ||
|
||
if (!isset($response['catalog_elements']['update']['errors'])) { | ||
return false; | ||
} | ||
|
||
return empty($response['catalog_elements']['update']['errors']); | ||
} | ||
|
||
/** | ||
* Удаление элементов каталога | ||
* | ||
* Метод позволяет удалять данные по уже существующим элементам каталога | ||
* | ||
* @link https://developers.amocrm.ru/rest_api/catalog_elements/set.php | ||
* @param int $id Уникальный идентификатор элемента элемента каталога | ||
* @return bool Флаг успешности выполнения запроса | ||
* @throws \AmoCRM\Exception | ||
*/ | ||
public function apiDelete($id) | ||
{ | ||
$this->checkId($id); | ||
|
||
$parameters = [ | ||
'catalog_elements' => [ | ||
'delete' => [$id], | ||
], | ||
]; | ||
|
||
$response = $this->postRequest('/private/api/v2/json/catalog_elements/set', $parameters); | ||
|
||
if (!isset($response['catalog_elements']['delete']['errors'])) { | ||
return false; | ||
} | ||
|
||
return empty($response['catalog_elements']['delete']['errors']); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.