1
0
mirror of synced 2024-11-26 23:36:08 +03:00
bitrix-module/intaro.intarocrm/install/index.php

1103 lines
51 KiB
PHP
Raw Normal View History

2013-08-14 10:44:27 +04:00
<?php
2013-09-05 12:11:29 +04:00
2013-08-14 10:44:27 +04:00
/**
* Module Install/Uninstall script
* Module name: intaro.intarocrm
* Class name: intaro_intarocrm
*/
global $MESS;
IncludeModuleLangFile(__FILE__);
if (class_exists('intaro_intarocrm'))
return;
2013-09-05 12:11:29 +04:00
class intaro_intarocrm extends CModule {
2013-08-14 10:44:27 +04:00
var $MODULE_ID = 'intaro.intarocrm';
var $MODULE_VERSION;
var $MODULE_VERSION_DATE;
var $MODULE_NAME;
var $MODULE_DESCRIPTION;
var $MODULE_GROUP_RIGHTS = 'N';
var $PARTNER_NAME;
var $PARTNER_URI;
var $INTARO_CRM_API;
var $INTARO_CRM_EXPORT = 'intarocrm';
2013-08-14 10:44:27 +04:00
var $CRM_API_HOST_OPTION = 'api_host';
var $CRM_API_KEY_OPTION = 'api_key';
var $CRM_ORDER_TYPES_ARR = 'order_types_arr';
var $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
var $CRM_PAYMENT_TYPES = 'pay_types_arr';
var $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
var $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
var $CRM_ORDER_LAST_ID = 'order_last_id';
2013-09-02 15:48:12 +04:00
var $CRM_ORDER_SITES = 'sites_ids';
2013-09-05 12:11:29 +04:00
var $CRM_ORDER_PROPS = 'order_props';
2013-09-12 19:00:46 +04:00
var $CRM_ORDER_DISCHARGE = 'order_discharge';
2013-08-14 10:44:27 +04:00
var $INSTALL_PATH;
2013-09-05 12:11:29 +04:00
function intaro_intarocrm() {
2013-08-14 10:44:27 +04:00
$arModuleVersion = array();
$path = str_replace("\\", "/", __FILE__);
$path = substr($path, 0, strlen($path) - strlen("/index.php"));
$this->INSTALL_PATH = $path;
2013-09-05 12:11:29 +04:00
include($path . "/version.php");
2013-08-14 10:44:27 +04:00
$this->MODULE_VERSION = $arModuleVersion["VERSION"];
$this->MODULE_VERSION_DATE = $arModuleVersion["VERSION_DATE"];
$this->MODULE_NAME = GetMessage('MODULE_NAME');
$this->MODULE_DESCRIPTION = GetMessage('MODULE_DESCRIPTION');
$this->PARTNER_NAME = GetMessage('MODULE_PARTNER_NAME');
$this->PARTNER_URI = GetMessage('MODULE_PARTNER_URI');
}
/**
* Functions DoInstall and DoUninstall are
* All other functions are optional
*/
2013-09-05 12:11:29 +04:00
function DoInstall() {
2013-08-14 10:44:27 +04:00
global $APPLICATION, $step, $arResult;
2013-08-14 19:56:52 +04:00
if (!in_array('curl', get_loaded_extensions())) {
2013-09-02 16:29:54 +04:00
$APPLICATION->ThrowException(GetMessage("INTAROCRM_CURL_ERR"));
2013-08-14 19:56:52 +04:00
return false;
}
2013-09-05 12:11:29 +04:00
2013-09-02 16:29:54 +04:00
if (!date_default_timezone_get()) {
if (!ini_get('date.timezone')) {
$APPLICATION->ThrowException(GetMessage("DATE_TIMEZONE_ERR"));
2013-09-05 12:11:29 +04:00
return false;
2013-09-02 16:29:54 +04:00
}
2013-09-02 16:23:15 +04:00
}
2013-08-14 10:44:27 +04:00
2013-09-03 13:22:05 +04:00
if (!date_default_timezone_get()) {
if (!ini_get('date.timezone')) {
$APPLICATION->ThrowException(GetMessage("DATE_TIMEZONE_ERR"));
return false;
}
}
2013-08-14 10:44:27 +04:00
include($this->INSTALL_PATH . '/../classes/general/RestApi.php');
include($this->INSTALL_PATH . '/../classes/general/ICrmOrderActions.php');
2013-08-14 19:56:52 +04:00
include($this->INSTALL_PATH . '/../classes/general/ICMLLoader.php');
2013-08-14 10:44:27 +04:00
$step = intval($_REQUEST['step']);
2013-09-05 12:11:29 +04:00
$arResult['orderProps'] = array(
array(
'NAME' => GetMessage('FIO'),
'ID' => 'fio'
),
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
),
array(
'NAME' => GetMessage('PHONE'),
'ID' => 'phone'
),
array(
'NAME' => GetMessage('EMAIL'),
'ID' => 'email'
),
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
),
array(
'NAME' => GetMessage('ADDRESS'),
'ID' => 'text'
),
// address
/* array(
'NAME' => GetMessage('COUNTRY'),
'ID' => 'country'
),
array(
'NAME' => GetMessage('REGION'),
'ID' => 'region'
),
array(
'NAME' => GetMessage('CITY'),
'ID' => 'city'
), */
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
),
array(
'NAME' => GetMessage('STREET'),
'ID' => 'street'
),
array(
'NAME' => GetMessage('BUILDING'),
'ID' => 'building'
),
array(
'NAME' => GetMessage('FLAT'),
'ID' => 'flat'
),
array(
2013-09-05 13:38:58 +04:00
'NAME' => GetMessage('INTERCOMCODE'),
'ID' => 'intercomcode'
2013-09-05 12:11:29 +04:00
),
array(
'NAME' => GetMessage('FLOOR'),
'ID' => 'floor'
),
array(
'NAME' => GetMessage('BLOCK'),
'ID' => 'block'
),
array(
'NAME' => GetMessage('HOUSE'),
'ID' => 'house'
)
);
2013-08-14 10:44:27 +04:00
if ($step <= 1) {
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("sale")) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_SALE';
}
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("iblock")) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_IBLOCK';
}
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("catalog")) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_CATALOG';
}
2013-09-02 15:48:12 +04:00
$arResult['arSites'] = array();
$rsSites = CSite::GetList($by, $sort, array());
while ($ar = $rsSites->Fetch())
$arResult['arSites'][] = $ar;
2013-08-14 10:44:27 +04:00
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'), $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
2013-08-14 10:44:27 +04:00
);
} else if ($step == 2) {
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("sale")) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_SALE';
}
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("iblock")) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_IBLOCK';
}
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("catalog")) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_CATALOG';
}
2013-09-05 12:11:29 +04:00
2013-09-02 15:48:12 +04:00
$arResult['arSites'] = array();
$rsSites = CSite::GetList($by, $sort, array());
while ($ar = $rsSites->Fetch())
$arResult['arSites'][] = $ar;
2013-08-14 10:44:27 +04:00
2013-09-02 15:48:12 +04:00
if (isset($arResult['errCode']) && $arResult['errCode']) {
2013-08-14 10:44:27 +04:00
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
2013-08-14 10:44:27 +04:00
);
return;
}
2013-09-05 12:11:29 +04:00
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') && isset($_POST['ajax']) && ($_POST['ajax'] == 1)) {
2013-08-14 11:05:37 +04:00
$api_host = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, 0);
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
//prepare crm lists
$arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList();
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
if ((int) $this->INTARO_CRM_API->getStatusCode() != 200) {
$APPLICATION->RestartBuffer();
header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET);
die(json_encode(array("success" => false)));
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList();
$arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList();
$arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses
$arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList();
$arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y",
), false, false, array()
2013-08-14 11:05:37 +04:00
);
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
//form order types ids arr
$orderTypesArr = array();
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$arResult['bitrixOrderTypesList'][] = $arOrderTypesList;
$orderTypesArr[$arOrderTypesList['ID']] = htmlspecialchars(trim($_POST['order-type-' . $arOrderTypesList['ID']]));
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y",
), false, false, array()
2013-08-14 11:05:37 +04:00
);
//form delivery types ids arr
$deliveryTypesArr = array();
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
$arResult['bitrixDeliveryTypesList'][] = $arDeliveryTypesList;
$deliveryTypesArr[$arDeliveryTypesList['ID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryTypesList['ID']]));
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y"
)
2013-08-14 11:05:37 +04:00
);
//form payment types ids arr
$paymentTypesArr = array();
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
$arResult['bitrixPaymentTypesList'][] = $arPaymentTypesList;
$paymentTypesArr[$arPaymentTypesList['ID']] = htmlspecialchars(trim($_POST['payment-type-' . $arPaymentTypesList['ID']]));
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
//bitrix paymentStatusesList
$dbPaymentStatusesList = CSaleStatus::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"LID" => "ru", //ru
"ACTIVE" => "Y"
)
2013-08-14 11:05:37 +04:00
);
//form payment statuses ids arr
$paymentStatusesArr['Y'] = htmlspecialchars(trim($_POST['payment-status-Y']));
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
$arResult['bitrixPaymentStatusesList'][] = $arPaymentStatusesList;
$paymentStatusesArr[$arPaymentStatusesList['ID']] = htmlspecialchars(trim($_POST['payment-status-' . $arPaymentStatusesList['ID']]));
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$arResult['bitrixPaymentStatusesList'][] = array(
'ID' => 'Y',
'NAME' => GetMessage('CANCELED')
);
//form payment ids arr
$paymentArr = array();
$paymentArr['Y'] = htmlspecialchars(trim($_POST['payment-Y']));
$paymentArr['N'] = htmlspecialchars(trim($_POST['payment-N']));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_TYPES_ARR, serialize($orderTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR, serialize($deliveryTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_TYPES, serialize($paymentTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr));
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
// generate updated select inputs
$input = array();
2013-09-05 12:11:29 +04:00
foreach ($arResult['bitrixDeliveryTypesList'] as $bitrixDeliveryType) {
2013-08-14 11:05:37 +04:00
$input['delivery-type-' . $bitrixDeliveryType['ID']] =
2013-09-05 12:11:29 +04:00
'<select name="delivery-type-' . $bitrixDeliveryType['ID'] . '" class="typeselect">';
2013-08-14 11:05:37 +04:00
$input['delivery-type-' . $bitrixDeliveryType['ID']] .= '<option value=""></option>';
2013-09-05 12:11:29 +04:00
foreach ($arResult['deliveryTypesList'] as $deliveryType) {
2013-08-14 11:05:37 +04:00
if ($deliveryTypesArr[$bitrixDeliveryType['ID']] == $deliveryType['code']) {
$input['delivery-type-' . $bitrixDeliveryType['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $deliveryType['code'] . '" selected>';
2013-08-14 11:05:37 +04:00
} else {
$input['delivery-type-' . $bitrixDeliveryType['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $deliveryType['code'] . '">';
}
$input['delivery-type-' . $bitrixDeliveryType['ID']] .=
2013-08-14 11:05:37 +04:00
$APPLICATION->ConvertCharset($deliveryType['name'], 'utf-8', SITE_CHARSET);
$input['delivery-type-' . $bitrixDeliveryType['ID']] .= '</option>';
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$input['delivery-type-' . $bitrixDeliveryType['ID']] .= '</select>';
}
2013-09-05 12:11:29 +04:00
foreach ($arResult['bitrixPaymentTypesList'] as $bitrixPaymentType) {
2013-08-14 11:05:37 +04:00
$input['payment-type-' . $bitrixPaymentType['ID']] =
2013-09-05 12:11:29 +04:00
'<select name="payment-type-' . $bitrixPaymentType['ID'] . '" class="typeselect">';
2013-08-14 11:05:37 +04:00
$input['payment-type-' . $bitrixPaymentType['ID']] .= '<option value=""></option>';
2013-09-05 12:11:29 +04:00
foreach ($arResult['paymentTypesList'] as $paymentType) {
2013-08-14 11:05:37 +04:00
if ($paymentTypesArr[$bitrixPaymentType['ID']] == $paymentType['code']) {
$input['payment-type-' . $bitrixPaymentType['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $paymentType['code'] . '" selected>';
2013-08-14 11:05:37 +04:00
} else {
$input['payment-type-' . $bitrixPaymentType['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $paymentType['code'] . '">';
}
2013-08-14 11:05:37 +04:00
$input['payment-type-' . $bitrixPaymentType['ID']] .=
2013-09-05 12:11:29 +04:00
$APPLICATION->ConvertCharset($paymentType['name'], 'utf-8', SITE_CHARSET);
2013-08-14 11:05:37 +04:00
$input['payment-type-' . $bitrixPaymentType['ID']] .= '</option>';
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$input['payment-type-' . $bitrixPaymentType['ID']] .= '</select>';
}
2013-09-05 12:11:29 +04:00
foreach ($arResult['bitrixPaymentStatusesList'] as $bitrixPaymentStatus) {
2013-08-14 11:05:37 +04:00
$input['payment-status-' . $bitrixPaymentStatus['ID']] =
2013-09-05 12:11:29 +04:00
'<select name="payment-status-' . $bitrixPaymentStatus['ID'] . '" class="typeselect">';
2013-08-14 11:05:37 +04:00
$input['payment-status-' . $bitrixPaymentStatus['ID']] .= '<option value=""></option>';
2013-09-05 12:11:29 +04:00
foreach ($arResult['paymentGroupList'] as $orderStatusGroup) {
if (empty($orderStatusGroup['statuses']))
continue;
$input['payment-status-' . $bitrixPaymentStatus['ID']].=
'<optgroup label="' . $orderStatusGroup['name'] . '">';
foreach ($orderStatusGroup['statuses'] as $payment) {
2013-08-14 11:05:37 +04:00
if ($paymentStatusesArr[$bitrixPaymentStatus['ID']] == $arResult['paymentList'][$payment]['code']) {
$input['payment-status-' . $bitrixPaymentStatus['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $arResult['paymentList'][$payment]['code'] . '" selected>';
2013-08-14 11:05:37 +04:00
} else {
$input['payment-status-' . $bitrixPaymentStatus['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $arResult['paymentList'][$payment]['code'] . '">';
}
2013-08-14 11:05:37 +04:00
$input['payment-status-' . $bitrixPaymentStatus['ID']] .=
2013-09-05 12:11:29 +04:00
$APPLICATION->ConvertCharset($arResult['paymentList'][$payment]['name'], 'utf-8', SITE_CHARSET);
2013-08-14 11:05:37 +04:00
$input['payment-status-' . $bitrixPaymentStatus['ID']] .= '</option>';
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$input['payment-status-' . $bitrixPaymentStatus['ID']] .= '</optgroup>';
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$input['payment-status-' . $bitrixPaymentStatus['ID']] .= '</select>';
}
2013-09-05 12:11:29 +04:00
foreach ($arResult['bitrixPaymentList'] as $bitrixPayment) {
2013-08-14 11:05:37 +04:00
$input['payment-' . $bitrixPayment['ID']] =
2013-09-05 12:11:29 +04:00
'<select name="payment-' . $bitrixPayment['ID'] . '" class="typeselect">';
2013-08-14 11:05:37 +04:00
$input['payment-' . $bitrixPayment['ID']] .= '<option value=""></option>';
2013-09-05 12:11:29 +04:00
foreach ($arResult['paymentStatusesList'] as $paymentStatus) {
2013-08-14 11:05:37 +04:00
if ($paymentArr[$bitrixPayment['ID']] == $paymentStatus['code']) {
$input['payment-' . $bitrixPayment['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $paymentStatus['code'] . '" selected>';
2013-08-14 11:05:37 +04:00
} else {
$input['payment-' . $bitrixPayment['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $paymentStatus['code'] . '">';
}
2013-08-14 11:05:37 +04:00
$input['payment-' . $bitrixPayment['ID']] .=
2013-09-05 12:11:29 +04:00
$APPLICATION->ConvertCharset($paymentStatus['name'], 'utf-8', SITE_CHARSET);
2013-08-14 11:05:37 +04:00
$input['payment-' . $bitrixPayment['ID']] .= '</option>';
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$input['payment-' . $bitrixPayment['ID']] .= '</select>';
}
2013-09-05 12:11:29 +04:00
foreach ($arResult['bitrixOrderTypesList'] as $bitrixOrderType) {
2013-08-14 11:05:37 +04:00
$input['order-type-' . $bitrixOrderType['ID']] =
2013-09-05 12:11:29 +04:00
'<select name="order-type-' . $bitrixOrderType['ID'] . '" class="typeselect">';
2013-08-14 11:05:37 +04:00
$input['order-type-' . $bitrixOrderType['ID']] .= '<option value=""></option>';
2013-09-05 12:11:29 +04:00
foreach ($arResult['orderTypesList'] as $orderType) {
2013-08-14 11:05:37 +04:00
if ($orderTypesArr[$bitrixOrderType['ID']] == $orderType['code']) {
$input['order-type-' . $bitrixOrderType['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $orderType['code'] . '" selected>';
2013-08-14 11:05:37 +04:00
} else {
$input['order-type-' . $bitrixOrderType['ID']] .=
2013-09-05 12:11:29 +04:00
'<option value="' . $orderType['code'] . '">';
}
$input['order-type-' . $bitrixOrderType['ID']] .=
$APPLICATION->ConvertCharset($orderType['name'], 'utf-8', SITE_CHARSET);
$input['order-type-' . $bitrixOrderType['ID']] .= '</option>';
2013-08-14 11:05:37 +04:00
}
2013-09-05 12:11:29 +04:00
$input['order-type-' . $bitrixOrderType['ID']] .= '</select>';
2013-08-14 11:05:37 +04:00
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:05:37 +04:00
$APPLICATION->RestartBuffer();
2013-09-05 12:11:29 +04:00
header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET);
die(json_encode(array("success" => true, "result" => $input)));
2013-08-14 11:05:37 +04:00
}
2013-08-14 10:44:27 +04:00
$api_host = htmlspecialchars(trim($_POST[$this->CRM_API_HOST_OPTION]));
$api_key = htmlspecialchars(trim($_POST[$this->CRM_API_KEY_OPTION]));
2013-09-05 12:11:29 +04:00
2013-09-02 15:48:12 +04:00
// empty == select all
$orderSites = array();
2013-09-05 12:11:29 +04:00
foreach ($_POST[$this->CRM_ORDER_SITES] as $site) {
2013-09-02 15:48:12 +04:00
$orderSites[] = htmlspecialchars(trim($site));
}
2013-08-14 10:44:27 +04:00
// form correct url
$api_host = parse_url($api_host);
$api_host = $api_host['scheme'] . '://' . $api_host['host'];
2013-09-05 12:11:29 +04:00
if (!$api_host || !$api_key) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_FIELDS_API_HOST';
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
2013-08-14 10:44:27 +04:00
);
return;
}
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
$this->INTARO_CRM_API->paymentStatusesList();
//check connection & apiKey valid
2013-09-05 12:11:29 +04:00
if ((int) $this->INTARO_CRM_API->getStatusCode() != 200) {
2013-08-14 10:44:27 +04:00
$arResult['errCode'] = 'ERR_' . $this->INTARO_CRM_API->getStatusCode();
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
2013-08-14 10:44:27 +04:00
);
return;
}
COption::SetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, $api_host);
COption::SetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, $api_key);
2013-09-02 15:48:12 +04:00
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_SITES, serialize($orderSites));
2013-08-14 10:44:27 +04:00
//prepare crm lists
$arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList();
$arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList();
$arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList();
$arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses
$arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList();
$arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y",
), false, false, array()
2013-08-14 10:44:27 +04:00
);
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$arResult['bitrixOrderTypesList'][] = $arOrderTypesList;
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
2013-09-05 12:11:29 +04:00
2013-08-14 10:44:27 +04:00
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y",
), false, false, array()
2013-08-14 10:44:27 +04:00
);
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
$arResult['bitrixDeliveryTypesList'][] = $arDeliveryTypesList;
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y"
)
2013-08-14 10:44:27 +04:00
);
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
$arResult['bitrixPaymentTypesList'][] = $arPaymentTypesList;
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
//bitrix paymentStatusesList --statuses
$dbPaymentStatusesList = CSaleStatus::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"LID" => "ru", //ru
"ACTIVE" => "Y"
)
2013-08-14 10:44:27 +04:00
);
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
$arResult['bitrixPaymentStatusesList'][] = $arPaymentStatusesList;
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
$arResult['bitrixPaymentStatusesList'][] = array(
2013-09-05 12:11:29 +04:00
'ID' => 'Y',
2013-08-14 10:44:27 +04:00
'NAME' => GetMessage('CANCELED')
);
$APPLICATION->IncludeAdminFile(
2013-09-05 13:38:58 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php'
2013-08-14 10:44:27 +04:00
);
} else if ($step == 3) {
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("sale")) {
2013-08-14 10:44:27 +04:00
//handler
}
if (isset($_POST['back']) && $_POST['back']) {
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
2013-08-14 10:44:27 +04:00
);
}
2013-09-05 12:11:29 +04:00
// api load
$api_host = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, 0);
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
2013-08-14 10:44:27 +04:00
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y",
), false, false, array()
2013-08-14 10:44:27 +04:00
);
//form order types ids arr
$orderTypesArr = array();
2013-09-05 13:38:58 +04:00
$arResult['bitrixOrderTypesList'] = array();
2013-08-14 10:44:27 +04:00
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$orderTypesArr[$arOrderTypesList['ID']] = htmlspecialchars(trim($_POST['order-type-' . $arOrderTypesList['ID']]));
2013-09-05 13:38:58 +04:00
$arResult['bitrixOrderTypesList'][] = $arOrderTypesList;
2013-08-14 10:44:27 +04:00
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y",
), false, false, array()
2013-08-14 10:44:27 +04:00
);
2013-09-05 12:11:29 +04:00
2013-08-14 10:44:27 +04:00
//form delivery types ids arr
$deliveryTypesArr = array();
2013-09-05 12:11:29 +04:00
2013-08-15 19:18:44 +04:00
if (htmlspecialchars(trim($_POST['delivery-types-export'])) == 'false') {
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
$deliveryTypesArr[$arDeliveryTypesList['ID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryTypesList['ID']]));
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
2013-08-15 19:18:44 +04:00
} elseif (htmlspecialchars(trim($_POST['delivery-types-export'])) == 'true') {
// send to intaro crm and save
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
// parse id
$arId = array();
$resultDeliveryTypeId = 0;
if (strpos($arDeliveryTypesList['ID'], ":") !== false)
$arId = explode(":", $arDeliveryTypesList['ID']);
if ($arId)
$resultDeliveryTypeId = $arId[0];
else
$resultDeliveryTypeId = $arDeliveryTypesList['ID'];
2013-09-05 12:11:29 +04:00
$deliveryTypesArr[$arDeliveryTypesList['ID']] = $resultDeliveryTypeId;
2013-09-05 12:11:29 +04:00
// send to crm
2013-08-15 19:18:44 +04:00
$this->INTARO_CRM_API->deliveryTypeEdit(ICrmOrderActions::clearArr(array(
2013-09-05 12:11:29 +04:00
'code' => $resultDeliveryTypeId,
'name' => ICrmOrderActions::toJSON($arDeliveryTypesList['NAME']),
'defaultCost' => $arDeliveryTypesList['PRICE'],
'description' => ICrmOrderActions::toJSON($arDeliveryTypesList['DESCRIPTION']),
'paymentTypes' => ''
2013-08-15 19:18:44 +04:00
)));
// error pushing customer
2013-09-05 12:11:29 +04:00
if ($this->INTARO_CRM_API->getStatusCode() != 200) {
2013-08-27 18:05:55 +04:00
if ($this->INTARO_CRM_API->getStatusCode() != 201) {
//handle err
ICrmOrderActions::eventLog('install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit', $this->INTARO_CRM_API->getLastError());
}
}
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
2013-08-14 10:44:27 +04:00
}
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y"
)
2013-08-14 10:44:27 +04:00
);
//form payment types ids arr
$paymentTypesArr = array();
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
$paymentTypesArr[$arPaymentTypesList['ID']] = htmlspecialchars(trim($_POST['payment-type-' . $arPaymentTypesList['ID']]));
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
//bitrix paymentStatusesList
$dbPaymentStatusesList = CSaleStatus::GetList(
2013-09-05 12:11:29 +04:00
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"LID" => "ru", //ru
"ACTIVE" => "Y"
)
2013-08-14 10:44:27 +04:00
);
//form payment statuses ids arr
$paymentStatusesArr['Y'] = htmlspecialchars(trim($_POST['payment-status-Y']));
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
$paymentStatusesArr[$arPaymentStatusesList['ID']] = htmlspecialchars(trim($_POST['payment-status-' . $arPaymentStatusesList['ID']]));
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
//form payment ids arr
$paymentArr = array();
$paymentArr['Y'] = htmlspecialchars(trim($_POST['payment-Y']));
$paymentArr['N'] = htmlspecialchars(trim($_POST['payment-N']));
2013-09-05 12:11:29 +04:00
//form orderProps
$dbProp = CSaleOrderProps::GetList(array(), array());
while ($arProp = $dbProp->GetNext()) {
$arResult['arProp'][] = $arProp;
}
2013-08-14 10:44:27 +04:00
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_TYPES_ARR, serialize($orderTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR, serialize($deliveryTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_TYPES, serialize($paymentTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
2013-09-12 19:00:46 +04:00
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE, 0);
2013-09-05 12:11:29 +04:00
2013-08-14 10:44:27 +04:00
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
2013-08-14 10:44:27 +04:00
);
} else if ($step == 4) {
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("sale")) {
//handler
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
&& isset($_POST['ajax']) && ($_POST['ajax'] == 1)) {
ICrmOrderActions::uploadOrders(); // each 50
$lastUpOrderId = COption::GetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
$countLeft = (int) CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId), array());
$countAll = (int) CSaleOrder::GetList(array("ID" => "ASC"), array(), array());
if (!isset($_POST['finish']))
$finish = 0;
else
$finish = (int) $_POST['finish'];
$percent = round(100 - ($countLeft * 100 / $countAll), 1);
if (!$countLeft)
$finish = 1;
$APPLICATION->RestartBuffer();
header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET);
die(json_encode(array("finish" => $finish, "percent" => $percent)));
}
if (isset($_POST['back']) && $_POST['back']) {
$APPLICATION->IncludeAdminFile(
2013-09-05 13:38:58 +04:00
GetMessage('MODULE_INSTALL_TITLE'), $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php'
2013-09-05 12:11:29 +04:00
);
}
2013-09-05 13:38:58 +04:00
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
), array(
"ACTIVE" => "Y",
), false, false, array()
);
//form order types ids arr
$orderTypesArr = array();
$orderTypesList = array();
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$orderTypesArr[$arOrderTypesList['ID']] = htmlspecialchars(trim($_POST['order-type-' . $arOrderTypesList['ID']]));
$orderTypesList[] = $arOrderTypesList;
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
2013-09-05 12:11:29 +04:00
$orderPropsArr = array();
2013-09-05 13:38:58 +04:00
foreach ($orderTypesList as $orderType) {
$propsCount = 0;
$_orderPropsArr = array();
foreach ($arResult['orderProps'] as $orderProp) {
if ((!(int) htmlspecialchars(trim($_POST['address-detail-' . $orderType['ID']]))) && $propsCount > 5)
break;
$_orderPropsArr[$orderProp['ID']] = htmlspecialchars(trim($_POST['order-prop-' . $orderProp['ID'] . '-' . $orderType['ID']]));
$propsCount++;
}
$orderPropsArr[$orderType['ID']] = $_orderPropsArr;
2013-09-05 12:11:29 +04:00
}
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_PROPS, serialize($orderPropsArr));
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
);
} else if ($step == 5) {
if (!CModule::IncludeModule("iblock")) {
2013-08-14 11:42:01 +04:00
$arResult['errCode'] = 'ERR_IBLOCK';
}
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("catalog")) {
2013-08-14 11:42:01 +04:00
$arResult['errCode'] = 'ERR_CATALOG';
}
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step5.php'
2013-08-14 11:42:01 +04:00
);
2013-09-05 12:11:29 +04:00
} else if ($step == 6) {
if (!CModule::IncludeModule("iblock")) {
2013-08-15 19:40:21 +04:00
$arResult['errCode'] = 'ERR_IBLOCK';
}
2013-09-05 12:11:29 +04:00
if (!CModule::IncludeModule("catalog")) {
2013-08-15 19:40:21 +04:00
$arResult['errCode'] = 'ERR_CATALOG';
}
2013-09-05 12:11:29 +04:00
if (isset($arResult['errCode']) && $arResult['errCode']) {
2013-08-14 11:42:01 +04:00
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step5.php'
2013-08-14 11:42:01 +04:00
);
return;
}
2013-09-05 12:11:29 +04:00
2013-08-14 11:42:01 +04:00
if (isset($_POST['back']) && $_POST['back']) {
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
2013-08-14 11:42:01 +04:00
);
}
2013-09-05 12:11:29 +04:00
if (!isset($_POST['IBLOCK_EXPORT']))
2013-08-15 19:40:21 +04:00
$arResult['errCode'] = 'ERR_FIELDS_IBLOCK';
2013-08-14 11:42:01 +04:00
else
$iblocks = $_POST['IBLOCK_EXPORT'];
2013-09-05 12:11:29 +04:00
if (!isset($_POST['IBLOCK_PROPERTY_ARTICLE']))
2013-08-15 19:40:21 +04:00
$arResult['errCode'] = 'ERR_FIELDS_ARTICLE';
2013-08-14 11:42:01 +04:00
else
$articleProperties = $_POST['IBLOCK_PROPERTY_ARTICLE'];
2013-09-05 12:11:29 +04:00
if (!isset($_POST['SETUP_FILE_NAME']))
2013-08-15 19:40:21 +04:00
$arResult['errCode'] = 'ERR_FIELDS_FILE';
2013-08-14 11:42:01 +04:00
else
$filename = $_POST['SETUP_FILE_NAME'];
2013-09-05 12:11:29 +04:00
2013-08-20 13:42:13 +04:00
if (count($iblocks) < count($articleProperties))
2013-08-20 12:53:32 +04:00
$arResult['errCode'] = 'ERR_ARTICLE_IBLOCK';
2013-09-05 12:11:29 +04:00
if (!isset($_POST['TYPE_LOADING']))
2013-08-14 11:42:01 +04:00
$typeLoading = 0;
else
$typeLoading = $_POST['TYPE_LOADING'];
2013-09-05 12:11:29 +04:00
if (!isset($_POST['SETUP_PROFILE_NAME']))
2013-08-15 19:40:21 +04:00
$profileName = "";
else
$profileName = $_POST['SETUP_PROFILE_NAME'];
2013-09-05 12:11:29 +04:00
2013-08-15 19:40:21 +04:00
if ($typeLoading != 'none' && $profileName == "")
$arResult['errCode'] = 'ERR_FIELDS_PROFILE';
2013-09-05 12:11:29 +04:00
if ($filename == "")
2013-08-20 13:42:13 +04:00
$arResult['errCode'] = 'ERR_FIELDS_FILE';
2013-09-05 12:11:29 +04:00
if (isset($arResult['errCode']) && $arResult['errCode']) {
2013-09-12 19:00:46 +04:00
2013-08-20 12:53:32 +04:00
$arOldValues = Array(
'IBLOCK_EXPORT' => $iblocks,
'IBLOCK_PROPERTY_ARTICLE' => $articleProperties,
'SETUP_FILE_NAME' => $filename,
'SETUP_PROFILE_NAME' => $profileName
);
global $oldValues;
$oldValues = $arOldValues;
2013-08-15 19:40:21 +04:00
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step5.php'
2013-08-15 19:40:21 +04:00
);
return;
}
2013-09-05 12:11:29 +04:00
2013-08-15 19:40:21 +04:00
RegisterModule($this->MODULE_ID);
RegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
2013-08-19 18:13:26 +04:00
$this->CopyFiles();
2013-08-14 11:42:01 +04:00
if (isset($_POST['LOAD_NOW'])) {
2013-09-05 12:11:29 +04:00
2013-08-14 11:42:01 +04:00
$loader = new ICMLLoader();
$loader->iblocks = $iblocks;
$loader->articleProperties = $articleProperties;
$loader->filename = $filename;
$loader->application = $APPLICATION;
$loader->Load();
2013-09-05 12:11:29 +04:00
}
2013-08-14 11:42:01 +04:00
if ($typeLoading == 'agent' || $typeLoading == 'cron') {
2013-08-19 18:13:26 +04:00
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/' . $this->INTARO_CRM_EXPORT . '_run.php')) {
2013-08-15 19:40:21 +04:00
$dbProfile = CCatalogExport::GetList(array(), array("FILE_NAME" => $this->INTARO_CRM_EXPORT));
2013-08-14 19:56:52 +04:00
2013-08-15 19:40:21 +04:00
while ($arProfile = $dbProfile->Fetch()) {
2013-09-05 12:11:29 +04:00
if ($arProfile["DEFAULT_PROFILE"] != "Y") {
CAgent::RemoveAgent("CCatalogExport::PreGenerateExport(" . $arProfile['ID'] . ");", "catalog");
2013-08-19 18:13:26 +04:00
CCatalogExport::Delete($arProfile['ID']);
}
2013-08-15 19:40:21 +04:00
}
2013-08-14 11:42:01 +04:00
}
$ar = $this->GetProfileSetupVars($iblocks, $articleProperties, $filename);
$PROFILE_ID = CCatalogExport::Add(array(
2013-09-05 12:11:29 +04:00
"LAST_USE" => false,
"FILE_NAME" => $this->INTARO_CRM_EXPORT,
"NAME" => $profileName,
"DEFAULT_PROFILE" => "N",
"IN_MENU" => "N",
"IN_AGENT" => "N",
"IN_CRON" => "N",
"NEED_EDIT" => "N",
"SETUP_VARS" => $ar
));
2013-08-14 11:42:01 +04:00
if (intval($PROFILE_ID) <= 0) {
$arResult['errCode'] = 'ERR_IBLOCK';
return;
}
if ($typeLoading == 'agent') {
2013-09-05 12:11:29 +04:00
2013-08-14 11:42:01 +04:00
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
2013-09-05 12:11:29 +04:00
"CCatalogExport::PreGenerateExport(" . $PROFILE_ID . ");", "catalog", "N", 86400, $dateAgent->format('d.m.Y H:i:s'), // date of first check
2013-08-14 11:42:01 +04:00
"Y", // агент активен
$dateAgent->format('d.m.Y H:i:s'), // date of first start
30
2013-09-05 12:11:29 +04:00
);
2013-08-14 11:42:01 +04:00
CCatalogExport::Update($PROFILE_ID, array(
2013-09-05 12:11:29 +04:00
"IN_AGENT" => "Y"
));
2013-08-14 11:42:01 +04:00
} else {
$agent_period = 24;
$agent_php_path = "/usr/local/php/bin/php";
2013-09-05 12:11:29 +04:00
if (!file_exists($_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS . "cron_frame.php")) {
CheckDirPath($_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS);
$tmp_file_size = filesize($_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS_DEF . "cron_frame.php");
$fp = fopen($_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS_DEF . "cron_frame.php", "rb");
2013-08-14 11:42:01 +04:00
$tmp_data = fread($fp, $tmp_file_size);
fclose($fp);
$tmp_data = str_replace("#DOCUMENT_ROOT#", $_SERVER["DOCUMENT_ROOT"], $tmp_data);
$tmp_data = str_replace("#PHP_PATH#", $agent_php_path, $tmp_data);
2013-09-05 12:11:29 +04:00
$fp = fopen($_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS . "cron_frame.php", "ab");
2013-08-14 11:42:01 +04:00
fwrite($fp, $tmp_data);
fclose($fp);
}
$cfg_data = "";
2013-09-05 12:11:29 +04:00
if (file_exists($_SERVER["DOCUMENT_ROOT"] . "/bitrix/crontab/crontab.cfg")) {
$cfg_file_size = filesize($_SERVER["DOCUMENT_ROOT"] . "/bitrix/crontab/crontab.cfg");
$fp = fopen($_SERVER["DOCUMENT_ROOT"] . "/bitrix/crontab/crontab.cfg", "rb");
$cfg_data = fread($fp, $cfg_file_size);
fclose($fp);
2013-08-14 11:42:01 +04:00
}
2013-09-05 12:11:29 +04:00
CheckDirPath($_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS . "logs/");
if ($arProfile["IN_CRON"] == "Y") {
// remove
$cfg_data = preg_replace("#^.*?" . preg_quote(CATALOG_PATH2EXPORTS) . "cron_frame.php +" . $PROFILE_ID . " *>.*?$#im", "", $cfg_data);
} else {
$strTime = "0 */" . $agent_period . " * * * ";
if (strlen($cfg_data) > 0)
2013-08-14 11:42:01 +04:00
$cfg_data .= "\n";
2013-09-05 12:11:29 +04:00
$cfg_data .= $strTime . $agent_php_path . " -f " . $_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS . "cron_frame.php " . $PROFILE_ID . " >" . $_SERVER["DOCUMENT_ROOT"] . CATALOG_PATH2EXPORTS . "logs/" . $PROFILE_ID . ".txt\n";
2013-08-14 11:42:01 +04:00
}
CCatalogExport::Update($PROFILE_ID, array(
2013-09-05 12:11:29 +04:00
"IN_CRON" => "Y"
));
CheckDirPath($_SERVER["DOCUMENT_ROOT"] . "/bitrix/crontab/");
2013-08-14 11:42:01 +04:00
$cfg_data = preg_replace("#[\r\n]{2,}#im", "\n", $cfg_data);
2013-09-05 12:11:29 +04:00
$fp = fopen($_SERVER["DOCUMENT_ROOT"] . "/bitrix/crontab/crontab.cfg", "wb");
2013-08-14 11:42:01 +04:00
fwrite($fp, $cfg_data);
fclose($fp);
$arRetval = array();
2013-09-05 12:11:29 +04:00
@exec("crontab " . $_SERVER["DOCUMENT_ROOT"] . "/bitrix/crontab/crontab.cfg", $arRetval, $return_var);
}
2013-08-14 11:42:01 +04:00
}
2013-08-14 10:44:27 +04:00
//agent
2013-08-15 19:40:21 +04:00
2013-08-14 10:44:27 +04:00
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
2013-09-05 12:11:29 +04:00
"ICrmOrderActions::uploadOrdersAgent();", $this->MODULE_ID, "N", 600, // interval - 10 mins
$dateAgent->format('d.m.Y H:i:s'), // date of first check
"Y", // агент активен
$dateAgent->format('d.m.Y H:i:s'), // date of first start
30
2013-09-04 20:49:07 +04:00
);
CAgent::AddAgent(
"ICrmOrderActions::orderHistoryAgent();",
$this->MODULE_ID,
"N",
600, // interval - 10 mins
2013-08-14 10:44:27 +04:00
$dateAgent->format('d.m.Y H:i:s'), // date of first check
"Y", // агент активен
$dateAgent->format('d.m.Y H:i:s'), // date of first start
30
);
$api_host = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, 0);
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
$this->INTARO_CRM_API->statisticUpdate();
2013-08-15 19:40:21 +04:00
2013-08-14 10:44:27 +04:00
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step6.php'
2013-08-14 10:44:27 +04:00
);
}
}
function DoUninstall() {
global $APPLICATION;
2013-09-05 12:11:29 +04:00
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $this->MODULE_ID);
2013-09-12 19:00:46 +04:00
CAgent::RemoveAgent("ICrmOrderActions::orderHistoryAgent();", $this->MODULE_ID);
2013-08-14 10:44:27 +04:00
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_HOST_OPTION);
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_KEY_OPTION);
COption::RemoveOption($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR);
COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_TYPES);
COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES);
COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_LAST_ID);
2013-09-05 13:38:58 +04:00
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_SITES);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_PROPS);
2013-09-12 19:00:46 +04:00
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE);
2013-09-05 12:11:29 +04:00
2013-08-14 10:44:27 +04:00
UnRegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder");
UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
2013-09-05 12:11:29 +04:00
if (CModule::IncludeModule("catalog")) {
2013-08-19 18:13:26 +04:00
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/' . $this->INTARO_CRM_EXPORT . '_run.php')) {
$dbProfile = CCatalogExport::GetList(array(), array("FILE_NAME" => $this->INTARO_CRM_EXPORT));
while ($arProfile = $dbProfile->Fetch()) {
2013-09-05 12:11:29 +04:00
if ($arProfile["DEFAULT_PROFILE"] != "Y") {
CAgent::RemoveAgent("CCatalogExport::PreGenerateExport(" . $arProfile['ID'] . ");", "catalog");
2013-08-19 18:13:26 +04:00
CCatalogExport::Delete($arProfile['ID']);
}
}
}
}
2013-09-05 12:11:29 +04:00
2013-08-14 10:44:27 +04:00
$this->DeleteFiles();
UnRegisterModule($this->MODULE_ID);
$APPLICATION->IncludeAdminFile(
2013-09-05 12:11:29 +04:00
GetMessage('MODULE_UNINSTALL_TITLE'), $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/unstep1.php'
2013-08-14 10:44:27 +04:00
);
}
function CopyFiles() {
CopyDirFiles(
2013-09-05 12:11:29 +04:00
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/export/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/', true, true
2013-08-14 10:44:27 +04:00
);
}
function DeleteFiles() {
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_run.php');
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_setup.php');
2013-08-14 12:12:09 +04:00
}
2013-09-05 12:11:29 +04:00
2013-08-15 19:40:21 +04:00
function GetProfileSetupVars($iblocks, $articleProperties, $filename) {
// Get string like IBLOCK_EXPORT[0]=3&
// IBLOCK_EXPORT[1]=6&
// IBLOCK_PROPERTY_ARTICLE[0]=ARTICLE&
// IBLOCK_PROPERTY_ARTICLE[1]=ARTNUMBER&
// SETUP_FILE_NAME=%2Fbitrix%2Fcatalog_export%2Ftestintarocrm.xml
//$arProfileFields = explode(",", $SETUP_FIELDS_LIST);
$strVars = "";
2013-09-05 12:11:29 +04:00
foreach ($iblocks as $key => $val)
2013-08-15 19:40:21 +04:00
$strVars .= 'IBLOCK_EXPORT[' . $key . ']=' . $val . '&';
2013-09-05 12:11:29 +04:00
foreach ($articleProperties as $key => $val)
2013-08-15 19:40:21 +04:00
$strVars .= 'IBLOCK_PROPERTY_ARTICLE[' . $key . ']=' . $val . '&';
2013-09-05 12:11:29 +04:00
2013-08-15 19:40:21 +04:00
$strVars .= 'SETUP_FILE_NAME=' . urlencode($filename);
2013-09-05 12:11:29 +04:00
2013-08-15 19:40:21 +04:00
return $strVars;
}
2013-09-05 12:11:29 +04:00
2013-08-14 12:12:09 +04:00
}