Skip to content

Commit

Permalink
Added 'files' api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gleemand authored Jul 28, 2022
1 parent bbc4586 commit 3436b4f
Show file tree
Hide file tree
Showing 21 changed files with 164 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v3.4.9
* Добавлены api методы для работы с файлами

## v3.4.8
* Исправлена синхронизация заказов при ненастроенном маппинге статусов

## v3.4.7
* Исправлена ошибка в работе воркеров публичной части сайта

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.8
3.4.9
2 changes: 1 addition & 1 deletion retailcrm/controllers/admin/RetailcrmOrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function postHandler()
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':
Expand Down
22 changes: 11 additions & 11 deletions retailcrm/controllers/admin/RetailcrmReferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ protected function getSpecifiedReferences()
$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 [
Expand Down
4 changes: 2 additions & 2 deletions retailcrm/lib/RetailcrmContextSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public static function runInContext($callback, $arguments = [])
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();
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/RetailcrmExportOrdersHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function getOrders($ordersIds = [], $withErrors = null, $page = 1)
$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)) . ')
Expand Down
22 changes: 11 additions & 11 deletions retailcrm/lib/RetailcrmHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -1002,7 +1002,7 @@ private static function createOrder($cart, $customer, $order, $deliveryType, $pa
$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;
Expand Down Expand Up @@ -1289,7 +1289,7 @@ private static function createOrderAddress($order, $orderToUpdate)
->setAlias($orderAddress->alias)
->build()
->getData()
;
;
}

private static function checkDeliveryTypeAndCost($order, $orderToUpdate)
Expand All @@ -1299,12 +1299,12 @@ private static function checkDeliveryTypeAndCost($order, $orderToUpdate)
$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);

Expand Down
4 changes: 1 addition & 3 deletions retailcrm/lib/RetailcrmOrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ private function buildRegularOrder($dataFromCart = false)
$this->cmsCustomer,
$this->cmsCart,
false,
false,
$dataFromCart,
'',
'',
Expand Down Expand Up @@ -861,7 +860,6 @@ private function buildCorporateOrder($dataFromCart = false)
$this->cmsCustomer,
$this->cmsCart,
false,
false,
$dataFromCart,
$contactPersonId,
$contactPersonExternalId,
Expand Down Expand Up @@ -902,7 +900,7 @@ public static function buildCrmOrder(
$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')) {
Expand Down
105 changes: 105 additions & 0 deletions retailcrm/lib/api/RetailcrmApiClientV5.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,111 @@ public function segmentsList(array $filter = [], $limit = null, $page = null)
);
}

/**
* 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
*
Expand Down
11 changes: 10 additions & 1 deletion retailcrm/lib/api/RetailcrmHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function makeRequest(
$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);
Expand All @@ -126,7 +130,12 @@ public function makeRequest(

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function checkResponseType($response)
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)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private function handleOrdersUpload(RetailcrmApiRequest $request, callable $next
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __invoke(RetailcrmApiRequest $request, callable $next = null)
|| '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
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/events/RetailcrmAbandonedCartsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function execute()
}

foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);

$syncCartsActive = Configuration::get(RetailCRM::SYNC_CARTS_ACTIVE);

Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/events/RetailcrmExportEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute()
$shops = $this->getShops();

foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);

$api = RetailcrmTools::getApiClient();

Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/events/RetailcrmIcmlEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute()
$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();
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/events/RetailcrmIcmlUpdateUrlEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute()
$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;
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/events/RetailcrmInventoriesEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute()
$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(
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/events/RetailcrmSyncEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute()
$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(
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/events/RetailcrmUpdateSinceIdEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute()
$shops = $this->getShops();

foreach ($shops as $shop) {
RetailcrmContextSwitcher::setShopContext((int) ($shop['id_shop']));
RetailcrmContextSwitcher::setShopContext((int) $shop['id_shop']);

$api = RetailcrmTools::getApiClient();

Expand Down
Loading

0 comments on commit 3436b4f

Please sign in to comment.