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

654 lines
26 KiB
PHP
Raw Normal View History

2013-07-05 18:19:42 +04:00
<?php
/**
* Module Install/Uninstall script
2013-07-16 18:50:05 +04:00
* Module name: intaro.intarocrm
* Class name: intaro_intarocrm
2013-07-05 18:19:42 +04:00
*/
global $MESS;
IncludeModuleLangFile(__FILE__);
2013-07-16 18:50:05 +04:00
if (class_exists('intaro_intarocrm'))
2013-07-05 18:19:42 +04:00
return;
2013-07-30 16:14:04 +04:00
class intaro_intarocrm extends CModule
2013-07-05 18:19:42 +04:00
{
2013-07-16 18:50:05 +04:00
var $MODULE_ID = 'intaro.intarocrm';
2013-07-05 18:19:42 +04:00
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-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
var $CRM_API_HOST_OPTION = 'api_host';
var $CRM_API_KEY_OPTION = 'api_key';
2013-07-05 21:15:23 +04:00
var $CRM_ORDER_TYPES_ARR = 'order_types_arr';
2013-07-05 18:19:42 +04:00
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
2013-07-09 19:03:56 +04:00
var $CRM_ORDER_LAST_ID = 'order_last_id';
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
var $INSTALL_PATH;
2013-07-30 16:14:04 +04:00
2013-07-16 18:50:05 +04:00
function intaro_intarocrm()
2013-07-05 18:19:42 +04:00
{
$arModuleVersion = array();
$path = str_replace("\\", "/", __FILE__);
$path = substr($path, 0, strlen($path) - strlen("/index.php"));
$this->INSTALL_PATH = $path;
include($path."/version.php");
$this->MODULE_VERSION = $arModuleVersion["VERSION"];
$this->MODULE_VERSION_DATE = $arModuleVersion["VERSION_DATE"];
$this->MODULE_NAME = GetMessage('MODULE_NAME');
$this->MODULE_DESCRIPTION = GetMessage('MODULE_DESCRIPTION');
2013-07-05 21:15:23 +04:00
$this->PARTNER_NAME = GetMessage('MODULE_PARTNER_NAME');
$this->PARTNER_URI = GetMessage('MODULE_PARTNER_URI');
2013-07-05 18:19:42 +04:00
}
2013-07-30 16:14:04 +04:00
/**
* Functions DoInstall and DoUninstall are
2013-07-05 18:19:42 +04:00
* All other functions are optional
*/
2013-07-30 16:14:04 +04:00
function DoInstall()
2013-07-05 18:19:42 +04:00
{
global $APPLICATION, $step, $arResult;
2013-07-30 16:14:04 +04:00
2013-08-06 15:09:27 +04:00
if (!in_array('curl', get_loaded_extensions())) {
$APPLICATION->ThrowException(GetMessage("INTAROCRM_CURL_ERR"));
return false;
}
2013-07-30 16:14:04 +04:00
include($this->INSTALL_PATH . '/../classes/general/RestApi.php');
2013-07-29 12:29:41 +04:00
include($this->INSTALL_PATH . '/../classes/general/ICrmOrderActions.php');
include($this->INSTALL_PATH . '/../classes/general/ICMLLoader.php');
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
$step = intval($_REQUEST['step']);
2013-07-30 16:14:04 +04:00
if ($step <= 1) {
2013-07-24 14:44:48 +04:00
if(!CModule::IncludeModule("sale")) {
$arResult['errCode'] = 'ERR_SALE';
}
2013-07-30 16:14:04 +04:00
2013-07-24 14:44:48 +04:00
if(!CModule::IncludeModule("iblock")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
2013-07-30 16:14:04 +04:00
2013-07-24 14:44:48 +04:00
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
$APPLICATION->IncludeAdminFile(
2013-07-30 16:14:04 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
2013-07-05 18:19:42 +04:00
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
} else if ($step == 2) {
if(!CModule::IncludeModule("sale")) {
2013-07-24 14:44:48 +04:00
$arResult['errCode'] = 'ERR_SALE';
}
2013-07-30 16:14:04 +04:00
2013-07-24 14:44:48 +04:00
if(!CModule::IncludeModule("iblock")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
2013-07-30 16:14:04 +04:00
2013-07-24 14:44:48 +04:00
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
2013-07-30 16:14:04 +04:00
2013-07-24 14:44:48 +04:00
if(isset($arResult['errCode']) && $arResult['errCode']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
2013-07-05 18:19:42 +04:00
}
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
$api_host = htmlspecialchars(trim($_POST[$this->CRM_API_HOST_OPTION]));
$api_key = htmlspecialchars(trim($_POST[$this->CRM_API_KEY_OPTION]));
2013-07-30 16:14:04 +04:00
2013-07-18 23:03:08 +04:00
// form correct url
$api_host = parse_url($api_host);
$api_host = $api_host['scheme'] . '://' . $api_host['host'];
2013-07-30 16:14:04 +04:00
if(!$api_host || !$api_key) {
2013-07-05 18:19:42 +04:00
$arResult['errCode'] = 'ERR_FIELDS_API_HOST';
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
}
2013-07-30 16:14:04 +04:00
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
$this->INTARO_CRM_API->paymentStatusesList();
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
//check connection & apiKey valid
if((int) $this->INTARO_CRM_API->getStatusCode() != 200) {
$arResult['errCode'] = 'ERR_' . $this->INTARO_CRM_API->getStatusCode();
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
return;
2013-07-18 22:55:52 +04:00
}
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
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-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
//prepare crm lists
2013-07-05 21:15:23 +04:00
$arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList();
2013-07-05 18:19:42 +04:00
$arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList();
$arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList();
$arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses
2013-07-09 19:03:56 +04:00
$arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList();
$arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups
2013-07-30 16:14:04 +04:00
2013-07-05 21:15:23 +04:00
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
2013-07-30 16:14:04 +04:00
2013-07-05 21:15:23 +04:00
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$arResult['bitrixOrderTypesList'][] = $arOrderTypesList;
2013-07-05 21:15:23 +04:00
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$arResult['bitrixDeliveryTypesList'][] = $arDeliveryTypesList;
2013-07-05 18:19:42 +04:00
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
array(
2013-07-30 16:14:04 +04:00
"SORT" => "ASC",
2013-07-05 18:19:42 +04:00
"NAME" => "ASC"
2013-07-30 16:14:04 +04:00
),
2013-07-05 18:19:42 +04:00
array(
"ACTIVE" => "Y"
)
);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$arResult['bitrixPaymentTypesList'][] = $arPaymentTypesList;
2013-07-05 18:19:42 +04:00
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
2013-07-30 16:14:04 +04:00
//bitrix paymentStatusesList --statuses
2013-07-05 18:19:42 +04:00
$dbPaymentStatusesList = CSaleStatus::GetList(
array(
2013-07-30 16:14:04 +04:00
"SORT" => "ASC",
2013-07-05 18:19:42 +04:00
"NAME" => "ASC"
2013-07-30 16:14:04 +04:00
),
2013-07-05 18:19:42 +04:00
array(
2013-07-30 16:14:04 +04:00
"LID" => "ru", //ru
2013-07-05 18:19:42 +04:00
"ACTIVE" => "Y"
)
);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$arResult['bitrixPaymentStatusesList'][] = $arPaymentStatusesList;
2013-07-05 18:19:42 +04:00
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
2013-07-30 16:14:04 +04:00
$arResult['bitrixPaymentStatusesList'][] = array(
'ID' => 'Y',
2013-07-24 14:44:48 +04:00
'NAME' => GetMessage('CANCELED')
);
2013-07-30 16:14:04 +04:00
$APPLICATION->IncludeAdminFile(
2013-07-30 16:14:04 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
2013-07-05 18:19:42 +04:00
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php'
);
2013-07-30 16:14:04 +04:00
2013-07-18 14:32:10 +04:00
} else if ($step == 3) {
2013-07-05 18:19:42 +04:00
if(!CModule::IncludeModule("sale")) {
//handler
}
2013-07-30 16:14:04 +04:00
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
&& isset($_POST['ajax']) && ($_POST['ajax'] == 1)) {
ICrmOrderActions::uploadOrders(true); // each 50
2013-07-30 16:14:04 +04:00
$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());
2013-07-30 16:14:04 +04:00
if(!isset($_POST['finish']))
$finish = 0;
2013-07-30 16:14:04 +04:00
else
$finish = (int) $_POST['finish'];
2013-07-30 16:14:04 +04:00
2013-07-24 14:44:48 +04:00
$percent = 100 - round(($countLeft * 100 / $countAll), 1);
2013-07-30 16:14:04 +04:00
2013-08-06 15:09:27 +04:00
if(!$countLeft)
$finish = 1;
2013-08-06 15:09:27 +04:00
$APPLICATION->RestartBuffer();
header('Content-Type: application/x-javascript; charset='.LANG_CHARSET);
die(json_encode(array("finish" => $finish, "percent" => $percent)));
}
2013-07-30 16:14:04 +04:00
2013-07-18 14:32:10 +04:00
if (isset($_POST['back']) && $_POST['back']) {
$APPLICATION->IncludeAdminFile(
2013-07-30 16:14:04 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
2013-07-18 14:32:10 +04:00
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
}
2013-07-30 16:14:04 +04:00
2013-07-05 21:15:23 +04:00
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
2013-07-30 16:14:04 +04:00
2013-07-05 21:15:23 +04:00
//form order types ids arr
$orderTypesArr = array();
2013-07-05 21:15:23 +04:00
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$orderTypesArr[$arOrderTypesList['ID']] = htmlspecialchars(trim($_POST['order-type-' . $arOrderTypesList['ID']]));
2013-07-05 21:15:23 +04:00
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
2013-07-30 16:14:04 +04:00
//bitrix deliveryTypesList
2013-07-05 18:19:42 +04:00
$dbDeliveryTypesList = CSaleDelivery::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
//form delivery types ids arr
$deliveryTypesArr = array();
2013-07-05 18:19:42 +04:00
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$deliveryTypesArr[$arDeliveryTypesList['ID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryTypesList['ID']]));
2013-07-05 18:19:42 +04:00
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
2013-07-30 16:14:04 +04:00
//bitrix paymentTypesList
2013-07-05 18:19:42 +04:00
$dbPaymentTypesList = CSalePaySystem::GetList(
array(
2013-07-30 16:14:04 +04:00
"SORT" => "ASC",
2013-07-05 18:19:42 +04:00
"NAME" => "ASC"
2013-07-30 16:14:04 +04:00
),
2013-07-05 18:19:42 +04:00
array(
"ACTIVE" => "Y"
)
);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
//form payment types ids arr
$paymentTypesArr = array();
2013-07-05 18:19:42 +04:00
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$paymentTypesArr[$arPaymentTypesList['ID']] = htmlspecialchars(trim($_POST['payment-type-' . $arPaymentTypesList['ID']]));
2013-07-05 18:19:42 +04:00
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
2013-07-30 16:14:04 +04:00
//bitrix paymentStatusesList
2013-07-05 18:19:42 +04:00
$dbPaymentStatusesList = CSaleStatus::GetList(
array(
2013-07-30 16:14:04 +04:00
"SORT" => "ASC",
2013-07-05 18:19:42 +04:00
"NAME" => "ASC"
2013-07-30 16:14:04 +04:00
),
2013-07-05 18:19:42 +04:00
array(
2013-07-30 16:14:04 +04:00
"LID" => "ru", //ru
2013-07-05 18:19:42 +04:00
"ACTIVE" => "Y"
)
);
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
//form payment statuses ids arr
$paymentStatusesArr['Y'] = htmlspecialchars(trim($_POST['payment-status-Y']));
2013-07-30 16:14:04 +04:00
2013-07-05 18:19:42 +04:00
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
2013-07-30 16:14:04 +04:00
$paymentStatusesArr[$arPaymentStatusesList['ID']] = htmlspecialchars(trim($_POST['payment-status-' . $arPaymentStatusesList['ID']]));
2013-07-05 18:19:42 +04:00
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
2013-07-30 16:14:04 +04:00
//form payment ids arr
$paymentArr = array();
$paymentArr['Y'] = htmlspecialchars(trim($_POST['payment-Y']));
$paymentArr['N'] = htmlspecialchars(trim($_POST['payment-N']));
2013-07-30 16:14:04 +04:00
2013-07-05 21:15:23 +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));
2013-07-05 18:19:42 +04:00
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-07-30 16:14:04 +04:00
$APPLICATION->IncludeAdminFile(
2013-07-30 16:14:04 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
);
} else if ($step == 4) {
if(!CModule::IncludeModule("iblock")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
2013-07-30 16:14:04 +04:00
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
$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")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
if(isset($arResult['errCode']) && $arResult['errCode']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
);
return;
}
if (isset($_POST['back']) && $_POST['back']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
);
}
if(!isset($_POST['IBLOCK_EXPORT']))
$arResult['errCode'] = 'ERR_FIELDS_IBLOCK';
else
$iblocks = $_POST['IBLOCK_EXPORT'];
if(!isset($_POST['IBLOCK_PROPERTY_ARTICLE']))
$arResult['errCode'] = 'ERR_FIELDS_ARTICLE';
else
$articleProperties = $_POST['IBLOCK_PROPERTY_ARTICLE'];
if(!isset($_POST['SETUP_FILE_NAME']))
$arResult['errCode'] = 'ERR_FIELDS_FILE';
else
$filename = $_POST['SETUP_FILE_NAME'];
if(!isset($_POST['TYPE_LOADING']))
$typeLoading = 0;
else
$typeLoading = $_POST['TYPE_LOADING'];
if(!isset($_POST['SETUP_PROFILE_NAME']) )
$profileName = "";
else
$profileName = $_POST['SETUP_PROFILE_NAME'];
if ($typeLoading != 'none' && $profileName == "")
$arResult['errCode'] = 'ERR_FIELDS_PROFILE';
if(isset($arResult['errCode']) && $arResult['errCode']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
);
return;
}
RegisterModule($this->MODULE_ID);
RegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
if (isset($_POST['LOAD_NOW'])) {
$loader = new ICMLLoader();
$loader->iblocks = $iblocks;
$loader->articleProperties = $articleProperties;
$loader->filename = $filename;
$loader->application = $APPLICATION;
$loader->Load();
}
if ($typeLoading == 'agent' || $typeLoading == 'cron') {
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/catalog_export/' . $this->INTARO_CRM_EXPORT . '_run.php')) {
$dbProfile = CCatalogExport::GetList(array(), array("FILE_NAME" => $this->INTARO_CRM_EXPORT));
while ($arProfile = $dbProfile->Fetch()) {
if ($arProfile["DEFAULT_PROFILE"]!="Y")
CAgent::RemoveAgent("CCatalogExport::PreGenerateExport(".$arProfile['ID'].");", "catalog");
}
}
$ar = $this->GetProfileSetupVars($iblocks, $articleProperties, $filename);
$PROFILE_ID = CCatalogExport::Add(array(
"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
));
if (intval($PROFILE_ID) <= 0) {
$arResult['errCode'] = 'ERR_IBLOCK';
return;
}
if ($typeLoading == 'agent') {
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
"CCatalogExport::PreGenerateExport(" . $PROFILE_ID . ");",
"catalog",
"N",
86400,
$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
);
CCatalogExport::Update($PROFILE_ID, array(
"IN_CRON" => ($arProfile["IN_AGENT"]=="Y" ? "N" : "Y")
));
} else {
$agent_period = 24;
$agent_php_path = "/usr/local/php/bin/php";
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");
$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);
$fp = fopen($_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS."cron_frame.php", "ab");
fwrite($fp, $tmp_data);
fclose($fp);
}
$cfg_data = "";
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);
}
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)
$cfg_data .= "\n";
$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";
}
CCatalogExport::Update($PROFILE_ID, array(
"IN_CRON" => ($arProfile["IN_CRON"]=="Y" ? "N" : "Y")
));
CheckDirPath($_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/");
$cfg_data = preg_replace("#[\r\n]{2,}#im", "\n", $cfg_data);
$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/crontab.cfg", "wb");
fwrite($fp, $cfg_data);
fclose($fp);
$arRetval = array();
@exec("crontab ".$_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/crontab.cfg", $arRetval, $return_var);
}
}
//
2013-07-30 16:14:04 +04:00
//agent
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
"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-07-30 16:14:04 +04:00
$this->CopyFiles();
2013-07-05 21:15:23 +04:00
$APPLICATION->IncludeAdminFile(
2013-07-30 16:14:04 +04:00
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step5.php'
2013-07-05 18:19:42 +04:00
);
2013-07-30 16:14:04 +04:00
}
2013-07-05 18:19:42 +04:00
}
function DoUninstall() {
2013-07-05 21:15:23 +04:00
global $APPLICATION;
2013-07-30 16:14:04 +04:00
2013-08-06 15:02:11 +04:00
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $this->MODULE_ID);
2013-07-30 16:14:04 +04:00
2013-07-05 21:15:23 +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);
2013-07-05 18:19:42 +04:00
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);
2013-07-09 19:03:56 +04:00
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_LAST_ID);
$this->DeleteFiles();
UnRegisterModule($this->MODULE_ID);
2013-07-30 16:14:04 +04:00
2013-07-05 21:15:23 +04:00
$APPLICATION->IncludeAdminFile(
2013-07-30 16:14:04 +04:00
GetMessage('MODULE_UNINSTALL_TITLE'),
2013-07-05 21:15:23 +04:00
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/unstep1.php'
2013-07-30 16:14:04 +04:00
);
2013-07-05 18:19:42 +04:00
}
function CopyFiles() {
2013-07-16 15:52:20 +04:00
CopyDirFiles(
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/export/',
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/',
true, true
);
}
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');
}
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 = "";
foreach ($iblocks as $key => $val)
$strVars .= 'IBLOCK_EXPORT[' . $key . ']=' . $val . '&';
foreach ($articleProperties as $key => $val)
$strVars .= 'IBLOCK_PROPERTY_ARTICLE[' . $key . ']=' . $val . '&';
$strVars .= 'SETUP_FILE_NAME=' . urlencode($filename);
return $strVars;
}
}