Added 'files' api methods

This commit is contained in:
gleemand 2022-07-28 11:59:40 +03:00 committed by GitHub
parent bbc458696e
commit 3436b4f956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 164 additions and 46 deletions

View File

@ -1,3 +1,9 @@
## v3.4.9
* Добавлены api методы для работы с файлами
## v3.4.8
* Исправлена синхронизация заказов при ненастроенном маппинге статусов
## v3.4.7
* Исправлена ошибка в работе воркеров публичной части сайта

View File

@ -1 +1 @@
3.4.8
3.4.9

View File

@ -52,7 +52,7 @@ class RetailcrmOrdersController extends RetailcrmAdminPostAbstractController
protected function getHandler()
{
$orders = Tools::getValue('orders', []);
$page = (int) (Tools::getValue('page', 1));
$page = (int) Tools::getValue('page', 1);
switch (Tools::getValue('filter')) {
case '1':

View File

@ -69,20 +69,20 @@ class RetailcrmReferencesController extends RetailcrmAdminPostAbstractController
$moduleReferences = new RetailcrmReferences($client);
switch (true) {
case Tools::getIsset('delivery'):
$references['deliveryTypesCRM'] = $moduleReferences->getApiDeliveryTypes();
case Tools::getIsset('delivery'):
$references['deliveryTypesCRM'] = $moduleReferences->getApiDeliveryTypes();
break;
case Tools::getIsset('payment'):
$references['paymentTypesCRM'] = $moduleReferences->getApiPaymentTypes();
break;
case Tools::getIsset('payment'):
$references['paymentTypesCRM'] = $moduleReferences->getApiPaymentTypes();
break;
case Tools::getIsset('status'):
$references['statusesCRM'] = $moduleReferences->getApiStatusesWithGroup();
break;
case Tools::getIsset('status'):
$references['statusesCRM'] = $moduleReferences->getApiStatusesWithGroup();
break;
default:
throw new Exception('Invalid request data');
break;
default:
throw new Exception('Invalid request data');
}
return [

View File

@ -61,8 +61,8 @@ class RetailcrmContextSwitcher
self::storeContext();
foreach (self::getShops() as $shop) {
self::setShopContext((int) ($shop['id_shop']));
$result[(int) ($shop['id_shop'])] = call_user_func_array($callback, $arguments);
self::setShopContext((int) $shop['id_shop']);
$result[(int) $shop['id_shop']] = call_user_func_array($callback, $arguments);
}
self::restoreContext();

View File

@ -91,7 +91,7 @@ class RetailcrmExportOrdersHelper
$sqlOrdersInfo = 'FROM `' . _DB_PREFIX_ . 'retailcrm_exported_orders` eo
LEFT JOIN `' . _DB_PREFIX_ . 'orders` o on o.`id_order` = eo.`id_order`
WHERE 1 ' . Shop::addSqlRestriction(false, 'o')
;
;
if (0 < count($ordersIds)) {
$sqlOrdersInfo .= ' AND (eo.`id_order` IN ( ' . pSQL(implode(', ', $ordersIds)) . ')

View File

@ -56,9 +56,9 @@ class RetailcrmHistory
private static function init()
{
self::$receiveOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING));
self::$sendOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_SENDING));
self::$cartStatus = (string) (Configuration::get(RetailCRM::SYNC_CARTS_STATUS));
self::$receiveOrderNumber = (bool) Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING);
self::$sendOrderNumber = (bool) Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_SENDING);
self::$cartStatus = (string) Configuration::get(RetailCRM::SYNC_CARTS_STATUS);
self::$statuses = array_flip(array_filter(json_decode(Configuration::get(RetailCRM::STATUS), true)));
self::$deliveries = array_flip(array_filter(json_decode(Configuration::get(RetailCRM::DELIVERY), true)));
self::$payments = array_flip(array_filter(json_decode(Configuration::get(RetailCRM::PAYMENT), true)));
@ -1002,7 +1002,7 @@ class RetailcrmHistory
$default_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
$newOrder = new Order();
$newOrder->id_shop = Context::getContext()->shop->id;
$newOrder->id_shop_group = (int) (Context::getContext()->shop->id_shop_group);
$newOrder->id_shop_group = (int) Context::getContext()->shop->id_shop_group;
$newOrder->id_address_delivery = isset($addressDelivery->id) ? (int) $addressDelivery->id : 0;
$newOrder->id_address_invoice = isset($addressInvoice->id) ? (int) $addressInvoice->id : 0;
$newOrder->id_cart = (int) $cart->id;
@ -1289,7 +1289,7 @@ class RetailcrmHistory
->setAlias($orderAddress->alias)
->build()
->getData()
;
;
}
private static function checkDeliveryTypeAndCost($order, $orderToUpdate)
@ -1299,12 +1299,12 @@ class RetailcrmHistory
$orderDeliveryCost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : null;
if ((
null !== $orderDeliveryCode
&& isset(self::$deliveries[$orderDeliveryCode])
&& null !== self::$deliveries[$orderDeliveryCode]
&& self::$deliveries[$orderDeliveryCode] !== $orderToUpdate->id_carrier
)
|| null !== $orderDeliveryCost
null !== $orderDeliveryCode
&& isset(self::$deliveries[$orderDeliveryCode])
&& null !== self::$deliveries[$orderDeliveryCode]
&& self::$deliveries[$orderDeliveryCode] !== $orderToUpdate->id_carrier
)
|| null !== $orderDeliveryCost
) {
$orderCarrier = self::getOrderCarrier($orderToUpdate);

View File

@ -295,7 +295,6 @@ class RetailcrmOrderBuilder
$this->cmsCustomer,
$this->cmsCart,
false,
false,
$dataFromCart,
'',
'',
@ -861,7 +860,6 @@ class RetailcrmOrderBuilder
$this->cmsCustomer,
$this->cmsCart,
false,
false,
$dataFromCart,
$contactPersonId,
$contactPersonExternalId,
@ -902,7 +900,7 @@ class RetailcrmOrderBuilder
$delivery = json_decode(Configuration::get(RetailCRM::DELIVERY), true);
$payment = json_decode(Configuration::get(RetailCRM::PAYMENT), true);
$status = json_decode(Configuration::get(RetailCRM::STATUS), true);
$sendOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_SENDING));
$sendOrderNumber = (bool) Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_SENDING);
$orderNumber = $sendOrderNumber ? $order->reference : null;
if (false === Module::getInstanceByName('advancedcheckout')) {

View File

@ -207,6 +207,111 @@ class RetailcrmApiClientV5
);
}
/**
* Get files list
*
* @param array $filter
* @param null $limit
* @param null $page
*
* @return RetailcrmApiResponse
*/
public function filesList(array $filter = [], $limit = null, $page = null)
{
$parameters = [];
if (count($filter)) {
$parameters['filter'] = $filter;
}
if (null !== $page) {
$parameters['page'] = (int) $page;
}
if (null !== $limit) {
$parameters['limit'] = (int) $limit;
}
return $this->client->makeRequest(
'/files',
RetailcrmHttpClient::METHOD_GET,
$parameters
);
}
/**
* Upload file
*
* @param $file
*
* @return RetailcrmApiResponse
*/
public function filesUpload($file)
{
return $this->client->makeRequest(
'/files/upload',
RetailcrmHttpClient::METHOD_POST,
['file' => $file]
);
}
/**
* Returns file data
*
* @param int $id file ID
*
* @throws \RetailCrm\Exception\InvalidJsonException
* @throws \RetailCrm\Exception\CurlException
* @throws \InvalidArgumentException
*
* @return RetailcrmApiResponse
*/
public function filesGet($id)
{
return $this->client->makeRequest("/files/$id", RetailcrmHttpClient::METHOD_GET);
}
/**
* Delete file
*
* @param string $id file id
*
* @return RetailcrmApiResponse
*/
public function filesDelete($id)
{
if (!$id) {
throw new \InvalidArgumentException(
'Parameter `id` must be set'
);
}
return $this->client->makeRequest(
sprintf('/files/%s/delete', $id),
RetailcrmHttpClient::METHOD_POST
);
}
/**
* Edit file
*
* @param array $file
*
* @return RetailcrmApiResponse
*/
public function filesEdit(array $file)
{
if (!count($file)) {
throw new \InvalidArgumentException(
'Parameter `file` must contains a data'
);
}
return $this->client->makeRequest(
"/files/{$file['id']}/edit",
RetailcrmHttpClient::METHOD_POST,
['file' => json_encode($file), 'id' => $file['id']]
);
}
/**
* Get custom fields list
*

View File

@ -114,6 +114,10 @@ class RetailcrmHttpClient
$url .= '?' . http_build_query($parameters, '', '&');
}
if (self::METHOD_POST === $method && '/files/upload' === $path) {
$url .= '?apiKey=' . $parameters['apiKey'];
}
$curlHandler = curl_init();
curl_setopt($curlHandler, CURLOPT_URL, $url);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
@ -126,7 +130,12 @@ class RetailcrmHttpClient
if (self::METHOD_POST === $method) {
curl_setopt($curlHandler, CURLOPT_POST, true);
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
if ('/files/upload' === $path) {
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, file_get_contents($parameters['file']));
} else {
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
}
}
$responseBody = curl_exec($curlHandler);

View File

@ -66,7 +66,7 @@ class RetailcrmExceptionMiddleware implements RetailcrmMiddlewareInterface
sprintf(
'Expected instance of `%s`, but `%s` given',
RetailcrmApiResponse::class,
(is_object($response) ? get_class($response) : gettype($response))
is_object($response) ? get_class($response) : gettype($response)
)
);
}

View File

@ -132,7 +132,7 @@ class RetailcrmExportOrdersMiddleware implements RetailcrmMiddlewareInterface
$uploadedOrders = [];
foreach ($orders as $order) {
RetailcrmExportOrdersHelper::updateExportState($order['externalId'], $order['id']);
$uploadedOrders[] = (int) ($order['externalId']);
$uploadedOrders[] = (int) $order['externalId'];
}
$notUploadedOrders = array_filter($requestedOrders, function ($orderId) use ($uploadedOrders) {

View File

@ -53,7 +53,7 @@ class RetailcrmReferenceMiddleware implements RetailcrmMiddlewareInterface
|| 'ordersEdit' === $request->getMethod()
)
) {
$receiveOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING));
$receiveOrderNumber = (bool) Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING);
$crmOrder = $response->order;
if ($receiveOrderNumber

View File

@ -58,7 +58,7 @@ class RetailcrmAbandonedCartsEvent extends RetailcrmAbstractEvent implements Ret
}
foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);
$syncCartsActive = Configuration::get(RetailCRM::SYNC_CARTS_ACTIVE);

View File

@ -54,7 +54,7 @@ class RetailcrmExportEvent extends RetailcrmAbstractEvent implements RetailcrmEv
$shops = $this->getShops();
foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);
$api = RetailcrmTools::getApiClient();

View File

@ -54,7 +54,7 @@ class RetailcrmIcmlEvent extends RetailcrmAbstractEvent implements RetailcrmEven
$shops = $this->getShops();
foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);
$job = new RetailcrmCatalog();
$data = $job->getData();

View File

@ -54,7 +54,7 @@ class RetailcrmIcmlUpdateUrlEvent extends RetailcrmAbstractEvent implements Reta
$shops = $this->getShops();
foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);
if (!file_exists(RetailcrmCatalogHelper::getIcmlFilePath())) {
continue;

View File

@ -54,7 +54,7 @@ class RetailcrmInventoriesEvent extends RetailcrmAbstractEvent implements Retail
$shops = $this->getShops();
foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);
if (!Configuration::get(RetailCRM::ENABLE_BALANCES_RECEIVING)) {
RetailcrmLogger::writeDebug(

View File

@ -54,7 +54,7 @@ class RetailcrmSyncEvent extends RetailcrmAbstractEvent implements RetailcrmEven
$shops = $this->getShops();
foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);
if (!Configuration::get(RetailCRM::ENABLE_HISTORY_UPLOADS)) {
RetailcrmLogger::writeDebug(

View File

@ -54,7 +54,7 @@ class RetailcrmUpdateSinceIdEvent extends RetailcrmAbstractEvent implements Reta
$shops = $this->getShops();
foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);
$api = RetailcrmTools::getApiClient();

View File

@ -48,7 +48,7 @@ require_once dirname(__FILE__) . '/bootstrap.php';
class RetailCRM extends Module
{
const VERSION = '3.4.8';
const VERSION = '3.4.9';
const API_URL = 'RETAILCRM_ADDRESS';
const API_KEY = 'RETAILCRM_API_TOKEN';
@ -204,7 +204,7 @@ class RetailCRM extends Module
&& ($this->use_new_hooks ? $this->registerHook('actionValidateCustomerAddressForm') : true)
&& $this->installDB()
&& $this->installTab()
;
;
}
/**
@ -342,21 +342,21 @@ class RetailCRM extends Module
&& Configuration::deleteByName(RetailcrmCli::CURRENT_TASK_CLI)
&& $this->uninstallDB()
&& $this->uninstallTab()
;
;
}
public function enable($force_all = false)
{
return parent::enable($force_all)
&& $this->installTab()
;
;
}
public function disable($force_all = false)
{
return parent::disable($force_all)
&& $this->uninstallTab()
;
;
}
public function installDB()